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 two tests below can be uncommented.
102 // See Issue CSPACE-401.
103 public void createWithMalformedXml() {}
104 public void createWithWrongXmlSchema() {}
108 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
109 public void createWithMalformedXml() {
112 setupCreateWithMalformedXml();
114 // Submit the request to the service and store the response.
115 String method = REQUEST_TYPE.httpMethodName();
116 String url = getServiceRootURL();
117 String mediaType = MediaType.APPLICATION_XML;
118 final String entity = MALFORMED_XML_DATA; // Constant from base class.
119 int statusCode = submitRequest(method, url, mediaType, entity);
121 // Check the status code of the response: does it match
122 // the expected response(s)?
123 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
130 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
131 public void createWithWrongXmlSchema() {
134 setupCreateWithWrongXmlSchema();
136 // Submit the request to the service and store the response.
137 String method = REQUEST_TYPE.httpMethodName();
138 String url = getServiceRootURL();
139 String mediaType = MediaType.APPLICATION_XML;
140 final String entity = WRONG_XML_SCHEMA_DATA;
141 int statusCode = submitRequest(method, url, mediaType, entity);
143 // Check the status code of the response: does it match
144 // the expected response(s)?
145 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
146 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
153 // ---------------------------------------------------------------
154 // CRUD tests : READ tests
155 // ---------------------------------------------------------------
160 @Test(dependsOnMethods = {"create"})
166 // Submit the request to the service and store the response.
167 ClientResponse<Relation> res = client.read(knownResourceId);
168 int statusCode = res.getStatus();
170 // Check the status code of the response: does it match
171 // the expected response(s)?
172 verbose("read: status = " + statusCode);
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 // Verify that the resource identifier
178 Relation returnedRelation = res.getEntity();
179 Assert.assertEquals(returnedRelation.getCsid(), knownResourceId);
186 @Test(dependsOnMethods = {"read"})
187 public void readNonExistent() {
190 setupReadNonExistent();
192 // Submit the request to the service and store the response.
193 ClientResponse<Relation> res = client.read(NON_EXISTENT_ID);
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 verbose("readNonExistent: status = " + res.getStatus());
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 // ---------------------------------------------------------------
206 // CRUD tests : READ_LIST tests
207 // ---------------------------------------------------------------
212 @Test(dependsOnMethods = {"createList"})
213 public void readList() {
218 // Submit the request to the service and store the response.
219 ClientResponse<RelationList> res = client.readList();
220 RelationList list = res.getEntity();
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 verbose("readList: status = " + res.getStatus());
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230 // Optionally output additional data about list members for debugging.
231 boolean iterateThroughList = false;
232 if (iterateThroughList && logger.isDebugEnabled()) {
233 List<RelationList.RelationListItem> items =
234 list.getRelationListItem();
236 for(RelationList.RelationListItem item : items){
237 verbose("readList: list-item[" + i + "] csid=" +
239 verbose("readList: list-item[" + i + "] URI=" +
252 // ---------------------------------------------------------------
253 // CRUD tests : UPDATE tests
254 // ---------------------------------------------------------------
259 @Test(dependsOnMethods = {"create"})
260 public void update() {
265 // Retrieve an existing resource that we can update.
266 ClientResponse<Relation> res = client.read(knownResourceId);
267 verbose("read: status = " + res.getStatus());
268 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
269 Relation relation = res.getEntity();
270 verbose("Got object to update with ID: " + knownResourceId,
271 relation, Relation.class);
273 // Update the content of this resource.
274 relation.setDocumentId1("updated-" + relation.getDocumentId1());
275 relation.setDocumentType1("updated-" + relation.getDocumentType1());
276 relation.setDocumentId2("updated-" + relation.getDocumentId2());
277 relation.setDocumentType2("updated-" + relation.getDocumentType2());
279 // Submit the request to the service and store the response.
280 res = client.update(knownResourceId, relation);
281 int statusCode = res.getStatus();
282 Relation updatedObject = res.getEntity();
284 // Check the status code of the response: does it match
285 // the expected response(s)?
286 verbose("update: status = " + res.getStatus());
287 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 // Check the contents of the response: does it match
292 // what was submitted?
293 verbose("update: ", updatedObject, Relation.class);
295 "Data in updated object did not match submitted data.";
297 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
299 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
301 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
303 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
309 // Placeholders until the two tests below can be uncommented.
310 // See Issue CSPACE-401.
311 public void updateWithMalformedXml() {}
312 public void updateWithWrongXmlSchema() {}
316 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
317 public void updateWithMalformedXml() {
320 setupUpdateWithMalformedXml();
322 // Submit the request to the service and store the response.
323 String method = REQUEST_TYPE.httpMethodName();
324 String url = getResourceURL(knownResourceId);
325 String mediaType = MediaType.APPLICATION_XML;
326 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
327 int statusCode = submitRequest(method, url, mediaType, entity);
329 // Check the status code of the response: does it match
330 // the expected response(s)?
331 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
332 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
333 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
334 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
338 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
339 public void updateWithWrongXmlSchema() {
342 setupUpdateWithWrongXmlSchema();
344 // Submit the request to the service and store the response.
345 String method = REQUEST_TYPE.httpMethodName();
346 String url = getResourceURL(knownResourceId);
347 String mediaType = MediaType.APPLICATION_XML;
348 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
349 int statusCode = submitRequest(method, url, mediaType, entity);
351 // Check the status code of the response: does it match
352 // the expected response(s)?
353 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
354 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
355 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
356 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
363 public void updateNonExistent() {
366 setupUpdateNonExistent();
368 // Submit the request to the service and store the response.
369 // Note: The ID used in this 'create' call may be arbitrary.
370 // The only relevant ID may be the one used in update(), below.
371 Relation relation = createRelationInstance(NON_EXISTENT_ID);
372 ClientResponse<Relation> res =
373 client.update(NON_EXISTENT_ID, relation);
374 int statusCode = res.getStatus();
376 // Check the status code of the response: does it match
377 // the expected response(s)?
378 verbose("updateNonExistent: status = " + res.getStatus());
379 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
380 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
381 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
385 // ---------------------------------------------------------------
386 // CRUD tests : DELETE tests
387 // ---------------------------------------------------------------
392 @Test(dependsOnMethods = {"create", "read", "update"})
393 public void delete() {
398 // Submit the request to the service and store the response.
399 ClientResponse<Response> res = client.delete(knownResourceId);
400 int statusCode = res.getStatus();
402 // Check the status code of the response: does it match
403 // the expected response(s)?
404 verbose("delete: status = " + res.getStatus());
405 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
413 @Test(dependsOnMethods = {"delete"})
414 public void deleteNonExistent() {
417 setupDeleteNonExistent();
419 // Submit the request to the service and store the response.
420 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
421 int statusCode = res.getStatus();
423 // Check the status code of the response: does it match
424 // the expected response(s)?
425 verbose("deleteNonExistent: status = " + res.getStatus());
426 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
427 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
428 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
432 // ---------------------------------------------------------------
433 // RELATE_OBJECT tests
434 // ---------------------------------------------------------------
436 @Test(dependsOnMethods = {"create"})
437 public void relateObjects() {
441 // ---------------------------------------------------------------
442 // Utility tests : tests of code used in tests above
443 // ---------------------------------------------------------------
446 * Tests the code for manually submitting data that is used by several
447 * of the methods above.
449 @Test(dependsOnMethods = {"create", "read"})
450 public void testSubmitRequest() {
452 // Expected status code: 200 OK
453 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
455 // Submit the request to the service and store the response.
456 String method = ServiceRequestType.READ.httpMethodName();
457 String url = getResourceURL(knownResourceId);
458 int statusCode = submitRequest(method, url);
460 // Check the status code of the response: does it match
461 // the expected response(s)?
462 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
468 // ---------------------------------------------------------------
469 // Utility methods used by tests above
470 // ---------------------------------------------------------------
473 public String getServicePathComponent() {
474 return SERVICE_PATH_COMPONENT;
477 private Relation createRelationInstance(String identifier) {
478 String result = null;
479 Relation relation = new Relation();
480 fillRelation(relation, identifier);
485 * Fills the relation.
487 * @param identifier the identifier
489 * @return the relation
491 private void fillRelation(Relation relation, String identifier) {
492 fillRelation(relation, "Subject-" + identifier,
493 "SubjectType-" + identifier + "-type",
494 "Object-" + identifier,
495 "ObjectType-" + identifier + "-type",
496 RelationshipType.COLLECTIONOBJECT_INTAKE);
500 * Fills the relation.
502 * @param documentId1 the document id1
503 * @param documentType1 the document type1
504 * @param documentId2 the document id2
505 * @param documentType2 the document type2
508 * @return the relation
510 private void fillRelation(Relation relation,
511 String documentId1, String documentType1,
512 String documentId2, String documentType2,
515 relation.setDocumentId1(documentId1);
516 relation.setDocumentType1(documentType1);
517 relation.setDocumentId2(documentId2);
518 relation.setDocumentType2(documentType2);
520 relation.setRelationshipType(rt);