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 dimensionValue = Long.toString(System.currentTimeMillis());
133 return createDimensionInstance(commonPartName,
134 "dimensionType-" + identifier,
136 "entryDate-" + identifier);
139 @Test void deleteCollectionObjectRelationshipToLockedDimension() {
140 // First create a CollectionObject
141 CollectionobjectsCommon co = new CollectionobjectsCommon();
142 fillCollectionObject(co, createIdentifier());
143 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
144 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
146 // Make the "create" call and check the response
147 Response response = collectionObjectClient.create(multipart);
148 String collectionObjectCsid = null;
150 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
152 collectionObjectCsid = extractId(response);
157 // Create a new dimension record
158 multipart = this.createDimensionInstance(createIdentifier());
159 // Make the call to create and check the response
160 response = dimensionClient.create(multipart);
161 String dimensionCsid1 = null;
163 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
164 dimensionCsid1 = extractId(response);
169 // Relate the entities, by creating a new relation object
170 RelationsCommon relation = new RelationsCommon();
171 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
172 dimensionCsid1, DimensionsCommon.class.getSimpleName(),
173 "collectionobject-dimension");
174 // Create the part and fill it with the relation object
175 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
176 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
177 // Create the relationship
178 response = relationClient.create(multipart);
179 String relationCsid1 = null;
181 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
182 relationCsid1 = extractId(response);
187 // Now lock the dimension record.
189 @SuppressWarnings("unused")
190 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_LOCK);
191 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
193 // Finally, try to delete the relationship
195 // Try to delete the relationship -should fail because we don't allow delete if one of the sides is locked.
196 response = relationClient.delete(relationCsid1);
198 Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
203 // Also, try to soft-delete. This should also fail.
204 workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_DELETE);
205 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
208 @Test void createCollectionObjectRelationshipToManyDimensions() {
210 // First create a CollectionObject
212 CollectionobjectsCommon co = new CollectionobjectsCommon();
213 fillCollectionObject(co, createIdentifier());
214 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
215 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
217 // Make the create call and check the response
218 Response response = collectionObjectClient.create(multipart);
219 String collectionObjectCsid = null;
221 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
223 collectionObjectCsid = extractId(response);
228 //Next, create the first of three Dimension records to relate the collection object record
229 multipart = this.createDimensionInstance(createIdentifier());
230 // Make the call to create and check the response
231 response = dimensionClient.create(multipart);
232 String dimensionCsid1 = null;
234 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
235 dimensionCsid1 = extractId(response);
240 //Next, create a the second Dimension record
241 multipart = this.createDimensionInstance(createIdentifier());
242 // Make the call to create and check the response
243 response = dimensionClient.create(multipart);
244 String dimensionCsid2 = null;
246 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
247 dimensionCsid2 = extractId(response);
252 //Next, create a the 3rd Dimension record
253 multipart = this.createDimensionInstance(createIdentifier());
254 // Make the call to create and check the response
255 response = dimensionClient.create(multipart);
256 String dimensionCsid3 = null;
258 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
259 dimensionCsid3 = extractId(response);
264 // Relate the entities, by creating a new relation object
265 RelationsCommon relation = new RelationsCommon();
266 fillRelation(relation,
267 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
268 dimensionCsid1, DimensionsCommon.class.getSimpleName(), // the object of the relationship
269 "collectionobject-dimension");
270 // Create the part and fill it with the relation object
271 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
272 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
273 // Create the relationship
274 response = relationClient.create(multipart);
275 String relationCsid1 = null;
277 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
278 relationCsid1 = extractId(response);
282 // Wait 1 second and create the second relationship
285 } catch (InterruptedException e) {
286 // TODO Auto-generated catch block
289 relation = new RelationsCommon();
290 fillRelation(relation,
291 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
292 dimensionCsid2, DimensionsCommon.class.getSimpleName(), // the object of the relationship
293 "collectionobject-dimension");
294 // Create the part and fill it with the relation object
295 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
296 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
297 // Create the relationship
298 response = relationClient.create(multipart);
299 @SuppressWarnings("unused")
300 String relationCsid2 = null;
302 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
303 relationCsid2 = extractId(response);
307 // Wait 1 second and create the 3rd relationship
310 } catch (InterruptedException e) {
311 // TODO Auto-generated catch block
314 relation = new RelationsCommon();
315 fillRelation(relation,
316 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
317 dimensionCsid3, DimensionsCommon.class.getSimpleName(), // the object of the relationship
318 "collectionobject-dimension");
319 // Create the part and fill it with the relation object
320 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
321 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
322 // Create the relationship
323 response = relationClient.create(multipart);
324 @SuppressWarnings("unused")
325 String relationCsid3 = null;
327 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
328 relationCsid3 = extractId(response);
334 @Test void releteCollectionObjectToLockedDimension() {
336 // First create a CollectionObject
338 CollectionobjectsCommon co = new CollectionobjectsCommon();
339 fillCollectionObject(co, createIdentifier());
341 // Next, create a part object
342 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
343 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
344 commonPart.setLabel(collectionObjectClient.getCommonPartName());
346 // Make the create call and check the response
347 Response response = collectionObjectClient.create(multipart);
348 String collectionObjectCsid = null;
350 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
352 collectionObjectCsid = extractId(response);
357 //Next, create a Dimension record to relate the collection object to
358 multipart = this.createDimensionInstance(createIdentifier());
359 // Make the call to create and check the response
360 response = dimensionClient.create(multipart);
361 String dimensionCsid = null;
363 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
364 dimensionCsid = extractId(response);
369 @SuppressWarnings("unused")
370 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
371 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
373 // Lastly, relate the two entities, by creating a new relation object
374 RelationsCommon relation = new RelationsCommon();
375 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
376 dimensionCsid, DimensionsCommon.class.getSimpleName(),
377 "collectionobject-dimension");
378 // Create the part and fill it with the relation object
379 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
380 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
381 commonPart.setLabel(relationClient.getCommonPartName());
383 // Make the call to crate
384 Response relationresponse = relationClient.create(multipart);
385 String relationCsid = null;
387 Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
388 relationCsid = extractId(response);
390 relationresponse.close();
395 public void relateCollectionObjectToIntake() {
397 // First create a CollectionObject
399 CollectionobjectsCommon co = new CollectionobjectsCommon();
400 fillCollectionObject(co, createIdentifier());
402 // Next, create a part object
403 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
404 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
405 commonPart.setLabel(collectionObjectClient.getCommonPartName());
407 // Make the create call and check the response
408 Response response = collectionObjectClient.create(multipart);
409 String collectionObjectCsid = null;
411 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
413 collectionObjectCsid = extractId(response);
419 // Next, create an Intake object
420 IntakesCommon intake = new IntakesCommon();
421 fillIntake(intake, createIdentifier());
422 // Create the a part object
423 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
424 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
425 commonPart.setLabel(intakeClient.getCommonPartName());
427 // Make the call to create and check the response
428 response = intakeClient.create(multipart);
429 String intakeCsid = null;
431 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
432 intakeCsid = extractId(response);
437 // Lastly, relate the two entities, by creating a new relation object
438 RelationsCommon relation = new RelationsCommon();
439 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
440 intakeCsid, IntakesCommon.class.getSimpleName(),
441 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
442 // Create the part and fill it with the relation object
443 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
444 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
445 commonPart.setLabel(relationClient.getCommonPartName());
447 // Make the call to crate
448 response = relationClient.create(multipart);
449 String relationCsid = null;
451 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
452 relationCsid = extractId(response);
458 // Now try to retrieve the Intake record of the CollectionObject.
460 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
461 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
462 collectionObjectCsid,
463 null, //CollectionobjectsCommon.class.getSimpleName(),
466 null ); //IntakesCommon.class.getSimpleName());
467 RelationsCommonList relationList = null;
469 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
470 relationList = resultResponse.getEntity();
472 resultResponse.close();
476 // Each relation returned in the list needs to match what we
479 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
480 Assert.assertFalse(relationListItems.isEmpty());
483 RelationsCommon resultRelation = null;
484 for(RelationsCommonList.RelationListItem listItem : relationListItems){
486 String foundCsid = listItem.getCsid();
487 Response multiPartResponse = null;
489 multiPartResponse = relationClient.read(foundCsid);
490 int responseStatus = multiPartResponse.getStatus();
491 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
492 PoxPayloadIn input = new PoxPayloadIn((String)multiPartResponse.getEntity());
493 resultRelation = (RelationsCommon) extractPart(input,
494 relationClient.getCommonPartName(),
495 RelationsCommon.class);
496 } catch (Exception e) {
499 multiPartResponse.close();
502 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
503 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
504 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
505 System.out.println();
511 public void relateCollectionObjectsToIntake() {
512 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
515 private void relateCollectionObjectsToIntake(int numberOfObjects) {
518 // First create a list of CollectionObjects
520 CollectionobjectsCommon co = new CollectionobjectsCommon();
521 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
522 for (int i = 0; i < numberOfObjects; i++) {
523 fillCollectionObject(co, createIdentifier());
525 // Next, create a part object
526 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
527 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
528 commonPart.setLabel(collectionObjectClient.getCommonPartName());
530 // Make the create call and check the response
531 Response response = collectionObjectClient.create(multipart);
532 String collectionObjectCsid = null;
534 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
536 collectionObjectCsid = extractId(response);
537 collectionObjectIDList.add(collectionObjectCsid);
544 // Next, create an Intake object
545 IntakesCommon intake = new IntakesCommon();
546 fillIntake(intake, createIdentifier());
547 // Create the a part object
548 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
549 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
550 commonPart.setLabel(intakeClient.getCommonPartName());
552 // Make the call to create and check the response
553 Response response = intakeClient.create(multipart);
554 String intakeCsid = null;
556 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
557 intakeCsid = extractId(response);
562 // Lastly, relate the two entities, by creating a new relation object
563 RelationsCommon relation = new RelationsCommon();
564 for (String collectionObjectCsid : collectionObjectIDList) {
565 fillRelation(relation,
566 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
567 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
568 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
569 // Create the part and fill it with the relation object
570 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
571 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
572 commonPart.setLabel(relationClient.getCommonPartName());
574 // Make the call to crate
575 response = relationClient.create(multipart);
576 String relationCsid = null;
578 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
579 relationCsid = extractId(response);
586 // Now try to retrieve the Intake record of the CollectionObject.
588 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
589 RelationsCommonList relationList = null;
590 for (String collectionObjectCsid : collectionObjectIDList) {
591 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
593 null, //IntakesCommon.class.getSimpleName(), //subject
595 collectionObjectCsid,
596 null); //CollectionobjectsCommon.class.getSimpleName()); //object
599 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
600 relationList = resultResponse.getEntity();
602 resultResponse.close();
606 // Each relation returned in the list needs to match what we
609 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
610 Assert.assertFalse(relationListItems.isEmpty());
613 RelationsCommon resultRelation = null;
614 for(RelationsCommonList.RelationListItem listItem : relationListItems){
615 String foundCsid = listItem.getCsid();
616 Response multiPartResponse = null;
618 multiPartResponse = relationClient.read(foundCsid);
619 int responseStatus = multiPartResponse.getStatus();
620 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
621 PoxPayloadIn input = new PoxPayloadIn((String)multiPartResponse.getEntity());
622 resultRelation = (RelationsCommon) extractPart(input,
623 relationClient.getCommonPartName(),
624 RelationsCommon.class);
625 } catch (Exception e) {
628 multiPartResponse.close();
631 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
632 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
633 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
634 System.out.println();