]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
db00786bf125d7421f7f4a573982801523c95f2a
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.client.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import javax.ws.rs.core.MultivaluedMap;
6 import javax.ws.rs.core.Response;
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.Marshaller;
9 import org.collectionspace.hello.CollectionObject;
10 import org.collectionspace.hello.CollectionObjectList;
11 import org.collectionspace.hello.client.CollectionObjectClient;
12 import org.jboss.resteasy.client.ClientResponse;
13 import org.testng.Assert;
14 import org.testng.annotations.Test;
15
16 /**
17  * A CollectionObjectTest.
18  * 
19  * @version $Revision:$
20  */
21 public class CollectionObjectTest {
22
23     private CollectionObjectClient client = CollectionObjectClient.getInstance();
24     private String updateId = "1";
25
26 /*
27     @Test
28     public void createCollectionObject() {
29         CollectionObject CollectionObject = createCollectionObject("Chris", "Hoffman");
30         ClientResponse<Response> res = CollectionObjectClient.createCollectionObject(CollectionObject);
31         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
32         //store updateId locally
33         updateId = extractId(res);
34     }
35
36     @Test
37     public void createCollectionObjectList() {
38         CollectionObject CollectionObject = createCollectionObject("Aron", "Roberts");
39         ClientResponse<Response> res = CollectionObjectClient.createCollectionObject(CollectionObject);
40         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
41         CollectionObject = createCollectionObject("Dan", "Sheppard");
42         res = CollectionObjectClient.createCollectionObject(CollectionObject);
43         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
44     }
45
46     @Test(dependsOnMethods = {"createCollectionObject"})
47     public void updateCollectionObject() {
48         CollectionObject touCollectionObject = CollectionObjectClient.getCollectionObject(updateId).getEntity();
49         verbose("got CollectionObject to update", touCollectionObject, CollectionObject.class);
50         touCollectionObject.setFirstName("Richard");
51         touCollectionObject.setLastName("Millet");
52         int initialVersion = touCollectionObject.getVersion();
53         CollectionObject uCollectionObject = CollectionObjectClient.updateCollectionObject(updateId, touCollectionObject).getEntity();
54         verbose("updated CollectionObject", uCollectionObject, CollectionObject.class);
55         Assert.assertNotSame(uCollectionObject.getVersion(), initialVersion);
56         Assert.assertEquals(uCollectionObject.getFirstName(), "Richard");
57     }
58
59     @Test(dependsOnMethods = {"createCollectionObject"})
60     public void getCollectionObjectList() {
61         //the resource method is expected to return at least an empty list
62         CollectionObjectList CollectionObjectList = CollectionObjectClient.getCollectionObjectList().getEntity();
63         List<CollectionObjectList.CollectionObjectListItem> list = CollectionObjectList.getCollectionObjectListItem();
64         int i = 0;
65         for (CollectionObjectList.CollectionObjectListItem pli : list) {
66             verbose("getCollectionObjectList: list-item[" + i + "] firstName=" + pli.getFirstName());
67             verbose("getCollectionObjectList: list-item[" + i + "] lastName=" + pli.getLastName());
68             verbose("getCollectionObjectList: list-item[" + i + "] uri=" + pli.getUri());
69             i++;
70         }
71     }
72
73 */
74     @Test
75     public void getNonExistingCollectionObject() {
76         ClientResponse<CollectionObject> res = client.getCollectionObject("foo");
77
78         Response.Status status = res.getResponseStatus();
79         verbose(this.getClass().getName() + ": " +
80                 "getNonExistingCollectionObject: Status: code=" + status.getStatusCode() +
81                 " message=" + status.toString());
82         verbose("getNonExistingCollectionObject: Metadata:");
83         verboseMap(res.getMetadata());
84         verbose("getNonExistingCollectionObject: Headers:");
85         verboseMap(res.getHeaders());
86         if (status.equals(Response.Status.NOT_FOUND)) {
87             String msg = res.getEntity(String.class, String.class);
88             verbose("getNonExistingCollectionObject: error message=" + msg);
89         }
90     }
91
92 /*
93     @Test(dependsOnMethods = {"updateCollectionObject"})
94     public void updateWrongCollectionObject() {
95         CollectionObject touCollectionObject = CollectionObjectClient.getCollectionObject(updateId).getEntity();
96         verbose("updateWrongCollectionObject: got CollectionObject to update", touCollectionObject, CollectionObject.class);
97         touCollectionObject.setFirstName("Richard");
98         touCollectionObject.setLastName("Millet");
99         //use non existing CollectionObject to update
100         ClientResponse<CollectionObject> res = CollectionObjectClient.updateCollectionObject(9999L, touCollectionObject);
101         if (res.getResponseStatus().equals(Response.Status.NOT_FOUND)) {
102             verbose("updateWrongCollectionObject: Status=" + res.getResponseStatus().toString());
103             String msg = res.getEntity(String.class, String.class);
104             verbose("updateWrongCollectionObject: application error message=" + msg);
105         }
106     }
107
108
109     @Test(dependsOnMethods = {"updateWrongCollectionObject"})
110     public void deleteCollectionObject() {
111         ClientResponse<Response> res = CollectionObjectClient.deleteCollectionObject(updateId);
112         verbose("deleteCollectionObject: id=" + updateId);
113         verbose("deleteCollectionObject: status = " + res.getStatus());
114         Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
115     }
116     
117     private CollectionObject createCollectionObject(String firstName, String lastName) {
118         CollectionObject CollectionObject = new CollectionObject();
119         CollectionObject.setFirstName(firstName);
120         CollectionObject.setLastName(lastName);
121         CollectionObject.setStreet("2195 Hearst Ave.");
122         CollectionObject.setCity("Berkeley");
123         CollectionObject.setState("CA");
124         CollectionObject.setZip("94704");
125         CollectionObject.setCountry("US");
126         return CollectionObject;
127     }
128
129     private Long extractId(ClientResponse<Response> res) {
130         MultivaluedMap mvm = res.getMetadata();
131         String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
132         String[] segments = uri.split("/");
133         verbose("id=" + segments[segments.length - 1]);
134         return Long.valueOf(segments[segments.length - 1]);
135     }
136
137 */
138
139     private void verbose(String msg) {
140         System.out.println("CollectionObjectListerviceTest : " + msg);
141     }
142
143     private void verbose(String msg, Object o, Class clazz) {
144         try {
145             verbose(msg);
146             JAXBContext jc = JAXBContext.newInstance(clazz);
147             Marshaller m = jc.createMarshaller();
148             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
149                     Boolean.TRUE);
150             m.marshal(o, System.out);
151         //m.marshal(new JAXBElement(new QName("uri", "local"), CollectionObject.class, p), System.out);
152         } catch (Exception e) {
153             e.printStackTrace();
154         }
155     }
156
157     private void verboseMap(MultivaluedMap map) {
158         for (Object entry : map.entrySet()) {
159             MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
160             verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
161         }
162     }
163     
164
165 }