]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
78cde6fe7abd13aed84127de1603ce8914a2757b
[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.List;
26
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
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;
40
41 import org.collectionspace.services.client.CollectionObjectClient;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43
44 import org.collectionspace.services.client.IntakeClient;
45 import org.collectionspace.services.intake.IntakesCommon;
46
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;
51
52 /**
53  * A ServiceTest.
54  * 
55  * @version $Revision:$
56  */
57 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
58
59         final Logger logger = LoggerFactory
60                         .getLogger(RelationIntegrationTest.class);
61         //
62         // Get clients for the CollectionSpace services
63         //
64         private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
65         private RelationClient relationClient = new RelationClient();
66         private IntakeClient intakeClient = new IntakeClient();
67         
68         @Test
69         public void relateCollectionObjectToIntake() {
70                 
71                 //
72                 // First create a CollectionObject
73                 //
74                 CollectionobjectsCommon co = new CollectionobjectsCommon();
75                 fillCollectionObject(co, createIdentifier());
76                 
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());
81                 
82                 // Make the create call and check the response
83                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
84                 String collectionObjectCsid = null;
85                 try {
86                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED
87                                         .getStatusCode());
88                         collectionObjectCsid = extractId(response);
89                 } finally {
90                         response.releaseConnection();
91                 }
92             
93             
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());
101
102             // Make the call to create and check the response
103             response = intakeClient.create(multipart);
104             String intakeCsid = null;
105             try {
106                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
107                     intakeCsid = extractId(response);
108             } finally {
109                 response.releaseConnection();
110             }
111             
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());
121
122             // Make the call to crate
123             response = relationClient.create(multipart);
124             String relationCsid = null;
125             try {
126                     Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
127                     relationCsid = extractId(response);
128             } finally {
129                 response.releaseConnection();
130             }
131             
132             //
133             // Now try to retrieve the Intake record of the CollectionObject.
134             //
135             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
136             ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
137                         collectionObjectCsid,
138                         CollectionobjectsCommon.class.getSimpleName(),
139                         predicate,
140                         intakeCsid,
141                         IntakesCommon.class.getSimpleName());
142         RelationsCommonList relationList = null;
143             try {
144                 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
145                 relationList = resultResponse.getEntity();
146             } finally {
147                 resultResponse.releaseConnection();
148             }
149             
150             //
151             // Each relation returned in the list needs to match what we
152             // requested.
153             //
154         List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
155         Assert.assertFalse(relationListItems.isEmpty());
156         
157         int i = 0;
158         RelationsCommon resultRelation = null;
159         for(RelationsCommonList.RelationListItem listItem : relationListItems){
160                 
161                 String foundCsid = listItem.getCsid();
162                 ClientResponse<MultipartInput> multiPartResponse = null;
163                 try {
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) {
172                         e.printStackTrace();
173                 } finally {
174                         multiPartResponse.releaseConnection();
175                 }
176                 
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();
181                 i++;            
182         }
183         }
184
185         /*
186          * Private Methods
187          */
188
189 }