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