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.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.RelationClient;
31 import org.collectionspace.services.relation.RelationsCommon;
32 import org.collectionspace.services.relation.RelationsCommonList;
33 import org.collectionspace.services.relation.RelationshipType;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * RelationServiceTest, carries out tests against a
49 * deployed and running Relation Service.
51 * $LastChangedRevision$
54 public class RelationServiceTest extends AbstractServiceTestImpl {
56 private final Logger logger =
57 LoggerFactory.getLogger(RelationServiceTest.class);
59 final String SERVICE_PATH_COMPONENT = "relations";
60 private String knownResourceId = null;
61 private List<String> allResourceIdsCreated = new ArrayList();
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
69 public void create(String testName) throws Exception {
71 // Perform setup, such as initializing the type of service request
72 // (e.g. CREATE, DELETE), its valid and expected status codes, and
73 // its associated HTTP method name (e.g. POST, DELETE).
74 setupCreate(testName);
76 // Submit the request to the service and store the response.
77 RelationClient client = new RelationClient();
78 String identifier = createIdentifier();
79 MultipartOutput multipart = createRelationInstance(identifier);
80 ClientResponse<Response> res = client.create(multipart);
81 int statusCode = res.getStatus();
83 // Check the status code of the response: does it match
84 // 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 if(logger.isDebugEnabled()){
89 logger.debug(testName + ": status = " + statusCode);
91 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95 // Store the ID returned from the first resource created
96 // for additional tests below.
97 if (knownResourceId == null){
98 knownResourceId = extractId(res);
99 if (logger.isDebugEnabled()) {
100 logger.debug(testName + ": knownResourceId=" + knownResourceId);
104 // Store the IDs from every resource created by tests,
105 // so they can be deleted after tests have been run.
106 allResourceIdsCreated.add(extractId(res));
110 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
111 dependsOnMethods = {"create"})
112 public void createList(String testName) throws Exception {
113 for(int i = 0; i < 3; i++){
119 // Placeholders until the three tests below can be uncommented.
120 // See Issue CSPACE-401.
121 public void createWithEmptyEntityBody(String testName) throws Exception {
124 public void createWithMalformedXml(String testName) throws Exception {
127 public void createWithWrongXmlSchema(String testName) throws Exception {
132 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
133 dependsOnMethods = {"create", "testSubmitRequest"})
134 public void createWithEmptyEntityBody(String testName) throws Exception {
137 setupCreateWithEmptyEntityBody(testName);
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 = "";
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 if(logger.isDebugEnabled()){
149 logger.debug(testName + ": url=" + url +
150 " status=" + statusCode);
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
158 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
159 dependsOnMethods = {"create", "testSubmitRequest"})
160 public void createWithMalformedXml(String testName) throws Exception {
163 setupCreateWithMalformedXml(testName);
165 // Submit the request to the service and store the response.
166 String method = REQUEST_TYPE.httpMethodName();
167 String url = getServiceRootURL();
168 String mediaType = MediaType.APPLICATION_XML;
169 final String entity = MALFORMED_XML_DATA; // Constant from base class.
170 int statusCode = submitRequest(method, url, mediaType, entity);
172 // Check the status code of the response: does it match
173 // the expected response(s)?
174 if(logger.isDebugEnabled()){
175 logger.debug(testName + ": url=" + url +
176 " status=" + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
184 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
185 dependsOnMethods = {"create", "testSubmitRequest"})
186 public void createWithWrongXmlSchema(String testName) throws Exception {
189 setupCreateWithWrongXmlSchema(testName);
191 // Submit the request to the service and store the response.
192 String method = REQUEST_TYPE.httpMethodName();
193 String url = getServiceRootURL();
194 String mediaType = MediaType.APPLICATION_XML;
195 final String entity = WRONG_XML_SCHEMA_DATA;
196 int statusCode = submitRequest(method, url, mediaType, entity);
198 // Check the status code of the response: does it match
199 // the expected response(s)?
200 if(logger.isDebugEnabled()){
201 logger.debug(testName + ": url=" + url +
202 " status=" + statusCode);
204 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
210 // ---------------------------------------------------------------
211 // CRUD tests : READ tests
212 // ---------------------------------------------------------------
215 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
216 dependsOnMethods = {"create"})
217 public void read(String testName) throws Exception {
222 // Submit the request to the service and store the response.
223 RelationClient client = new RelationClient();
224 ClientResponse<MultipartInput> res = client.read(knownResourceId);
225 int statusCode = res.getStatus();
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 if(logger.isDebugEnabled()){
230 logger.debug(testName + ": status = " + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236 // Verify that the resource identifier ...
237 MultipartInput input = (MultipartInput) res.getEntity();
238 RelationsCommon relation = (RelationsCommon) extractPart(input,
239 client.getCommonPartName(), RelationsCommon.class);
240 Assert.assertNotNull(relation);
246 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
247 dependsOnMethods = {"read"})
248 public void readNonExistent(String testName) throws Exception {
251 setupReadNonExistent(testName);
253 // Submit the request to the service and store the response.
254 RelationClient client = new RelationClient();
255 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
256 int statusCode = res.getStatus();
258 // Check the status code of the response: does it match
259 // the expected response(s)?
260 if(logger.isDebugEnabled()){
261 logger.debug(testName + ": status = " + statusCode);
263 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268 // ---------------------------------------------------------------
269 // CRUD tests : READ_LIST tests
270 // ---------------------------------------------------------------
273 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
274 dependsOnMethods = {"createList", "read"})
275 public void readList(String testName) throws Exception {
278 setupReadList(testName);
280 // Submit the request to the service and store the response.
281 RelationClient client = new RelationClient();
282 ClientResponse<RelationsCommonList> res = client.readList();
283 RelationsCommonList list = res.getEntity();
284 int statusCode = res.getStatus();
286 // Check the status code of the response: does it match
287 // the expected response(s)?
288 if(logger.isDebugEnabled()){
289 logger.debug(testName + ": status = " + statusCode);
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 // Optionally output additional data about list members for debugging.
296 boolean iterateThroughList = false;
297 if(iterateThroughList && logger.isDebugEnabled()){
298 List<RelationsCommonList.RelationListItem> items =
299 list.getRelationListItem();
301 for(RelationsCommonList.RelationListItem item : items){
302 logger.debug(testName + ": list-item[" + i + "] csid=" +
304 logger.debug(testName + ": list-item[" + i + "] URI=" +
315 // ---------------------------------------------------------------
316 // CRUD tests : UPDATE tests
317 // ---------------------------------------------------------------
321 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
322 dependsOnMethods = {"read"})
323 public void update(String testName) throws Exception {
326 setupUpdate(testName);
328 // Retrieve an existing resource that we can update.
329 RelationClient client = new RelationClient();
330 ClientResponse<MultipartInput> res =
331 client.read(knownResourceId);
332 if(logger.isDebugEnabled()){
333 logger.debug(testName + ": read status = " + res.getStatus());
335 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
336 if(logger.isDebugEnabled()){
337 logger.debug("Got object to update with ID: " + knownResourceId);
339 MultipartInput input = (MultipartInput) res.getEntity();
340 RelationsCommon relation = (RelationsCommon) extractPart(input,
341 client.getCommonPartName(), RelationsCommon.class);
342 Assert.assertNotNull(relation);
344 // Update the content of this resource.
345 relation.setDocumentId1("updated-" + relation.getDocumentId1());
346 relation.setDocumentType1("updated-" + relation.getDocumentType1());
347 relation.setDocumentId2("updated-" + relation.getDocumentId2());
348 relation.setDocumentType2("updated-" + relation.getDocumentType2());
349 if(logger.isDebugEnabled()){
350 logger.debug("updated object");
351 logger.debug(objectAsXmlString(relation, RelationsCommon.class));
354 // Submit the request to the service and store the response.
355 MultipartOutput output = new MultipartOutput();
356 OutputPart commonPart = output.addPart(relation, MediaType.APPLICATION_XML_TYPE);
357 commonPart.getHeaders().add("label", client.getCommonPartName());
358 res = client.update(knownResourceId, output);
359 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match the expected response(s)?
362 if(logger.isDebugEnabled()){
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369 input = (MultipartInput) res.getEntity();
370 RelationsCommon updatedObject = (RelationsCommon) extractPart(
371 input, client.getCommonPartName(),
372 RelationsCommon.class);
373 Assert.assertNotNull(updatedObject);
376 "Data in updated object did not match submitted data.";
378 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
380 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
382 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
384 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
389 // Placeholders until the three tests below can be uncommented.
390 // See Issue CSPACE-401.
391 public void updateWithEmptyEntityBody(String testName) throws Exception {
394 public void updateWithMalformedXml(String testName) throws Exception {
397 public void updateWithWrongXmlSchema(String testName) throws Exception {
402 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
403 dependsOnMethods = {"create", "update", "testSubmitRequest"})
404 public void updateWithEmptyEntityBody(String testName) throws Exception {
407 setupUpdateWithEmptyEntityBody(testName);
409 // Submit the request to the service and store the response.
410 String method = REQUEST_TYPE.httpMethodName();
411 String url = getResourceURL(knownResourceId);
412 String mediaType = MediaType.APPLICATION_XML;
413 final String entity = "";
414 int statusCode = submitRequest(method, url, mediaType, entity);
416 // Check the status code of the response: does it match
417 // the expected response(s)?
418 if(logger.isDebugEnabled()){
419 logger.debug(testName + ": url=" + url +
420 " status=" + statusCode);
422 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
423 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
424 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
428 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
429 dependsOnMethods = {"create", "update", "testSubmitRequest"})
430 public void updateWithMalformedXml(String testName) throws Exception {
433 setupUpdateWithMalformedXml(testName);
435 // Submit the request to the service and store the response.
436 String method = REQUEST_TYPE.httpMethodName();
437 String url = getResourceURL(knownResourceId);
438 String mediaType = MediaType.APPLICATION_XML;
439 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
440 int statusCode = submitRequest(method, url, mediaType, entity);
442 // Check the status code of the response: does it match
443 // the expected response(s)?
444 if(logger.isDebugEnabled()){
445 logger.debug(testName + ": url=" + url +
446 " status=" + statusCode);
448 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
449 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
450 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
454 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
455 dependsOnMethods = {"create", "update", "testSubmitRequest"})
456 public void updateWithWrongXmlSchema(String testName) throws Exception {
459 setupUpdateWithWrongXmlSchema(testName);
461 // Submit the request to the service and store the response.
462 String method = REQUEST_TYPE.httpMethodName();
463 String url = getResourceURL(knownResourceId);
464 String mediaType = MediaType.APPLICATION_XML;
465 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
466 int statusCode = submitRequest(method, url, mediaType, entity);
468 // Check the status code of the response: does it match
469 // the expected response(s)?
470 if(logger.isDebugEnabled()){
471 logger.debug(testName + ": url=" + url +
472 " status=" + statusCode);
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
481 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
482 dependsOnMethods = {"update", "testSubmitRequest"})
483 public void updateNonExistent(String testName) throws Exception {
486 setupUpdateNonExistent(testName);
488 // Submit the request to the service and store the response.
489 // Note: The ID used in this 'create' call may be arbitrary.
490 // The only relevant ID may be the one used in update(), below.
491 RelationClient client = new RelationClient();
492 MultipartOutput multipart = createRelationInstance(NON_EXISTENT_ID);
493 ClientResponse<MultipartInput> res =
494 client.update(NON_EXISTENT_ID, multipart);
495 int statusCode = res.getStatus();
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 if(logger.isDebugEnabled()){
500 logger.debug(testName + ": status = " + statusCode);
502 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
503 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
504 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
507 // ---------------------------------------------------------------
508 // CRUD tests : DELETE tests
509 // ---------------------------------------------------------------
512 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
513 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
514 public void delete(String testName) throws Exception {
517 setupDelete(testName);
519 // Submit the request to the service and store the response.
520 RelationClient client = new RelationClient();
521 ClientResponse<Response> res = client.delete(knownResourceId);
522 int statusCode = res.getStatus();
524 // Check the status code of the response: does it match
525 // the expected response(s)?
526 if(logger.isDebugEnabled()){
527 logger.debug(testName + ": status = " + statusCode);
529 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
530 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
531 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
537 dependsOnMethods = {"delete"})
538 public void deleteNonExistent(String testName) throws Exception {
541 setupDeleteNonExistent(testName);
543 // Submit the request to the service and store the response.
544 RelationClient client = new RelationClient();
545 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
546 int statusCode = res.getStatus();
548 // Check the status code of the response: does it match
549 // the expected response(s)?
550 if(logger.isDebugEnabled()){
551 logger.debug(testName + ": status = " + statusCode);
553 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
554 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
555 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
558 // ---------------------------------------------------------------
559 // RELATE_OBJECT tests
560 // ---------------------------------------------------------------
561 @Test(dependsOnMethods = {"create"})
562 public void relateObjects() {
565 // ---------------------------------------------------------------
566 // Utility tests : tests of code used in tests above
567 // ---------------------------------------------------------------
569 * Tests the code for manually submitting data that is used by several
570 * of the methods above.
572 @Test(dependsOnMethods = {"create", "read"})
573 public void testSubmitRequest() {
575 // Expected status code: 200 OK
576 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
578 // Submit the request to the service and store the response.
579 String method = ServiceRequestType.READ.httpMethodName();
580 String url = getResourceURL(knownResourceId);
581 int statusCode = submitRequest(method, url);
583 // Check the status code of the response: does it match
584 // the expected response(s)?
585 if(logger.isDebugEnabled()){
586 logger.debug("testSubmitRequest: url=" + url +
587 " status=" + statusCode);
589 Assert.assertEquals(statusCode, EXPECTED_STATUS);
593 // ---------------------------------------------------------------
594 // Cleanup of resources created during testing
595 // ---------------------------------------------------------------
598 * Deletes all resources created by tests, after all tests have been run.
600 * This cleanup method will always be run, even if one or more tests fail.
601 * For this reason, it attempts to remove all resources created
602 * at any point during testing, even if some of those resources
603 * may be expected to be deleted by certain tests.
605 @AfterClass(alwaysRun=true)
606 public void cleanUp() {
607 if (logger.isDebugEnabled()) {
608 logger.debug("Cleaning up temporary resources created for testing ...");
610 RelationClient client = new RelationClient();
611 for (String resourceId : allResourceIdsCreated) {
612 // Note: Any non-success responses are ignored and not reported.
613 ClientResponse<Response> res = client.delete(resourceId);
617 // ---------------------------------------------------------------
618 // Utility methods used by tests above
619 // ---------------------------------------------------------------
621 public String getServicePathComponent() {
622 return SERVICE_PATH_COMPONENT;
625 private MultipartOutput createRelationInstance(String identifier) {
626 RelationsCommon relation = new RelationsCommon();
627 fillRelation(relation, identifier);
629 MultipartOutput multipart = new MultipartOutput();
630 OutputPart commonPart =
631 multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
632 commonPart.getHeaders().add("label", new RelationClient().getCommonPartName());
633 if(logger.isDebugEnabled()){
634 logger.debug("to be created, relation common");
635 logger.debug(objectAsXmlString(relation, RelationsCommon.class));
641 * Fills the relation.
643 * @param identifier the identifier
645 * @return the relation
647 private void fillRelation(RelationsCommon relation, String identifier) {
648 fillRelation(relation, "Subject-" + identifier,
649 "SubjectType-" + identifier + "-type",
650 "Object-" + identifier,
651 "ObjectType-" + identifier + "-type",
652 RelationshipType.COLLECTIONOBJECT_INTAKE);
656 * Fills the relation.
658 * @param documentId1 the document id1
659 * @param documentType1 the document type1
660 * @param documentId2 the document id2
661 * @param documentType2 the document type2
664 * @return the relation
666 private void fillRelation(RelationsCommon relation,
667 String documentId1, String documentType1,
668 String documentId2, String documentType2,
669 RelationshipType rt) {
670 relation.setDocumentId1(documentId1);
671 relation.setDocumentType1(documentType1);
672 relation.setDocumentId2(documentId2);
673 relation.setDocumentType2(documentType2);
675 relation.setRelationshipType(rt);