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.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
31 import org.collectionspace.services.client.CollectionObjectClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.collectionobject.CollectionObject;
34 import org.collectionspace.services.collectionobject.CollectionObjectList;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * CollectionObjectServiceTest, carries out tests against a
43 * deployed and running CollectionObject Service.
45 * $LastChangedRevision$
48 public class CollectionObjectServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private CollectionObjectClient client = new CollectionObjectClient();
52 final String SERVICE_PATH_COMPONENT = "collectionobjects";
53 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
63 public void create() {
65 // Perform setup, such as initializing the type of service request
66 // (e.g. CREATE, DELETE), its valid and expected status codes, and
67 // its associated HTTP method name (e.g. POST, DELETE).
70 // Submit the request to the service and store the response.
71 String identifier = createIdentifier();
72 CollectionObject collectionObject =
73 createCollectionObjectInstance(identifier);
74 ClientResponse<Response> res = client.create(collectionObject);
75 int statusCode = res.getStatus();
77 // Check the status code of the response: does it match
78 // the expected response(s)?
81 // Does it fall within the set of valid status codes?
82 // Does it exactly match the expected status code?
83 verbose("create: status = " + statusCode);
84 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
85 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
88 // Store the ID returned from this create operation
89 // for additional tests below.
90 knownResourceId = extractId(res);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 for(int i = 0; i < 3; i++){
103 // Placeholders until the three tests below can be uncommented.
104 // See Issue CSPACE-401.
105 public void createWithEmptyEntityBody() {}
106 public void createWithMalformedXml() {}
107 public void createWithWrongXmlSchema() {}
111 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112 public void createWithEmptyEntityBody() {
115 setupCreateWithEmptyEntityBody();
117 // Submit the request to the service and store the response.
118 String method = REQUEST_TYPE.httpMethodName();
119 String url = getServiceRootURL();
120 String mediaType = MediaType.APPLICATION_XML;
121 final String entity = "";
122 int statusCode = submitRequest(method, url, mediaType, entity);
124 // Check the status code of the response: does it match
125 // the expected response(s)?
126 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
133 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
134 public void createWithMalformedXml() {
137 setupCreateWithMalformedXml();
139 // Submit the request to the service and store the response.
140 String method = REQUEST_TYPE.httpMethodName();
141 String url = getServiceRootURL();
142 String mediaType = MediaType.APPLICATION_XML;
143 final String entity = MALFORMED_XML_DATA; // Constant from base class.
144 int statusCode = submitRequest(method, url, mediaType, entity);
146 // Check the status code of the response: does it match
147 // the expected response(s)?
148 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
156 public void createWithWrongXmlSchema() {
159 setupCreateWithWrongXmlSchema();
161 // Submit the request to the service and store the response.
162 String method = REQUEST_TYPE.httpMethodName();
163 String url = getServiceRootURL();
164 String mediaType = MediaType.APPLICATION_XML;
165 final String entity = WRONG_XML_SCHEMA_DATA;
166 int statusCode = submitRequest(method, url, mediaType, entity);
168 // Check the status code of the response: does it match
169 // the expected response(s)?
170 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 // ---------------------------------------------------------------
178 // CRUD tests : READ tests
179 // ---------------------------------------------------------------
184 @Test(dependsOnMethods = {"create"})
190 // Submit the request to the service and store the response.
191 ClientResponse<CollectionObject> res = client.read(knownResourceId);
192 int statusCode = res.getStatus();
194 // Check the status code of the response: does it match
195 // the expected response(s)?
196 verbose("read: status = " + statusCode);
197 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 @Test(dependsOnMethods = {"read"})
206 public void readNonExistent() {
209 setupReadNonExistent();
211 // Submit the request to the service and store the response.
212 ClientResponse<CollectionObject> res = client.read(NON_EXISTENT_ID);
213 int statusCode = res.getStatus();
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 verbose("readNonExistent: status = " + res.getStatus());
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
224 // ---------------------------------------------------------------
225 // CRUD tests : READ_LIST tests
226 // ---------------------------------------------------------------
231 @Test(dependsOnMethods = {"createList"})
232 public void readList() {
237 // Submit the request to the service and store the response.
238 ClientResponse<CollectionObjectList> res = client.readList();
239 CollectionObjectList list = res.getEntity();
240 int statusCode = res.getStatus();
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 verbose("readList: status = " + res.getStatus());
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 // Optionally output additional data about list members for debugging.
250 boolean iterateThroughList = false;
251 if (iterateThroughList && logger.isDebugEnabled()) {
252 List<CollectionObjectList.CollectionObjectListItem> items =
253 list.getCollectionObjectListItem();
255 for(CollectionObjectList.CollectionObjectListItem item : items){
256 verbose("readList: list-item[" + i + "] csid=" +
258 verbose("readList: list-item[" + i + "] objectNumber=" +
259 item.getObjectNumber());
260 verbose("readList: list-item[" + i + "] URI=" +
273 // ---------------------------------------------------------------
274 // CRUD tests : UPDATE tests
275 // ---------------------------------------------------------------
280 @Test(dependsOnMethods = {"create"})
281 public void update() {
286 // Retrieve an existing resource that we can update.
287 ClientResponse<CollectionObject> res = client.read(knownResourceId);
288 verbose("read: status = " + res.getStatus());
289 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290 CollectionObject collectionObject = res.getEntity();
291 verbose("Got object to update with ID: " + knownResourceId,
292 collectionObject, CollectionObject.class);
294 // Update the content of this resource.
295 collectionObject.setObjectNumber("updated-" +
296 collectionObject.getObjectNumber());
297 collectionObject.setObjectName("updated-" +
298 collectionObject.getObjectName());
300 // Submit the request to the service and store the response.
301 res = client.update(knownResourceId, collectionObject);
302 int statusCode = res.getStatus();
304 // Check the status code of the response: does it match
305 // the expected response(s)?
306 verbose("update: status = " + res.getStatus());
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 // Check the contents of the response: does it match
312 // what was submitted?
313 CollectionObject updatedObject = res.getEntity();
314 verbose("update: ", updatedObject, CollectionObject.class);
315 Assert.assertEquals(updatedObject.getObjectName(),
316 collectionObject.getObjectName(),
317 "Data in updated object did not match submitted data.");
322 // Placeholders until the three tests below can be uncommented.
323 // See Issue CSPACE-401.
324 public void updateWithEmptyEntityBody() {}
325 public void updateWithMalformedXml() {}
326 public void updateWithWrongXmlSchema() {}
330 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
331 public void updateWithEmptyEntityBody() {
334 setupUpdateWithEmptyEntityBody();
336 // Submit the request to the service and store the response.
337 String method = REQUEST_TYPE.httpMethodName();
338 String url = getResourceURL(knownResourceId);
339 String mediaType = MediaType.APPLICATION_XML;
340 final String entity = "";
341 int statusCode = submitRequest(method, url, mediaType, entity);
343 // Check the status code of the response: does it match
344 // the expected response(s)?
345 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
352 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
353 public void updateWithMalformedXml() {
356 setupUpdateWithMalformedXml();
358 // Submit the request to the service and store the response.
359 String method = REQUEST_TYPE.httpMethodName();
360 String url = getResourceURL(knownResourceId);
361 final String entity = MALFORMED_XML_DATA;
362 String mediaType = MediaType.APPLICATION_XML;
363 int statusCode = submitRequest(method, url, mediaType, entity);
365 // Check the status code of the response: does it match
366 // the expected response(s)?
367 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
374 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
375 public void updateWithWrongXmlSchema() {
378 setupUpdateWithWrongXmlSchema();
380 // Submit the request to the service and store the response.
381 String method = REQUEST_TYPE.httpMethodName();
382 String url = getResourceURL(knownResourceId);
383 String mediaType = MediaType.APPLICATION_XML;
384 final String entity = WRONG_XML_SCHEMA_DATA;
385 int statusCode = submitRequest(method, url, mediaType, entity);
387 // Check the status code of the response: does it match
388 // the expected response(s)?
389 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
390 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
397 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
398 public void updateNonExistent() {
401 setupUpdateNonExistent();
403 // Submit the request to the service and store the response.
404 // Note: The ID used in this 'create' call may be arbitrary.
405 // The only relevant ID may be the one used in update(), below.
406 CollectionObject collectionObject = createCollectionObjectInstance(NON_EXISTENT_ID);
407 ClientResponse<CollectionObject> res =
408 client.update(NON_EXISTENT_ID, collectionObject);
409 int statusCode = res.getStatus();
411 // Check the status code of the response: does it match
412 // the expected response(s)?
413 verbose("updateNonExistent: status = " + res.getStatus());
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
419 // ---------------------------------------------------------------
420 // CRUD tests : DELETE tests
421 // ---------------------------------------------------------------
426 @Test(dependsOnMethods =
427 {"create", "read", "update"})
428 public void delete() {
433 // Submit the request to the service and store the response.
434 ClientResponse<Response> res = client.delete(knownResourceId);
435 int statusCode = res.getStatus();
437 // Check the status code of the response: does it match
438 // the expected response(s)?
439 verbose("delete: status = " + res.getStatus());
440 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
441 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
442 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 @Test(dependsOnMethods = {"delete"})
449 public void deleteNonExistent() {
452 setupDeleteNonExistent();
454 // Submit the request to the service and store the response.
455 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
456 int statusCode = res.getStatus();
458 // Check the status code of the response: does it match
459 // the expected response(s)?
460 verbose("deleteNonExistent: status = " + res.getStatus());
461 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
467 // ---------------------------------------------------------------
468 // Utility tests : tests of code used in tests above
469 // ---------------------------------------------------------------
472 * Tests the code for manually submitting data that is used by several
473 * of the methods above.
475 @Test(dependsOnMethods = {"create", "read"})
476 public void testSubmitRequest() {
478 // Expected status code: 200 OK
479 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
481 // Submit the request to the service and store the response.
482 String method = ServiceRequestType.READ.httpMethodName();
483 String url = getResourceURL(knownResourceId);
484 int statusCode = submitRequest(method, url);
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
493 // ---------------------------------------------------------------
494 // Utility methods used by tests above
495 // ---------------------------------------------------------------
498 public String getServicePathComponent() {
499 // @TODO Determine if it is possible to obtain this
500 // value programmatically.
502 // We set this in an annotation in the CollectionObjectProxy
503 // interface, for instance. We also set service-specific
504 // constants in each service module, which might also
505 // return this value.
506 return SERVICE_PATH_COMPONENT;
509 private CollectionObject createCollectionObjectInstance(String identifier) {
510 CollectionObject collectionObject =
511 createCollectionObjectInstance(
512 "objectNumber-" + identifier,
513 "objectName-" + identifier);
514 return collectionObject;
517 private CollectionObject createCollectionObjectInstance(
518 String objectNumber, String objectName) {
519 CollectionObject collectionObject = new CollectionObject();
520 collectionObject.setObjectNumber(objectNumber);
521 collectionObject.setObjectName(objectName);
522 return collectionObject;