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 (c) 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.IntegrationTests.test;
25 import java.io.StringWriter;
26 import java.math.BigDecimal;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Marshaller;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import org.jboss.resteasy.client.ClientResponse;
43 import org.collectionspace.services.client.CollectionObjectClient;
44 import org.collectionspace.services.client.DimensionClient;
45 import org.collectionspace.services.client.DimensionFactory;
46 import org.collectionspace.services.client.PayloadOutputPart;
47 import org.collectionspace.services.client.PoxPayloadIn;
48 import org.collectionspace.services.client.PoxPayloadOut;
49 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
51 import org.collectionspace.services.client.IntakeClient;
52 import org.collectionspace.services.intake.IntakesCommon;
54 import org.collectionspace.services.client.RelationClient;
55 import org.collectionspace.services.client.workflow.WorkflowClient;
56 import org.collectionspace.services.dimension.DimensionsCommon;
57 import org.collectionspace.services.relation.RelationsCommon;
58 import org.collectionspace.services.relation.RelationsCommonList;
59 import org.collectionspace.services.relation.RelationshipType;
64 * @version $Revision:$
66 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
68 final Logger logger = LoggerFactory
69 .getLogger(RelationIntegrationTest.class);
71 // Get clients for the CollectionSpace services
73 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
74 private RelationClient relationClient = new RelationClient();
75 private IntakeClient intakeClient = new IntakeClient();
76 private DimensionClient dimensionClient = new DimensionClient();
78 private static final int OBJECTS_TO_INTAKE = 1;
82 * Object as xml string.
85 * @param clazz the clazz
88 static protected String objectAsXmlString(Object o, Class<?> clazz) {
89 StringWriter sw = new StringWriter();
91 JAXBContext jc = JAXBContext.newInstance(clazz);
92 Marshaller m = jc.createMarshaller();
93 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
96 } catch (Exception e) {
102 private PoxPayloadOut createDimensionInstance(String commonPartName, String dimensionType, String dimensionValue, String entryDate) {
103 DimensionsCommon dimensionsCommon = new DimensionsCommon();
104 dimensionsCommon.setDimension(dimensionType);
105 dimensionsCommon.setValue(new BigDecimal(dimensionValue));
106 dimensionsCommon.setValueDate(entryDate);
107 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(
108 commonPartName, dimensionsCommon);
110 if (logger.isDebugEnabled()) {
111 logger.debug("to be created, dimension common");
112 logger.debug(objectAsXmlString(dimensionsCommon,
113 DimensionsCommon.class));
119 protected PoxPayloadOut createDimensionInstance(String identifier) {
120 DimensionClient client = new DimensionClient();
121 return createDimensionInstance(client.getCommonPartName(), identifier);
125 * Creates the dimension instance.
127 * @param identifier the identifier
128 * @return the multipart output
130 protected PoxPayloadOut createDimensionInstance(String commonPartName, String identifier) {
131 final String DIMENSION_VALUE = "78.306";
133 return createDimensionInstance(commonPartName,
134 "dimensionType-" + identifier,
136 "entryDate-" + identifier);
139 @Test void deleteCollectionObjectRelationshipToLockedDimension() {
141 // First create a CollectionObject
143 CollectionobjectsCommon co = new CollectionobjectsCommon();
144 fillCollectionObject(co, createIdentifier());
146 // Next, create a part object
147 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
148 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
150 // Make the create call and check the response
151 ClientResponse<Response> response = collectionObjectClient.create(multipart);
152 String collectionObjectCsid = null;
154 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
156 collectionObjectCsid = extractId(response);
158 response.releaseConnection();
161 //Next, create a two Dimension records to relate the collection object to
162 multipart = this.createDimensionInstance(createIdentifier());
163 // Make the call to create and check the response
164 response = dimensionClient.create(multipart);
165 String dimensionCsid1 = null;
167 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
168 dimensionCsid1 = extractId(response);
170 response.releaseConnection();
172 //Next, create a the second Dimension record
173 multipart = this.createDimensionInstance(createIdentifier());
174 // Make the call to create and check the response
175 response = dimensionClient.create(multipart);
176 String dimensionCsid2 = null;
178 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
179 dimensionCsid2 = extractId(response);
181 response.releaseConnection();
185 // Relate the entities, by creating a new relation object
186 RelationsCommon relation = new RelationsCommon();
187 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
188 dimensionCsid1, DimensionsCommon.class.getSimpleName(),
189 "collectionobject-dimension");
190 // Create the part and fill it with the relation object
191 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
192 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
193 // Create the relationship
194 response = relationClient.create(multipart);
195 String relationCsid1 = null;
197 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
198 relationCsid1 = extractId(response);
200 response.releaseConnection();
202 // Create the second relationship
203 relation = new RelationsCommon();
204 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
205 dimensionCsid2, DimensionsCommon.class.getSimpleName(),
206 "collectionobject-dimension");
207 // Create the part and fill it with the relation object
208 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
209 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
210 // Create the relationship
211 response = relationClient.create(multipart);
212 @SuppressWarnings("unused")
213 String relationCsid2 = null;
215 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
216 relationCsid2 = extractId(response);
218 response.releaseConnection();
222 // Now lock the dimension record.
224 @SuppressWarnings("unused")
225 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_LOCK);
226 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
228 // Finally, try to delete the relationship
230 // Try to delete the relationship -should fail because we don't allow delete if one of the sides is locked.
231 response = relationClient.delete(relationCsid1);
233 Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
235 response.releaseConnection();
238 // Also, try to soft-delete. This should also fail.
239 workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_DELETE);
240 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
244 @Test void releteCollectionObjectToLockedDimension() {
246 // First create a CollectionObject
248 CollectionobjectsCommon co = new CollectionobjectsCommon();
249 fillCollectionObject(co, createIdentifier());
251 // Next, create a part object
252 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
253 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
254 commonPart.setLabel(collectionObjectClient.getCommonPartName());
256 // Make the create call and check the response
257 ClientResponse<Response> response = collectionObjectClient.create(multipart);
258 String collectionObjectCsid = null;
260 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
262 collectionObjectCsid = extractId(response);
264 response.releaseConnection();
267 //Next, create a Dimension record to relate the collection object to
268 multipart = this.createDimensionInstance(createIdentifier());
269 // Make the call to create and check the response
270 response = dimensionClient.create(multipart);
271 String dimensionCsid = null;
273 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
274 dimensionCsid = extractId(response);
276 response.releaseConnection();
279 @SuppressWarnings("unused")
280 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
281 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
283 // Lastly, relate the two entities, by creating a new relation object
284 RelationsCommon relation = new RelationsCommon();
285 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
286 dimensionCsid, DimensionsCommon.class.getSimpleName(),
287 "collectionobject-dimension");
288 // Create the part and fill it with the relation object
289 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
290 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
291 commonPart.setLabel(relationClient.getCommonPartName());
293 // Make the call to crate
294 ClientResponse<Response> relationresponse = relationClient.create(multipart);
295 @SuppressWarnings("unused")
296 String relationCsid = null;
298 Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
299 relationCsid = extractId(response);
301 relationresponse.releaseConnection();
307 public void relateCollectionObjectToIntake() {
309 // First create a CollectionObject
311 CollectionobjectsCommon co = new CollectionobjectsCommon();
312 fillCollectionObject(co, createIdentifier());
314 // Next, create a part object
315 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
316 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
317 commonPart.setLabel(collectionObjectClient.getCommonPartName());
319 // Make the create call and check the response
320 ClientResponse<Response> response = collectionObjectClient.create(multipart);
321 String collectionObjectCsid = null;
323 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
325 collectionObjectCsid = extractId(response);
327 response.releaseConnection();
331 // Next, create an Intake object
332 IntakesCommon intake = new IntakesCommon();
333 fillIntake(intake, createIdentifier());
334 // Create the a part object
335 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
336 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
337 commonPart.setLabel(intakeClient.getCommonPartName());
339 // Make the call to create and check the response
340 response = intakeClient.create(multipart);
341 String intakeCsid = null;
343 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
344 intakeCsid = extractId(response);
346 response.releaseConnection();
349 // Lastly, relate the two entities, by creating a new relation object
350 RelationsCommon relation = new RelationsCommon();
351 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
352 intakeCsid, IntakesCommon.class.getSimpleName(),
353 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
354 // Create the part and fill it with the relation object
355 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
356 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
357 commonPart.setLabel(relationClient.getCommonPartName());
359 // Make the call to crate
360 response = relationClient.create(multipart);
361 String relationCsid = null;
363 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
364 relationCsid = extractId(response);
366 response.releaseConnection();
370 // Now try to retrieve the Intake record of the CollectionObject.
372 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
373 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
374 collectionObjectCsid,
375 null, //CollectionobjectsCommon.class.getSimpleName(),
378 null ); //IntakesCommon.class.getSimpleName());
379 RelationsCommonList relationList = null;
381 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
382 relationList = resultResponse.getEntity();
384 resultResponse.releaseConnection();
388 // Each relation returned in the list needs to match what we
391 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
392 Assert.assertFalse(relationListItems.isEmpty());
395 RelationsCommon resultRelation = null;
396 for(RelationsCommonList.RelationListItem listItem : relationListItems){
398 String foundCsid = listItem.getCsid();
399 ClientResponse<String> multiPartResponse = null;
401 multiPartResponse = relationClient.read(foundCsid);
402 int responseStatus = multiPartResponse.getStatus();
403 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
404 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
405 resultRelation = (RelationsCommon) extractPart(input,
406 relationClient.getCommonPartName(),
407 RelationsCommon.class);
408 } catch (Exception e) {
411 multiPartResponse.releaseConnection();
414 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
415 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
416 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
417 System.out.println();
423 public void relateCollectionObjectsToIntake() {
424 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
427 private void relateCollectionObjectsToIntake(int numberOfObjects) {
430 // First create a list of CollectionObjects
432 CollectionobjectsCommon co = new CollectionobjectsCommon();
433 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
434 for (int i = 0; i < numberOfObjects; i++) {
435 fillCollectionObject(co, createIdentifier());
437 // Next, create a part object
438 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
439 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
440 commonPart.setLabel(collectionObjectClient.getCommonPartName());
442 // Make the create call and check the response
443 ClientResponse<Response> response = collectionObjectClient.create(multipart);
444 String collectionObjectCsid = null;
446 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
448 collectionObjectCsid = extractId(response);
449 collectionObjectIDList.add(collectionObjectCsid);
451 response.releaseConnection();
456 // Next, create an Intake object
457 IntakesCommon intake = new IntakesCommon();
458 fillIntake(intake, createIdentifier());
459 // Create the a part object
460 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
461 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
462 commonPart.setLabel(intakeClient.getCommonPartName());
464 // Make the call to create and check the response
465 ClientResponse<Response> response = intakeClient.create(multipart);
466 String intakeCsid = null;
468 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
469 intakeCsid = extractId(response);
471 response.releaseConnection();
474 // Lastly, relate the two entities, by creating a new relation object
475 RelationsCommon relation = new RelationsCommon();
476 for (String collectionObjectCsid : collectionObjectIDList) {
477 fillRelation(relation,
478 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
479 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
480 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
481 // Create the part and fill it with the relation object
482 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
483 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
484 commonPart.setLabel(relationClient.getCommonPartName());
486 // Make the call to crate
487 response = relationClient.create(multipart);
488 String relationCsid = null;
490 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
491 relationCsid = extractId(response);
493 response.releaseConnection();
498 // Now try to retrieve the Intake record of the CollectionObject.
500 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
501 RelationsCommonList relationList = null;
502 for (String collectionObjectCsid : collectionObjectIDList) {
503 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
505 null, //IntakesCommon.class.getSimpleName(), //subject
507 collectionObjectCsid,
508 null); //CollectionobjectsCommon.class.getSimpleName()); //object
511 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
512 relationList = resultResponse.getEntity();
514 resultResponse.releaseConnection();
518 // Each relation returned in the list needs to match what we
521 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
522 Assert.assertFalse(relationListItems.isEmpty());
525 RelationsCommon resultRelation = null;
526 for(RelationsCommonList.RelationListItem listItem : relationListItems){
527 String foundCsid = listItem.getCsid();
528 ClientResponse<String> multiPartResponse = null;
530 multiPartResponse = relationClient.read(foundCsid);
531 int responseStatus = multiPartResponse.getStatus();
532 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
533 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
534 resultRelation = (RelationsCommon) extractPart(input,
535 relationClient.getCommonPartName(),
536 RelationsCommon.class);
537 } catch (Exception e) {
540 multiPartResponse.releaseConnection();
543 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
544 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
545 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
546 System.out.println();