1 package org.collectionspace.services.client.test;
4 import javax.ws.rs.core.Response;
5 import javax.ws.rs.core.Response.Status;
7 import org.collectionspace.services.client.RelationClient;
8 import org.collectionspace.services.client.test.ServiceRequestType;
9 import org.collectionspace.services.relation.Relation;
10 import org.collectionspace.services.relation.RelationList;
11 import org.collectionspace.services.relation.RelationshipType;
13 import org.jboss.resteasy.client.ClientResponse;
15 import org.testng.Assert;
16 import org.testng.annotations.Test;
18 import org.collectionspace.services.common.relation.RelationJAXBSchema;
21 * A RelationNuxeoServiceTest.
23 * @version $Revision:$
25 public class RelationServiceTest extends AbstractServiceTest {
27 private RelationClient client = new RelationClient();
28 final String SERVICE_PATH_COMPONENT = "relations";
29 private String knownObjectId = null;
31 // ---------------------------------------------------------------
32 // CRUD tests : CREATE tests
33 // ---------------------------------------------------------------
39 public void create() {
41 // Perform setup, such as initializing the type of service request
42 // (e.g. CREATE, DELETE), its valid and expected status codes, and
43 // its associated HTTP method name (e.g. POST, DELETE).
46 // Submit the request to the service and store the response.
47 Relation relation = new Relation();
48 String identifier = createIdentifier();
49 fillRelation(relation, identifier);
50 ClientResponse<Response> res = client.create(relation);
51 int statusCode = res.getStatus();
53 // Check the status code of the response: does it match the expected response(s)?
55 // Does it fall within the set of valid status codes?
56 // Does it exactly match the expected status code?
57 verbose("create: status = " + statusCode);
58 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
59 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
60 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
62 // Store the ID returned from this create operation for additional tests below.
63 knownObjectId = extractId(res);
67 @Test(dependsOnMethods = {"create"})
68 public void createList() {
69 for(int i = 0; i < 3; i++){
77 @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
78 public void createNull() {
79 ClientResponse<Response> res = client.create(null);
82 // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401.
83 public void createWithMalformedXml() {}
84 public void createWithWrongXmlSchema() {}
88 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
89 public void createWithMalformedXml() {
92 setupCreateWithMalformedXml();
94 // Submit the request to the service and store the response.
95 String method = REQUEST_TYPE.httpMethodName();
96 String url = getServiceRootURL();
97 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
98 int statusCode = submitRequest(method, url, entity);
100 // Check the status code of the response: does it match the expected response(s)?
101 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
102 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
103 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
104 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
108 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
109 public void createWithWrongXmlSchema() {
112 setupCreateWithWrongXmlSchema();
114 // Submit the request to the service and store the response.
115 String method = REQUEST_TYPE.httpMethodName();
116 String url = getServiceRootURL();
117 final String entity = WRONG_XML_SCHEMA_DATA;
118 int statusCode = submitRequest(method, url, entity);
120 // Check the status code of the response: does it match the expected response(s)?
121 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
129 // ---------------------------------------------------------------
130 // CRUD tests : READ tests
131 // ---------------------------------------------------------------
136 @Test(dependsOnMethods = {"create"})
142 // Submit the request to the service and store the response.
143 ClientResponse<Relation> res = client.read(knownObjectId);
144 int statusCode = res.getStatus();
146 // Check the status code of the response: does it match the expected response(s)?
147 verbose("read: status = " + statusCode);
148 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
152 Relation returnedRelation = res.getEntity();
153 Assert.assertEquals(returnedRelation.getCsid(), knownObjectId);
160 @Test(dependsOnMethods = {"read"})
161 public void readNonExistent() {
164 setupReadNonExistent();
166 // Submit the request to the service and store the response.
167 ClientResponse<Relation> res = client.read(NON_EXISTENT_ID);
168 int statusCode = res.getStatus();
170 // Check the status code of the response: does it match the expected response(s)?
171 verbose("readNonExistent: status = " + res.getStatus());
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 // ---------------------------------------------------------------
179 // CRUD tests : READ_LIST tests
180 // ---------------------------------------------------------------
185 @Test(dependsOnMethods = {"createList"})
186 public void readList() {
191 // Submit the request to the service and store the response.
192 ClientResponse<RelationList> res = client.readList();
193 RelationList list = res.getEntity();
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match the expected response(s)?
197 verbose("readList: status = " + res.getStatus());
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
202 // Optionally output additional data about list members for debugging.
203 boolean iterateThroughList = false;
204 if (iterateThroughList && logger.isDebugEnabled()) {
205 List<RelationList.RelationListItem> items =
206 list.getRelationListItem();
208 for(RelationList.RelationListItem item : items){
209 verbose("readList: list-item[" + i + "] csid=" + item.getCsid());
210 verbose("readList: list-item[" + i + "] URI=" + item.getUri());
222 // ---------------------------------------------------------------
223 // CRUD tests : UPDATE tests
224 // ---------------------------------------------------------------
229 @Test(dependsOnMethods = {"create"})
230 public void update() {
235 // Retrieve an existing resource that we can update.
236 ClientResponse<Relation> res = client.read(knownObjectId);
237 verbose("read: status = " + res.getStatus());
238 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
239 Relation relation = res.getEntity();
240 verbose("Got object to update with ID: " + knownObjectId,
241 relation, Relation.class);
243 // Update the content of this resource.
244 relation.setDocumentId1("updated-" + relation.getDocumentId1());
245 relation.setDocumentType1("updated-" + relation.getDocumentType1());
246 relation.setDocumentId2("updated-" + relation.getDocumentId2());
247 relation.setDocumentType2("updated-" + relation.getDocumentType2());
249 // Submit the request to the service and store the response.
250 res = client.update(knownObjectId, relation);
251 int statusCode = res.getStatus();
252 Relation updatedObject = res.getEntity();
254 // Check the status code of the response: does it match the expected response(s)?
255 verbose("update: status = " + res.getStatus());
256 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
260 // Check the contents of the response: does it match what was submitted?
261 verbose("update: ", updatedObject, Relation.class);
262 final String msg = "Data in updated object did not match submitted data.";
264 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
266 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
268 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
270 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
276 // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401.
277 public void updateWithMalformedXml() {}
278 public void updateWithWrongXmlSchema() {}
282 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
283 public void updateWithMalformedXml() {
286 setupUpdateWithMalformedXml();
288 // Submit the request to the service and store the response.
289 String method = REQUEST_TYPE.httpMethodName();
290 String url = getResourceURL(knownObjectId);
291 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
292 int statusCode = submitRequest(method, url, entity);
294 // Check the status code of the response: does it match the expected response(s)?
295 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
296 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
297 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
298 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
302 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
303 public void updateWithWrongXmlSchema() {
306 setupUpdateWithWrongXmlSchema();
308 // Submit the request to the service and store the response.
309 String method = REQUEST_TYPE.httpMethodName();
310 String url = getResourceURL(knownObjectId);
311 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
312 int statusCode = submitRequest(method, url, entity);
314 // Check the status code of the response: does it match the expected response(s)?
315 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
316 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
317 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
318 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
324 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
325 public void updateNonExistent() {
328 setupUpdateNonExistent();
330 // Submit the request to the service and store the response.
331 // Note: The ID used in this 'fill' call may be arbitrary.
332 // The only relevant ID may be the one used in update(), below.
333 Relation relation = new Relation();
334 fillRelation(relation, NON_EXISTENT_ID);
335 ClientResponse<Relation> res =
336 client.update(NON_EXISTENT_ID, relation);
337 int statusCode = res.getStatus();
339 // Check the status code of the response: does it match the expected response(s)?
340 verbose("updateNonExistent: status = " + res.getStatus());
341 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
342 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
343 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
347 // ---------------------------------------------------------------
348 // CRUD tests : DELETE tests
349 // ---------------------------------------------------------------
354 @Test(dependsOnMethods = {"create", "read", "update"})
355 public void delete() {
360 // Submit the request to the service and store the response.
361 String relationID = this.createEntity("0");
362 Assert.assertNotNull(relationID, "Could not create a new object to delete.");
363 ClientResponse<Response> res = client.delete(relationID);
364 int statusCode = res.getStatus();
366 // Check the status code of the response: does it match the expected response(s)?
367 verbose("delete: status = " + res.getStatus() + " csid = " + relationID);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
376 @Test(dependsOnMethods = {"delete"})
377 public void deleteNonExistent() {
380 setupDeleteNonExistent();
382 // Submit the request to the service and store the response.
383 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
384 int statusCode = res.getStatus();
386 // Check the status code of the response: does it match the expected response(s)?
387 verbose("deleteNonExistent: status = " + res.getStatus());
388 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
394 // ---------------------------------------------------------------
395 // RELATE_OBJECT tests
396 // ---------------------------------------------------------------
398 @Test(dependsOnMethods = {"create"})
399 public void relateObjects() {
403 // ---------------------------------------------------------------
404 // Utility tests : tests of code used in tests above
405 // ---------------------------------------------------------------
408 * Tests the code for manually submitting data that is used by several
409 * of the methods above.
411 @Test(dependsOnMethods = {"create", "read"})
412 public void testSubmitRequest() {
414 // Expected status code: 200 OK
415 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
417 // Submit the request to the service and store the response.
418 String method = ServiceRequestType.READ.httpMethodName();
419 String url = getResourceURL(knownObjectId);
420 int statusCode = submitRequest(method, url);
422 // Check the status code of the response: does it match the expected response(s)?
423 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
424 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
429 // ---------------------------------------------------------------
430 // Utility methods used by tests above
431 // ---------------------------------------------------------------
434 public String getServicePathComponent() {
435 return SERVICE_PATH_COMPONENT;
438 private String createEntity(String identifier) {
440 String result = null;
442 Relation relation = new Relation();
443 fillRelation(relation, identifier);
444 ClientResponse<Response> res = client.create(relation);
445 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
447 result = extractId(res);
448 String responseString = res.toString();
449 System.out.println(responseString);
455 * Fills the relation.
457 * @param identifier the identifier
459 * @return the relation
461 private void fillRelation(Relation relation, String identifier) {
462 fillRelation(relation, "Subject-" + identifier,
463 "SubjectType-" + identifier + "-type",
464 "Object-" + identifier,
465 "ObjectType-" + identifier + "-type",
466 RelationshipType.COLLECTIONOBJECT_INTAKE);
470 * Creates the relation.
472 * @param documentId1 the document id1
473 * @param documentType1 the document type1
474 * @param documentId2 the document id2
475 * @param documentType2 the document type2
478 * @return the relation
480 private void fillRelation(Relation relation,
481 String documentId1, String documentType1,
482 String documentId2, String documentType2,
485 relation.setDocumentId1(documentId1);
486 relation.setDocumentType1(documentType1);
487 relation.setDocumentId2(documentId2);
488 relation.setDocumentType2(documentType2);
490 relation.setRelationshipType(rt);