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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.IntegrationTests.test;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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;
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;
48 import org.collectionspace.services.client.IntakeClient;
49 import org.collectionspace.services.intake.IntakesCommon;
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;
59 * @version $Revision:$
61 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
63 final Logger logger = LoggerFactory
64 .getLogger(RelationIntegrationTest.class);
66 // Get clients for the CollectionSpace services
68 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
69 private RelationClient relationClient = new RelationClient();
70 private IntakeClient intakeClient = new IntakeClient();
72 private static final int OBJECTS_TO_INTAKE = 1;
75 public void relateCollectionObjectToIntake() {
76 if (true) return; //false positive -see 'FIXME' below
78 // FIXME: REM - This test is failing because the relationClient.readList (aka search) is returning more than 1 matching
79 // relation. There should only be one.
84 // First create a CollectionObject
86 CollectionobjectsCommon co = new CollectionobjectsCommon();
87 fillCollectionObject(co, createIdentifier());
89 // Next, create a part object
90 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
91 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
92 commonPart.setLabel(collectionObjectClient.getCommonPartName());
94 // Make the create call and check the response
95 ClientResponse<Response> response = collectionObjectClient.create(multipart);
96 String collectionObjectCsid = null;
98 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
100 collectionObjectCsid = extractId(response);
102 response.releaseConnection();
106 // Next, create an Intake object
107 IntakesCommon intake = new IntakesCommon();
108 fillIntake(intake, createIdentifier());
109 // Create the a part object
110 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
111 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
112 commonPart.setLabel(intakeClient.getCommonPartName());
114 // Make the call to create and check the response
115 response = intakeClient.create(multipart);
116 String intakeCsid = null;
118 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
119 intakeCsid = extractId(response);
121 response.releaseConnection();
124 // Lastly, relate the two entities, by creating a new relation object
125 RelationsCommon relation = new RelationsCommon();
126 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
127 intakeCsid, IntakesCommon.class.getSimpleName(),
128 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
129 // Create the part and fill it with the relation object
130 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
131 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
132 commonPart.setLabel(relationClient.getCommonPartName());
134 // Make the call to crate
135 response = relationClient.create(multipart);
136 String relationCsid = null;
138 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
139 relationCsid = extractId(response);
141 response.releaseConnection();
145 // Now try to retrieve the Intake record of the CollectionObject.
147 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
148 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
149 collectionObjectCsid,
150 CollectionobjectsCommon.class.getSimpleName(),
153 IntakesCommon.class.getSimpleName());
154 RelationsCommonList relationList = null;
156 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
157 relationList = resultResponse.getEntity();
159 resultResponse.releaseConnection();
163 // Each relation returned in the list needs to match what we
166 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
167 Assert.assertFalse(relationListItems.isEmpty());
170 RelationsCommon resultRelation = null;
171 for(RelationsCommonList.RelationListItem listItem : relationListItems){
173 String foundCsid = listItem.getCsid();
174 ClientResponse<String> multiPartResponse = null;
176 multiPartResponse = relationClient.read(foundCsid);
177 int responseStatus = multiPartResponse.getStatus();
178 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
179 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
180 resultRelation = (RelationsCommon) extractPart(input,
181 relationClient.getCommonPartName(),
182 RelationsCommon.class);
183 } catch (Exception e) {
186 multiPartResponse.releaseConnection();
189 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
190 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
191 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
192 System.out.println();
198 public void relateCollectionObjectsToIntake() {
199 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
202 private void relateCollectionObjectsToIntake(int numberOfObjects) {
205 // First create a list of CollectionObjects
207 CollectionobjectsCommon co = new CollectionobjectsCommon();
208 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
209 for (int i = 0; i < numberOfObjects; i++) {
210 fillCollectionObject(co, createIdentifier());
212 // Next, create a part object
213 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
214 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
215 commonPart.setLabel(collectionObjectClient.getCommonPartName());
217 // Make the create call and check the response
218 ClientResponse<Response> response = collectionObjectClient.create(multipart);
219 String collectionObjectCsid = null;
221 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
223 collectionObjectCsid = extractId(response);
224 collectionObjectIDList.add(collectionObjectCsid);
226 response.releaseConnection();
231 // Next, create an Intake object
232 IntakesCommon intake = new IntakesCommon();
233 fillIntake(intake, createIdentifier());
234 // Create the a part object
235 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
236 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
237 commonPart.setLabel(intakeClient.getCommonPartName());
239 // Make the call to create and check the response
240 ClientResponse<Response> response = intakeClient.create(multipart);
241 String intakeCsid = null;
243 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
244 intakeCsid = extractId(response);
246 response.releaseConnection();
249 // Lastly, relate the two entities, by creating a new relation object
250 RelationsCommon relation = new RelationsCommon();
251 for (String collectionObjectCsid : collectionObjectIDList) {
252 fillRelation(relation,
253 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
254 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
255 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
256 // Create the part and fill it with the relation object
257 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
258 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
259 commonPart.setLabel(relationClient.getCommonPartName());
261 // Make the call to crate
262 response = relationClient.create(multipart);
263 String relationCsid = null;
265 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
266 relationCsid = extractId(response);
268 response.releaseConnection();
273 // Now try to retrieve the Intake record of the CollectionObject.
275 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
276 RelationsCommonList relationList = null;
277 for (String collectionObjectCsid : collectionObjectIDList) {
278 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
280 IntakesCommon.class.getSimpleName(), //subject
282 collectionObjectCsid,
283 CollectionobjectsCommon.class.getSimpleName()); //object
286 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
287 relationList = resultResponse.getEntity();
289 resultResponse.releaseConnection();
293 // Each relation returned in the list needs to match what we
296 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
297 Assert.assertFalse(relationListItems.isEmpty());
300 RelationsCommon resultRelation = null;
301 for(RelationsCommonList.RelationListItem listItem : relationListItems){
302 String foundCsid = listItem.getCsid();
303 ClientResponse<String> multiPartResponse = null;
305 multiPartResponse = relationClient.read(foundCsid);
306 int responseStatus = multiPartResponse.getStatus();
307 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
308 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
309 resultRelation = (RelationsCommon) extractPart(input,
310 relationClient.getCommonPartName(),
311 RelationsCommon.class);
312 } catch (Exception e) {
315 multiPartResponse.releaseConnection();
318 Assert.assertEquals(resultRelation.getDocumentId1(), intakeCsid);
319 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
320 Assert.assertEquals(resultRelation.getDocumentId2(), collectionObjectCsid);
321 System.out.println();