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(co, MediaType.APPLICATION_XML_TYPE);
149 commonPart.setLabel(collectionObjectClient.getCommonPartName());
151 // Make the create call and check the response
152 ClientResponse<Response> response = collectionObjectClient.create(multipart);
153 String collectionObjectCsid = null;
155 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
157 collectionObjectCsid = extractId(response);
159 response.releaseConnection();
162 //Next, create a Dimension record to relate the collection object to
163 multipart = this.createDimensionInstance(createIdentifier());
164 // Make the call to create and check the response
165 response = dimensionClient.create(multipart);
166 String dimensionCsid = null;
168 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
169 dimensionCsid = extractId(response);
171 response.releaseConnection();
174 // Relate the two entities, by creating a new relation object
175 RelationsCommon relation = new RelationsCommon();
176 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
177 dimensionCsid, DimensionsCommon.class.getSimpleName(),
178 "collectionobject-dimension");
179 // Create the part and fill it with the relation object
180 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
181 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
182 commonPart.setLabel(relationClient.getCommonPartName());
184 // Create the relationship
185 response = relationClient.create(multipart);
186 @SuppressWarnings("unused")
187 String relationCsid = null;
189 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
190 relationCsid = extractId(response);
192 response.releaseConnection();
195 // Now lock the dimension record.
197 @SuppressWarnings("unused")
198 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
199 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
201 // Finally, try to delete the relationship
203 // Try to delete the relationship -should fail because we don't allow delete if one of the sides is locked.
204 response = relationClient.delete(relationCsid);
206 Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
208 response.releaseConnection();
211 // Also, try to soft-delete. This should also fail.
212 workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_DELETE);
213 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
217 @Test void releteCollectionObjectToLockedDimension() {
219 // First create a CollectionObject
221 CollectionobjectsCommon co = new CollectionobjectsCommon();
222 fillCollectionObject(co, createIdentifier());
224 // Next, create a part object
225 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
226 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
227 commonPart.setLabel(collectionObjectClient.getCommonPartName());
229 // Make the create call and check the response
230 ClientResponse<Response> response = collectionObjectClient.create(multipart);
231 String collectionObjectCsid = null;
233 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
235 collectionObjectCsid = extractId(response);
237 response.releaseConnection();
240 //Next, create a Dimension record to relate the collection object to
241 multipart = this.createDimensionInstance(createIdentifier());
242 // Make the call to create and check the response
243 response = dimensionClient.create(multipart);
244 String dimensionCsid = null;
246 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
247 dimensionCsid = extractId(response);
249 response.releaseConnection();
252 @SuppressWarnings("unused")
253 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
254 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
256 // Lastly, relate the two entities, by creating a new relation object
257 RelationsCommon relation = new RelationsCommon();
258 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
259 dimensionCsid, DimensionsCommon.class.getSimpleName(),
260 "collectionobject-dimension");
261 // Create the part and fill it with the relation object
262 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
263 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
264 commonPart.setLabel(relationClient.getCommonPartName());
266 // Make the call to crate
267 ClientResponse<Response> relationresponse = relationClient.create(multipart);
268 @SuppressWarnings("unused")
269 String relationCsid = null;
271 Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
272 relationCsid = extractId(response);
274 relationresponse.releaseConnection();
280 public void relateCollectionObjectToIntake() {
282 // First create a CollectionObject
284 CollectionobjectsCommon co = new CollectionobjectsCommon();
285 fillCollectionObject(co, createIdentifier());
287 // Next, create a part object
288 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
289 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
290 commonPart.setLabel(collectionObjectClient.getCommonPartName());
292 // Make the create call and check the response
293 ClientResponse<Response> response = collectionObjectClient.create(multipart);
294 String collectionObjectCsid = null;
296 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
298 collectionObjectCsid = extractId(response);
300 response.releaseConnection();
304 // Next, create an Intake object
305 IntakesCommon intake = new IntakesCommon();
306 fillIntake(intake, createIdentifier());
307 // Create the a part object
308 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
309 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
310 commonPart.setLabel(intakeClient.getCommonPartName());
312 // Make the call to create and check the response
313 response = intakeClient.create(multipart);
314 String intakeCsid = null;
316 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
317 intakeCsid = extractId(response);
319 response.releaseConnection();
322 // Lastly, relate the two entities, by creating a new relation object
323 RelationsCommon relation = new RelationsCommon();
324 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
325 intakeCsid, IntakesCommon.class.getSimpleName(),
326 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
327 // Create the part and fill it with the relation object
328 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
329 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
330 commonPart.setLabel(relationClient.getCommonPartName());
332 // Make the call to crate
333 response = relationClient.create(multipart);
334 String relationCsid = null;
336 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
337 relationCsid = extractId(response);
339 response.releaseConnection();
343 // Now try to retrieve the Intake record of the CollectionObject.
345 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
346 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
347 collectionObjectCsid,
348 null, //CollectionobjectsCommon.class.getSimpleName(),
351 null ); //IntakesCommon.class.getSimpleName());
352 RelationsCommonList relationList = null;
354 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
355 relationList = resultResponse.getEntity();
357 resultResponse.releaseConnection();
361 // Each relation returned in the list needs to match what we
364 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
365 Assert.assertFalse(relationListItems.isEmpty());
368 RelationsCommon resultRelation = null;
369 for(RelationsCommonList.RelationListItem listItem : relationListItems){
371 String foundCsid = listItem.getCsid();
372 ClientResponse<String> multiPartResponse = null;
374 multiPartResponse = relationClient.read(foundCsid);
375 int responseStatus = multiPartResponse.getStatus();
376 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
377 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
378 resultRelation = (RelationsCommon) extractPart(input,
379 relationClient.getCommonPartName(),
380 RelationsCommon.class);
381 } catch (Exception e) {
384 multiPartResponse.releaseConnection();
387 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
388 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
389 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
390 System.out.println();
396 public void relateCollectionObjectsToIntake() {
397 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
400 private void relateCollectionObjectsToIntake(int numberOfObjects) {
403 // First create a list of CollectionObjects
405 CollectionobjectsCommon co = new CollectionobjectsCommon();
406 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
407 for (int i = 0; i < numberOfObjects; i++) {
408 fillCollectionObject(co, createIdentifier());
410 // Next, create a part object
411 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
412 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
413 commonPart.setLabel(collectionObjectClient.getCommonPartName());
415 // Make the create call and check the response
416 ClientResponse<Response> response = collectionObjectClient.create(multipart);
417 String collectionObjectCsid = null;
419 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
421 collectionObjectCsid = extractId(response);
422 collectionObjectIDList.add(collectionObjectCsid);
424 response.releaseConnection();
429 // Next, create an Intake object
430 IntakesCommon intake = new IntakesCommon();
431 fillIntake(intake, createIdentifier());
432 // Create the a part object
433 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
434 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
435 commonPart.setLabel(intakeClient.getCommonPartName());
437 // Make the call to create and check the response
438 ClientResponse<Response> response = intakeClient.create(multipart);
439 String intakeCsid = null;
441 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
442 intakeCsid = extractId(response);
444 response.releaseConnection();
447 // Lastly, relate the two entities, by creating a new relation object
448 RelationsCommon relation = new RelationsCommon();
449 for (String collectionObjectCsid : collectionObjectIDList) {
450 fillRelation(relation,
451 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
452 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
453 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
454 // Create the part and fill it with the relation object
455 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
456 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
457 commonPart.setLabel(relationClient.getCommonPartName());
459 // Make the call to crate
460 response = relationClient.create(multipart);
461 String relationCsid = null;
463 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
464 relationCsid = extractId(response);
466 response.releaseConnection();
471 // Now try to retrieve the Intake record of the CollectionObject.
473 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
474 RelationsCommonList relationList = null;
475 for (String collectionObjectCsid : collectionObjectIDList) {
476 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
478 null, //IntakesCommon.class.getSimpleName(), //subject
480 collectionObjectCsid,
481 null); //CollectionobjectsCommon.class.getSimpleName()); //object
484 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
485 relationList = resultResponse.getEntity();
487 resultResponse.releaseConnection();
491 // Each relation returned in the list needs to match what we
494 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
495 Assert.assertFalse(relationListItems.isEmpty());
498 RelationsCommon resultRelation = null;
499 for(RelationsCommonList.RelationListItem listItem : relationListItems){
500 String foundCsid = listItem.getCsid();
501 ClientResponse<String> multiPartResponse = null;
503 multiPartResponse = relationClient.read(foundCsid);
504 int responseStatus = multiPartResponse.getStatus();
505 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
506 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
507 resultRelation = (RelationsCommon) extractPart(input,
508 relationClient.getCommonPartName(),
509 RelationsCommon.class);
510 } catch (Exception e) {
513 multiPartResponse.releaseConnection();
516 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
517 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
518 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
519 System.out.println();