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 public String getServicePathComponent() {
66 return client.getServicePathComponent();
69 // ---------------------------------------------------------------
70 // CRUD tests : CREATE tests
71 // ---------------------------------------------------------------
76 public void create() 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).
83 // Submit the request to the service and store the response.
84 String identifier = createIdentifier();
86 MultipartOutput multipart = createCollectionObjectInstance(client.getCommonPartName(), identifier);
87 ClientResponse<Response> res = client.create(multipart);
89 int statusCode = res.getStatus();
91 // Check the status code of the response: does it match
92 // the expected response(s)?
95 // Does it fall within the set of valid status codes?
96 // Does it exactly match the expected status code?
97 if(logger.isDebugEnabled()){
98 logger.debug("create: status = " + statusCode);
100 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
101 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
102 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
104 // Store the ID returned from this create operation
105 // for additional tests below.
106 knownResourceId = extractId(res);
107 if(logger.isDebugEnabled()){
108 logger.debug("create: knownResourceId=" + knownResourceId);
113 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
116 @Test(dependsOnMethods = {"create"})
117 public void createList() 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() throws Exception {}
130 public void createWithMalformedXml() throws Exception {}
132 public void createWithWrongXmlSchema() throws Exception {}
137 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138 public void createWithEmptyEntityBody() throwsException {
141 setupCreateWithEmptyEntityBody();
143 // Submit the request to the service and store the response.
144 String method = REQUEST_TYPE.httpMethodName();
145 String url = getServiceRootURL();
146 String mediaType = MediaType.APPLICATION_XML;
147 final String entity = "";
148 int statusCode = submitRequest(method, url, mediaType, entity);
150 // Check the status code of the response: does it match
151 // the expected response(s)?
152 if(logger.isDebugEnabled()){
153 logger.debug("createWithEmptyEntityBody url=" + url +
154 " status=" + statusCode);
156 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
157 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
158 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
162 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
163 public void createWithMalformedXml() throws Exception {
166 setupCreateWithMalformedXml();
168 // Submit the request to the service and store the response.
169 String method = REQUEST_TYPE.httpMethodName();
170 String url = getServiceRootURL();
171 String mediaType = MediaType.APPLICATION_XML;
172 final String entity = MALFORMED_XML_DATA; // Constant from base class.
173 int statusCode = submitRequest(method, url, mediaType, entity);
175 // Check the status code of the response: does it match
176 // the expected response(s)?
177 if(logger.isDebugEnabled()){
178 logger.debug("createWithMalformedXml url=" + url +
179 " status=" + statusCode);
181 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
182 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
183 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
187 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
188 public void createWithWrongXmlSchema() throws Exception {
191 setupCreateWithWrongXmlSchema();
193 // Submit the request to the service and store the response.
194 String method = REQUEST_TYPE.httpMethodName();
195 String url = getServiceRootURL();
196 String mediaType = MediaType.APPLICATION_XML;
197 final String entity = WRONG_XML_SCHEMA_DATA;
198 int statusCode = submitRequest(method, url, mediaType, entity);
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 if(logger.isDebugEnabled()){
203 logger.debug("createWithWrongXmlSchema url=" + url +
204 " status=" + statusCode);
206 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
207 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
208 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
212 // ---------------------------------------------------------------
213 // CRUD tests : READ tests
214 // ---------------------------------------------------------------
218 @Test(dependsOnMethods = {"create"})
219 public void read() throws Exception {
224 // Submit the request to the service and store the response.
225 ClientResponse<MultipartInput> res = client.read(knownResourceId);
226 int statusCode = res.getStatus();
228 // Check the status code of the response: does it match
229 // the expected response(s)?
230 if(logger.isDebugEnabled()){
231 logger.debug("read: status = " + statusCode);
233 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 MultipartInput input = (MultipartInput) res.getEntity();
238 CollectionobjectsCommon collectionObject =
239 (CollectionobjectsCommon) extractPart(input,
240 client.getCommonPartName(), CollectionobjectsCommon.class);
241 Assert.assertNotNull(collectionObject);
247 @Test(dependsOnMethods = {"read"})
248 public void readNonExistent() throws Exception {
251 setupReadNonExistent();
253 // Submit the request to the service and store the response.
254 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
255 int statusCode = res.getStatus();
257 // Check the status code of the response: does it match
258 // the expected response(s)?
259 if(logger.isDebugEnabled()){
260 logger.debug("readNonExistent: status = " + res.getStatus());
262 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
267 // ---------------------------------------------------------------
268 // CRUD tests : READ_LIST tests
269 // ---------------------------------------------------------------
272 @Test(dependsOnMethods = {"createList", "read"})
273 public void readList() throws Exception {
277 // Submit the request to the service and store the response.
278 ClientResponse<CollectionobjectsCommonList> res = client.readList();
279 CollectionobjectsCommonList list = res.getEntity();
281 int statusCode = res.getStatus();
283 // Check the status code of the response: does it match
284 // the expected response(s)?
285 if(logger.isDebugEnabled()){
286 logger.debug("readList: status = " + res.getStatus());
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 // Optionally output additional data about list members for debugging.
293 boolean iterateThroughList = false;
294 if (iterateThroughList && logger.isDebugEnabled()) {
295 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
296 list.getCollectionObjectListItem();
299 for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
300 logger.debug("readList: list-item[" + i + "] csid=" +
302 logger.debug("readList: list-item[" + i + "] objectNumber=" +
303 item.getObjectNumber());
304 logger.debug("readList: list-item[" + i + "] URI=" +
314 // ---------------------------------------------------------------
315 // CRUD tests : UPDATE tests
316 // ---------------------------------------------------------------
319 @Test(dependsOnMethods = {"read"})
320 public void update() throws Exception {
325 ClientResponse<MultipartInput> res =
326 client.read(knownResourceId);
327 if(logger.isDebugEnabled()){
328 logger.debug("update: read status = " + res.getStatus());
330 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
332 if(logger.isDebugEnabled()){
333 logger.debug("got object to update with ID: " + knownResourceId);
335 MultipartInput input = (MultipartInput) res.getEntity();
336 CollectionobjectsCommon collectionObject =
337 (CollectionobjectsCommon) extractPart(input,
338 client.getCommonPartName(), CollectionobjectsCommon.class);
339 Assert.assertNotNull(collectionObject);
341 // Update the content of this resource.
342 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
343 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
344 if(logger.isDebugEnabled()){
345 verbose("updated object", collectionObject,
346 CollectionobjectsCommon.class);
348 // Submit the request to the service and store the response.
349 MultipartOutput output = new MultipartOutput();
350 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
351 commonPart.getHeaders().add("label", client.getCommonPartName());
353 res = client.update(knownResourceId, output);
354 int statusCode = res.getStatus();
355 // Check the status code of the response: does it match the expected response(s)?
356 if(logger.isDebugEnabled()){
357 logger.debug("update: status = " + res.getStatus());
359 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
360 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
361 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
364 input = (MultipartInput) res.getEntity();
365 CollectionobjectsCommon updatedCollectionObject =
366 (CollectionobjectsCommon) extractPart(input,
367 client.getCommonPartName(), CollectionobjectsCommon.class);
368 Assert.assertNotNull(updatedCollectionObject);
370 Assert.assertEquals(updatedCollectionObject.getObjectName(),
371 collectionObject.getObjectName(),
372 "Data in updated object did not match submitted data.");
378 // Placeholders until the three tests below can be uncommented.
379 // See Issue CSPACE-401.
381 public void updateWithEmptyEntityBody() throws Exception {}
383 public void updateWithMalformedXml() throws Exception {}
385 public void updateWithWrongXmlSchema() throws Exception {}
390 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
391 public void updateWithEmptyEntityBody() throws Exception {
394 setupUpdateWithEmptyEntityBody();
396 // Submit the request to the service and store the response.
397 String method = REQUEST_TYPE.httpMethodName();
398 String url = getResourceURL(knownResourceId);
399 String mediaType = MediaType.APPLICATION_XML;
400 final String entity = "";
401 int statusCode = submitRequest(method, url, mediaType, entity);
403 // Check the status code of the response: does it match
404 // the expected response(s)?
405 if(logger.isDebugEnabled()){
406 logger.debug("updateWithEmptyEntityBody url=" + url +
407 " status=" + statusCode);
409 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
410 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
411 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
415 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
416 public void updateWithMalformedXml() throws Exception {
419 setupUpdateWithMalformedXml();
421 // Submit the request to the service and store the response.
422 String method = REQUEST_TYPE.httpMethodName();
423 String url = getResourceURL(knownResourceId);
424 final String entity = MALFORMED_XML_DATA;
425 String mediaType = MediaType.APPLICATION_XML;
426 int statusCode = submitRequest(method, url, mediaType, entity);
428 // Check the status code of the response: does it match
429 // the expected response(s)?
430 if(logger.isDebugEnabled()){
431 logger.debug("updateWithMalformedXml: url=" + url +
432 " status=" + statusCode);
434 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
440 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
441 public void updateWithWrongXmlSchema() throws Exception {
444 setupUpdateWithWrongXmlSchema();
446 // Submit the request to the service and store the response.
447 String method = REQUEST_TYPE.httpMethodName();
448 String url = getResourceURL(knownResourceId);
449 String mediaType = MediaType.APPLICATION_XML;
450 final String entity = WRONG_XML_SCHEMA_DATA;
451 int statusCode = submitRequest(method, url, mediaType, entity);
453 // Check the status code of the response: does it match
454 // the expected response(s)?
455 if(logger.isDebugEnabled()){
456 logger.debug("updateWithWrongXmlSchema: url=" + url +
457 " status=" + statusCode);
459 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
466 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
467 public void updateNonExistent() throws Exception {
470 setupUpdateNonExistent();
472 // Submit the request to the service and store the response.
473 // Note: The ID used in this 'create' call may be arbitrary.
475 // The only relevant ID may be the one used in updateCollectionObject(), below.
476 MultipartOutput multipart =
477 createCollectionObjectInstance(client.getCommonPartName(),
479 ClientResponse<MultipartInput> res =
480 client.update(NON_EXISTENT_ID, multipart);
482 int statusCode = res.getStatus();
484 // Check the status code of the response: does it match
485 // the expected response(s)?
486 if(logger.isDebugEnabled()){
487 logger.debug("updateNonExistent: status = " + res.getStatus());
489 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
490 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
491 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
494 // ---------------------------------------------------------------
495 // CRUD tests : DELETE tests
496 // ---------------------------------------------------------------
499 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
500 public void delete() throws Exception {
505 // Submit the request to the service and store the response.
506 ClientResponse<Response> res = client.delete(knownResourceId);
507 int statusCode = res.getStatus();
509 // Check the status code of the response: does it match
510 // the expected response(s)?
511 if(logger.isDebugEnabled()){
512 logger.debug("delete: status = " + res.getStatus());
514 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
515 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
516 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
521 @Test(dependsOnMethods = {"delete"})
522 public void deleteNonExistent() throws Exception {
525 setupDeleteNonExistent();
527 // Submit the request to the service and store the response.
528 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
529 int statusCode = res.getStatus();
531 // Check the status code of the response: does it match
532 // the expected response(s)?
533 if(logger.isDebugEnabled()){
534 logger.debug("deleteNonExistent: status = " + res.getStatus());
536 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
541 // ---------------------------------------------------------------
542 // Utility tests : tests of code used in tests above
543 // ---------------------------------------------------------------
545 * Tests the code for manually submitting data that is used by several
546 * of the methods above.
548 @Test(dependsOnMethods = {"create", "read"})
549 public void testSubmitRequest() throws Exception {
551 // Expected status code: 200 OK
552 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
554 // Submit the request to the service and store the response.
555 String method = ServiceRequestType.READ.httpMethodName();
556 String url = getResourceURL(knownResourceId);
557 int statusCode = submitRequest(method, url);
559 // Check the status code of the response: does it match
560 // the expected response(s)?
561 if(logger.isDebugEnabled()){
562 logger.debug("testSubmitRequest: url=" + url +
563 " status=" + statusCode);
565 Assert.assertEquals(statusCode, EXPECTED_STATUS);
569 // ---------------------------------------------------------------
570 // Utility methods used by tests above
571 // ---------------------------------------------------------------
573 private MultipartOutput createCollectionObjectInstance(String commonPartName,
575 return createCollectionObjectInstance(commonPartName,
576 "objectNumber-" + identifier,
577 "objectName-" + identifier);
580 private MultipartOutput createCollectionObjectInstance(String commonPartName,
581 String objectNumber, String objectName) {
582 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
584 collectionObject.setObjectNumber(objectNumber);
585 collectionObject.setObjectName(objectName);
586 MultipartOutput multipart = new MultipartOutput();
587 OutputPart commonPart = multipart.addPart(collectionObject,
588 MediaType.APPLICATION_XML_TYPE);
589 commonPart.getHeaders().add("label", commonPartName);
591 if(logger.isDebugEnabled()){
592 verbose("to be created, collectionobject common ",
593 collectionObject, CollectionobjectsCommon.class);
596 CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
597 conh.setNhString("test-string");
599 conh.setNhLong(9999);
600 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
601 nhPart.getHeaders().add("label", getNHPartName());
603 if(logger.isDebugEnabled()){
604 verbose("to be created, collectionobject nhistory",
605 conh, CollectionObjectNaturalhistory.class);
611 private String getNHPartName() {
612 return "collectionobjects-naturalhistory";