]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
aa5392a18c3750194f2160bf3fcb246fd56b2978
[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.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import org.jboss.resteasy.client.ClientResponse;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
40 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
41
42 import org.collectionspace.services.client.CollectionObjectClient;
43 import org.collectionspace.services.client.PayloadOutputPart;
44 import org.collectionspace.services.client.PoxPayloadIn;
45 import org.collectionspace.services.client.PoxPayloadOut;
46 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
47
48 import org.collectionspace.services.client.IntakeClient;
49 import org.collectionspace.services.intake.IntakesCommon;
50
51 import org.collectionspace.services.client.RelationClient;
52 import org.collectionspace.services.relation.RelationsCommon;
53 import org.collectionspace.services.relation.RelationsCommonList;
54 import org.collectionspace.services.relation.RelationshipType;
55
56 /**
57  * A ServiceTest.
58  * 
59  * @version $Revision:$
60  */
61 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
62
63         final Logger logger = LoggerFactory
64                         .getLogger(RelationIntegrationTest.class);
65         //
66         // Get clients for the CollectionSpace services
67         //
68         private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
69         private RelationClient relationClient = new RelationClient();
70         private IntakeClient intakeClient = new IntakeClient();
71         
72         private static final int OBJECTS_TO_INTAKE = 1;
73         
74         @Test
75         public void relateCollectionObjectToIntake() {
76                 
77                 //
78                 // First create a CollectionObject
79                 //
80                 CollectionobjectsCommon co = new CollectionobjectsCommon();
81                 fillCollectionObject(co, createIdentifier());
82                 
83                 // Next, create a part object
84                 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
85                 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
86                 commonPart.setLabel(collectionObjectClient.getCommonPartName());
87                 
88                 // Make the create call and check the response
89                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
90                 String collectionObjectCsid = null;
91                 try {
92                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
93                                         .getStatusCode());
94                         collectionObjectCsid = extractId(response);
95                 } finally {
96                         response.releaseConnection();
97                 }
98             
99             
100             // Next, create an Intake object
101             IntakesCommon intake = new IntakesCommon();
102             fillIntake(intake, createIdentifier());
103             // Create the a part object
104             multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
105             commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
106             commonPart.setLabel(intakeClient.getCommonPartName());
107
108             // Make the call to create and check the response
109             response = intakeClient.create(multipart);
110             String intakeCsid = null;
111             try {
112                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
113                     intakeCsid = extractId(response);
114             } finally {
115                 response.releaseConnection();
116             }
117             
118             // Lastly, relate the two entities, by creating a new relation object
119             RelationsCommon relation = new RelationsCommon();
120             fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
121                         intakeCsid, IntakesCommon.class.getSimpleName(),
122                         RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
123             // Create the part and fill it with the relation object
124             multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
125             commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
126             commonPart.setLabel(relationClient.getCommonPartName());
127
128             // Make the call to crate
129             response = relationClient.create(multipart);
130             String relationCsid = null;
131             try {
132                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
133                     relationCsid = extractId(response);
134             } finally {
135                 response.releaseConnection();
136             }
137             
138             //
139             // Now try to retrieve the Intake record of the CollectionObject.
140             //
141             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
142             ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
143                         collectionObjectCsid,
144                         CollectionobjectsCommon.class.getSimpleName(),
145                         predicate,
146                         intakeCsid,
147                         IntakesCommon.class.getSimpleName());
148         RelationsCommonList relationList = null;
149             try {
150                 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
151                 relationList = resultResponse.getEntity();
152             } finally {
153                 resultResponse.releaseConnection();
154             }
155             
156             //
157             // Each relation returned in the list needs to match what we
158             // requested.
159             //
160         List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
161         Assert.assertFalse(relationListItems.isEmpty());
162         
163         int i = 0;
164         RelationsCommon resultRelation = null;
165         for(RelationsCommonList.RelationListItem listItem : relationListItems){
166                 
167                 String foundCsid = listItem.getCsid();
168                 ClientResponse<String> multiPartResponse = null;
169                 try {
170                         multiPartResponse = relationClient.read(foundCsid);
171                         int responseStatus = multiPartResponse.getStatus();
172                         Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
173                         PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
174                         resultRelation = (RelationsCommon) extractPart(input,
175                                         relationClient.getCommonPartName(),
176                                         RelationsCommon.class);
177                 } catch (Exception e) {
178                         e.printStackTrace();
179                 } finally {
180                         multiPartResponse.releaseConnection();
181                 }
182                 
183                 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
184                 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
185                 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
186             System.out.println();
187                 i++;            
188         }
189         }
190         
191 //      @Test
192         public void relateCollectionObjectsToIntake() {
193                 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
194         }
195
196         private void relateCollectionObjectsToIntake(int numberOfObjects) {
197                 
198                 //
199                 // First create a list of CollectionObjects
200                 //
201                 CollectionobjectsCommon co = new CollectionobjectsCommon();
202                 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
203                 for (int i = 0; i < numberOfObjects; i++) {
204                         fillCollectionObject(co, createIdentifier());
205                         
206                         // Next, create a part object
207                         PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
208                         PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
209                         commonPart.setLabel(collectionObjectClient.getCommonPartName());
210                         
211                         // Make the create call and check the response
212                         ClientResponse<Response> response = collectionObjectClient.create(multipart);
213                         String collectionObjectCsid = null;
214                         try {
215                                 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
216                                                 .getStatusCode());
217                                 collectionObjectCsid = extractId(response);
218                                 collectionObjectIDList.add(collectionObjectCsid);
219                         } finally {
220                                 response.releaseConnection();
221                         }
222                 }
223             
224             
225             // Next, create an Intake object
226             IntakesCommon intake = new IntakesCommon();
227             fillIntake(intake, createIdentifier());
228             // Create the a part object
229             PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
230             PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
231             commonPart.setLabel(intakeClient.getCommonPartName());
232
233             // Make the call to create and check the response
234             ClientResponse<Response> response = intakeClient.create(multipart);
235             String intakeCsid = null;
236             try {
237                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
238                     intakeCsid = extractId(response);
239             } finally {
240                 response.releaseConnection();
241             }
242             
243             // Lastly, relate the two entities, by creating a new relation object
244             RelationsCommon relation = new RelationsCommon();
245             for (String collectionObjectCsid : collectionObjectIDList) {
246                     fillRelation(relation,
247                                 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
248                                 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object                           
249                                 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
250                     // Create the part and fill it with the relation object
251                     multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
252                     commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
253                     commonPart.setLabel(relationClient.getCommonPartName());
254         
255                     // Make the call to crate
256                     response = relationClient.create(multipart);
257                     String relationCsid = null;
258                     try {
259                             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
260                             relationCsid = extractId(response);
261                     } finally {
262                         response.releaseConnection();
263                     }
264             }
265             
266             //
267             // Now try to retrieve the Intake record of the CollectionObject.
268             //
269             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
270                 RelationsCommonList relationList = null;
271             for (String collectionObjectCsid : collectionObjectIDList) {
272                     ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
273                                 intakeCsid,
274                                 IntakesCommon.class.getSimpleName(), //subject
275                                 predicate,
276                                 collectionObjectCsid,
277                                 CollectionobjectsCommon.class.getSimpleName()); //object
278
279                     try {
280                         Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
281                         relationList = resultResponse.getEntity();
282                     } finally {
283                         resultResponse.releaseConnection();
284                     }
285             
286                     //
287                     // Each relation returned in the list needs to match what we
288                     // requested.
289                     //
290                 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
291                 Assert.assertFalse(relationListItems.isEmpty());
292                 
293                 int i = 0;
294                 RelationsCommon resultRelation = null;
295                 for(RelationsCommonList.RelationListItem listItem : relationListItems){
296                         String foundCsid = listItem.getCsid();
297                         ClientResponse<String> multiPartResponse = null;
298                         try {
299                                 multiPartResponse = relationClient.read(foundCsid);
300                                 int responseStatus = multiPartResponse.getStatus();
301                                 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
302                                 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
303                                 resultRelation = (RelationsCommon) extractPart(input,
304                                                 relationClient.getCommonPartName(),
305                                                 RelationsCommon.class);
306                         } catch (Exception e) {
307                                 e.printStackTrace();
308                         } finally {
309                                 multiPartResponse.releaseConnection();
310                         }
311         
312                         Assert.assertEquals(resultRelation.getDocumentId1(), intakeCsid);
313                         Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
314                         Assert.assertEquals(resultRelation.getDocumentId2(), collectionObjectCsid);
315                         System.out.println();
316                         i++;            
317                 }
318             }
319         }
320         
321
322         /*
323          * Private Methods
324          */
325
326 }