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 //FIXME: Remove this method once ALL the services use "_common" instead of "-common"
57 public String getCommonPartName() {
58 return getServicePathComponent() + "_common";
61 // ---------------------------------------------------------------
62 // CRUD tests : CREATE tests
63 // ---------------------------------------------------------------
67 public void create() {
69 // Perform setup, such as initializing the type of service request
70 // (e.g. CREATE, DELETE), its valid and expected status codes, and
71 // its associated HTTP method name (e.g. POST, DELETE).
74 // Submit the request to the service and store the response.
75 String identifier = createIdentifier();
77 MultipartOutput multipart = createCollectionObjectInstance(identifier);
78 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 // Check the status code of the response: does it match
83 // the expected response(s)?
86 // Does it fall within the set of valid status codes?
87 // Does it exactly match the expected status code?
88 verbose("create: status = " + statusCode);
89 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
90 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
91 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
93 // Store the ID returned from this create operation
94 // for additional tests below.
95 knownResourceId = extractId(res);
96 verbose("create: knownResourceId=" + knownResourceId);
100 @Test(dependsOnMethods = {"create"})
101 public void createList() {
102 for(int i = 0; i < 3; i++){
108 // Placeholders until the three tests below can be uncommented.
109 // See Issue CSPACE-401.
110 public void createWithEmptyEntityBody() {}
111 public void createWithMalformedXml() {}
112 public void createWithWrongXmlSchema() {}
116 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
117 public void createWithEmptyEntityBody() {
120 setupCreateWithEmptyEntityBody();
122 // Submit the request to the service and store the response.
123 String method = REQUEST_TYPE.httpMethodName();
124 String url = getServiceRootURL();
125 String mediaType = MediaType.APPLICATION_XML;
126 final String entity = "";
127 int statusCode = submitRequest(method, url, mediaType, entity);
129 // Check the status code of the response: does it match
130 // the expected response(s)?
131 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
132 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
133 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
134 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
138 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
139 public void createWithMalformedXml() {
142 setupCreateWithMalformedXml();
144 // Submit the request to the service and store the response.
145 String method = REQUEST_TYPE.httpMethodName();
146 String url = getServiceRootURL();
147 String mediaType = MediaType.APPLICATION_XML;
148 final String entity = MALFORMED_XML_DATA; // Constant from base class.
149 int statusCode = submitRequest(method, url, mediaType, entity);
151 // Check the status code of the response: does it match
152 // the expected response(s)?
153 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
154 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
155 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
156 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
160 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
161 public void createWithWrongXmlSchema() {
164 setupCreateWithWrongXmlSchema();
166 // Submit the request to the service and store the response.
167 String method = REQUEST_TYPE.httpMethodName();
168 String url = getServiceRootURL();
169 String mediaType = MediaType.APPLICATION_XML;
170 final String entity = WRONG_XML_SCHEMA_DATA;
171 int statusCode = submitRequest(method, url, mediaType, entity);
173 // Check the status code of the response: does it match
174 // the expected response(s)?
175 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
176 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 // ---------------------------------------------------------------
183 // CRUD tests : READ tests
184 // ---------------------------------------------------------------
187 @Test(dependsOnMethods = {"create"})
193 // Submit the request to the service and store the response.
194 ClientResponse<MultipartInput> res = client.read(knownResourceId);
195 int statusCode = res.getStatus();
197 // Check the status code of the response: does it match
198 // the expected response(s)?
199 verbose("read: status = " + statusCode);
200 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203 //FIXME: remove the following try catch once Aron fixes signatures
205 MultipartInput input = (MultipartInput) res.getEntity();
206 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
207 getCommonPartName(), CollectionobjectsCommon.class);
208 Assert.assertNotNull(collectionObject);
210 throw new RuntimeException(e);
217 @Test(dependsOnMethods = {"read"})
218 public void readNonExistent() {
221 setupReadNonExistent();
223 // Submit the request to the service and store the response.
224 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
225 int statusCode = res.getStatus();
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 verbose("readNonExistent: status = " + res.getStatus());
230 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 // ---------------------------------------------------------------
236 // CRUD tests : READ_LIST tests
237 // ---------------------------------------------------------------
240 @Test(dependsOnMethods = {"createList", "read"})
241 public void readList() {
245 // Submit the request to the service and store the response.
246 ClientResponse<CollectionobjectsCommonList> res = client.readList();
247 CollectionobjectsCommonList list = res.getEntity();
249 int statusCode = res.getStatus();
251 // Check the status code of the response: does it match
252 // the expected response(s)?
253 verbose("readList: status = " + res.getStatus());
254 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
258 // Optionally output additional data about list members for debugging.
259 boolean iterateThroughList = false;
260 if (iterateThroughList && logger.isDebugEnabled()) {
261 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
262 list.getCollectionObjectListItem();
265 for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
266 verbose("readList: list-item[" + i + "] csid=" +
268 verbose("readList: list-item[" + i + "] objectNumber=" +
269 item.getObjectNumber());
270 verbose("readList: list-item[" + i + "] URI=" +
279 // ---------------------------------------------------------------
280 // CRUD tests : UPDATE tests
281 // ---------------------------------------------------------------
284 @Test(dependsOnMethods = {"read"})
285 public void update() {
289 try{ //ideally, just remove try-catch and let the exception bubble up
290 // Retrieve an existing resource that we can update.
291 ClientResponse<MultipartInput> res =
292 client.read(knownResourceId);
293 verbose("update: read status = " + res.getStatus());
294 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
296 verbose("got object to update with ID: " + knownResourceId);
297 MultipartInput input = (MultipartInput) res.getEntity();
298 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
299 getCommonPartName(), CollectionobjectsCommon.class);
300 Assert.assertNotNull(collectionObject);
302 // Update the content of this resource.
303 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
304 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
305 verbose("updated object", collectionObject, CollectionobjectsCommon.class);
306 // Submit the request to the service and store the response.
307 MultipartOutput output = new MultipartOutput();
308 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
309 commonPart.getHeaders().add("label", getCommonPartName());
311 res = client.update(knownResourceId, output);
312 int statusCode = res.getStatus();
313 // Check the status code of the response: does it match the expected response(s)?
314 verbose("update: status = " + res.getStatus());
315 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
316 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
317 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
320 input = (MultipartInput) res.getEntity();
321 CollectionobjectsCommon updatedCollectionObject =
322 (CollectionobjectsCommon) extractPart(input,
323 getCommonPartName(), CollectionobjectsCommon.class);
324 Assert.assertNotNull(updatedCollectionObject);
326 Assert.assertEquals(updatedCollectionObject.getObjectName(),
327 collectionObject.getObjectName(),
328 "Data in updated object did not match submitted data.");
336 // Placeholders until the three tests below can be uncommented.
337 // See Issue CSPACE-401.
338 public void updateWithEmptyEntityBody() {}
339 public void updateWithMalformedXml() {}
340 public void updateWithWrongXmlSchema() {}
345 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
346 public void updateWithEmptyEntityBody() {
349 setupUpdateWithEmptyEntityBody();
351 // Submit the request to the service and store the response.
352 String method = REQUEST_TYPE.httpMethodName();
353 String url = getResourceURL(knownResourceId);
354 String mediaType = MediaType.APPLICATION_XML;
355 final String entity = "";
356 int statusCode = submitRequest(method, url, mediaType, entity);
358 // Check the status code of the response: does it match
359 // the expected response(s)?
360 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
361 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
362 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
363 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
367 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
368 public void updateWithMalformedXml() {
371 setupUpdateWithMalformedXml();
373 // Submit the request to the service and store the response.
374 String method = REQUEST_TYPE.httpMethodName();
375 String url = getResourceURL(knownResourceId);
376 final String entity = MALFORMED_XML_DATA;
377 String mediaType = MediaType.APPLICATION_XML;
378 int statusCode = submitRequest(method, url, mediaType, entity);
380 // Check the status code of the response: does it match
381 // the expected response(s)?
382 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
383 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
384 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
385 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
389 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
390 public void updateWithWrongXmlSchema() {
393 setupUpdateWithWrongXmlSchema();
395 // Submit the request to the service and store the response.
396 String method = REQUEST_TYPE.httpMethodName();
397 String url = getResourceURL(knownResourceId);
398 String mediaType = MediaType.APPLICATION_XML;
399 final String entity = WRONG_XML_SCHEMA_DATA;
400 int statusCode = submitRequest(method, url, mediaType, entity);
402 // Check the status code of the response: does it match
403 // the expected response(s)?
404 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
405 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
413 public void updateNonExistent() {
416 setupUpdateNonExistent();
418 // Submit the request to the service and store the response.
419 // Note: The ID used in this 'create' call may be arbitrary.
421 // The only relevant ID may be the one used in updateCollectionObject(), below.
422 MultipartOutput multipart = createCollectionObjectInstance(NON_EXISTENT_ID);
423 ClientResponse<MultipartInput> res =
424 client.update(NON_EXISTENT_ID, multipart);
426 int statusCode = res.getStatus();
428 // Check the status code of the response: does it match
429 // the expected response(s)?
430 verbose("updateNonExistent: status = " + res.getStatus());
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 // ---------------------------------------------------------------
437 // CRUD tests : DELETE tests
438 // ---------------------------------------------------------------
441 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
442 public void delete() {
447 // Submit the request to the service and store the response.
448 ClientResponse<Response> res = client.delete(knownResourceId);
449 int statusCode = res.getStatus();
451 // Check the status code of the response: does it match
452 // the expected response(s)?
453 verbose("delete: status = " + res.getStatus());
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
461 @Test(dependsOnMethods = {"delete"})
462 public void deleteNonExistent() {
465 setupDeleteNonExistent();
467 // Submit the request to the service and store the response.
468 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
469 int statusCode = res.getStatus();
471 // Check the status code of the response: does it match
472 // the expected response(s)?
473 verbose("deleteNonExistent: status = " + res.getStatus());
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 // ---------------------------------------------------------------
480 // Utility tests : tests of code used in tests above
481 // ---------------------------------------------------------------
483 * Tests the code for manually submitting data that is used by several
484 * of the methods above.
486 @Test(dependsOnMethods = {"create", "read"})
487 public void testSubmitRequest() {
489 // Expected status code: 200 OK
490 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
492 // Submit the request to the service and store the response.
493 String method = ServiceRequestType.READ.httpMethodName();
494 String url = getResourceURL(knownResourceId);
495 int statusCode = submitRequest(method, url);
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
500 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 // ---------------------------------------------------------------
505 // Utility methods used by tests above
506 // ---------------------------------------------------------------
508 public String getServicePathComponent() {
509 // @TODO Determine if it is possible to obtain this
510 // value programmatically.
512 // We set this in an annotation in the CollectionObjectProxy
513 // interface, for instance. We also set service-specific
514 // constants in each service module, which might also
515 // return this value.
516 return SERVICE_PATH_COMPONENT;
519 private MultipartOutput createCollectionObjectInstance(String identifier) {
520 return createCollectionObjectInstance("objectNumber-" + identifier,
521 "objectName-" + identifier);
524 private MultipartOutput createCollectionObjectInstance(String objectNumber, String objectName) {
525 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
527 collectionObject.setObjectNumber(objectNumber);
528 collectionObject.setObjectName(objectName);
529 MultipartOutput multipart = new MultipartOutput();
530 OutputPart commonPart = multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
531 commonPart.getHeaders().add("label", getCommonPartName());
533 verbose("to be created, collectionobject common ", collectionObject, CollectionobjectsCommon.class);
535 CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
536 conh.setNhString("test-string");
538 conh.setNhLong(9999);
539 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
540 nhPart.getHeaders().add("label", getNHPartName());
542 verbose("to be created, collectionobject nhistory", conh, CollectionObjectNaturalhistory.class);
547 private String getNHPartName() {
548 return "collectionobjects-naturalhistory";