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.
24 package org.collectionspace.services.client.test;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
31 import org.collectionspace.services.client.RelationClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.relation.Relation;
34 import org.collectionspace.services.relation.RelationList;
35 import org.collectionspace.services.relation.RelationshipType;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
43 * RelationServiceTest, carries out tests against a
44 * deployed and running Relation Service.
46 * $LastChangedRevision$
49 public class RelationServiceTest extends AbstractServiceTest {
51 private RelationClient client = new RelationClient();
52 final String SERVICE_PATH_COMPONENT = "relations";
53 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
63 public void create() {
65 // Perform setup, such as initializing the type of service request
66 // (e.g. CREATE, DELETE), its valid and expected status codes, and
67 // its associated HTTP method name (e.g. POST, DELETE).
70 // Submit the request to the service and store the response.
71 String identifier = createIdentifier();
72 Relation relation = createRelationInstance(identifier);
73 ClientResponse<Response> res = client.create(relation);
74 int statusCode = res.getStatus();
76 // Check the status code of the response: does it match
77 // the expected response(s)?
79 // Does it fall within the set of valid status codes?
80 // Does it exactly match the expected status code?
81 verbose("create: status = " + statusCode);
82 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
83 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
84 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
86 // Store the ID returned from this create operation for
87 // additional tests below.
88 knownResourceId = extractId(res);
92 @Test(dependsOnMethods = {"create"})
93 public void createList() {
94 for(int i = 0; i < 3; i++){
101 // Placeholders until the three tests below can be uncommented.
102 // See Issue CSPACE-401.
103 public void createWithEmptyEntityBody() {}
104 public void createWithMalformedXml() {}
105 public void createWithWrongXmlSchema() {}
109 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
110 public void createWithEmptyEntityBody() {
113 setupCreateWithEmptyEntityBody();
115 // Submit the request to the service and store the response.
116 String method = REQUEST_TYPE.httpMethodName();
117 String url = getServiceRootURL();
118 String mediaType = MediaType.APPLICATION_XML;
119 final String entity = "";
120 int statusCode = submitRequest(method, url, mediaType, entity);
122 // Check the status code of the response: does it match
123 // the expected response(s)?
124 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
125 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
132 public void createWithMalformedXml() {
135 setupCreateWithMalformedXml();
137 // Submit the request to the service and store the response.
138 String method = REQUEST_TYPE.httpMethodName();
139 String url = getServiceRootURL();
140 String mediaType = MediaType.APPLICATION_XML;
141 final String entity = MALFORMED_XML_DATA; // Constant from base class.
142 int statusCode = submitRequest(method, url, mediaType, entity);
144 // Check the status code of the response: does it match
145 // the expected response(s)?
146 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
153 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
154 public void createWithWrongXmlSchema() {
157 setupCreateWithWrongXmlSchema();
159 // Submit the request to the service and store the response.
160 String method = REQUEST_TYPE.httpMethodName();
161 String url = getServiceRootURL();
162 String mediaType = MediaType.APPLICATION_XML;
163 final String entity = WRONG_XML_SCHEMA_DATA;
164 int statusCode = submitRequest(method, url, mediaType, entity);
166 // Check the status code of the response: does it match
167 // the expected response(s)?
168 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176 // ---------------------------------------------------------------
177 // CRUD tests : READ tests
178 // ---------------------------------------------------------------
183 @Test(dependsOnMethods = {"create"})
189 // Submit the request to the service and store the response.
190 ClientResponse<Relation> res = client.read(knownResourceId);
191 int statusCode = res.getStatus();
193 // Check the status code of the response: does it match
194 // the expected response(s)?
195 verbose("read: status = " + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
200 // Verify that the resource identifier
201 Relation returnedRelation = res.getEntity();
202 Assert.assertEquals(returnedRelation.getCsid(), knownResourceId);
209 @Test(dependsOnMethods = {"read"})
210 public void readNonExistent() {
213 setupReadNonExistent();
215 // Submit the request to the service and store the response.
216 ClientResponse<Relation> res = client.read(NON_EXISTENT_ID);
217 int statusCode = res.getStatus();
219 // Check the status code of the response: does it match
220 // the expected response(s)?
221 verbose("readNonExistent: status = " + res.getStatus());
222 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
228 // ---------------------------------------------------------------
229 // CRUD tests : READ_LIST tests
230 // ---------------------------------------------------------------
235 @Test(dependsOnMethods = {"createList"})
236 public void readList() {
241 // Submit the request to the service and store the response.
242 ClientResponse<RelationList> res = client.readList();
243 RelationList list = res.getEntity();
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 verbose("readList: status = " + res.getStatus());
249 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 // Optionally output additional data about list members for debugging.
254 boolean iterateThroughList = false;
255 if (iterateThroughList && logger.isDebugEnabled()) {
256 List<RelationList.RelationListItem> items =
257 list.getRelationListItem();
259 for(RelationList.RelationListItem item : items){
260 verbose("readList: list-item[" + i + "] csid=" +
262 verbose("readList: list-item[" + i + "] URI=" +
275 // ---------------------------------------------------------------
276 // CRUD tests : UPDATE tests
277 // ---------------------------------------------------------------
282 @Test(dependsOnMethods = {"create"})
283 public void update() {
288 // Retrieve an existing resource that we can update.
289 ClientResponse<Relation> res = client.read(knownResourceId);
290 verbose("read: status = " + res.getStatus());
291 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
292 Relation relation = res.getEntity();
293 verbose("Got object to update with ID: " + knownResourceId,
294 relation, Relation.class);
296 // Update the content of this resource.
297 relation.setDocumentId1("updated-" + relation.getDocumentId1());
298 relation.setDocumentType1("updated-" + relation.getDocumentType1());
299 relation.setDocumentId2("updated-" + relation.getDocumentId2());
300 relation.setDocumentType2("updated-" + relation.getDocumentType2());
302 // Submit the request to the service and store the response.
303 res = client.update(knownResourceId, relation);
304 int statusCode = res.getStatus();
305 Relation updatedObject = res.getEntity();
307 // Check the status code of the response: does it match
308 // the expected response(s)?
309 verbose("update: status = " + res.getStatus());
310 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
314 // Check the contents of the response: does it match
315 // what was submitted?
316 verbose("update: ", updatedObject, Relation.class);
318 "Data in updated object did not match submitted data.";
320 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
322 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
324 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
326 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
332 // Placeholders until the three tests below can be uncommented.
333 // See Issue CSPACE-401.
334 public void updateWithEmptyEntityBody() {}
335 public void updateWithMalformedXml() {}
336 public void updateWithWrongXmlSchema() {}
340 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
341 public void updateWithEmptyEntityBody() {
344 setupUpdateWithEmptyEntityBody();
346 // Submit the request to the service and store the response.
347 String method = REQUEST_TYPE.httpMethodName();
348 String url = getResourceURL(knownResourceId);
349 String mediaType = MediaType.APPLICATION_XML;
350 final String entity = "";
351 int statusCode = submitRequest(method, url, mediaType, entity);
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
356 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
357 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
358 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
363 public void updateWithMalformedXml() {
366 setupUpdateWithMalformedXml();
368 // Submit the request to the service and store the response.
369 String method = REQUEST_TYPE.httpMethodName();
370 String url = getResourceURL(knownResourceId);
371 String mediaType = MediaType.APPLICATION_XML;
372 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
373 int statusCode = submitRequest(method, url, mediaType, entity);
375 // Check the status code of the response: does it match
376 // the expected response(s)?
377 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
384 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
385 public void updateWithWrongXmlSchema() {
388 setupUpdateWithWrongXmlSchema();
390 // Submit the request to the service and store the response.
391 String method = REQUEST_TYPE.httpMethodName();
392 String url = getResourceURL(knownResourceId);
393 String mediaType = MediaType.APPLICATION_XML;
394 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
395 int statusCode = submitRequest(method, url, mediaType, entity);
397 // Check the status code of the response: does it match
398 // the expected response(s)?
399 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
400 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
401 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
402 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
409 public void updateNonExistent() {
412 setupUpdateNonExistent();
414 // Submit the request to the service and store the response.
415 // Note: The ID used in this 'create' call may be arbitrary.
416 // The only relevant ID may be the one used in update(), below.
417 Relation relation = createRelationInstance(NON_EXISTENT_ID);
418 ClientResponse<Relation> res =
419 client.update(NON_EXISTENT_ID, relation);
420 int statusCode = res.getStatus();
422 // Check the status code of the response: does it match
423 // the expected response(s)?
424 verbose("updateNonExistent: status = " + res.getStatus());
425 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
426 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
427 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431 // ---------------------------------------------------------------
432 // CRUD tests : DELETE tests
433 // ---------------------------------------------------------------
438 @Test(dependsOnMethods = {"create", "read", "update"})
439 public void delete() {
444 // Submit the request to the service and store the response.
445 ClientResponse<Response> res = client.delete(knownResourceId);
446 int statusCode = res.getStatus();
448 // Check the status code of the response: does it match
449 // the expected response(s)?
450 verbose("delete: status = " + res.getStatus());
451 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
452 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
453 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
459 @Test(dependsOnMethods = {"delete"})
460 public void deleteNonExistent() {
463 setupDeleteNonExistent();
465 // Submit the request to the service and store the response.
466 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
467 int statusCode = res.getStatus();
469 // Check the status code of the response: does it match
470 // the expected response(s)?
471 verbose("deleteNonExistent: status = " + res.getStatus());
472 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
473 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
474 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
478 // ---------------------------------------------------------------
479 // RELATE_OBJECT tests
480 // ---------------------------------------------------------------
482 @Test(dependsOnMethods = {"create"})
483 public void relateObjects() {
487 // ---------------------------------------------------------------
488 // Utility tests : tests of code used in tests above
489 // ---------------------------------------------------------------
492 * Tests the code for manually submitting data that is used by several
493 * of the methods above.
495 @Test(dependsOnMethods = {"create", "read"})
496 public void testSubmitRequest() {
498 // Expected status code: 200 OK
499 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
501 // Submit the request to the service and store the response.
502 String method = ServiceRequestType.READ.httpMethodName();
503 String url = getResourceURL(knownResourceId);
504 int statusCode = submitRequest(method, url);
506 // Check the status code of the response: does it match
507 // the expected response(s)?
508 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
509 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
514 // ---------------------------------------------------------------
515 // Utility methods used by tests above
516 // ---------------------------------------------------------------
519 public String getServicePathComponent() {
520 return SERVICE_PATH_COMPONENT;
523 private Relation createRelationInstance(String identifier) {
524 String result = null;
525 Relation relation = new Relation();
526 fillRelation(relation, identifier);
531 * Fills the relation.
533 * @param identifier the identifier
535 * @return the relation
537 private void fillRelation(Relation relation, String identifier) {
538 fillRelation(relation, "Subject-" + identifier,
539 "SubjectType-" + identifier + "-type",
540 "Object-" + identifier,
541 "ObjectType-" + identifier + "-type",
542 RelationshipType.COLLECTIONOBJECT_INTAKE);
546 * Fills the relation.
548 * @param documentId1 the document id1
549 * @param documentType1 the document type1
550 * @param documentId2 the document id2
551 * @param documentType2 the document type2
554 * @return the relation
556 private void fillRelation(Relation relation,
557 String documentId1, String documentType1,
558 String documentId2, String documentType2,
561 relation.setDocumentId1(documentId1);
562 relation.setDocumentType1(documentType1);
563 relation.setDocumentId2(documentId2);
564 relation.setDocumentType2(documentType2);
566 relation.setRelationshipType(rt);