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;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * CollectionObjectServiceTest, carries out tests against a
47 * deployed and running CollectionObject Service.
49 * $LastChangedRevision$
52 public class CollectionObjectServiceTest extends AbstractServiceTest {
54 private final Logger logger =
55 LoggerFactory.getLogger(CollectionObjectServiceTest.class);
57 // Instance variables specific to this test.
58 private CollectionObjectClient client = new CollectionObjectClient();
59 private String knownResourceId = null;
62 * This method is called only by the parent class, AbstractServiceTest
65 protected String getServicePathComponent() {
66 return client.getServicePathComponent();
69 // ---------------------------------------------------------------
70 // CRUD tests : CREATE tests
71 // ---------------------------------------------------------------
75 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
76 public void create(String testName) throws Exception {
78 // Perform setup, such as initializing the type of service request
79 // (e.g. CREATE, DELETE), its valid and expected status codes, and
80 // its associated HTTP method name (e.g. POST, DELETE).
81 setupCreate(testName);
83 // Submit the request to the service and store the response.
84 String identifier = createIdentifier();
85 MultipartOutput multipart =
86 createCollectionObjectInstance(client.getCommonPartName(), identifier);
87 ClientResponse<Response> res = client.create(multipart);
88 int statusCode = res.getStatus();
90 // Check the status code of the response: does it match
91 // the expected response(s)?
94 // Does it fall within the set of valid status codes?
95 // Does it exactly match the expected status code?
96 if(logger.isDebugEnabled()){
97 logger.debug(testName + ": status = " + statusCode);
99 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
100 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
101 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
103 // Store the ID returned from this create operation
104 // for additional tests below.
105 knownResourceId = extractId(res);
106 if(logger.isDebugEnabled()){
107 logger.debug(testName + ": knownResourceId=" + knownResourceId);
112 * @see org.collectionspace.services.client.test.ServiceTest#createList()
115 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
116 dependsOnMethods = {"create"})
117 public void createList(String testName) throws Exception {
118 for(int i = 0; i < 3; i++){
125 // Placeholders until the three tests below can be uncommented.
126 // See Issue CSPACE-401.
128 public void createWithEmptyEntityBody(String testName) throws Exception {}
130 public void createWithMalformedXml(String testName) throws Exception {}
132 public void createWithWrongXmlSchema(String testName) throws Exception {}
137 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
138 dependsOnMethods = {"create", "testSubmitRequest"})
139 public void createWithEmptyEntityBody(String testName) throwsException {
142 setupCreateWithEmptyEntityBody(testName);
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 = "";
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 if(logger.isDebugEnabled()){
154 logger.debug(testName + ": url=" + url +
155 " status=" + statusCode);
157 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
158 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
159 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
163 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
164 dependsOnMethods = {"create", "testSubmitRequest"})
165 public void createWithMalformedXml(String testName) throws Exception {
168 setupCreateWithMalformedXml(testName);
170 // Submit the request to the service and store the response.
171 String method = REQUEST_TYPE.httpMethodName();
172 String url = getServiceRootURL();
173 String mediaType = MediaType.APPLICATION_XML;
174 final String entity = MALFORMED_XML_DATA; // Constant from base class.
175 int statusCode = submitRequest(method, url, mediaType, entity);
177 // Check the status code of the response: does it match
178 // the expected response(s)?
179 if(logger.isDebugEnabled()){
180 logger.debug(testName + ": url=" + url +
181 " status=" + statusCode);
183 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
185 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
189 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
190 dependsOnMethods = {"create", "testSubmitRequest"})
191 public void createWithWrongXmlSchema(String testName) throws Exception {
194 setupCreateWithWrongXmlSchema(testName);
196 // Submit the request to the service and store the response.
197 String method = REQUEST_TYPE.httpMethodName();
198 String url = getServiceRootURL();
199 String mediaType = MediaType.APPLICATION_XML;
200 final String entity = WRONG_XML_SCHEMA_DATA;
201 int statusCode = submitRequest(method, url, mediaType, entity);
203 // Check the status code of the response: does it match
204 // the expected response(s)?
205 if(logger.isDebugEnabled()){
206 logger.debug(testName + ": url=" + url +
207 " status=" + statusCode);
209 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
215 // ---------------------------------------------------------------
216 // CRUD tests : READ tests
217 // ---------------------------------------------------------------
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create"})
223 public void read(String testName) throws Exception {
228 // Submit the request to the service and store the response.
229 ClientResponse<MultipartInput> res = client.read(knownResourceId);
230 int statusCode = res.getStatus();
232 // Check the status code of the response: does it match
233 // the expected response(s)?
234 if(logger.isDebugEnabled()){
235 logger.debug(testName + ": status = " + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 MultipartInput input = (MultipartInput) res.getEntity();
242 CollectionobjectsCommon collectionObject =
243 (CollectionobjectsCommon) extractPart(input,
244 client.getCommonPartName(), CollectionobjectsCommon.class);
245 Assert.assertNotNull(collectionObject);
251 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
252 dependsOnMethods = {"read"})
253 public void readNonExistent(String testName) throws Exception {
256 setupReadNonExistent(testName);
258 // Submit the request to the service and store the response.
259 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
260 int statusCode = res.getStatus();
262 // Check the status code of the response: does it match
263 // the expected response(s)?
264 if(logger.isDebugEnabled()){
265 logger.debug(testName + ": status = " + statusCode);
267 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 // ---------------------------------------------------------------
273 // CRUD tests : READ_LIST tests
274 // ---------------------------------------------------------------
277 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
278 dependsOnMethods = {"createList", "read"})
279 public void readList(String testName) throws Exception {
282 setupReadList(testName);
284 // Submit the request to the service and store the response.
285 ClientResponse<CollectionobjectsCommonList> res = client.readList();
286 CollectionobjectsCommonList list = res.getEntity();
287 int statusCode = res.getStatus();
289 // Check the status code of the response: does it match
290 // the expected response(s)?
291 if(logger.isDebugEnabled()){
292 logger.debug(testName + ": status = " + statusCode);
294 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
295 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
296 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 // Optionally output additional data about list members for debugging.
299 boolean iterateThroughList = false;
300 if (iterateThroughList && logger.isDebugEnabled()) {
301 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
302 list.getCollectionObjectListItem();
305 for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
306 logger.debug(testName + ": list-item[" + i + "] csid=" +
308 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
309 item.getObjectNumber());
310 logger.debug(testName + ": list-item[" + i + "] URI=" +
321 // ---------------------------------------------------------------
322 // CRUD tests : UPDATE tests
323 // ---------------------------------------------------------------
326 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
327 dependsOnMethods = {"read"})
328 public void update(String testName) throws Exception {
331 setupUpdate(testName);
333 ClientResponse<MultipartInput> res =
334 client.read(knownResourceId);
335 if(logger.isDebugEnabled()){
336 logger.debug(testName + ": read status = " + res.getStatus());
338 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
340 if(logger.isDebugEnabled()){
341 logger.debug("got object to update with ID: " + knownResourceId);
343 MultipartInput input = (MultipartInput) res.getEntity();
344 CollectionobjectsCommon collectionObject =
345 (CollectionobjectsCommon) extractPart(input,
346 client.getCommonPartName(), CollectionobjectsCommon.class);
347 Assert.assertNotNull(collectionObject);
349 // Update the content of this resource.
350 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
351 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
352 if(logger.isDebugEnabled()){
353 verbose("updated object", collectionObject,
354 CollectionobjectsCommon.class);
356 // Submit the request to the service and store the response.
357 MultipartOutput output = new MultipartOutput();
358 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
359 commonPart.getHeaders().add("label", client.getCommonPartName());
361 res = client.update(knownResourceId, output);
362 int statusCode = res.getStatus();
363 // Check the status code of the response: does it match the expected response(s)?
364 if(logger.isDebugEnabled()){
365 logger.debug(testName + ": status = " + statusCode);
367 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372 input = (MultipartInput) res.getEntity();
373 CollectionobjectsCommon updatedCollectionObject =
374 (CollectionobjectsCommon) extractPart(input,
375 client.getCommonPartName(), CollectionobjectsCommon.class);
376 Assert.assertNotNull(updatedCollectionObject);
378 Assert.assertEquals(updatedCollectionObject.getObjectName(),
379 collectionObject.getObjectName(),
380 "Data in updated object did not match submitted data.");
386 // Placeholders until the three tests below can be uncommented.
387 // See Issue CSPACE-401.
389 public void updateWithEmptyEntityBody(String testName) throws Exception {}
391 public void updateWithMalformedXml(String testName) throws Exception {}
393 public void updateWithWrongXmlSchema(String testName) throws Exception {}
397 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
398 dependsOnMethods = {"create", "update", "testSubmitRequest"})
399 public void updateWithEmptyEntityBody(String testName) throws Exception {
402 setupUpdateWithEmptyEntityBody(testName);
404 // Submit the request to the service and store the response.
405 String method = REQUEST_TYPE.httpMethodName();
406 String url = getResourceURL(knownResourceId);
407 String mediaType = MediaType.APPLICATION_XML;
408 final String entity = "";
409 int statusCode = submitRequest(method, url, mediaType, entity);
411 // Check the status code of the response: does it match
412 // the expected response(s)?
413 if(logger.isDebugEnabled()){
414 logger.debug(testName + ": url=" + url +
415 " status=" + statusCode);
417 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
418 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
419 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
424 dependsOnMethods = {"create", "update", "testSubmitRequest"})
425 public void updateWithMalformedXml() throws Exception {
428 setupUpdateWithMalformedXml(testName);
430 // Submit the request to the service and store the response.
431 String method = REQUEST_TYPE.httpMethodName();
432 String url = getResourceURL(knownResourceId);
433 final String entity = MALFORMED_XML_DATA;
434 String mediaType = MediaType.APPLICATION_XML;
435 int statusCode = submitRequest(method, url, mediaType, entity);
437 // Check the status code of the response: does it match
438 // the expected response(s)?
439 if(logger.isDebugEnabled()){
440 logger.debug(testName + ": url=" + url +
441 " status=" + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
450 dependsOnMethods = {"create", "update", "testSubmitRequest"})
451 public void updateWithWrongXmlSchema(String testName) throws Exception {
454 setupUpdateWithWrongXmlSchema(String testName);
456 // Submit the request to the service and store the response.
457 String method = REQUEST_TYPE.httpMethodName();
458 String url = getResourceURL(knownResourceId);
459 String mediaType = MediaType.APPLICATION_XML;
460 final String entity = WRONG_XML_SCHEMA_DATA;
461 int statusCode = submitRequest(method, url, mediaType, entity);
463 // Check the status code of the response: does it match
464 // the expected response(s)?
465 if(logger.isDebugEnabled()){
466 logger.debug(testName + ": url=" + url +
467 " status=" + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
477 dependsOnMethods = {"update", "testSubmitRequest"})
478 public void updateNonExistent(String testName) throws Exception {
481 setupUpdateNonExistent(testName);
483 // Submit the request to the service and store the response.
485 // Note: The ID used in this 'create' call may be arbitrary.
486 // The only relevant ID may be the one used in updateCollectionObject(), below.
487 MultipartOutput multipart =
488 createCollectionObjectInstance(client.getCommonPartName(),
490 ClientResponse<MultipartInput> res =
491 client.update(NON_EXISTENT_ID, multipart);
492 int statusCode = res.getStatus();
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if(logger.isDebugEnabled()){
497 logger.debug(testName + ": status = " + statusCode);
499 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 // ---------------------------------------------------------------
505 // CRUD tests : DELETE tests
506 // ---------------------------------------------------------------
509 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
510 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
511 public void delete(String testName) throws Exception {
514 setupDelete(testName);
516 // Submit the request to the service and store the response.
517 ClientResponse<Response> res = client.delete(knownResourceId);
518 int statusCode = res.getStatus();
520 // Check the status code of the response: does it match
521 // the expected response(s)?
522 if(logger.isDebugEnabled()){
523 logger.debug(testName + ": status = " + statusCode);
525 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
526 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
527 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
532 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
533 dependsOnMethods = {"delete"})
534 public void deleteNonExistent(String testName) throws Exception {
537 setupDeleteNonExistent(testName);
539 // Submit the request to the service and store the response.
540 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
541 int statusCode = res.getStatus();
543 // Check the status code of the response: does it match
544 // the expected response(s)?
545 if(logger.isDebugEnabled()){
546 logger.debug(testName + ": status = " + statusCode);
548 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
549 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
550 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
553 // ---------------------------------------------------------------
554 // Utility tests : tests of code used in tests above
555 // ---------------------------------------------------------------
557 * Tests the code for manually submitting data that is used by several
558 * of the methods above.
560 @Test(dependsOnMethods = {"create", "read"})
561 public void testSubmitRequest() throws Exception {
563 // Expected status code: 200 OK
564 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
566 // Submit the request to the service and store the response.
567 String method = ServiceRequestType.READ.httpMethodName();
568 String url = getResourceURL(knownResourceId);
569 int statusCode = submitRequest(method, url);
571 // Check the status code of the response: does it match
572 // the expected response(s)?
573 if(logger.isDebugEnabled()){
574 logger.debug("testSubmitRequest: url=" + url +
575 " status=" + statusCode);
577 Assert.assertEquals(statusCode, EXPECTED_STATUS);
581 // ---------------------------------------------------------------
582 // Utility methods used by tests above
583 // ---------------------------------------------------------------
585 private MultipartOutput createCollectionObjectInstance(String commonPartName,
587 return createCollectionObjectInstance(commonPartName,
588 "objectNumber-" + identifier,
589 "objectName-" + identifier);
592 private MultipartOutput createCollectionObjectInstance(String commonPartName,
593 String objectNumber, String objectName) {
594 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
596 collectionObject.setObjectNumber(objectNumber);
597 collectionObject.setObjectName(objectName);
598 collectionObject.setAge(""); //test for null string
599 collectionObject.setBriefDescription("Papier mache bird mask with horns, " +
600 "painted red with black and yellow spots. " +
601 "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
602 MultipartOutput multipart = new MultipartOutput();
603 OutputPart commonPart = multipart.addPart(collectionObject,
604 MediaType.APPLICATION_XML_TYPE);
605 commonPart.getHeaders().add("label", commonPartName);
607 if(logger.isDebugEnabled()){
608 verbose("to be created, collectionobject common ",
609 collectionObject, CollectionobjectsCommon.class);
612 CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
613 conh.setNhString("test-string");
615 conh.setNhLong(9999);
616 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
617 nhPart.getHeaders().add("label", getNHPartName());
619 if(logger.isDebugEnabled()){
620 verbose("to be created, collectionobject nhistory",
621 conh, CollectionObjectNaturalhistory.class);
627 private String getNHPartName() {
628 return "collectionobjects-naturalhistory";