]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1e7a46faaa9157ec334c708a667704dabd543e16
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.IntegrationTests.test;
24
25 import java.io.StringWriter;
26 import java.math.BigDecimal;
27 import java.util.ArrayList;
28 import java.util.List;
29
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;
34
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import org.jboss.resteasy.client.ClientResponse;
42
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;
50
51 import org.collectionspace.services.client.IntakeClient;
52 import org.collectionspace.services.intake.IntakesCommon;
53
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;
60
61 /**
62  * A ServiceTest.
63  * 
64  * @version $Revision:$
65  */
66 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
67
68         final Logger logger = LoggerFactory
69                         .getLogger(RelationIntegrationTest.class);
70         //
71         // Get clients for the CollectionSpace services
72         //
73         private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
74         private RelationClient relationClient = new RelationClient();
75         private IntakeClient intakeClient = new IntakeClient();
76         private DimensionClient dimensionClient = new DimensionClient();
77         
78         private static final int OBJECTS_TO_INTAKE = 1;
79         
80         
81     /**
82      * Object as xml string.
83      *
84      * @param o the o
85      * @param clazz the clazz
86      * @return the string
87      */
88     static protected String objectAsXmlString(Object o, Class<?> clazz) {
89         StringWriter sw = new StringWriter();
90         try {
91             JAXBContext jc = JAXBContext.newInstance(clazz);
92             Marshaller m = jc.createMarshaller();
93             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
94                     Boolean.TRUE);
95             m.marshal(o, sw);
96         } catch (Exception e) {
97             e.printStackTrace();
98         }
99         return sw.toString();
100     }
101         
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);
109
110         if (logger.isDebugEnabled()) {
111             logger.debug("to be created, dimension common");
112             logger.debug(objectAsXmlString(dimensionsCommon,
113                     DimensionsCommon.class));
114         }
115
116         return multipart;
117     }
118         
119     protected PoxPayloadOut createDimensionInstance(String identifier) {
120         DimensionClient client = new DimensionClient();
121         return createDimensionInstance(client.getCommonPartName(), identifier);
122     }
123     
124     /**
125      * Creates the dimension instance.
126      *
127      * @param identifier the identifier
128      * @return the multipart output
129      */
130     protected PoxPayloadOut createDimensionInstance(String commonPartName, String identifier) {
131         final String DIMENSION_VALUE = "78.306";
132         
133         return createDimensionInstance(commonPartName, 
134                 "dimensionType-" + identifier,
135                 DIMENSION_VALUE,
136                 "entryDate-" + identifier);
137     }
138     
139         @Test void deleteCollectionObjectRelationshipToLockedDimension() {
140                 //
141                 // First create a CollectionObject
142                 //
143                 CollectionobjectsCommon co = new CollectionobjectsCommon();
144                 fillCollectionObject(co, createIdentifier());
145                 
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());
150                 
151                 // Make the create call and check the response
152                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
153                 String collectionObjectCsid = null;
154                 try {
155                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
156                                         .getStatusCode());
157                         collectionObjectCsid = extractId(response);
158                 } finally {
159                         response.releaseConnection();
160                 }
161                 
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;
167             try {
168                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
169                     dimensionCsid = extractId(response);
170             } finally {
171                 response.releaseConnection();
172             }
173             
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());
183
184             // Create the relationship
185             response = relationClient.create(multipart);
186             @SuppressWarnings("unused")
187                 String relationCsid = null;
188             try {
189                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
190                     relationCsid = extractId(response);
191             } finally {
192                 response.releaseConnection();
193             }
194             
195             // Now lock the dimension record.
196             
197                 @SuppressWarnings("unused")
198                 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
199             System.out.println("Locked dimension record with CSID=" + dimensionCsid);
200             
201             // Finally, try to delete the relationship
202             
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);
205             try {
206                     Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
207             } finally {
208                 response.releaseConnection();
209             }
210             
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);
214         }
215     
216         
217         @Test void releteCollectionObjectToLockedDimension() {
218                 //
219                 // First create a CollectionObject
220                 //
221                 CollectionobjectsCommon co = new CollectionobjectsCommon();
222                 fillCollectionObject(co, createIdentifier());
223                 
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());
228                 
229                 // Make the create call and check the response
230                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
231                 String collectionObjectCsid = null;
232                 try {
233                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
234                                         .getStatusCode());
235                         collectionObjectCsid = extractId(response);
236                 } finally {
237                         response.releaseConnection();
238                 }
239                 
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;
245             try {
246                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
247                     dimensionCsid = extractId(response);
248             } finally {
249                 response.releaseConnection();
250             }
251             
252             @SuppressWarnings("unused")
253                 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
254             System.out.println("Locked dimension record with CSID=" + dimensionCsid);
255             
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());
265
266             // Make the call to crate
267             ClientResponse<Response> relationresponse = relationClient.create(multipart);
268             @SuppressWarnings("unused")
269                 String relationCsid = null;
270             try {
271                     Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
272                     relationCsid = extractId(response);
273             } finally {
274                 relationresponse.releaseConnection();
275             }
276             
277         }
278         
279         @Test
280         public void relateCollectionObjectToIntake() {
281                 //
282                 // First create a CollectionObject
283                 //
284                 CollectionobjectsCommon co = new CollectionobjectsCommon();
285                 fillCollectionObject(co, createIdentifier());
286                 
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());
291                 
292                 // Make the create call and check the response
293                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
294                 String collectionObjectCsid = null;
295                 try {
296                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
297                                         .getStatusCode());
298                         collectionObjectCsid = extractId(response);
299                 } finally {
300                         response.releaseConnection();
301                 }
302             
303             
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());
311
312             // Make the call to create and check the response
313             response = intakeClient.create(multipart);
314             String intakeCsid = null;
315             try {
316                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
317                     intakeCsid = extractId(response);
318             } finally {
319                 response.releaseConnection();
320             }
321             
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());
331
332             // Make the call to crate
333             response = relationClient.create(multipart);
334             String relationCsid = null;
335             try {
336                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
337                     relationCsid = extractId(response);
338             } finally {
339                 response.releaseConnection();
340             }
341             
342             //
343             // Now try to retrieve the Intake record of the CollectionObject.
344             //
345             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
346             ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
347                         collectionObjectCsid,
348                         null, //CollectionobjectsCommon.class.getSimpleName(),
349                         predicate,
350                         intakeCsid,
351                         null ); //IntakesCommon.class.getSimpleName());
352         RelationsCommonList relationList = null;
353             try {
354                 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
355                 relationList = resultResponse.getEntity();
356             } finally {
357                 resultResponse.releaseConnection();
358             }
359             
360             //
361             // Each relation returned in the list needs to match what we
362             // requested.
363             //
364         List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
365         Assert.assertFalse(relationListItems.isEmpty());
366         
367         int i = 0;
368         RelationsCommon resultRelation = null;
369         for(RelationsCommonList.RelationListItem listItem : relationListItems){
370                 
371                 String foundCsid = listItem.getCsid();
372                 ClientResponse<String> multiPartResponse = null;
373                 try {
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) {
382                         e.printStackTrace();
383                 } finally {
384                         multiPartResponse.releaseConnection();
385                 }
386                 
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();
391                 i++;            
392         }
393         }
394         
395         @Test
396         public void relateCollectionObjectsToIntake() {
397                 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
398         }
399
400         private void relateCollectionObjectsToIntake(int numberOfObjects) {
401                 
402                 //
403                 // First create a list of CollectionObjects
404                 //
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());
409                         
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());
414                         
415                         // Make the create call and check the response
416                         ClientResponse<Response> response = collectionObjectClient.create(multipart);
417                         String collectionObjectCsid = null;
418                         try {
419                                 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
420                                                 .getStatusCode());
421                                 collectionObjectCsid = extractId(response);
422                                 collectionObjectIDList.add(collectionObjectCsid);
423                         } finally {
424                                 response.releaseConnection();
425                         }
426                 }
427             
428             
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());
436
437             // Make the call to create and check the response
438             ClientResponse<Response> response = intakeClient.create(multipart);
439             String intakeCsid = null;
440             try {
441                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
442                     intakeCsid = extractId(response);
443             } finally {
444                 response.releaseConnection();
445             }
446             
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());
458         
459                     // Make the call to crate
460                     response = relationClient.create(multipart);
461                     String relationCsid = null;
462                     try {
463                             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
464                             relationCsid = extractId(response);
465                     } finally {
466                         response.releaseConnection();
467                     }
468             }
469             
470             //
471             // Now try to retrieve the Intake record of the CollectionObject.
472             //
473             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
474                 RelationsCommonList relationList = null;
475             for (String collectionObjectCsid : collectionObjectIDList) {
476                     ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
477                                 intakeCsid,
478                                 null, //IntakesCommon.class.getSimpleName(), //subject
479                                 predicate,
480                                 collectionObjectCsid,
481                                 null); //CollectionobjectsCommon.class.getSimpleName()); //object
482
483                     try {
484                         Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
485                         relationList = resultResponse.getEntity();
486                     } finally {
487                         resultResponse.releaseConnection();
488                     }
489             
490                     //
491                     // Each relation returned in the list needs to match what we
492                     // requested.
493                     //
494                 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
495                 Assert.assertFalse(relationListItems.isEmpty());
496                 
497                 int i = 0;
498                 RelationsCommon resultRelation = null;
499                 for(RelationsCommonList.RelationListItem listItem : relationListItems){
500                         String foundCsid = listItem.getCsid();
501                         ClientResponse<String> multiPartResponse = null;
502                         try {
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) {
511                                 e.printStackTrace();
512                         } finally {
513                                 multiPartResponse.releaseConnection();
514                         }
515         
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();
520                         i++;            
521                 }
522             }
523         }
524         
525
526         /*
527          * Private Methods
528          */
529
530 }