]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
feaabf021670119107f0c177d44cdc6361124b16
[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 © 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.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.PayloadInputPart;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.client.RelationClient;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.relation.RelationsCommon;
44 import org.collectionspace.services.relation.RelationsCommonList;
45 import org.collectionspace.services.relation.RelationshipType;
46
47 import org.jboss.resteasy.client.ClientResponse;
48
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.AfterSuite;
52 import org.testng.annotations.BeforeClass;
53 import org.testng.annotations.BeforeSuite;
54 import org.testng.annotations.Test;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * RelationServiceTest, carries out tests against a
61  * deployed and running Relation Service.
62  * 
63  * $LastChangedRevision$
64  * $LastChangedDate$
65  */
66 public class RelationServiceTest extends AbstractPoxServiceTestImpl<RelationsCommonList, RelationsCommon> {
67
68    /** The logger. */
69     private final String CLASS_NAME = RelationServiceTest.class.getName();
70     private final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
71     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
72     private List<String> personIdsCreated = new ArrayList<String>();
73     
74     private static final String UNINITIALIZED_CSID = "-1";
75     private static final String UNINITIALIZED_REFNAME = "null";
76     
77     private static final String PERSONS_DOCUMENT_TYPE = "Person";
78     private String samSubjectPersonCSID = UNINITIALIZED_CSID;
79     private String oliveObjectPersonCSID = UNINITIALIZED_REFNAME;
80     private String samSubjectRefName = UNINITIALIZED_CSID;
81     private String oliveObjectRefName = UNINITIALIZED_REFNAME;
82     
83     private String personAuthCSID = null;
84     private String personShortId = PERSON_AUTHORITY_NAME;
85     
86
87     /** The SERVICE path component. */
88     final String SERVICE_PATH_COMPONENT = "relations";
89     
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
92      */
93     @Override
94     protected CollectionSpaceClient getClientInstance() {
95         return new RelationClient();
96     }
97      
98     protected Class<RelationsCommonList> getCommonListType() {
99         return (Class<RelationsCommonList>)RelationsCommonList.class;
100     }
101         
102     /**
103      * Creates the person refs as a precondition for running the tests in this class.
104      */
105     @BeforeSuite
106     private void createPersonRefs() {
107         setupCreate();
108
109         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
110         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
111                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
112         ClientResponse<Response> res = personAuthClient.create(multipart);
113         int statusCode = res.getStatus();
114
115         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
116                 invalidStatusCodeMessage(testRequestType, statusCode));
117         Assert.assertEquals(statusCode, STATUS_CREATED);
118         personAuthCSID = extractId(res);
119
120         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
121
122         String csid = createPerson("Sam", "Subject", "samSubject", authRefName);
123         Assert.assertNotNull(csid);
124         samSubjectPersonCSID = csid;
125         samSubjectRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
126         Assert.assertNotNull(samSubjectRefName);
127         personIdsCreated.add(csid);
128
129         csid = createPerson("Olive", "Object", "oliveObject", authRefName);
130         Assert.assertNotNull(csid);
131         oliveObjectRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
132         oliveObjectPersonCSID = csid;
133         Assert.assertNotNull(oliveObjectRefName);
134         personIdsCreated.add(csid);
135     }
136     
137     @AfterSuite
138     private void deletePersonRefs() {
139         //
140         // Delete all the persons we created for the tests
141         //
142     }
143
144     private String createPerson(String firstName, String surName, String shortId, String authRefName) {
145         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
146         Map<String, String> personInfo = new HashMap<String, String>();
147         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
148         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
149         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
150         PoxPayloadOut multipart =
151                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
152                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
153         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
154         int statusCode = res.getStatus();
155
156         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
157                 invalidStatusCodeMessage(testRequestType, statusCode));
158         Assert.assertEquals(statusCode, STATUS_CREATED);
159         return extractId(res);
160     }    
161     
162     @Test(dataProvider="testName",
163             dependsOnMethods = {"create"})
164     public void createWithSelfRelation(String testName) throws Exception {
165         // Test result codes setup
166         setupCreateWithInvalidBody();
167
168         // Submit the request to the service and store the response.
169         RelationClient client = new RelationClient();
170         String identifier = createIdentifier();
171         RelationsCommon relationsCommon = createRelationsCommon(identifier);
172         // Make the subject ID equal to the object ID
173         relationsCommon.setSubjectCsid(relationsCommon.getObjectCsid());
174         PoxPayloadOut multipart = createRelationInstance(relationsCommon);
175         ClientResponse<Response> res = client.create(multipart);
176         int statusCode = res.getStatus();
177
178         // Check the status code of the response: does it match
179         // the expected response(s)?
180         //
181         // Does it fall within the set of valid status codes?
182         // Does it exactly match the expected status code?
183         if(logger.isDebugEnabled()){
184             logger.debug(testName + ": status = " + statusCode);
185         }
186         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
187                 invalidStatusCodeMessage(testRequestType, statusCode));
188         Assert.assertEquals(statusCode, STATUS_BAD_REQUEST);   //should be an error: same objectID and subjectID are not allowed by validator.
189     }
190        
191     /**
192      * This method is called by the base class method (test) readList().
193      * @param testName
194      * @param list
195      */
196     @Override
197     protected void printList(String testName, RelationsCommonList list) {
198         List<RelationsCommonList.RelationListItem> items =
199                 list.getRelationListItem();
200         int i = 0;
201         for(RelationsCommonList.RelationListItem item : items){
202             logger.debug(testName + ": list-item[" + i + "] csid=" +
203                     item.getCsid());
204             logger.debug(testName + ": list-item[" + i + "] URI=" +
205                     item.getUri());
206             i++;
207         }
208     }
209
210     // ---------------------------------------------------------------
211     // Utility methods used by tests above
212     // ---------------------------------------------------------------
213     /* (non-Javadoc)
214      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
215      */
216     @Override
217     public String getServicePathComponent() {
218         return SERVICE_PATH_COMPONENT;
219     }
220
221     private RelationsCommon createRelationsCommon(String identifier) {
222         RelationsCommon relationCommon = new RelationsCommon();
223         fillRelation(relationCommon, identifier);
224         return relationCommon;
225     }
226
227     private PoxPayloadOut createRelationInstance(RelationsCommon relation) {
228         PoxPayloadOut result = new PoxPayloadOut(this.getServicePathComponent());
229         PayloadOutputPart commonPart =
230                 result.addPart(new RelationClient().getCommonPartName(), relation);
231         if(logger.isDebugEnabled()){
232           logger.debug("to be created, relation common");
233           logger.debug(objectAsXmlString(relation, RelationsCommon.class));
234         }
235         return result;
236     }
237     
238     /**
239      * Creates the relation instance.
240      *
241      * @param identifier the identifier
242      * @return the multipart output
243      */
244     private PoxPayloadOut createRelationInstance(String identifier) {
245         RelationsCommon relation = createRelationsCommon(identifier);
246         PoxPayloadOut result = createRelationInstance(relation);
247         return result;
248     }
249
250     /**
251      * Fills the relation.
252      *
253      * @param relationCommon the relation
254      * @param identifier the identifier
255      */
256     private void fillRelation(RelationsCommon relationCommon, String identifier) {
257         fillRelation(relationCommon, samSubjectPersonCSID, null, oliveObjectPersonCSID, null,
258                 RelationshipType.COLLECTIONOBJECT_INTAKE.toString(),
259                 RelationshipType.COLLECTIONOBJECT_INTAKE + ".displayName-" + identifier);
260     }
261
262     /**
263      * Fills the relation.
264      *
265      * @param relationCommon the relation
266      * @param subjectCsid the subject document id
267      * @param subjectDocumentType the subject document type
268      * @param objectCsid the object document id
269      * @param objectDocumentType the object document type
270      * @param rt the rt
271      */
272     private void fillRelation(RelationsCommon relationCommon,
273             String subjectCsid, String subjectDocumentType,
274             String objectCsid, String objectDocumentType,
275             String rt,
276             String rtDisplayName) {
277         relationCommon.setSubjectCsid(subjectCsid);
278         relationCommon.setSubjectDocumentType(subjectDocumentType);
279         relationCommon.setObjectCsid(objectCsid);
280         relationCommon.setObjectDocumentType(objectDocumentType);
281
282         relationCommon.setRelationshipType(rt);
283         relationCommon.setPredicateDisplayName(rtDisplayName);
284     }
285
286         @Override
287         protected String getServiceName() {
288                 return RelationClient.SERVICE_NAME;
289         }
290
291         @Override
292         public void CRUDTests(String testName) {
293                 // TODO Auto-generated method stub
294                 
295         }
296
297         @Override
298         protected PoxPayloadOut createInstance(String commonPartName,
299                         String identifier) {
300                 return createRelationInstance(identifier);
301         }
302
303         @Override
304         protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
305                 RelationsCommon result = new RelationsCommon();
306                         
307         // Update the content of this resource, inverting subject and object
308         result.setSubjectCsid(relationCommon.getObjectCsid());
309         result.setSubjectDocumentType("Hooey"); // DocumentType changes should be ignored.
310         result.setObjectCsid(relationCommon.getSubjectCsid());
311         result.setObjectDocumentType("Fooey"); // DocumentType changes should be ignored.
312         result.setPredicateDisplayName("updated-" + relationCommon.getPredicateDisplayName());
313                 
314                 return result;
315         }
316
317         @Override
318         protected void compareUpdatedInstances(RelationsCommon original,
319                         RelationsCommon updated) throws Exception {
320         final String msg =
321                 "Data in updated object did not match submitted data.";
322         final String msg2 =
323                 "Data in updated object was not correctly computed.";
324         Assert.assertEquals(
325                         updated.getSubjectCsid(), original.getSubjectCsid(), msg);
326         Assert.assertEquals(
327                         updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
328         Assert.assertEquals(
329                         updated.getObjectCsid(), original.getObjectCsid(), msg);
330         Assert.assertEquals(
331                         updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
332         Assert.assertEquals(
333                         updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);
334         }
335 }