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.ItegrationTests.test;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.MultivaluedMap;
31 import javax.ws.rs.core.Response;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Marshaller;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
40 import org.apache.commons.httpclient.Header;
41 import org.apache.commons.httpclient.HttpClient;
42 import org.apache.commons.httpclient.HttpException;
43 import org.apache.commons.httpclient.HttpStatus;
44 import org.apache.commons.httpclient.methods.GetMethod;
45 import org.apache.commons.httpclient.methods.HeadMethod;
46 import org.apache.commons.httpclient.methods.OptionsMethod;
47 import org.apache.commons.httpclient.methods.TraceMethod;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import org.jboss.resteasy.client.ClientResponse;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
57 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
59 import org.collectionspace.services.client.TestServiceClient;
61 import org.collectionspace.services.CollectionObjectJAXBSchema;
62 import org.collectionspace.services.client.CollectionObjectClient;
63 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
64 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
66 import org.collectionspace.services.IntakeJAXBSchema;
67 import org.collectionspace.services.client.IntakeClient;
68 import org.collectionspace.services.intake.IntakesCommon;
69 import org.collectionspace.services.intake.IntakesCommonList;
71 import org.collectionspace.services.common.relation.RelationJAXBSchema;
72 import org.collectionspace.services.client.RelationClient;
73 import org.collectionspace.services.relation.RelationsCommon;
74 import org.collectionspace.services.relation.RelationsCommonList;
75 import org.collectionspace.services.relation.RelationshipType;
80 * @version $Revision:$
82 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
84 final Logger logger = LoggerFactory
85 .getLogger(RelationIntegrationTest.class);
87 // Get clients for the CollectionSpace services
89 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
90 private RelationClient relationClient = new RelationClient();
91 private IntakeClient intakeClient = new IntakeClient();
94 public void relateCollectionObjectToIntake() {
97 // First create a CollectionObject
99 CollectionobjectsCommon co = new CollectionobjectsCommon();
100 fillCollectionObject(co, createIdentifier());
102 // Next, create a part object
103 MultipartOutput multipart = new MultipartOutput();
104 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
105 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
106 // Make the create call and check the response
107 ClientResponse<Response> response = collectionObjectClient.create(multipart);
108 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
110 String collectionObjectCsid = extractId(response);
113 // Next, create an Intake object
114 IntakesCommon intake = new IntakesCommon();
115 fillIntake(intake, createIdentifier());
116 // Create the a part object
117 multipart = new MultipartOutput();
118 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
119 commonPart.getHeaders().add("label", intakeClient.getCommonPartName());
120 // Make the call to create and check the response
121 response = intakeClient.create(multipart);
122 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
123 String intakeCsid = extractId(response);
125 // Lastly, relate the two entities, by creating a new relation object
126 RelationsCommon relation = new RelationsCommon();
127 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
128 intakeCsid, IntakesCommon.class.getSimpleName(),
129 RelationshipType.COLLECTIONOBJECT_INTAKE);
130 // Create the part and fill it with the relation object
131 multipart = new MultipartOutput();
132 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
133 commonPart.getHeaders().add("label", relationClient.getCommonPartName());
134 // Make the call to crate
135 response = relationClient.create(multipart);
136 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
137 String relationCsid = extractId(response);
140 // Now try to retrieve the Intake record of the CollectionObject.
142 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
143 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList_SPO(collectionObjectCsid,
146 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
149 // Each relation returned in the list needs to match what we
152 RelationsCommonList relationList = resultResponse.getEntity();
153 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
154 Assert.assertFalse(relationListItems.isEmpty());
156 ClientResponse<RelationsCommon> resultRelationResponse;
157 RelationsCommon resultRelation = null;
159 for(RelationsCommonList.RelationListItem listItem : relationListItems){
161 String foundCsid = listItem.getCsid();
163 ClientResponse<MultipartInput> multiPartResponse = relationClient.read(foundCsid);
164 int responseStatus = multiPartResponse.getStatus();
165 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
166 MultipartInput input = (MultipartInput) multiPartResponse.getEntity();
167 resultRelation = (RelationsCommon) extractPart(input,
168 relationClient.getCommonPartName(),
169 RelationsCommon.class);
170 } catch (Exception e) {
173 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
174 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE);
175 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
176 System.out.println();