2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 package org.collectionspace.services.client.test;
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.Marshaller;
32 import org.jboss.resteasy.client.ClientResponse;
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
36 import org.collectionspace.services.collectionobject.CollectionObject;
37 import org.collectionspace.services.collectionobject.CollectionObjectList;
38 import org.collectionspace.services.client.CollectionObjectClient;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * CollectionObjectServiceTest, carries out tests against a
44 * deployed and running CollectionObject Service.
46 * $LastChangedRevision$
49 public class CollectionObjectServiceTest {
51 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
52 private String knownCollectionObjectId = null;
53 private final String NON_EXISTENT_ID = createNonExistentIdentifier();
54 final Logger logger = LoggerFactory.getLogger(CollectionObjectServiceTest.class);
57 // ---------------------------------------------------------------
58 // Service Discovery tests
59 // ---------------------------------------------------------------
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
72 * Tests creation of a new CollectionObject.
74 * Expected status code: 200 OK
76 * Also expected: The 'Location' header contains the URL for the newly created object.
77 * This is required by the extractId() utility method, below.
79 * The newly-created CollectionObject is also used by other test(s)
80 * (e.g. update, delete) which follow, below.
83 public void createCollectionObject() {
84 String identifier = this.createIdentifier();
86 CollectionObject collectionObject = createCollectionObject(identifier);
87 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
88 verbose("createCollectionObject: status = " + res.getStatus());
89 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
91 // Store the ID returned from this create operation for additional tests below.
92 knownCollectionObjectId = extractId(res);
96 * Tests creation of two or more new CollectionObjects.
98 * Expected status code: 200 OK
100 * The newly-created CollectionObjects are also used by other test(s)
101 * (e.g. read multiple/list) which follow, below.
103 @Test(dependsOnMethods = {"createCollectionObject"})
104 public void createCollection() {
105 for(int i = 0; i < 3; i++){
106 this.createCollectionObject();
114 * Tests creation of a CollectionObject by sending a null to the client proxy.
116 * Expected status code: (none)
118 * Expected result: NullPointerException
119 * (Make sure this is a reported exception in the called class.)
121 @Test(dependsOnMethods = {"createCollectionObject"}, expectedExceptions = NullPointerException.class)
122 public void createNullCollectionObject() {
123 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(null);
127 * Tests creation of a CollectionObject by sending data in the wrong format
128 * (i.e. any format that doesn't match the CollectionObject schema) in the
129 * entity body of the request.
131 * Expected status code: 400 Bad Request
134 @Test(dependsOnMethods = {"createCollectionObject"})
135 public void createCollectionObjectWithWrongDataFormat() {
136 // Currently only a stub
141 * Tests creation of a duplicate CollectionObject, whose unique resource identifier
142 * duplicates that of an existing CollectionObject.
144 * Expected status code: 409 Conflict
147 @Test(dependsOnMethods = {"createCollectionObject"})
148 public void createDuplicateCollectionObject() {
149 CollectionObject collectionObject = createCollectionObject(knownCollectionObjectId);
150 ClientResponse<Response> res =
151 collectionObjectClient.createCollectionObject(collectionObject);
152 verbose("createDuplicateCollectionObject: status = " + res.getStatus());
153 Assert.assertEquals(res.getStatus(), Response.Status.CONFLICT.getStatusCode());
157 // ---------------------------------------------------------------
158 // CRUD tests : READ tests
159 // ---------------------------------------------------------------
165 * Tests reading (i.e. retrieval) of a CollectionObject.
167 * Expected status code: 200 OK
169 @Test(dependsOnMethods = {"createCollectionObject"})
170 public void getCollectionObject() {
171 ClientResponse<CollectionObject> res =
172 collectionObjectClient.getCollectionObject(knownCollectionObjectId);
173 verbose("getCollectionObject: status = " + res.getStatus());
174 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
181 * Tests reading (i.e. retrieval) of a non-existent CollectionObject,
182 * whose resource identifier does not exist at the specified URL.
184 * Expected status code: 404 Not Found
186 @Test(dependsOnMethods = {"createCollectionObject"})
187 public void getNonExistentCollectionObject() {
188 ClientResponse<CollectionObject> res =
189 collectionObjectClient.getCollectionObject(NON_EXISTENT_ID);
190 verbose("getNonExistentCollectionObject: status = " + res.getStatus());
191 Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
194 // ---------------------------------------------------------------
195 // CRUD tests : READ (list, or multiple) tests
196 // ---------------------------------------------------------------
202 * Tests reading (i.e. retrieval) of a list of multiple CollectionObjects.
204 * Expected status code: 200 OK
206 * Also expected: The entity body in the response contains
207 * a representation of the list of CollectionObjects.
209 @Test(dependsOnMethods = {"createCollection"})
210 public void getCollectionObjectList() {
211 // The resource method is expected to return at least an empty list
212 ClientResponse<CollectionObjectList> res = collectionObjectClient.getCollectionObjectList();
213 CollectionObjectList coList = res.getEntity();
214 verbose("getCollectionObjectList: status = " + res.getStatus());
215 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
217 List<CollectionObjectList.CollectionObjectListItem> coItemList =
218 coList.getCollectionObjectListItem();
220 for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
221 verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
222 verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
223 verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
229 * Tests reading (i.e. retrieval) of a list of multiple CollectionObjects
230 * when the contents of the list are expected to be empty.
232 * Expected status code: 200 OK
233 * (Note: *not* 204 No Content)
235 * Also expected: The entity body in the response contains
236 * a representation of an empty list of CollectionObjects.
239 @Test(dependsOnMethods = {"createCollection"})
240 public void getCollectionObjectEmptyList() {
247 // None known at present.
250 // ---------------------------------------------------------------
251 // CRUD tests : UPDATE tests
252 // ---------------------------------------------------------------
258 * Tests updating the content of a CollectionObject.
260 * Expected status code: 200 OK
262 * Also expected: The entity body in the response contains
263 * a representation of the updated CollectionObject.
265 @Test(dependsOnMethods = {"createCollectionObject"})
266 public void updateCollectionObject() {
267 ClientResponse<CollectionObject> res =
268 collectionObjectClient.getCollectionObject(knownCollectionObjectId);
269 verbose("getCollectionObject: status = " + res.getStatus());
270 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
271 CollectionObject collectionObject = res.getEntity();
272 verbose("Got CollectionObject to update with ID: " + knownCollectionObjectId,
273 collectionObject, CollectionObject.class);
275 //collectionObject.setCsid("updated-" + knownCollectionObjectId);
276 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
277 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
279 // make call to update service
281 collectionObjectClient.updateCollectionObject(knownCollectionObjectId, collectionObject);
282 verbose("updateCollectionObject: status = " + res.getStatus());
283 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
285 // check the response
286 CollectionObject updatedCollectionObject = res.getEntity();
287 Assert.assertEquals(updatedCollectionObject.getObjectName(),
288 collectionObject.getObjectName());
289 verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
296 * Tests updating the content of a non-existent CollectionObject, whose
297 * resource identifier does not exist.
299 * Expected status code: 404 Not Found
301 @Test(dependsOnMethods = {"updateCollectionObject"})
302 public void updateNonExistentCollectionObject() {
303 // Note: The ID used in this call may not be relevant, only the ID used
304 // in updateCollectionObject(), below.
305 CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
306 // make call to update service
307 ClientResponse<CollectionObject> res =
308 collectionObjectClient.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
309 verbose("createCollectionObject: status = " + res.getStatus());
310 Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
314 // ---------------------------------------------------------------
315 // CRUD tests : DELETE tests
316 // ---------------------------------------------------------------
322 * Tests deleting a CollectionObject.
324 * Expected status code: 200 OK
326 @Test(dependsOnMethods = {"createCollectionObject"})
327 public void deleteCollectionObject() {
328 verbose("Calling deleteCollectionObject:" + knownCollectionObjectId);
329 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId);
330 verbose("deleteCollectionObject: status = " + res.getStatus());
331 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
338 * Tests deleting a non-existent CollectionObject, whose
339 * resource identifier does not exist at the specified URL.
341 * Expected status code: 404 Not Found
343 @Test(dependsOnMethods = {"deleteCollectionObject"})
344 public void deleteNonExistentCollectionObject() {
345 verbose("Calling deleteCollectionObject:" + NON_EXISTENT_ID);
346 ClientResponse<Response> res =
347 collectionObjectClient.deleteCollectionObject(NON_EXISTENT_ID);
348 verbose("deleteCollectionObject: status = " + res.getStatus());
349 Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
353 // ---------------------------------------------------------------
354 // Utility methods used by tests above
355 // ---------------------------------------------------------------
357 private CollectionObject createCollectionObject(String identifier) {
358 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
359 "objectName-" + identifier);
361 return collectionObject;
364 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
365 CollectionObject collectionObject = new CollectionObject();
367 collectionObject.setObjectNumber(objectNumber);
368 collectionObject.setObjectName(objectName);
370 return collectionObject;
373 private String extractId(ClientResponse<Response> res) {
374 MultivaluedMap mvm = res.getMetadata();
375 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
376 verbose("extractId:uri=" + uri);
377 String[] segments = uri.split("/");
378 String id = segments[segments.length - 1];
383 private void verbose(String msg) {
384 // if(logger.isInfoEnabled()){
385 // logger.debug(msg);
387 System.out.println(msg);
390 private void verbose(String msg, Object o, Class clazz) {
393 JAXBContext jc = JAXBContext.newInstance(clazz);
394 Marshaller m = jc.createMarshaller();
395 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
397 m.marshal(o, System.out);
403 private void verboseMap(MultivaluedMap map) {
404 for(Object entry : map.entrySet()){
405 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
406 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
410 private String createIdentifier() {
411 long identifier = System.currentTimeMillis();
412 return Long.toString(identifier);
415 private String createNonExistentIdentifier() {
416 return Long.toString(Long.MAX_VALUE);