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 private RelationClient client = new RelationClient();
60 final String SERVICE_PATH_COMPONENT = "relations";
61 private String knownResourceId = null;
62 private List<String> allResourceIdsCreated = new ArrayList();
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
69 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
70 public void create(String testName) throws Exception {
72 // Perform setup, such as initializing the type of service request
73 // (e.g. CREATE, DELETE), its valid and expected status codes, and
74 // its associated HTTP method name (e.g. POST, DELETE).
75 setupCreate(testName);
77 // Submit the request to the service and store the response.
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 ClientResponse<MultipartInput> res = client.read(knownResourceId);
224 int statusCode = res.getStatus();
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ": status = " + statusCode);
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 // Verify that the resource identifier ...
236 MultipartInput input = (MultipartInput) res.getEntity();
237 RelationsCommon relation = (RelationsCommon) extractPart(input,
238 client.getCommonPartName(), RelationsCommon.class);
239 Assert.assertNotNull(relation);
245 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
246 dependsOnMethods = {"read"})
247 public void readNonExistent(String testName) throws Exception {
250 setupReadNonExistent(testName);
252 // Submit the request to the service and store the response.
253 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
254 int statusCode = res.getStatus();
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if(logger.isDebugEnabled()){
259 logger.debug(testName + ": status = " + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 // ---------------------------------------------------------------
267 // CRUD tests : READ_LIST tests
268 // ---------------------------------------------------------------
271 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
272 dependsOnMethods = {"createList", "read"})
273 public void readList(String testName) throws Exception {
276 setupReadList(testName);
278 // Submit the request to the service and store the response.
279 ClientResponse<RelationsCommonList> res = client.readList();
280 RelationsCommonList 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(testName + ": status = " + statusCode);
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<RelationsCommonList.RelationListItem> items =
296 list.getRelationListItem();
298 for(RelationsCommonList.RelationListItem item : items){
299 logger.debug(testName + ": list-item[" + i + "] csid=" +
301 logger.debug(testName + ": list-item[" + i + "] URI=" +
312 // ---------------------------------------------------------------
313 // CRUD tests : UPDATE tests
314 // ---------------------------------------------------------------
318 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
319 dependsOnMethods = {"read"})
320 public void update(String testName) throws Exception {
323 setupUpdate(testName);
325 // Retrieve an existing resource that we can update.
326 ClientResponse<MultipartInput> res =
327 client.read(knownResourceId);
328 if(logger.isDebugEnabled()){
329 logger.debug(testName + ": read status = " + res.getStatus());
331 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 RelationsCommon relation = (RelationsCommon) extractPart(input,
337 client.getCommonPartName(), RelationsCommon.class);
338 Assert.assertNotNull(relation);
340 // Update the content of this resource.
341 relation.setDocumentId1("updated-" + relation.getDocumentId1());
342 relation.setDocumentType1("updated-" + relation.getDocumentType1());
343 relation.setDocumentId2("updated-" + relation.getDocumentId2());
344 relation.setDocumentType2("updated-" + relation.getDocumentType2());
345 if(logger.isDebugEnabled()){
346 logger.debug("updated object");
347 logger.debug(objectAsXmlString(relation, RelationsCommon.class));
350 // Submit the request to the service and store the response.
351 MultipartOutput output = new MultipartOutput();
352 OutputPart commonPart = output.addPart(relation, MediaType.APPLICATION_XML_TYPE);
353 commonPart.getHeaders().add("label", client.getCommonPartName());
354 res = client.update(knownResourceId, output);
355 int statusCode = res.getStatus();
357 // Check the status code of the response: does it match the expected response(s)?
358 if(logger.isDebugEnabled()){
359 logger.debug(testName + ": status = " + statusCode);
361 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
362 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
363 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
365 input = (MultipartInput) res.getEntity();
366 RelationsCommon updatedObject = (RelationsCommon) extractPart(
367 input, client.getCommonPartName(),
368 RelationsCommon.class);
369 Assert.assertNotNull(updatedObject);
372 "Data in updated object did not match submitted data.";
374 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
376 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
378 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
380 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
385 // Placeholders until the three tests below can be uncommented.
386 // See Issue CSPACE-401.
387 public void updateWithEmptyEntityBody(String testName) throws Exception {
390 public void updateWithMalformedXml(String testName) throws Exception {
393 public void updateWithWrongXmlSchema(String testName) throws Exception {
398 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
399 dependsOnMethods = {"create", "update", "testSubmitRequest"})
400 public void updateWithEmptyEntityBody(String testName) throws Exception {
403 setupUpdateWithEmptyEntityBody(testName);
405 // Submit the request to the service and store the response.
406 String method = REQUEST_TYPE.httpMethodName();
407 String url = getResourceURL(knownResourceId);
408 String mediaType = MediaType.APPLICATION_XML;
409 final String entity = "";
410 int statusCode = submitRequest(method, url, mediaType, entity);
412 // Check the status code of the response: does it match
413 // the expected response(s)?
414 if(logger.isDebugEnabled()){
415 logger.debug(testName + ": url=" + url +
416 " status=" + statusCode);
418 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
424 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
425 dependsOnMethods = {"create", "update", "testSubmitRequest"})
426 public void updateWithMalformedXml(String testName) throws Exception {
429 setupUpdateWithMalformedXml(testName);
431 // Submit the request to the service and store the response.
432 String method = REQUEST_TYPE.httpMethodName();
433 String url = getResourceURL(knownResourceId);
434 String mediaType = MediaType.APPLICATION_XML;
435 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
436 int statusCode = submitRequest(method, url, mediaType, entity);
438 // Check the status code of the response: does it match
439 // the expected response(s)?
440 if(logger.isDebugEnabled()){
441 logger.debug(testName + ": url=" + url +
442 " status=" + statusCode);
444 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
445 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
446 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
450 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
451 dependsOnMethods = {"create", "update", "testSubmitRequest"})
452 public void updateWithWrongXmlSchema(String testName) throws Exception {
455 setupUpdateWithWrongXmlSchema(testName);
457 // Submit the request to the service and store the response.
458 String method = REQUEST_TYPE.httpMethodName();
459 String url = getResourceURL(knownResourceId);
460 String mediaType = MediaType.APPLICATION_XML;
461 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
462 int statusCode = submitRequest(method, url, mediaType, entity);
464 // Check the status code of the response: does it match
465 // the expected response(s)?
466 if(logger.isDebugEnabled()){
467 logger.debug(testName + ": url=" + url +
468 " status=" + statusCode);
470 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
478 dependsOnMethods = {"update", "testSubmitRequest"})
479 public void updateNonExistent(String testName) throws Exception {
482 setupUpdateNonExistent(testName);
484 // 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 update(), below.
487 MultipartOutput multipart = createRelationInstance(NON_EXISTENT_ID);
488 ClientResponse<MultipartInput> res =
489 client.update(NON_EXISTENT_ID, multipart);
490 int statusCode = res.getStatus();
492 // Check the status code of the response: does it match
493 // the expected response(s)?
494 if(logger.isDebugEnabled()){
495 logger.debug(testName + ": status = " + statusCode);
497 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
498 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
499 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
502 // ---------------------------------------------------------------
503 // CRUD tests : DELETE tests
504 // ---------------------------------------------------------------
507 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
508 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
509 public void delete(String testName) throws Exception {
512 setupDelete(testName);
514 // Submit the request to the service and store the response.
515 ClientResponse<Response> res = client.delete(knownResourceId);
516 int statusCode = res.getStatus();
518 // Check the status code of the response: does it match
519 // the expected response(s)?
520 if(logger.isDebugEnabled()){
521 logger.debug(testName + ": status = " + statusCode);
523 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
524 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
525 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
530 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
531 dependsOnMethods = {"delete"})
532 public void deleteNonExistent(String testName) throws Exception {
535 setupDeleteNonExistent(testName);
537 // Submit the request to the service and store the response.
538 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
539 int statusCode = res.getStatus();
541 // Check the status code of the response: does it match
542 // the expected response(s)?
543 if(logger.isDebugEnabled()){
544 logger.debug(testName + ": status = " + statusCode);
546 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
547 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
548 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
551 // ---------------------------------------------------------------
552 // RELATE_OBJECT tests
553 // ---------------------------------------------------------------
554 @Test(dependsOnMethods = {"create"})
555 public void relateObjects() {
558 // ---------------------------------------------------------------
559 // Utility tests : tests of code used in tests above
560 // ---------------------------------------------------------------
562 * Tests the code for manually submitting data that is used by several
563 * of the methods above.
565 @Test(dependsOnMethods = {"create", "read"})
566 public void testSubmitRequest() {
568 // Expected status code: 200 OK
569 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
571 // Submit the request to the service and store the response.
572 String method = ServiceRequestType.READ.httpMethodName();
573 String url = getResourceURL(knownResourceId);
574 int statusCode = submitRequest(method, url);
576 // Check the status code of the response: does it match
577 // the expected response(s)?
578 if(logger.isDebugEnabled()){
579 logger.debug("testSubmitRequest: url=" + url +
580 " status=" + statusCode);
582 Assert.assertEquals(statusCode, EXPECTED_STATUS);
586 // ---------------------------------------------------------------
587 // Cleanup of resources created during testing
588 // ---------------------------------------------------------------
591 * Deletes all resources created by tests, after all tests have been run.
593 * This cleanup method will always be run, even if one or more tests fail.
594 * For this reason, it attempts to remove all resources created
595 * at any point during testing, even if some of those resources
596 * may be expected to be deleted by certain tests.
598 @AfterClass(alwaysRun=true)
599 public void cleanUp() {
600 if (logger.isDebugEnabled()) {
601 logger.debug("Cleaning up temporary resources created for testing ...");
603 for (String resourceId : allResourceIdsCreated) {
604 // Note: Any non-success responses are ignored and not reported.
605 ClientResponse<Response> res = client.delete(resourceId);
609 // ---------------------------------------------------------------
610 // Utility methods used by tests above
611 // ---------------------------------------------------------------
613 public String getServicePathComponent() {
614 return SERVICE_PATH_COMPONENT;
617 private MultipartOutput createRelationInstance(String identifier) {
618 RelationsCommon relation = new RelationsCommon();
619 fillRelation(relation, identifier);
621 MultipartOutput multipart = new MultipartOutput();
622 OutputPart commonPart =
623 multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
624 commonPart.getHeaders().add("label", client.getCommonPartName());
625 if(logger.isDebugEnabled()){
626 logger.debug("to be created, relation common");
627 logger.debug(objectAsXmlString(relation, RelationsCommon.class));
633 * Fills the relation.
635 * @param identifier the identifier
637 * @return the relation
639 private void fillRelation(RelationsCommon relation, String identifier) {
640 fillRelation(relation, "Subject-" + identifier,
641 "SubjectType-" + identifier + "-type",
642 "Object-" + identifier,
643 "ObjectType-" + identifier + "-type",
644 RelationshipType.COLLECTIONOBJECT_INTAKE);
648 * Fills the relation.
650 * @param documentId1 the document id1
651 * @param documentType1 the document type1
652 * @param documentId2 the document id2
653 * @param documentType2 the document type2
656 * @return the relation
658 private void fillRelation(RelationsCommon relation,
659 String documentId1, String documentType1,
660 String documentId2, String documentType2,
661 RelationshipType rt) {
662 relation.setDocumentId1(documentId1);
663 relation.setDocumentType1(documentType1);
664 relation.setDocumentId2(documentId2);
665 relation.setDocumentType2(documentType2);
667 relation.setRelationshipType(rt);