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;
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 // ---------------------------------------------------------------
61 public void create() {
63 // Perform setup, such as initializing the type of service request
64 // (e.g. CREATE, DELETE), its valid and expected status codes, and
65 // its associated HTTP method name (e.g. POST, DELETE).
68 // Submit the request to the service and store the response.
69 String identifier = createIdentifier();
70 MultipartOutput multipart = createRelationInstance(identifier);
71 ClientResponse<Response> res = client.create(multipart);
72 int statusCode = res.getStatus();
74 // Check the status code of the response: does it match
75 // the expected response(s)?
77 // Does it fall within the set of valid status codes?
78 // Does it exactly match the expected status code?
79 verbose("create: status = " + statusCode);
80 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
81 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
82 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
84 // Store the ID returned from this create operation for
85 // additional tests below.
86 knownResourceId = extractId(res);
87 verbose("create: knownResourceId=" + knownResourceId);
91 @Test(dependsOnMethods = {"create"})
92 public void createList() {
93 for(int i = 0; i < 3; i++){
99 // Placeholders until the three tests below can be uncommented.
100 // See Issue CSPACE-401.
101 public void createWithEmptyEntityBody() {
104 public void createWithMalformedXml() {
107 public void createWithWrongXmlSchema() {
112 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
113 public void createWithEmptyEntityBody() {
116 setupCreateWithEmptyEntityBody();
118 // Submit the request to the service and store the response.
119 String method = REQUEST_TYPE.httpMethodName();
120 String url = getServiceRootURL();
121 String mediaType = MediaType.APPLICATION_XML;
122 final String entity = "";
123 int statusCode = submitRequest(method, url, mediaType, entity);
125 // Check the status code of the response: does it match
126 // the expected response(s)?
127 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
128 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
129 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
134 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
135 public void createWithMalformedXml() {
138 setupCreateWithMalformedXml();
140 // Submit the request to the service and store the response.
141 String method = REQUEST_TYPE.httpMethodName();
142 String url = getServiceRootURL();
143 String mediaType = MediaType.APPLICATION_XML;
144 final String entity = MALFORMED_XML_DATA; // Constant from base class.
145 int statusCode = submitRequest(method, url, mediaType, entity);
147 // Check the status code of the response: does it match
148 // the expected response(s)?
149 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
156 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
157 public void createWithWrongXmlSchema() {
160 setupCreateWithWrongXmlSchema();
162 // Submit the request to the service and store the response.
163 String method = REQUEST_TYPE.httpMethodName();
164 String url = getServiceRootURL();
165 String mediaType = MediaType.APPLICATION_XML;
166 final String entity = WRONG_XML_SCHEMA_DATA;
167 int statusCode = submitRequest(method, url, mediaType, entity);
169 // Check the status code of the response: does it match
170 // the expected response(s)?
171 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 // ---------------------------------------------------------------
178 // CRUD tests : READ tests
179 // ---------------------------------------------------------------
182 @Test(dependsOnMethods = {"create"})
188 // Submit the request to the service and store the response.
189 ClientResponse<MultipartInput> res = client.read(knownResourceId);
190 int statusCode = res.getStatus();
192 // Check the status code of the response: does it match
193 // the expected response(s)?
194 verbose("read: status = " + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
199 // Verify that the resource identifier
200 //FIXME: remove the following try catch once Aron fixes signatures
202 MultipartInput input = (MultipartInput) res.getEntity();
203 RelationsCommon relation = (RelationsCommon) extractPart(input,
204 client.getCommonPartName(), RelationsCommon.class);
205 Assert.assertNotNull(relation);
206 } catch(Exception e){
207 throw new RuntimeException(e);
214 @Test(dependsOnMethods = {"read"})
215 public void readNonExistent() {
218 setupReadNonExistent();
220 // Submit the request to the service and store the response.
221 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
222 int statusCode = res.getStatus();
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 verbose("readNonExistent: status = " + res.getStatus());
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 // ---------------------------------------------------------------
233 // CRUD tests : READ_LIST tests
234 // ---------------------------------------------------------------
237 @Test(dependsOnMethods = {"createList", "read"})
238 public void readList() {
243 // Submit the request to the service and store the response.
244 ClientResponse<RelationsCommonList> res = client.readList();
245 RelationsCommonList list = res.getEntity();
246 int statusCode = res.getStatus();
248 // Check the status code of the response: does it match
249 // the expected response(s)?
250 verbose("readList: status = " + res.getStatus());
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 // Optionally output additional data about list members for debugging.
256 boolean iterateThroughList = false;
257 if(iterateThroughList && logger.isDebugEnabled()){
258 List<RelationsCommonList.RelationListItem> items =
259 list.getRelationListItem();
261 for(RelationsCommonList.RelationListItem item : items){
262 verbose("readList: list-item[" + i + "] csid=" +
264 verbose("readList: list-item[" + i + "] URI=" +
274 // ---------------------------------------------------------------
275 // CRUD tests : UPDATE tests
276 // ---------------------------------------------------------------
279 @Test(dependsOnMethods = {"read"})
280 public void update() {
285 // Retrieve an existing resource that we can update.
286 ClientResponse<MultipartInput> res =
287 client.read(knownResourceId);
288 verbose("update: read status = " + res.getStatus());
289 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290 verbose("Got object to update with ID: " + knownResourceId);
291 MultipartInput input = (MultipartInput) res.getEntity();
292 RelationsCommon relation = (RelationsCommon) extractPart(input,
293 client.getCommonPartName(), RelationsCommon.class);
294 Assert.assertNotNull(relation);
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());
301 verbose("updated object", relation, RelationsCommon.class);
303 // Submit the request to the service and store the response.
304 MultipartOutput output = new MultipartOutput();
305 OutputPart commonPart = output.addPart(relation, MediaType.APPLICATION_XML_TYPE);
306 commonPart.getHeaders().add("label", client.getCommonPartName());
307 res = client.update(knownResourceId, output);
308 int statusCode = res.getStatus();
309 // Check the status code of the response: does it match the expected response(s)?
310 verbose("update: status = " + res.getStatus());
311 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
312 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
313 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
315 input = (MultipartInput) res.getEntity();
316 RelationsCommon updatedObject = (RelationsCommon) extractPart(
317 input, client.getCommonPartName(),
318 RelationsCommon.class);
319 Assert.assertNotNull(updatedObject);
322 "Data in updated object did not match submitted data.";
324 updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
326 updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
328 updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
330 updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
338 // Placeholders until the three tests below can be uncommented.
339 // See Issue CSPACE-401.
340 public void updateWithEmptyEntityBody() {
343 public void updateWithMalformedXml() {
346 public void updateWithWrongXmlSchema() {
351 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
352 public void updateWithEmptyEntityBody() {
355 setupUpdateWithEmptyEntityBody();
357 // Submit the request to the service and store the response.
358 String method = REQUEST_TYPE.httpMethodName();
359 String url = getResourceURL(knownResourceId);
360 String mediaType = MediaType.APPLICATION_XML;
361 final String entity = "";
362 int statusCode = submitRequest(method, url, mediaType, entity);
364 // Check the status code of the response: does it match
365 // the expected response(s)?
366 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
367 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
374 public void updateWithMalformedXml() {
377 setupUpdateWithMalformedXml();
379 // Submit the request to the service and store the response.
380 String method = REQUEST_TYPE.httpMethodName();
381 String url = getResourceURL(knownResourceId);
382 String mediaType = MediaType.APPLICATION_XML;
383 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
384 int statusCode = submitRequest(method, url, mediaType, entity);
386 // Check the status code of the response: does it match
387 // the expected response(s)?
388 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
389 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
396 public void updateWithWrongXmlSchema() {
399 setupUpdateWithWrongXmlSchema();
401 // Submit the request to the service and store the response.
402 String method = REQUEST_TYPE.httpMethodName();
403 String url = getResourceURL(knownResourceId);
404 String mediaType = MediaType.APPLICATION_XML;
405 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
406 int statusCode = submitRequest(method, url, mediaType, entity);
408 // Check the status code of the response: does it match
409 // the expected response(s)?
410 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
411 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
412 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
413 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
418 public void updateNonExistent() {
421 setupUpdateNonExistent();
423 // Submit the request to the service and store the response.
424 // Note: The ID used in this 'create' call may be arbitrary.
425 // The only relevant ID may be the one used in update(), below.
426 MultipartOutput multipart = createRelationInstance(NON_EXISTENT_ID);
427 ClientResponse<MultipartInput> res =
428 client.update(NON_EXISTENT_ID, multipart);
429 int statusCode = res.getStatus();
431 // Check the status code of the response: does it match
432 // the expected response(s)?
433 verbose("updateNonExistent: status = " + res.getStatus());
434 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
439 // ---------------------------------------------------------------
440 // CRUD tests : DELETE tests
441 // ---------------------------------------------------------------
444 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
445 public void delete() {
450 // Submit the request to the service and store the response.
451 ClientResponse<Response> res = client.delete(knownResourceId);
452 int statusCode = res.getStatus();
454 // Check the status code of the response: does it match
455 // the expected response(s)?
456 verbose("delete: status = " + res.getStatus());
457 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
458 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
459 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 @Test(dependsOnMethods = {"delete"})
465 public void deleteNonExistent() {
468 setupDeleteNonExistent();
470 // Submit the request to the service and store the response.
471 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
472 int statusCode = res.getStatus();
474 // Check the status code of the response: does it match
475 // the expected response(s)?
476 verbose("deleteNonExistent: status = " + res.getStatus());
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
482 // ---------------------------------------------------------------
483 // RELATE_OBJECT tests
484 // ---------------------------------------------------------------
485 @Test(dependsOnMethods = {"create"})
486 public void relateObjects() {
489 // ---------------------------------------------------------------
490 // Utility tests : tests of code used in tests above
491 // ---------------------------------------------------------------
493 * Tests the code for manually submitting data that is used by several
494 * of the methods above.
496 @Test(dependsOnMethods = {"create", "read"})
497 public void testSubmitRequest() {
499 // Expected status code: 200 OK
500 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
502 // Submit the request to the service and store the response.
503 String method = ServiceRequestType.READ.httpMethodName();
504 String url = getResourceURL(knownResourceId);
505 int statusCode = submitRequest(method, url);
507 // Check the status code of the response: does it match
508 // the expected response(s)?
509 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
514 // ---------------------------------------------------------------
515 // Utility methods used by tests above
516 // ---------------------------------------------------------------
518 public String getServicePathComponent() {
519 return SERVICE_PATH_COMPONENT;
522 private MultipartOutput createRelationInstance(String identifier) {
523 RelationsCommon relation = new RelationsCommon();
524 fillRelation(relation, identifier);
526 MultipartOutput multipart = new MultipartOutput();
527 OutputPart commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
528 commonPart.getHeaders().add("label", client.getCommonPartName());
529 verbose("to be created, relation common ", relation, RelationsCommon.class);
535 * Fills the relation.
537 * @param identifier the identifier
539 * @return the relation
541 private void fillRelation(RelationsCommon relation, String identifier) {
542 fillRelation(relation, "Subject-" + identifier,
543 "SubjectType-" + identifier + "-type",
544 "Object-" + identifier,
545 "ObjectType-" + identifier + "-type",
546 RelationshipType.COLLECTIONOBJECT_INTAKE);
550 * Fills the relation.
552 * @param documentId1 the document id1
553 * @param documentType1 the document type1
554 * @param documentId2 the document id2
555 * @param documentType2 the document type2
558 * @return the relation
560 private void fillRelation(RelationsCommon relation,
561 String documentId1, String documentType1,
562 String documentId2, String documentType2,
563 RelationshipType rt) {
564 relation.setDocumentId1(documentId1);
565 relation.setDocumentType1(documentType1);
566 relation.setDocumentId2(documentId2);
567 relation.setDocumentType2(documentType2);
569 relation.setRelationshipType(rt);