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.
23 package org.collectionspace.services.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionObjectClient;
30 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
31 import org.collectionspace.services.collectionobject.domain.naturalhistory.CollectionObjectNaturalhistory;
32 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
34 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
43 * CollectionObjectServiceTest, carries out tests against a
44 * deployed and running CollectionObject Service.
46 * $LastChangedRevision$
49 public class CollectionObjectServiceTest extends AbstractServiceTest {
51 // Instance variables specific to this test.
52 private CollectionObjectClient client = new CollectionObjectClient();
53 final String SERVICE_PATH_COMPONENT = "collectionobjects";
54 private String knownResourceId = null;
56 // ---------------------------------------------------------------
57 // CRUD tests : CREATE tests
58 // ---------------------------------------------------------------
62 public void create() {
64 // Perform setup, such as initializing the type of service request
65 // (e.g. CREATE, DELETE), its valid and expected status codes, and
66 // its associated HTTP method name (e.g. POST, DELETE).
69 // Submit the request to the service and store the response.
70 String identifier = createIdentifier();
72 MultipartOutput multipart = createCollectionObjectInstance(identifier);
73 ClientResponse<Response> res = client.create(multipart);
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);
91 verbose("create: knownResourceId=" + knownResourceId);
95 @Test(dependsOnMethods = {"create"})
96 public void createList() {
97 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 // ---------------------------------------------------------------
182 @Test(dependsOnMethods = {"create"})
188 // Submit the request to the service and store the response.
189 ClientResponse<MultipartInput> res = client.read(knownResourceId);
190 int statusCode = res.getStatus();
192 // Check the status code of the response: does it match
193 // the expected response(s)?
194 verbose("read: status = " + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198 //FIXME: remove the following try catch once Aron fixes signatures
200 MultipartInput input = (MultipartInput) res.getEntity();
201 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
202 getCommonPartName(), CollectionobjectsCommon.class);
203 Assert.assertNotNull(collectionObject);
205 throw new RuntimeException(e);
212 @Test(dependsOnMethods = {"read"})
213 public void readNonExistent() {
216 setupReadNonExistent();
218 // Submit the request to the service and store the response.
219 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
220 int statusCode = res.getStatus();
222 // Check the status code of the response: does it match
223 // the expected response(s)?
224 verbose("readNonExistent: status = " + res.getStatus());
225 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
226 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
227 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230 // ---------------------------------------------------------------
231 // CRUD tests : READ_LIST tests
232 // ---------------------------------------------------------------
235 @Test(dependsOnMethods = {"createList", "read"})
236 public void readList() {
240 // Submit the request to the service and store the response.
241 ClientResponse<CollectionobjectsCommonList> res = client.readList();
242 CollectionobjectsCommonList list = res.getEntity();
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 verbose("readList: status = " + res.getStatus());
249 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 // Optionally output additional data about list members for debugging.
254 boolean iterateThroughList = false;
255 if (iterateThroughList && logger.isDebugEnabled()) {
256 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
257 list.getCollectionObjectListItem();
260 for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
261 verbose("readList: list-item[" + i + "] csid=" +
263 verbose("readList: list-item[" + i + "] objectNumber=" +
264 item.getObjectNumber());
265 verbose("readList: list-item[" + i + "] URI=" +
274 // ---------------------------------------------------------------
275 // CRUD tests : UPDATE tests
276 // ---------------------------------------------------------------
279 @Test(dependsOnMethods = {"read"})
280 public void update() {
284 try{ //ideally, just remove try-catch and let the exception bubble up
285 // Retrieve an existing resource that we can update.
286 ClientResponse<MultipartInput> res =
287 client.read(knownResourceId);
288 verbose("update: read status = " + res.getStatus());
289 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
291 verbose("got object to update with ID: " + knownResourceId);
292 MultipartInput input = (MultipartInput) res.getEntity();
293 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
294 getCommonPartName(), CollectionobjectsCommon.class);
295 Assert.assertNotNull(collectionObject);
297 // Update the content of this resource.
298 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
299 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
300 verbose("updated object", collectionObject, CollectionobjectsCommon.class);
301 // Submit the request to the service and store the response.
302 MultipartOutput output = new MultipartOutput();
303 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
304 commonPart.getHeaders().add("label", getCommonPartName());
306 res = client.update(knownResourceId, output);
307 int statusCode = res.getStatus();
308 // Check the status code of the response: does it match the expected response(s)?
309 verbose("update: status = " + res.getStatus());
310 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
315 input = (MultipartInput) res.getEntity();
316 CollectionobjectsCommon updatedCollectionObject =
317 (CollectionobjectsCommon) extractPart(input,
318 getCommonPartName(), CollectionobjectsCommon.class);
319 Assert.assertNotNull(updatedCollectionObject);
321 Assert.assertEquals(updatedCollectionObject.getObjectName(),
322 collectionObject.getObjectName(),
323 "Data in updated object did not match submitted data.");
331 // Placeholders until the three tests below can be uncommented.
332 // See Issue CSPACE-401.
333 public void updateWithEmptyEntityBody() {}
334 public void updateWithMalformedXml() {}
335 public void updateWithWrongXmlSchema() {}
340 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
341 public void updateWithEmptyEntityBody() {
344 setupUpdateWithEmptyEntityBody();
346 // Submit the request to the service and store the response.
347 String method = REQUEST_TYPE.httpMethodName();
348 String url = getResourceURL(knownResourceId);
349 String mediaType = MediaType.APPLICATION_XML;
350 final String entity = "";
351 int statusCode = submitRequest(method, url, mediaType, entity);
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
356 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
357 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
358 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
363 public void updateWithMalformedXml() {
366 setupUpdateWithMalformedXml();
368 // Submit the request to the service and store the response.
369 String method = REQUEST_TYPE.httpMethodName();
370 String url = getResourceURL(knownResourceId);
371 final String entity = MALFORMED_XML_DATA;
372 String mediaType = MediaType.APPLICATION_XML;
373 int statusCode = submitRequest(method, url, mediaType, entity);
375 // Check the status code of the response: does it match
376 // the expected response(s)?
377 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
384 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
385 public void updateWithWrongXmlSchema() {
388 setupUpdateWithWrongXmlSchema();
390 // Submit the request to the service and store the response.
391 String method = REQUEST_TYPE.httpMethodName();
392 String url = getResourceURL(knownResourceId);
393 String mediaType = MediaType.APPLICATION_XML;
394 final String entity = WRONG_XML_SCHEMA_DATA;
395 int statusCode = submitRequest(method, url, mediaType, entity);
397 // Check the status code of the response: does it match
398 // the expected response(s)?
399 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
400 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
401 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
402 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
407 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
408 public void updateNonExistent() {
411 setupUpdateNonExistent();
413 // Submit the request to the service and store the response.
414 // Note: The ID used in this 'create' call may be arbitrary.
416 // The only relevant ID may be the one used in updateCollectionObject(), below.
417 MultipartOutput multipart = createCollectionObjectInstance(NON_EXISTENT_ID);
418 ClientResponse<MultipartInput> res =
419 client.update(NON_EXISTENT_ID, multipart);
421 int statusCode = res.getStatus();
423 // Check the status code of the response: does it match
424 // the expected response(s)?
425 verbose("updateNonExistent: status = " + res.getStatus());
426 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
427 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
428 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431 // ---------------------------------------------------------------
432 // CRUD tests : DELETE tests
433 // ---------------------------------------------------------------
436 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
437 public void delete() {
442 // Submit the request to the service and store the response.
443 ClientResponse<Response> res = client.delete(knownResourceId);
444 int statusCode = res.getStatus();
446 // Check the status code of the response: does it match
447 // the expected response(s)?
448 verbose("delete: status = " + res.getStatus());
449 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
450 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
451 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
456 @Test(dependsOnMethods = {"delete"})
457 public void deleteNonExistent() {
460 setupDeleteNonExistent();
462 // Submit the request to the service and store the response.
463 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
464 int statusCode = res.getStatus();
466 // Check the status code of the response: does it match
467 // the expected response(s)?
468 verbose("deleteNonExistent: status = " + res.getStatus());
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
474 // ---------------------------------------------------------------
475 // Utility tests : tests of code used in tests above
476 // ---------------------------------------------------------------
478 * Tests the code for manually submitting data that is used by several
479 * of the methods above.
481 @Test(dependsOnMethods = {"create", "read"})
482 public void testSubmitRequest() {
484 // Expected status code: 200 OK
485 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
487 // Submit the request to the service and store the response.
488 String method = ServiceRequestType.READ.httpMethodName();
489 String url = getResourceURL(knownResourceId);
490 int statusCode = submitRequest(method, url);
492 // Check the status code of the response: does it match
493 // the expected response(s)?
494 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
495 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
499 // ---------------------------------------------------------------
500 // Utility methods used by tests above
501 // ---------------------------------------------------------------
503 public String getServicePathComponent() {
504 // @TODO Determine if it is possible to obtain this
505 // value programmatically.
507 // We set this in an annotation in the CollectionObjectProxy
508 // interface, for instance. We also set service-specific
509 // constants in each service module, which might also
510 // return this value.
511 return SERVICE_PATH_COMPONENT;
514 private MultipartOutput createCollectionObjectInstance(String identifier) {
515 return createCollectionObjectInstance("objectNumber-" + identifier,
516 "objectName-" + identifier);
519 private MultipartOutput createCollectionObjectInstance(String objectNumber, String objectName) {
520 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
522 collectionObject.setObjectNumber(objectNumber);
523 collectionObject.setObjectName(objectName);
524 MultipartOutput multipart = new MultipartOutput();
525 OutputPart commonPart = multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
526 commonPart.getHeaders().add("label", getCommonPartName());
528 verbose("to be created, collectionobject common ", collectionObject, CollectionobjectsCommon.class);
530 CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
531 conh.setNhString("test-string");
533 conh.setNhLong(9999);
534 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
535 nhPart.getHeaders().add("label", getNHPartName());
537 verbose("to be created, collectionobject nhistory", conh, CollectionObjectNaturalhistory.class);
542 private String getNHPartName() {
543 return "collectionobjects-naturalhistory";