]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
853141a767654cdbc7b3893241b4cd48f73755b3
[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 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.Marshaller;
32
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37
38 import org.apache.commons.httpclient.Header;
39 import org.apache.commons.httpclient.HttpClient;
40 import org.apache.commons.httpclient.HttpException;
41 import org.apache.commons.httpclient.HttpStatus;
42 import org.apache.commons.httpclient.methods.GetMethod;
43 import org.apache.commons.httpclient.methods.HeadMethod;
44 import org.apache.commons.httpclient.methods.OptionsMethod;
45 import org.apache.commons.httpclient.methods.TraceMethod;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import org.jboss.resteasy.client.ClientResponse;
53
54 import org.collectionspace.services.client.TestServiceClient;
55
56 import org.collectionspace.services.CollectionObjectJAXBSchema;
57 import org.collectionspace.services.client.CollectionObjectClient;
58 import org.collectionspace.services.collectionobject.CollectionObject;
59 import org.collectionspace.services.collectionobject.CollectionObjectList;
60
61 import org.collectionspace.services.IntakeJAXBSchema;
62 import org.collectionspace.services.client.IntakeClient;
63 import org.collectionspace.services.intake.Intake;
64 import org.collectionspace.services.intake.IntakeList;
65
66 import org.collectionspace.services.common.relation.RelationJAXBSchema;
67 import org.collectionspace.services.client.RelationClient;
68 import org.collectionspace.services.relation.Relation;
69 import org.collectionspace.services.relation.RelationList;
70 import org.collectionspace.services.relation.RelationshipType;
71
72 /**
73  * A ServiceTest.
74  * 
75  * @version $Revision:$
76  */
77 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
78
79         final Logger logger = LoggerFactory
80                         .getLogger(RelationIntegrationTest.class);
81         //
82         // Get clients for the CollectionSpace services
83         //
84         private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
85         private RelationClient relationClient = new RelationClient();
86         private IntakeClient intakeClient = new IntakeClient();
87         
88         @Test
89         public void relateCollectionObjectToIntake() {
90                 
91                 //
92                 // First create a CollectionObject
93                 //
94                 CollectionObject co = new CollectionObject();
95                 fillCollectionObject(co, createIdentifier());
96             ClientResponse<Response> coResponse = collectionObjectClient.create(co);
97             Assert.assertEquals(coResponse.getStatus(), Response.Status.CREATED.getStatusCode());
98             String collectionObjectCsid = extractId(coResponse);
99             
100             // Next, create an Intake record
101             Intake intake = new Intake();
102             fillIntake(intake, createIdentifier());
103             ClientResponse<Response> intakeResponse = intakeClient.create(intake);
104             Assert.assertEquals(intakeResponse.getStatus(), Response.Status.CREATED.getStatusCode());
105             String intakeCsid = extractId(intakeResponse);
106             
107             // Lastly, relate the two entities
108             Relation relation = new Relation();
109             fillRelation(relation, collectionObjectCsid, CollectionObject.class.getSimpleName(),
110                         intakeCsid, Intake.class.getSimpleName(),
111                         RelationshipType.COLLECTIONOBJECT_INTAKE);
112             ClientResponse<Response> relationResponse = relationClient.create(relation); 
113             Assert.assertEquals(relationResponse.getStatus(), Response.Status.CREATED.getStatusCode());
114             String relationCsid = extractId(relationResponse);
115             
116             //
117             // Now try to retrieve the Intake record of the CollectionObject.
118             //
119             String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
120             ClientResponse<RelationList> resultResponse = relationClient.readList_SPO(collectionObjectCsid,
121                         predicate,
122                         intakeCsid);
123             
124             //
125             // Each relation returned in the list needs to match what we
126             // requested.
127             //
128         RelationList relationList = resultResponse.getEntity();
129         List<RelationList.RelationListItem> relationListItems = relationList.getRelationListItem();
130         ClientResponse<Relation> resultRelationResponse;
131         Relation resultRelation = null;
132         int i = 0;
133         for(RelationList.RelationListItem listItem : relationListItems){
134                 
135                 String foundCsid = listItem.getCsid();
136                 try {
137                         resultRelationResponse = relationClient.read(foundCsid);
138                         resultRelation = resultRelationResponse.getEntity();
139                 } catch (Exception e) {
140                         e.printStackTrace();
141                 }
142                 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
143                 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE);
144                 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
145             System.out.println();
146                 i++;            
147         }
148         }
149
150         /*
151          * Private Methods
152          */
153
154 }