]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b5aa197aa7f05e8261b997685d848737396ba29e
[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.CollectionObjectListItem;
12 import org.collectionspace.hello.DefaultCollectionObject;
13 import org.collectionspace.hello.client.CollectionObjectClient;
14 import org.jboss.resteasy.client.ClientResponse;
15 import org.testng.Assert;
16 import org.testng.annotations.Test;
17
18 /**
19  * A CollectionObjectTest.
20  * 
21  * @version $Revision:$
22  */
23 public class CollectionObjectTest {
24
25     private CollectionObjectClient client = CollectionObjectClient.getInstance();
26     
27     private String updateId = "1";
28     private String nonexistentId = "foo";
29
30
31     @Test
32     public void createCollectionObject() {
33         CollectionObject co =
34           createCollectionObject("1984.021.0049", "Radio News, vol. 10, no. 2, August 1928");
35         ClientResponse<Response> res = client.createCollectionObject(co);
36         verbose("created status=" + Response.Status.CREATED.getStatusCode());
37         verbose("response status=" + Response.Status.CREATED.getStatusCode());
38         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
39         //store updateId locally
40         updateId = extractId(res);
41     }
42
43     @Test
44     public void createCollectionObjectList() {
45         CollectionObject co =
46           createCollectionObject("1997.005.0437", "Toy, Gotham City Police Helicopter, 1992");
47         ClientResponse<Response> res = client.createCollectionObject(co);
48         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
49         co = createCollectionObject(
50           "1984.052.0001", " Pathescope Model 9061 28mm motion picture projector, 1914");
51         res = client.createCollectionObject(co);
52         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
53     }
54
55     @Test(dependsOnMethods = {"createCollectionObject"})
56     public void updateCollectionObject() {
57         CollectionObject touCollectionObject =
58           client.getCollectionObject(updateId).getEntity();
59         verbose("got CollectionObject to update", touCollectionObject, CollectionObject.class);
60         if ( touCollectionObject.getDefaultCollectionObject() == null ) {
61           touCollectionObject.setDefaultCollectionObject( new DefaultCollectionObject() );
62         }
63         String updatedObjNum = "1981.297.0049";
64         touCollectionObject.getDefaultCollectionObject().setObjectNumber(updatedObjNum);
65         touCollectionObject.getDefaultCollectionObject().setObjectName(
66           "Preview slide, It's the Old Army Game, 1926");
67         // int initialVersion = touCollectionObject.getVersion();
68         CollectionObject uCollectionObject =
69         client.updateCollectionObject(updateId, touCollectionObject).getEntity();
70         verbose("updated CollectionObject", uCollectionObject, CollectionObject.class);
71         // Assert.assertNotSame(uCollectionObject.getVersion(), initialVersion);
72         Assert.assertEquals(
73           uCollectionObject.getDefaultCollectionObject().getObjectNumber(), updatedObjNum);
74     }
75
76     @Test(dependsOnMethods = {"createCollectionObject"})
77     public void getCollectionObjectList() {
78         //the resource method is expected to return at least an empty list
79         CollectionObjectList coList = client.getCollectionObjectList().getEntity();
80         List<CollectionObjectListItem> list = coList.getCollectionObjectListItem();
81         int i = 0;
82         for (CollectionObjectListItem pli : list) {
83             verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
84             verbose("getCollectionObjectList: list-item[" + i + "] objectName=" + pli.getObjectName());
85             verbose("getCollectionObjectList: list-item[" + i + "] uri=" + pli.getUri());
86             i++;
87         }
88     }
89
90
91     @Test
92     public void getNonExistingCollectionObject() {
93         ClientResponse<CollectionObject> res = client.getCollectionObject(nonexistentId);
94
95         Response.Status status = res.getResponseStatus();
96         verbose(this.getClass().getName() + ": " +
97                 "getNonExistingCollectionObject: Status: code=" + status.getStatusCode() +
98                 " message=" + status.toString());
99         verbose("getNonExistingCollectionObject: Metadata:");
100         verboseMap(res.getMetadata());
101         verbose("getNonExistingCollectionObject: Headers:");
102         verboseMap(res.getHeaders());
103         if (status.equals(Response.Status.NOT_FOUND)) {
104             String msg = res.getEntity(String.class, String.class);
105             verbose("getNonExistingCollectionObject: error message=" + msg);
106         }
107     }
108
109     @Test(dependsOnMethods = {"updateCollectionObject"})
110     public void updateWrongCollectionObject() {
111         CollectionObject touCollectionObject =
112           client.getCollectionObject(updateId).getEntity();
113         verbose("updateWrongCollectionObject: got CollectionObject to update", touCollectionObject, CollectionObject.class);
114         if ( touCollectionObject.getDefaultCollectionObject() == null ) {
115           touCollectionObject.setDefaultCollectionObject( new DefaultCollectionObject() );
116         }
117         String updatedObjNum = "1981.297.0049";
118         touCollectionObject.getDefaultCollectionObject().setObjectNumber(updatedObjNum);
119         touCollectionObject.getDefaultCollectionObject().setObjectName(
120           "Preview slide, It's the Old Army Game, 1926");
121         //use non existing CollectionObject to update
122         ClientResponse<CollectionObject> res =
123           client.updateCollectionObject(nonexistentId, touCollectionObject);
124         if (res.getResponseStatus().equals(Response.Status.NOT_FOUND)) {
125             verbose("updateWrongCollectionObject: Status=" + res.getResponseStatus().toString());
126             String msg = res.getEntity(String.class, String.class);
127             verbose("updateWrongCollectionObject: application error message=" + msg);
128         }
129     }
130
131
132     @Test(dependsOnMethods = {"updateWrongCollectionObject"})
133     public void deleteCollectionObject() {
134         ClientResponse<Response> res = client.deleteCollectionObject(updateId);
135         verbose("deleteCollectionObject: id=" + updateId);
136         verbose("deleteCollectionObject: status = " + res.getStatus());
137         Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
138     }
139     
140     // ###################################################################################
141     // Utility methods used by tests above
142     // ###################################################################################
143     
144     private CollectionObject createCollectionObject(String objectNumber, String objectName) {
145         verbose("objectNumber=" + objectNumber);
146         verbose("objectName=" + objectName);
147         verbose("Before new CollectionObject() ...");
148         CollectionObject co = new CollectionObject();
149         verbose("Before co.getDefaultCollectionObject().setObjectNumber ...");
150         co.setDefaultCollectionObject( new DefaultCollectionObject() );
151         co.getDefaultCollectionObject().setObjectNumber(objectNumber);
152         co.getDefaultCollectionObject().setObjectName(objectName);
153         return co;
154     }
155
156     private String extractId(ClientResponse<Response> res) {
157         MultivaluedMap mvm = res.getMetadata();
158         String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
159         String[] segments = uri.split("/");
160         String id = segments[segments.length - 1];
161         verbose("id=" + id);
162         return id;
163     }
164
165
166     private void verbose(String msg) {
167         System.out.println("CollectionObjectServiceTest : " + msg);
168     }
169
170     private void verbose(String msg, Object o, Class clazz) {
171         try {
172             verbose(msg);
173             JAXBContext jc = JAXBContext.newInstance(clazz);
174             Marshaller m = jc.createMarshaller();
175             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
176                     Boolean.TRUE);
177             m.marshal(o, System.out);
178         //m.marshal(new JAXBElement(new QName("uri", "local"), CollectionObject.class, p), System.out);
179         } catch (Exception e) {
180             e.printStackTrace();
181         }
182     }
183
184     private void verboseMap(MultivaluedMap map) {
185         for (Object entry : map.entrySet()) {
186             MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
187             verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
188         }
189     }
190     
191
192 }