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