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.RelationClient;
30 import org.collectionspace.services.relation.RelationsCommon;
31 import org.collectionspace.services.relation.RelationsCommonList;
32 import org.collectionspace.services.relation.RelationshipType;
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 * RelationServiceTest, carries out tests against a
47 * deployed and running Relation Service.
49 * $LastChangedRevision$
52 public class RelationServiceTest extends AbstractServiceTest {
54 private final Logger logger =
55 LoggerFactory.getLogger(RelationServiceTest.class);
57 private RelationClient client = new RelationClient();
58 final String SERVICE_PATH_COMPONENT = "relations";
59 private String knownResourceId = null;
61 // ---------------------------------------------------------------
62 // CRUD tests : CREATE tests
63 // ---------------------------------------------------------------
66 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
67 public void create(String testName) throws Exception {
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).
72 setupCreate(testName);
74 // Submit the request to the service and store the response.
75 String identifier = createIdentifier();
76 MultipartOutput multipart = createRelationInstance(identifier);
77 ClientResponse<Response> res = client.create(multipart);
78 int statusCode = res.getStatus();
80 // Check the status code of the response: does it match
81 // the expected response(s)?
83 // Does it fall within the set of valid status codes?
84 // Does it exactly match the expected status code?
85 if(logger.isDebugEnabled()){
86 logger.debug(testName + ": status = " + statusCode);
88 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
89 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
90 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
92 // Store the ID returned from this create operation for
93 // additional tests below.
94 knownResourceId = extractId(res);
95 if(logger.isDebugEnabled()){
96 logger.debug("create: knownResourceId=" + knownResourceId);
101 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
102 dependsOnMethods = {"create"})
103 public void createList(String testName) throws Exception {
104 for(int i = 0; i < 3; i++){
110 // Placeholders until the three tests below can be uncommented.
111 // See Issue CSPACE-401.
112 public void createWithEmptyEntityBody(String testName) throws Exception {
115 public void createWithMalformedXml(String testName) throws Exception {
118 public void createWithWrongXmlSchema(String testName) throws Exception {
123 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
124 dependsOnMethods = {"create", "testSubmitRequest"})
125 public void createWithEmptyEntityBody(String testName) throws Exception {
128 setupCreateWithEmptyEntityBody(testName);
130 // Submit the request to the service and store the response.
131 String method = REQUEST_TYPE.httpMethodName();
132 String url = getServiceRootURL();
133 String mediaType = MediaType.APPLICATION_XML;
134 final String entity = "";
135 int statusCode = submitRequest(method, url, mediaType, entity);
137 // Check the status code of the response: does it match
138 // the expected response(s)?
139 if(logger.isDebugEnabled()){
140 logger.debug(testName + ": url=" + url +
141 " status=" + statusCode);
143 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
144 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
149 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
150 dependsOnMethods = {"create", "testSubmitRequest"})
151 public void createWithMalformedXml(String testName) throws Exception {
154 setupCreateWithMalformedXml(testName);
156 // Submit the request to the service and store the response.
157 String method = REQUEST_TYPE.httpMethodName();
158 String url = getServiceRootURL();
159 String mediaType = MediaType.APPLICATION_XML;
160 final String entity = MALFORMED_XML_DATA; // Constant from base class.
161 int statusCode = submitRequest(method, url, mediaType, entity);
163 // Check the status code of the response: does it match
164 // the expected response(s)?
165 if(logger.isDebugEnabled()){
166 logger.debug(testName + ": url=" + url +
167 " status=" + statusCode);
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
176 dependsOnMethods = {"create", "testSubmitRequest"})
177 public void createWithWrongXmlSchema(String testName) throws Exception {
180 setupCreateWithWrongXmlSchema(testName);
182 // Submit the request to the service and store the response.
183 String method = REQUEST_TYPE.httpMethodName();
184 String url = getServiceRootURL();
185 String mediaType = MediaType.APPLICATION_XML;
186 final String entity = WRONG_XML_SCHEMA_DATA;
187 int statusCode = submitRequest(method, url, mediaType, entity);
189 // Check the status code of the response: does it match
190 // the expected response(s)?
191 if(logger.isDebugEnabled()){
192 logger.debug(testName + ": url=" + url +
193 " status=" + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 // ---------------------------------------------------------------
202 // CRUD tests : READ tests
203 // ---------------------------------------------------------------
206 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
207 dependsOnMethods = {"create"})
208 public void read(String testName) throws Exception {
213 // Submit the request to the service and store the response.
214 ClientResponse<MultipartInput> res = client.read(knownResourceId);
215 int statusCode = res.getStatus();
217 // Check the status code of the response: does it match
218 // the expected response(s)?
219 if(logger.isDebugEnabled()){
220 logger.debug(testName + ": status = " + statusCode);
222 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
226 // Verify that the resource identifier ...
227 MultipartInput input = (MultipartInput) res.getEntity();
228 RelationsCommon relation = (RelationsCommon) extractPart(input,
229 client.getCommonPartName(), RelationsCommon.class);
230 Assert.assertNotNull(relation);
236 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
237 dependsOnMethods = {"read"})
238 public void readNonExistent(String testName) throws Exception {
241 setupReadNonExistent(testName);
243 // Submit the request to the service and store the response.
244 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
245 int statusCode = res.getStatus();
247 // Check the status code of the response: does it match
248 // the expected response(s)?
249 if(logger.isDebugEnabled()){
250 logger.debug(testName + ": status = " + statusCode);
252 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
257 // ---------------------------------------------------------------
258 // CRUD tests : READ_LIST tests
259 // ---------------------------------------------------------------
262 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
263 dependsOnMethods = {"createList", "read"})
264 public void readList(String testName) throws Exception {
267 setupReadList(testName);
269 // Submit the request to the service and store the response.
270 ClientResponse<RelationsCommonList> res = client.readList();
271 RelationsCommonList list = res.getEntity();
272 int statusCode = res.getStatus();
274 // Check the status code of the response: does it match
275 // the expected response(s)?
276 if(logger.isDebugEnabled()){
277 logger.debug(testName + ": status = " + statusCode);
279 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
280 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
281 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
283 // Optionally output additional data about list members for debugging.
284 boolean iterateThroughList = false;
285 if(iterateThroughList && logger.isDebugEnabled()){
286 List<RelationsCommonList.RelationListItem> items =
287 list.getRelationListItem();
289 for(RelationsCommonList.RelationListItem item : items){
290 logger.debug(testName + ": list-item[" + i + "] csid=" +
292 logger.debug(testName + ": list-item[" + i + "] URI=" +
303 // ---------------------------------------------------------------
304 // CRUD tests : UPDATE tests
305 // ---------------------------------------------------------------
309 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
310 dependsOnMethods = {"read"})
311 public void update(String testName) throws Exception {
314 setupUpdate(testName);
316 // Retrieve an existing resource that we can update.
317 ClientResponse<MultipartInput> res =
318 client.read(knownResourceId);
319 if(logger.isDebugEnabled()){
320 logger.debug(testName + ": read status = " + res.getStatus());
322 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
323 if(logger.isDebugEnabled()){
324 logger.debug("Got object to update with ID: " + knownResourceId);
326 MultipartInput input = (MultipartInput) res.getEntity();
327 RelationsCommon relation = (RelationsCommon) extractPart(input,
328 client.getCommonPartName(), RelationsCommon.class);
329 Assert.assertNotNull(relation);
331 // Update the content of this resource.
332 relation.setDocumentId1("updated-" + relation.getDocumentId1());
333 relation.setDocumentType1("updated-" + relation.getDocumentType1());
334 relation.setDocumentId2("updated-" + relation.getDocumentId2());
335 relation.setDocumentType2("updated-" + relation.getDocumentType2());
336 if(logger.isDebugEnabled()){
337 verbose("updated object", relation, RelationsCommon.class);
340 // Submit the request to the service and store the response.
341 MultipartOutput output = new MultipartOutput();
342 OutputPart commonPart = output.addPart(relation, MediaType.APPLICATION_XML_TYPE);
343 commonPart.getHeaders().add("label", client.getCommonPartName());
344 res = client.update(knownResourceId, output);
345 int statusCode = res.getStatus();
347 // Check the status code of the response: does it match the expected response(s)?
348 if(logger.isDebugEnabled()){
349 logger.debug(testName + ": status = " + statusCode);
351 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
352 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
353 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
355 input = (MultipartInput) res.getEntity();
356 RelationsCommon updatedObject = (RelationsCommon) extractPart(
357 input, client.getCommonPartName(),
358 RelationsCommon.class);
359 Assert.assertNotNull(updatedObject);
362 "Data in updated object did not match submitted data.";
364 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
366 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
368 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
370 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
375 // Placeholders until the three tests below can be uncommented.
376 // See Issue CSPACE-401.
377 public void updateWithEmptyEntityBody(String testName) throws Exception {
380 public void updateWithMalformedXml(String testName) throws Exception {
383 public void updateWithWrongXmlSchema(String testName) throws Exception {
388 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
389 dependsOnMethods = {"create", "update", "testSubmitRequest"})
390 public void updateWithEmptyEntityBody(String testName) throws Exception {
393 setupUpdateWithEmptyEntityBody(testName);
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 = "";
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 if(logger.isDebugEnabled()){
405 logger.debug(testName + ": url=" + url +
406 " status=" + statusCode);
408 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
409 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
410 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
414 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
415 dependsOnMethods = {"create", "update", "testSubmitRequest"})
416 public void updateWithMalformedXml(String testName) throws Exception {
419 setupUpdateWithMalformedXml(testName);
421 // Submit the request to the service and store the response.
422 String method = REQUEST_TYPE.httpMethodName();
423 String url = getResourceURL(knownResourceId);
424 String mediaType = MediaType.APPLICATION_XML;
425 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
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(testName + ": 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(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
441 dependsOnMethods = {"create", "update", "testSubmitRequest"})
442 public void updateWithWrongXmlSchema(String testName) throws Exception {
445 setupUpdateWithWrongXmlSchema(testName);
447 // Submit the request to the service and store the response.
448 String method = REQUEST_TYPE.httpMethodName();
449 String url = getResourceURL(knownResourceId);
450 String mediaType = MediaType.APPLICATION_XML;
451 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
452 int statusCode = submitRequest(method, url, mediaType, entity);
454 // Check the status code of the response: does it match
455 // the expected response(s)?
456 if(logger.isDebugEnabled()){
457 logger.debug(testName + ": url=" + url +
458 " status=" + statusCode);
460 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
467 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
468 dependsOnMethods = {"update", "testSubmitRequest"})
469 public void updateNonExistent(String testName) throws Exception {
472 setupUpdateNonExistent(testName);
474 // Submit the request to the service and store the response.
475 // Note: The ID used in this 'create' call may be arbitrary.
476 // The only relevant ID may be the one used in update(), below.
477 MultipartOutput multipart = createRelationInstance(NON_EXISTENT_ID);
478 ClientResponse<MultipartInput> res =
479 client.update(NON_EXISTENT_ID, multipart);
480 int statusCode = res.getStatus();
482 // Check the status code of the response: does it match
483 // the expected response(s)?
484 if(logger.isDebugEnabled()){
485 logger.debug(testName + ": status = " + statusCode);
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 // ---------------------------------------------------------------
493 // CRUD tests : DELETE tests
494 // ---------------------------------------------------------------
497 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
498 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
499 public void delete(String testName) throws Exception {
502 setupDelete(testName);
504 // Submit the request to the service and store the response.
505 ClientResponse<Response> res = client.delete(knownResourceId);
506 int statusCode = res.getStatus();
508 // Check the status code of the response: does it match
509 // the expected response(s)?
510 if(logger.isDebugEnabled()){
511 logger.debug(testName + ": status = " + statusCode);
513 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
520 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
521 dependsOnMethods = {"delete"})
522 public void deleteNonExistent(String testName) throws Exception {
525 setupDeleteNonExistent(testName);
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(testName + ": status = " + statusCode);
536 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
541 // ---------------------------------------------------------------
542 // RELATE_OBJECT tests
543 // ---------------------------------------------------------------
544 @Test(dependsOnMethods = {"create"})
545 public void relateObjects() {
548 // ---------------------------------------------------------------
549 // Utility tests : tests of code used in tests above
550 // ---------------------------------------------------------------
552 * Tests the code for manually submitting data that is used by several
553 * of the methods above.
555 @Test(dependsOnMethods = {"create", "read"})
556 public void testSubmitRequest() {
558 // Expected status code: 200 OK
559 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
561 // Submit the request to the service and store the response.
562 String method = ServiceRequestType.READ.httpMethodName();
563 String url = getResourceURL(knownResourceId);
564 int statusCode = submitRequest(method, url);
566 // Check the status code of the response: does it match
567 // the expected response(s)?
568 if(logger.isDebugEnabled()){
569 logger.debug("testSubmitRequest: url=" + url +
570 " status=" + statusCode);
572 Assert.assertEquals(statusCode, EXPECTED_STATUS);
576 // ---------------------------------------------------------------
577 // Utility methods used by tests above
578 // ---------------------------------------------------------------
580 public String getServicePathComponent() {
581 return SERVICE_PATH_COMPONENT;
584 private MultipartOutput createRelationInstance(String identifier) {
585 RelationsCommon relation = new RelationsCommon();
586 fillRelation(relation, identifier);
588 MultipartOutput multipart = new MultipartOutput();
589 OutputPart commonPart =
590 multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
591 commonPart.getHeaders().add("label", client.getCommonPartName());
592 if(logger.isDebugEnabled()){
593 verbose("to be created, relation common ", relation,
594 RelationsCommon.class);
600 * Fills the relation.
602 * @param identifier the identifier
604 * @return the relation
606 private void fillRelation(RelationsCommon relation, String identifier) {
607 fillRelation(relation, "Subject-" + identifier,
608 "SubjectType-" + identifier + "-type",
609 "Object-" + identifier,
610 "ObjectType-" + identifier + "-type",
611 RelationshipType.COLLECTIONOBJECT_INTAKE);
615 * Fills the relation.
617 * @param documentId1 the document id1
618 * @param documentType1 the document type1
619 * @param documentId2 the document id2
620 * @param documentType2 the document type2
623 * @return the relation
625 private void fillRelation(RelationsCommon relation,
626 String documentId1, String documentType1,
627 String documentId2, String documentType2,
628 RelationshipType rt) {
629 relation.setDocumentId1(documentId1);
630 relation.setDocumentType1(documentType1);
631 relation.setDocumentId2(documentId2);
632 relation.setDocumentType2(documentType2);
634 relation.setRelationshipType(rt);