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