]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
57d0e277bef1c3a3938351c0431a29e1ddf69b66
[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.ItegrationTests.test;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28
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;
34
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39
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;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
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;
58
59 import org.collectionspace.services.client.TestServiceClient;
60
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;
65
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;
70
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;
76
77 /**
78  * A ServiceTest.
79  * 
80  * @version $Revision:$
81  */
82 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
83
84         final Logger logger = LoggerFactory
85                         .getLogger(RelationIntegrationTest.class);
86         //
87         // Get clients for the CollectionSpace services
88         //
89         private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
90         private RelationClient relationClient = new RelationClient();
91         private IntakeClient intakeClient = new IntakeClient();
92         
93         @Test
94         public void relateCollectionObjectToIntake() {
95                 
96                 //
97                 // First create a CollectionObject
98                 //
99                 CollectionobjectsCommon co = new CollectionobjectsCommon();
100                 fillCollectionObject(co, createIdentifier());
101                 
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
109                                 .getStatusCode());
110                 String collectionObjectCsid = extractId(response);
111             
112             
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);
124             
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);
138             
139             //
140             // Now try to retrieve the Intake record of the CollectionObject.
141             //
142             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
143             ClientResponse<RelationsCommonList> resultResponse = relationClient.readList_SPO(collectionObjectCsid,
144                         predicate,
145                         intakeCsid);
146             Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
147             
148             //
149             // Each relation returned in the list needs to match what we
150             // requested.
151             //
152         RelationsCommonList relationList = resultResponse.getEntity();        
153         List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
154         Assert.assertFalse(relationListItems.isEmpty());
155         
156         ClientResponse<RelationsCommon> resultRelationResponse;
157         RelationsCommon resultRelation = null;
158         int i = 0;
159         for(RelationsCommonList.RelationListItem listItem : relationListItems){
160                 
161                 String foundCsid = listItem.getCsid();
162                 try {
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) {
171                         e.printStackTrace();
172                 }
173                 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
174                 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE);
175                 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
176             System.out.println();
177                 i++;            
178         }
179         }
180
181         /*
182          * Private Methods
183          */
184
185 }