From 7deda59e81468109d54f81dcea69b14378a57715 Mon Sep 17 00:00:00 2001 From: Vojtech Janota Date: Wed, 16 Sep 2015 12:32:04 +0200 Subject: [PATCH] issue 39: include xml declaration --- .../alm/ali/idea/services/AttachmentService.java | 5 +++-- .../alm/ali/idea/services/DevMotiveService.java | 5 +++-- .../hp/alm/ali/idea/services/EntityService.java | 11 ++++++----- .../ali/idea/services/DevMotiveServiceTest.java | 2 +- .../alm/ali/idea/services/EntityServiceTest.java | 12 ++++++------ .../hp/alm/ali/rest/client/AliRestClient.java | 6 +++--- .../ali/rest/client}/XMLOutputterFactory.java | 2 +- .../alm/ali/rest/client/AliRestClientTest.java | 12 ++++++------ .../src/main/java/com/hp/alm/ali/Handler.java | 16 ++++++++++++++-- 9 files changed, 43 insertions(+), 28 deletions(-) rename {ali-plugin-main/src/main/java/com/hp/alm/ali/idea/cfg => ali-rest-client/src/main/java/com/hp/alm/ali/rest/client}/XMLOutputterFactory.java (90%) diff --git a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/AttachmentService.java b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/AttachmentService.java index ff21bfe..45176c7 100644 --- a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/AttachmentService.java +++ b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/AttachmentService.java @@ -16,7 +16,7 @@ package com.hp.alm.ali.idea.services; -import com.hp.alm.ali.idea.cfg.XMLOutputterFactory; +import com.hp.alm.ali.rest.client.XMLOutputterFactory; import com.hp.alm.ali.idea.entity.EntityListener; import com.hp.alm.ali.idea.entity.EntityQuery; import com.hp.alm.ali.idea.entity.EntityRef; @@ -28,6 +28,7 @@ import com.hp.alm.ali.idea.model.Entity; import com.hp.alm.ali.idea.model.parser.EntityList; import org.apache.commons.httpclient.HttpStatus; +import org.jdom.Document; import java.util.Collections; import java.util.HashMap; @@ -103,7 +104,7 @@ public boolean updateAttachmentContent(String name, EntityRef parent, Indicating public Entity updateAttachmentProperty(String name, EntityRef parent, String propertyName, String propertyValue, boolean silent) { Entity attachment = new Entity("attachment", 0); attachment.setProperty(propertyName, propertyValue); - String xml = XMLOutputterFactory.getXMLOutputter().outputString(attachment.toElement(Collections.singleton(propertyName))); + String xml = XMLOutputterFactory.getXMLOutputter().outputString(new Document(attachment.toElement(Collections.singleton(propertyName)))); MyResultInfo result = new MyResultInfo(); if(restService.put(xml, result, "{0}s/{1}/attachments/{2}", parent.type, parent.id, EntityQuery.encode(name)) != HttpStatus.SC_OK) { if(!silent) { diff --git a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/DevMotiveService.java b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/DevMotiveService.java index 8e5469e..4037317 100644 --- a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/DevMotiveService.java +++ b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/DevMotiveService.java @@ -17,7 +17,7 @@ package com.hp.alm.ali.idea.services; import com.hp.alm.ali.idea.cfg.WorkspaceConfiguration; -import com.hp.alm.ali.idea.cfg.XMLOutputterFactory; +import com.hp.alm.ali.rest.client.XMLOutputterFactory; import com.hp.alm.ali.idea.content.devmotive.Commit; import com.hp.alm.ali.idea.content.devmotive.CommitInfo; import com.hp.alm.ali.idea.entity.EntityRef; @@ -27,6 +27,7 @@ import com.hp.alm.ali.idea.ui.editor.field.CommentField; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; +import org.jdom.Document; import org.jdom.Element; import java.util.HashMap; @@ -65,7 +66,7 @@ public Map> getRelatedEntities(List commits) { commitElem.addContent(messageElem); commitsElem.addContent(commitElem); } - String commitRequest = XMLOutputterFactory.getXMLOutputter().outputString(commitsElem); + String commitRequest = XMLOutputterFactory.getXMLOutputter().outputString(new Document(commitsElem)); MyResultInfo result = new MyResultInfo(); int code = restService.post(commitRequest, result, "workspace/{0}/ali/linked-items/commits", workspaceId); diff --git a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/EntityService.java b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/EntityService.java index 3ed8cb7..4fc60f4 100644 --- a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/EntityService.java +++ b/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/services/EntityService.java @@ -16,7 +16,7 @@ package com.hp.alm.ali.idea.services; -import com.hp.alm.ali.idea.cfg.XMLOutputterFactory; +import com.hp.alm.ali.rest.client.XMLOutputterFactory; import com.hp.alm.ali.idea.entity.EntityCrossFilter; import com.hp.alm.ali.idea.entity.EntityFilter; import com.hp.alm.ali.idea.entity.EntityQuery; @@ -38,6 +38,7 @@ import com.intellij.openapi.ui.Messages; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; +import org.jdom.Document; import javax.swing.SortOrder; import java.io.InputStream; @@ -265,7 +266,7 @@ private String orderToString(EntityQuery query) { } private Entity updateOldDefectLink(Entity entity, boolean silent, boolean fireUpdate) { - String xml = XMLOutputterFactory.getXMLOutputter().outputString(DefectLinkList.linkToXml(entity)); + String xml = XMLOutputterFactory.getXMLOutputter().outputString(new Document(DefectLinkList.linkToXml(entity))); MyResultInfo result = new MyResultInfo(); if(restService.put(xml, result, "defects/{0}/defect-links/{1}", entity.getPropertyValue("first-endpoint-id"), entity.getId()) != HttpStatus.SC_OK) { if(!silent) { @@ -293,7 +294,7 @@ public Entity updateEntity(Entity entity, Set fieldsToUpdate, boolean si if("defect-link".equals(entity.getType()) && restService.getServerStrategy().hasSecondLevelDefectLink()) { return updateOldDefectLink(entity, silent, fireUpdate); } - String xml = XMLOutputterFactory.getXMLOutputter().outputString(entity.toElement(fieldsToUpdate)); + String xml = XMLOutputterFactory.getXMLOutputter().outputString(new Document(entity.toElement(fieldsToUpdate))); MyResultInfo result = new MyResultInfo(); if(restService.put(xml, result, "{0}s/{1}", entity.getType(), entity.getId()) != HttpStatus.SC_OK) { if(!silent) { @@ -317,7 +318,7 @@ public Entity updateEntity(Entity entity, Set fieldsToUpdate, boolean si } private Entity createOldDefectLink(Entity entity, boolean silent) { - String xml = XMLOutputterFactory.getXMLOutputter().outputString(DefectLinkList.linkToXml(entity)); + String xml = XMLOutputterFactory.getXMLOutputter().outputString(new Document(DefectLinkList.linkToXml(entity))); MyResultInfo result = new MyResultInfo(); if(restService.post(xml, result, "defects/{0}/defect-links", entity.getPropertyValue("first-endpoint-id")) != HttpStatus.SC_CREATED) { if(!silent) { @@ -335,7 +336,7 @@ public Entity createEntity(Entity entity, boolean silent) { if("defect-link".equals(entity.getType()) && restService.getServerStrategy().hasSecondLevelDefectLink()) { return createOldDefectLink(entity, silent); } - String xml = XMLOutputterFactory.getXMLOutputter().outputString(entity.toElement(null)); + String xml = XMLOutputterFactory.getXMLOutputter().outputString(new Document(entity.toElement(null))); MyResultInfo result = new MyResultInfo(); if(restService.post(xml, result, "{0}s", entity.getType()) != HttpStatus.SC_CREATED) { if(!silent) { diff --git a/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/DevMotiveServiceTest.java b/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/DevMotiveServiceTest.java index 487c478..94d2630 100644 --- a/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/DevMotiveServiceTest.java +++ b/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/DevMotiveServiceTest.java @@ -51,7 +51,7 @@ public void preClean() throws Throwable { @Test public void testGetRelatedEntities() throws IOException { handler.addRequest("POST", "/qcbin/rest/domains/domain/projects/project/workspace/1000/ali/linked-items/commits", 200) - .expectBody(handler.getContent("devMotiveServiceTest_input.xml")) + .expectXmlBody(handler.getContent("devMotiveServiceTest_input.xml")) .content("devMotiveServiceTest_output.xml"); Commit commit1 = new Commit(new MyFileRevision(new MyRevisionNumber("1"), "commit1", new Date(0)), "authorName1", "authorEmail1", "committerName1", "committerEmail1"); Commit commit2 = new Commit(new MyFileRevision(new MyRevisionNumber("2"), "commit2", new Date(1000)), "authorName2", "authorEmail2", null, null); diff --git a/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/EntityServiceTest.java b/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/EntityServiceTest.java index a204b92..d7c7af2 100644 --- a/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/EntityServiceTest.java +++ b/ali-plugin-main/src/test/java/com/hp/alm/ali/idea/services/EntityServiceTest.java @@ -211,7 +211,7 @@ public void testGetDefectLink_Maya() { @TestTarget(ServerVersion.AGM) public void testUpdateEntity() { handler.addRequest(false, "PUT", "/qcbin/rest/domains/domain/projects/project/defects/86", 200) - .expectBody("Closed") + .expectXmlBody("Closed") .content("entityServiceTest_entity.xml"); handler.async(); @@ -287,7 +287,7 @@ public void testUpdateEntity_silentReloadFailure() { @TestTarget(ServerVersion.AGM) public void testUpdateEntity_noEvent() { handler.addRequest(false, "PUT", "/qcbin/rest/domains/domain/projects/project/defects/86", 200) - .expectBody("Closed") + .expectXmlBody("Closed") .content("entityServiceTest_entity.xml"); addEntityListener(new EntityAdapter() { @@ -308,7 +308,7 @@ public void entityLoaded(final Entity entity, final Event event) { @TestTarget(ServerVersion.ALM11) public void testUpdateEntity_MayaLink() { handler.addRequest(false, "PUT", "/qcbin/rest/domains/domain/projects/project/defects/86/defect-links/2763", 200) - .expectBody("Yes!86") + .expectXmlBody("Yes!86") .content("entityServiceTest_defectLink.xml"); handler.async(); @@ -350,7 +350,7 @@ public void testUpdateEntity_MayaLink_silentFailure() { @TestTarget(ServerVersion.ALM11) public void testUpdateEntity_MayaLink_noEvent() { handler.addRequest(false, "PUT", "/qcbin/rest/domains/domain/projects/project/defects/86/defect-links/2763", 200) - .expectBody("Yes!86") + .expectXmlBody("Yes!86") .content("entityServiceTest_defectLink.xml"); addEntityListener(new EntityAdapter() { @@ -371,7 +371,7 @@ public void entityLoaded(final Entity entity, final Event event) { @TestTarget(ServerVersion.AGM) public void testCreateEntity() { handler.addRequest(false, "POST", "/qcbin/rest/domains/domain/projects/project/defects", 201) - .expectBody("New") + .expectXmlBody("New") .content("entityServiceTest_entity.xml"); handler.async(); @@ -410,7 +410,7 @@ public void testCreateEntity_silentFailure() { @TestTarget(ServerVersion.ALM11) public void testCreateEntity_MayaLink() { handler.addRequest(false, "POST", "/qcbin/rest/domains/domain/projects/project/defects/86/defect-links", 201) - .expectBody("There86") + .expectXmlBody("There86") .content("entityServiceTest_defectLink.xml"); handler.async(); diff --git a/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/AliRestClient.java b/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/AliRestClient.java index e9df50c..e5315ff 100644 --- a/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/AliRestClient.java +++ b/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/AliRestClient.java @@ -29,8 +29,8 @@ import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.io.IOUtils; +import org.jdom.Document; import org.jdom.Element; -import org.jdom.output.XMLOutputter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; @@ -295,7 +295,7 @@ private String createAuthXml() { Element passElem = new Element("password"); passElem.setText(password); authElem.addContent(passElem); - return new XMLOutputter().outputString(authElem); + return XMLOutputterFactory.getXMLOutputter().outputString(new Document(authElem)); } private String createRestSessionXml() { @@ -303,7 +303,7 @@ private String createRestSessionXml() { Element clientTypeElem = new Element("client-type"); sessionParamElem.addContent(clientTypeElem); clientTypeElem.setText(CLIENT_TYPE); - return new XMLOutputter().outputString(sessionParamElem); + return XMLOutputterFactory.getXMLOutputter().outputString(new Document(sessionParamElem)); } private void addTenantCookie(Cookie ssoCookie) { diff --git a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/cfg/XMLOutputterFactory.java b/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/XMLOutputterFactory.java similarity index 90% rename from ali-plugin-main/src/main/java/com/hp/alm/ali/idea/cfg/XMLOutputterFactory.java rename to ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/XMLOutputterFactory.java index 8903aa6..4c633b6 100644 --- a/ali-plugin-main/src/main/java/com/hp/alm/ali/idea/cfg/XMLOutputterFactory.java +++ b/ali-rest-client/src/main/java/com/hp/alm/ali/rest/client/XMLOutputterFactory.java @@ -1,6 +1,6 @@ // (C) Copyright 2003-2015 Hewlett-Packard Development Company, L.P. -package com.hp.alm.ali.idea.cfg; +package com.hp.alm.ali.rest.client; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; diff --git a/ali-rest-client/src/test/java/com/hp/alm/ali/rest/client/AliRestClientTest.java b/ali-rest-client/src/test/java/com/hp/alm/ali/rest/client/AliRestClientTest.java index 745cb79..4fb3f49 100644 --- a/ali-rest-client/src/test/java/com/hp/alm/ali/rest/client/AliRestClientTest.java +++ b/ali-rest-client/src/test/java/com/hp/alm/ali/rest/client/AliRestClientTest.java @@ -108,7 +108,7 @@ public void testLogin_Maya() throws Exception { handler.addRequest("GET", "/qcbin/authentication-point/authenticate", 200) .expectHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA=="); handler.addRequest("POST", "/qcbin/rest/site-session", 200) - .expectBody("ALI_IDEA_plugin"); + .expectXmlBody("ALI_IDEA_plugin"); AliRestClient client = AliRestClient.create(handler.getQcUrl(), "domain", "project", "user", "password", RestClient.SessionStrategy.NONE); client.login(); @@ -125,7 +125,7 @@ public void testLogin_ignoreNTLM() { handler.addRequest("GET", "/qcbin/authentication-point/authenticate", 200) .expectHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA=="); handler.addRequest("POST", "/qcbin/rest/site-session", 200) - .expectBody("ALI_IDEA_plugin"); + .expectXmlBody("ALI_IDEA_plugin"); AliRestClient client = AliRestClient.create(handler.getQcUrl(), "domain", "project", "user", "password", RestClient.SessionStrategy.NONE); client.login(); @@ -178,9 +178,9 @@ public void testSetHttpProxyCredentials() throws Exception { .responseBody("Proxy Authentication Required"); handler.addRequest("POST", "/qcbin/authentication-point/alm-authenticate", 200) .expectHeader("Proxy-Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") - .expectBody("qc_userqc_password"); + .expectXmlBody("qc_userqc_password"); handler.addRequest("POST", "/qcbin/rest/site-session", 200) - .expectBody("ALI_IDEA_plugin"); + .expectXmlBody("ALI_IDEA_plugin"); handler.addRequest("GET", "/qcbin/rest/domains/domain/projects/project/test", 200); AliRestClient client = AliRestClient.create("http://foo/qcbin", "domain", "project", "qc_user", "qc_password", RestClient.SessionStrategy.AUTO_LOGIN); @@ -197,9 +197,9 @@ public void testNTLMEnabledProxy() throws Exception { .responseBody("Proxy Authentication Required"); handler.addRequest("POST", "/qcbin/authentication-point/alm-authenticate", 200) .expectHeader("Proxy-Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") - .expectBody("qc_userqc_password"); + .expectXmlBody("qc_userqc_password"); handler.addRequest("POST", "/qcbin/rest/site-session", 200) - .expectBody("ALI_IDEA_plugin"); + .expectXmlBody("ALI_IDEA_plugin"); handler.addRequest("GET", "/qcbin/rest/domains/domain/projects/project/test", 200); AliRestClient client = AliRestClient.create("http://foo/qcbin", "domain", "project", "qc_user", "qc_password", RestClient.SessionStrategy.AUTO_LOGIN); diff --git a/ali-test-support/src/main/java/com/hp/alm/ali/Handler.java b/ali-test-support/src/main/java/com/hp/alm/ali/Handler.java index f51f4b8..d9d942a 100644 --- a/ali-test-support/src/main/java/com/hp/alm/ali/Handler.java +++ b/ali-test-support/src/main/java/com/hp/alm/ali/Handler.java @@ -173,9 +173,9 @@ public void handle(String url, Request baseRequest, HttpServletRequest request, public void authenticate() { addRequest("POST", "/qcbin/authentication-point/alm-authenticate", 200) - .expectBody("userpassword"); + .expectXmlBody("userpassword"); addRequest("POST", "/qcbin/rest/site-session", 200) - .expectBody("ALI_IDEA_plugin"); + .expectXmlBody("ALI_IDEA_plugin"); } public void checkpoint() throws Throwable { @@ -289,6 +289,18 @@ public void evaluate(Request baseRequest, HttpServletRequest request, HttpServle return this; } + public MyRequest expectXmlBody(final String value) { + assertions.add(new Assertion() { + @Override + public void evaluate(Request baseRequest, HttpServletRequest request, HttpServletResponse response, boolean soft) throws IOException { + if(!soft) { + Assert.assertEquals("\n" + value.replaceAll("\r\n", "\n") + "\n", IOUtils.toString(baseRequest.getInputStream()).replaceAll("\r\n", "\n")); + } + } + }); + return this; + } + public MyRequest expectBody(final String value) { assertions.add(new Assertion() { @Override