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.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
41 import org.collectionspace.services.client.CollectionObjectClient;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
44 import org.collectionspace.services.client.IntakeClient;
45 import org.collectionspace.services.intake.IntakesCommon;
47 import org.collectionspace.services.client.RelationClient;
48 import org.collectionspace.services.relation.RelationsCommon;
49 import org.collectionspace.services.relation.RelationsCommonList;
50 import org.collectionspace.services.relation.RelationshipType;
55 * @version $Revision:$
57 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
59 final Logger logger = LoggerFactory
60 .getLogger(RelationIntegrationTest.class);
62 // Get clients for the CollectionSpace services
64 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
65 private RelationClient relationClient = new RelationClient();
66 private IntakeClient intakeClient = new IntakeClient();
69 public void relateCollectionObjectToIntake() {
72 // First create a CollectionObject
74 CollectionobjectsCommon co = new CollectionobjectsCommon();
75 fillCollectionObject(co, createIdentifier());
77 // Next, create a part object
78 MultipartOutput multipart = new MultipartOutput();
79 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
80 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
82 // Make the create call and check the response
83 ClientResponse<Response> response = collectionObjectClient.create(multipart);
84 String collectionObjectCsid = null;
86 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
88 collectionObjectCsid = extractId(response);
90 response.releaseConnection();
94 // Next, create an Intake object
95 IntakesCommon intake = new IntakesCommon();
96 fillIntake(intake, createIdentifier());
97 // Create the a part object
98 multipart = new MultipartOutput();
99 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
100 commonPart.getHeaders().add("label", intakeClient.getCommonPartName());
102 // Make the call to create and check the response
103 response = intakeClient.create(multipart);
104 String intakeCsid = null;
106 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
107 intakeCsid = extractId(response);
109 response.releaseConnection();
112 // Lastly, relate the two entities, by creating a new relation object
113 RelationsCommon relation = new RelationsCommon();
114 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
115 intakeCsid, IntakesCommon.class.getSimpleName(),
116 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
117 // Create the part and fill it with the relation object
118 multipart = new MultipartOutput();
119 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
120 commonPart.getHeaders().add("label", relationClient.getCommonPartName());
122 // Make the call to crate
123 response = relationClient.create(multipart);
124 String relationCsid = null;
126 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
127 relationCsid = extractId(response);
129 response.releaseConnection();
133 // Now try to retrieve the Intake record of the CollectionObject.
135 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
136 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
137 collectionObjectCsid,
138 CollectionobjectsCommon.class.getSimpleName(),
141 IntakesCommon.class.getSimpleName());
142 RelationsCommonList relationList = null;
144 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
145 relationList = resultResponse.getEntity();
147 resultResponse.releaseConnection();
151 // Each relation returned in the list needs to match what we
154 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
155 Assert.assertFalse(relationListItems.isEmpty());
158 RelationsCommon resultRelation = null;
159 for(RelationsCommonList.RelationListItem listItem : relationListItems){
161 String foundCsid = listItem.getCsid();
162 ClientResponse<MultipartInput> multiPartResponse = null;
164 multiPartResponse = relationClient.read(foundCsid);
165 int responseStatus = multiPartResponse.getStatus();
166 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
167 MultipartInput input = (MultipartInput) multiPartResponse.getEntity();
168 resultRelation = (RelationsCommon) extractPart(input,
169 relationClient.getCommonPartName(),
170 RelationsCommon.class);
171 } catch (Exception e) {
174 multiPartResponse.releaseConnection();
177 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
178 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
179 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
180 System.out.println();