Skip to content

Commit

Permalink
issue 39: include xml declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojtech Janota committed Sep 16, 2015
1 parent 9bcab3a commit 7deda59
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Map<Commit, List<EntityRef>> getRelatedEntities(List<Commit> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -293,7 +294,7 @@ public Entity updateEntity(Entity entity, Set<String> 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) {
Expand All @@ -317,7 +318,7 @@ public Entity updateEntity(Entity entity, Set<String> 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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>Closed</Value></Field></Fields></Entity>")
.expectXmlBody("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>Closed</Value></Field></Fields></Entity>")
.content("entityServiceTest_entity.xml");

handler.async();
Expand Down Expand Up @@ -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("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>Closed</Value></Field></Fields></Entity>")
.expectXmlBody("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>Closed</Value></Field></Fields></Entity>")
.content("entityServiceTest_entity.xml");

addEntityListener(new EntityAdapter() {
Expand All @@ -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("<defect-link><comment>Yes!</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.expectXmlBody("<defect-link><comment>Yes!</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.content("entityServiceTest_defectLink.xml");

handler.async();
Expand Down Expand Up @@ -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("<defect-link><comment>Yes!</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.expectXmlBody("<defect-link><comment>Yes!</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.content("entityServiceTest_defectLink.xml");

addEntityListener(new EntityAdapter() {
Expand All @@ -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("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>New</Value></Field></Fields></Entity>")
.expectXmlBody("<Entity Type=\"defect\"><Fields><Field Name=\"status\"><Value>New</Value></Field></Fields></Entity>")
.content("entityServiceTest_entity.xml");

handler.async();
Expand Down Expand Up @@ -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("<defect-link><comment>There</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.expectXmlBody("<defect-link><comment>There</comment><first-endpoint-id>86</first-endpoint-id></defect-link>")
.content("entityServiceTest_defectLink.xml");

handler.async();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -295,15 +295,15 @@ 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() {
Element sessionParamElem = new Element("session-parameters");
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
.expectXmlBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");

AliRestClient client = AliRestClient.create(handler.getQcUrl(), "domain", "project", "user", "password", RestClient.SessionStrategy.NONE);
client.login();
Expand All @@ -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("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
.expectXmlBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");

AliRestClient client = AliRestClient.create(handler.getQcUrl(), "domain", "project", "user", "password", RestClient.SessionStrategy.NONE);
client.login();
Expand Down Expand Up @@ -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("<alm-authentication><user>qc_user</user><password>qc_password</password></alm-authentication>");
.expectXmlBody("<alm-authentication><user>qc_user</user><password>qc_password</password></alm-authentication>");
handler.addRequest("POST", "/qcbin/rest/site-session", 200)
.expectBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
.expectXmlBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
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);
Expand All @@ -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("<alm-authentication><user>qc_user</user><password>qc_password</password></alm-authentication>");
.expectXmlBody("<alm-authentication><user>qc_user</user><password>qc_password</password></alm-authentication>");
handler.addRequest("POST", "/qcbin/rest/site-session", 200)
.expectBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
.expectXmlBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
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);
Expand Down
16 changes: 14 additions & 2 deletions ali-test-support/src/main/java/com/hp/alm/ali/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<alm-authentication><user>user</user><password>password</password></alm-authentication>");
.expectXmlBody("<alm-authentication><user>user</user><password>password</password></alm-authentication>");
addRequest("POST", "/qcbin/rest/site-session", 200)
.expectBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
.expectXmlBody("<session-parameters><client-type>ALI_IDEA_plugin</client-type></session-parameters>");
}

public void checkpoint() throws Throwable {
Expand Down Expand Up @@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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
Expand Down

0 comments on commit 7deda59

Please sign in to comment.