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.Response;
28 import javax.ws.rs.core.Response.Status;
30 import org.collectionspace.services.client.RelationClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.relation.Relation;
33 import org.collectionspace.services.relation.RelationList;
34 import org.collectionspace.services.relation.RelationshipType;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * RelationServiceTest, carries out tests against a
43 * deployed and running Relation Service.
45 * $LastChangedRevision$
48 public class RelationServiceTest extends AbstractServiceTest {
50 private RelationClient client = new RelationClient();
51 final String SERVICE_PATH_COMPONENT = "relations";
52 private String knownResourceId = null;
54 // ---------------------------------------------------------------
55 // CRUD tests : CREATE tests
56 // ---------------------------------------------------------------
62 public void create() {
64 // Perform setup, such as initializing the type of service request
65 // (e.g. CREATE, DELETE), its valid and expected status codes, and
66 // its associated HTTP method name (e.g. POST, DELETE).
69 // Submit the request to the service and store the response.
70 String identifier = createIdentifier();
71 Relation relation = createRelationInstance(identifier);
72 ClientResponse<Response> res = client.create(relation);
73 int statusCode = res.getStatus();
75 // Check the status code of the response: does it match
76 // the expected response(s)?
78 // Does it fall within the set of valid status codes?
79 // Does it exactly match the expected status code?
80 verbose("create: status = " + statusCode);
81 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
82 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
83 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
85 // Store the ID returned from this create operation for
86 // additional tests below.
87 knownResourceId = extractId(res);
91 @Test(dependsOnMethods = {"create"})
92 public void createList() {
93 for(int i = 0; i < 3; i++){
101 @Test(dependsOnMethods = {"create"},
102 expectedExceptions = IllegalArgumentException.class)
103 public void createNull() {
104 ClientResponse<Response> res = client.create(null);
107 // Placeholders until the two tests below can be uncommented.
108 // See Issue CSPACE-401.
109 public void createWithMalformedXml() {}
110 public void createWithWrongXmlSchema() {}
114 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
115 public void createWithMalformedXml() {
118 setupCreateWithMalformedXml();
120 // Submit the request to the service and store the response.
121 String method = REQUEST_TYPE.httpMethodName();
122 String url = getServiceRootURL();
123 final String entity = MALFORMED_XML_DATA; // Constant from base class.
124 int statusCode = submitRequest(method, url, entity);
126 // Check the status code of the response: does it match
127 // the expected response(s)?
128 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
129 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
130 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
131 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
135 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
136 public void createWithWrongXmlSchema() {
139 setupCreateWithWrongXmlSchema();
141 // Submit the request to the service and store the response.
142 String method = REQUEST_TYPE.httpMethodName();
143 String url = getServiceRootURL();
144 final String entity = WRONG_XML_SCHEMA_DATA;
145 int statusCode = submitRequest(method, url, entity);
147 // Check the status code of the response: does it match
148 // the expected response(s)?
149 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
157 // ---------------------------------------------------------------
158 // CRUD tests : READ tests
159 // ---------------------------------------------------------------
164 @Test(dependsOnMethods = {"create"})
170 // Submit the request to the service and store the response.
171 ClientResponse<Relation> res = client.read(knownResourceId);
172 int statusCode = res.getStatus();
174 // Check the status code of the response: does it match
175 // the expected response(s)?
176 verbose("read: status = " + statusCode);
177 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 // Verify that the resource identifier
182 Relation returnedRelation = res.getEntity();
183 Assert.assertEquals(returnedRelation.getCsid(), knownResourceId);
190 @Test(dependsOnMethods = {"read"})
191 public void readNonExistent() {
194 setupReadNonExistent();
196 // Submit the request to the service and store the response.
197 ClientResponse<Relation> res = client.read(NON_EXISTENT_ID);
198 int statusCode = res.getStatus();
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 verbose("readNonExistent: status = " + res.getStatus());
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 // ---------------------------------------------------------------
210 // CRUD tests : READ_LIST tests
211 // ---------------------------------------------------------------
216 @Test(dependsOnMethods = {"createList"})
217 public void readList() {
222 // Submit the request to the service and store the response.
223 ClientResponse<RelationList> res = client.readList();
224 RelationList list = res.getEntity();
225 int statusCode = res.getStatus();
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 verbose("readList: status = " + res.getStatus());
230 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 // Optionally output additional data about list members for debugging.
235 boolean iterateThroughList = false;
236 if (iterateThroughList && logger.isDebugEnabled()) {
237 List<RelationList.RelationListItem> items =
238 list.getRelationListItem();
240 for(RelationList.RelationListItem item : items){
241 verbose("readList: list-item[" + i + "] csid=" +
243 verbose("readList: list-item[" + i + "] URI=" +
256 // ---------------------------------------------------------------
257 // CRUD tests : UPDATE tests
258 // ---------------------------------------------------------------
263 @Test(dependsOnMethods = {"create"})
264 public void update() {
269 // Retrieve an existing resource that we can update.
270 ClientResponse<Relation> res = client.read(knownResourceId);
271 verbose("read: status = " + res.getStatus());
272 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
273 Relation relation = res.getEntity();
274 verbose("Got object to update with ID: " + knownResourceId,
275 relation, Relation.class);
277 // Update the content of this resource.
278 relation.setDocumentId1("updated-" + relation.getDocumentId1());
279 relation.setDocumentType1("updated-" + relation.getDocumentType1());
280 relation.setDocumentId2("updated-" + relation.getDocumentId2());
281 relation.setDocumentType2("updated-" + relation.getDocumentType2());
283 // Submit the request to the service and store the response.
284 res = client.update(knownResourceId, relation);
285 int statusCode = res.getStatus();
286 Relation updatedObject = res.getEntity();
288 // Check the status code of the response: does it match
289 // the expected response(s)?
290 verbose("update: status = " + res.getStatus());
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 // Check the contents of the response: does it match
296 // what was submitted?
297 verbose("update: ", updatedObject, Relation.class);
299 "Data in updated object did not match submitted data.";
301 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
303 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
305 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
307 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
313 // Placeholders until the two tests below can be uncommented.
314 // See Issue CSPACE-401.
315 public void updateWithMalformedXml() {}
316 public void updateWithWrongXmlSchema() {}
320 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
321 public void updateWithMalformedXml() {
324 setupUpdateWithMalformedXml();
326 // Submit the request to the service and store the response.
327 String method = REQUEST_TYPE.httpMethodName();
328 String url = getResourceURL(knownResourceId);
329 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
330 int statusCode = submitRequest(method, url, entity);
332 // Check the status code of the response: does it match
333 // the expected response(s)?
334 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
341 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
342 public void updateWithWrongXmlSchema() {
345 setupUpdateWithWrongXmlSchema();
347 // Submit the request to the service and store the response.
348 String method = REQUEST_TYPE.httpMethodName();
349 String url = getResourceURL(knownResourceId);
350 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
351 int statusCode = submitRequest(method, url, entity);
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
356 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
357 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
358 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
364 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
365 public void updateNonExistent() {
368 setupUpdateNonExistent();
370 // Submit the request to the service and store the response.
371 // Note: The ID used in this 'create' call may be arbitrary.
372 // The only relevant ID may be the one used in update(), below.
373 Relation relation = createRelationInstance(NON_EXISTENT_ID);
374 ClientResponse<Relation> res =
375 client.update(NON_EXISTENT_ID, relation);
376 int statusCode = res.getStatus();
378 // Check the status code of the response: does it match
379 // the expected response(s)?
380 verbose("updateNonExistent: status = " + res.getStatus());
381 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
382 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
383 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
387 // ---------------------------------------------------------------
388 // CRUD tests : DELETE tests
389 // ---------------------------------------------------------------
394 @Test(dependsOnMethods = {"create", "read", "update"})
395 public void delete() {
400 // Submit the request to the service and store the response.
401 ClientResponse<Response> res = client.delete(knownResourceId);
402 int statusCode = res.getStatus();
404 // Check the status code of the response: does it match
405 // the expected response(s)?
406 verbose("delete: status = " + res.getStatus());
407 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
415 @Test(dependsOnMethods = {"delete"})
416 public void deleteNonExistent() {
419 setupDeleteNonExistent();
421 // Submit the request to the service and store the response.
422 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
423 int statusCode = res.getStatus();
425 // Check the status code of the response: does it match
426 // the expected response(s)?
427 verbose("deleteNonExistent: status = " + res.getStatus());
428 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 // ---------------------------------------------------------------
435 // RELATE_OBJECT tests
436 // ---------------------------------------------------------------
438 @Test(dependsOnMethods = {"create"})
439 public void relateObjects() {
443 // ---------------------------------------------------------------
444 // Utility tests : tests of code used in tests above
445 // ---------------------------------------------------------------
448 * Tests the code for manually submitting data that is used by several
449 * of the methods above.
451 @Test(dependsOnMethods = {"create", "read"})
452 public void testSubmitRequest() {
454 // Expected status code: 200 OK
455 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
457 // Submit the request to the service and store the response.
458 String method = ServiceRequestType.READ.httpMethodName();
459 String url = getResourceURL(knownResourceId);
460 int statusCode = submitRequest(method, url);
462 // Check the status code of the response: does it match
463 // the expected response(s)?
464 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
465 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470 // ---------------------------------------------------------------
471 // Utility methods used by tests above
472 // ---------------------------------------------------------------
475 public String getServicePathComponent() {
476 return SERVICE_PATH_COMPONENT;
479 private Relation createRelationInstance(String identifier) {
480 String result = null;
481 Relation relation = new Relation();
482 fillRelation(relation, identifier);
487 * Fills the relation.
489 * @param identifier the identifier
491 * @return the relation
493 private void fillRelation(Relation relation, String identifier) {
494 fillRelation(relation, "Subject-" + identifier,
495 "SubjectType-" + identifier + "-type",
496 "Object-" + identifier,
497 "ObjectType-" + identifier + "-type",
498 RelationshipType.COLLECTIONOBJECT_INTAKE);
502 * Fills the relation.
504 * @param documentId1 the document id1
505 * @param documentType1 the document type1
506 * @param documentId2 the document id2
507 * @param documentType2 the document type2
510 * @return the relation
512 private void fillRelation(Relation relation,
513 String documentId1, String documentType1,
514 String documentId2, String documentType2,
517 relation.setDocumentId1(documentId1);
518 relation.setDocumentType1(documentType1);
519 relation.setDocumentId2(documentId2);
520 relation.setDocumentType2(documentType2);
522 relation.setRelationshipType(rt);