]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
523a23ae490192ca12e9d5dfeaa825bd1d57e150
[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(collectionObjectClient.getCommonPartName(), co);
149                 
150                 // Make the create call and check the response
151                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
152                 String collectionObjectCsid = null;
153                 try {
154                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
155                                         .getStatusCode());
156                         collectionObjectCsid = extractId(response);
157                 } finally {
158                         response.releaseConnection();
159                 }
160                 
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;
166             try {
167                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
168                     dimensionCsid1 = extractId(response);
169             } finally {
170                 response.releaseConnection();
171             }
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;
177             try {
178                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
179                     dimensionCsid2 = extractId(response);
180             } finally {
181                 response.releaseConnection();
182             }
183             
184             
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;
196             try {
197                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
198                     relationCsid1 = extractId(response);
199             } finally {
200                 response.releaseConnection();
201             }
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;
214             try {
215                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
216                     relationCsid2 = extractId(response);
217             } finally {
218                 response.releaseConnection();
219             }
220             
221             
222             // Now lock the dimension record.
223             
224                 @SuppressWarnings("unused")
225                 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_LOCK);
226             System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
227             
228             // Finally, try to delete the relationship
229             
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);
232             try {
233                     Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
234             } finally {
235                 response.releaseConnection();
236             }
237             
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);
241         }
242     
243         
244         @Test void releteCollectionObjectToLockedDimension() {
245                 //
246                 // First create a CollectionObject
247                 //
248                 CollectionobjectsCommon co = new CollectionobjectsCommon();
249                 fillCollectionObject(co, createIdentifier());
250                 
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());
255                 
256                 // Make the create call and check the response
257                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
258                 String collectionObjectCsid = null;
259                 try {
260                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
261                                         .getStatusCode());
262                         collectionObjectCsid = extractId(response);
263                 } finally {
264                         response.releaseConnection();
265                 }
266                 
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;
272             try {
273                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
274                     dimensionCsid = extractId(response);
275             } finally {
276                 response.releaseConnection();
277             }
278             
279             @SuppressWarnings("unused")
280                 ClientResponse<String> workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid, WorkflowClient.WORKFLOWTRANSITION_LOCK);
281             System.out.println("Locked dimension record with CSID=" + dimensionCsid);
282             
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());
292
293             // Make the call to crate
294             ClientResponse<Response> relationresponse = relationClient.create(multipart);
295             @SuppressWarnings("unused")
296                 String relationCsid = null;
297             try {
298                     Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
299                     relationCsid = extractId(response);
300             } finally {
301                 relationresponse.releaseConnection();
302             }
303             
304         }
305         
306         @Test
307         public void relateCollectionObjectToIntake() {
308                 //
309                 // First create a CollectionObject
310                 //
311                 CollectionobjectsCommon co = new CollectionobjectsCommon();
312                 fillCollectionObject(co, createIdentifier());
313                 
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());
318                 
319                 // Make the create call and check the response
320                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
321                 String collectionObjectCsid = null;
322                 try {
323                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
324                                         .getStatusCode());
325                         collectionObjectCsid = extractId(response);
326                 } finally {
327                         response.releaseConnection();
328                 }
329             
330             
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());
338
339             // Make the call to create and check the response
340             response = intakeClient.create(multipart);
341             String intakeCsid = null;
342             try {
343                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
344                     intakeCsid = extractId(response);
345             } finally {
346                 response.releaseConnection();
347             }
348             
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());
358
359             // Make the call to crate
360             response = relationClient.create(multipart);
361             String relationCsid = null;
362             try {
363                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
364                     relationCsid = extractId(response);
365             } finally {
366                 response.releaseConnection();
367             }
368             
369             //
370             // Now try to retrieve the Intake record of the CollectionObject.
371             //
372             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
373             ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
374                         collectionObjectCsid,
375                         null, //CollectionobjectsCommon.class.getSimpleName(),
376                         predicate,
377                         intakeCsid,
378                         null ); //IntakesCommon.class.getSimpleName());
379         RelationsCommonList relationList = null;
380             try {
381                 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
382                 relationList = resultResponse.getEntity();
383             } finally {
384                 resultResponse.releaseConnection();
385             }
386             
387             //
388             // Each relation returned in the list needs to match what we
389             // requested.
390             //
391         List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
392         Assert.assertFalse(relationListItems.isEmpty());
393         
394         int i = 0;
395         RelationsCommon resultRelation = null;
396         for(RelationsCommonList.RelationListItem listItem : relationListItems){
397                 
398                 String foundCsid = listItem.getCsid();
399                 ClientResponse<String> multiPartResponse = null;
400                 try {
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) {
409                         e.printStackTrace();
410                 } finally {
411                         multiPartResponse.releaseConnection();
412                 }
413                 
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();
418                 i++;            
419         }
420         }
421         
422         @Test
423         public void relateCollectionObjectsToIntake() {
424                 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
425         }
426
427         private void relateCollectionObjectsToIntake(int numberOfObjects) {
428                 
429                 //
430                 // First create a list of CollectionObjects
431                 //
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());
436                         
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());
441                         
442                         // Make the create call and check the response
443                         ClientResponse<Response> response = collectionObjectClient.create(multipart);
444                         String collectionObjectCsid = null;
445                         try {
446                                 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
447                                                 .getStatusCode());
448                                 collectionObjectCsid = extractId(response);
449                                 collectionObjectIDList.add(collectionObjectCsid);
450                         } finally {
451                                 response.releaseConnection();
452                         }
453                 }
454             
455             
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());
463
464             // Make the call to create and check the response
465             ClientResponse<Response> response = intakeClient.create(multipart);
466             String intakeCsid = null;
467             try {
468                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
469                     intakeCsid = extractId(response);
470             } finally {
471                 response.releaseConnection();
472             }
473             
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());
485         
486                     // Make the call to crate
487                     response = relationClient.create(multipart);
488                     String relationCsid = null;
489                     try {
490                             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
491                             relationCsid = extractId(response);
492                     } finally {
493                         response.releaseConnection();
494                     }
495             }
496             
497             //
498             // Now try to retrieve the Intake record of the CollectionObject.
499             //
500             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
501                 RelationsCommonList relationList = null;
502             for (String collectionObjectCsid : collectionObjectIDList) {
503                     ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
504                                 intakeCsid,
505                                 null, //IntakesCommon.class.getSimpleName(), //subject
506                                 predicate,
507                                 collectionObjectCsid,
508                                 null); //CollectionobjectsCommon.class.getSimpleName()); //object
509
510                     try {
511                         Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
512                         relationList = resultResponse.getEntity();
513                     } finally {
514                         resultResponse.releaseConnection();
515                     }
516             
517                     //
518                     // Each relation returned in the list needs to match what we
519                     // requested.
520                     //
521                 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
522                 Assert.assertFalse(relationListItems.isEmpty());
523                 
524                 int i = 0;
525                 RelationsCommon resultRelation = null;
526                 for(RelationsCommonList.RelationListItem listItem : relationListItems){
527                         String foundCsid = listItem.getCsid();
528                         ClientResponse<String> multiPartResponse = null;
529                         try {
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) {
538                                 e.printStackTrace();
539                         } finally {
540                                 multiPartResponse.releaseConnection();
541                         }
542         
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();
547                         i++;            
548                 }
549             }
550         }
551         
552
553         /*
554          * Private Methods
555          */
556
557 }