]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
768e8f3bbe8253aaba26caf62e43bacddfb59c70
[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.PersonClient;
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 import org.testng.Assert;
43 import org.testng.annotations.AfterSuite;
44 import org.testng.annotations.BeforeSuite;
45 import org.testng.annotations.Test;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * RelationServiceTest, carries out tests against a
51  * deployed and running Relation Service.
52  * 
53  * $LastChangedRevision$
54  * $LastChangedDate$
55  */
56 public class RelationServiceTest extends AbstractPoxServiceTestImpl<RelationsCommonList, RelationsCommon> {
57
58    /** The logger. */
59     private final String CLASS_NAME = RelationServiceTest.class.getName();
60     private final String PERSON_AUTHORITY_NAME = "TestPersonAuthForRelationTest";
61     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
62     private List<String> personIdsCreated = new ArrayList<String>();
63     
64     private static final String UNINITIALIZED_CSID = "-1";
65     private static final String UNINITIALIZED_REFNAME = "null";
66     
67     private static final String PERSONS_DOCUMENT_TYPE = "Person";
68     private String samSubjectPersonCSID = UNINITIALIZED_CSID;
69     private String oliveObjectPersonCSID = UNINITIALIZED_REFNAME;
70     private String samSubjectRefName = UNINITIALIZED_CSID;
71     private String oliveObjectRefName = UNINITIALIZED_REFNAME;
72     
73     private String personAuthCSID = null;
74     private String personAuthShortId = PERSON_AUTHORITY_NAME + System.currentTimeMillis();
75     private String personAuthDisplayName = personAuthShortId;
76     
77
78     /** The SERVICE path component. */
79     final String SERVICE_PATH_COMPONENT = "relations";
80     
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
83      */
84     @Override
85     protected CollectionSpaceClient getClientInstance() throws Exception {
86         return new RelationClient();
87     }
88
89         @Override
90         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
91         return new RelationClient(clientPropertiesFilename);
92         }
93      
94     protected Class<RelationsCommonList> getCommonListType() {
95         return (Class<RelationsCommonList>)RelationsCommonList.class;
96     }
97         
98     /**
99      * Creates the person refs as a precondition for running the tests in this class.
100      * @throws Exception 
101      */
102     @BeforeSuite
103     private void createPersonRefs() throws Exception {
104         setupCreate();
105
106         PersonClient personAuthClient = new PersonClient();
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() throws Exception {
139         PersonClient personAuthClient = new PersonClient();
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) throws Exception {
169         String result = null;
170         
171         PersonClient personAuthClient = new PersonClient();
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) throws Exception {
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      * @throws Exception 
281      */
282     private PoxPayloadOut createRelationInstance(String identifier) throws Exception {
283         RelationsCommon relation = createRelationsCommon(identifier);
284         PoxPayloadOut result = createRelationInstance(relation);
285         return result;
286     }
287
288     /**
289      * Fills the relation.
290      *
291      * @param relationCommon the relation
292      * @param identifier the identifier
293      */
294     private void fillRelation(RelationsCommon relationCommon, String identifier) {
295         fillRelation(relationCommon, samSubjectPersonCSID, null, oliveObjectPersonCSID, null,
296                 RelationshipType.COLLECTIONOBJECT_INTAKE.toString(),
297                 RelationshipType.COLLECTIONOBJECT_INTAKE + ".displayName-" + identifier);
298     }
299
300     /**
301      * Fills the relation.
302      *
303      * @param relationCommon the relation
304      * @param subjectCsid the subject document id
305      * @param subjectDocumentType the subject document type
306      * @param objectCsid the object document id
307      * @param objectDocumentType the object document type
308      * @param rt the rt
309      */
310     private void fillRelation(RelationsCommon relationCommon,
311             String subjectCsid, String subjectDocumentType,
312             String objectCsid, String objectDocumentType,
313             String rt,
314             String rtDisplayName) {
315         relationCommon.setSubjectCsid(subjectCsid);
316         relationCommon.setSubjectDocumentType(subjectDocumentType);
317         relationCommon.setObjectCsid(objectCsid);
318         relationCommon.setObjectDocumentType(objectDocumentType);
319
320         relationCommon.setRelationshipType(rt);
321         relationCommon.setPredicateDisplayName(rtDisplayName);
322     }
323
324         @Override
325         protected String getServiceName() {
326                 return RelationClient.SERVICE_NAME;
327         }
328
329         @Override
330         public void CRUDTests(String testName) {
331                 // TODO Auto-generated method stub
332                 
333         }
334
335         @Override
336         protected PoxPayloadOut createInstance(String commonPartName,
337                         String identifier) throws Exception {
338                 return createRelationInstance(identifier);
339         }
340
341         @Override
342         protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
343                 RelationsCommon result = new RelationsCommon();
344                         
345         // Update the content of this resource, inverting subject and object
346         result.setSubjectCsid(relationCommon.getObjectCsid());
347         result.setSubjectDocumentType("Hooey"); // DocumentType changes should be ignored.
348         result.setObjectCsid(relationCommon.getSubjectCsid());
349         result.setObjectDocumentType("Fooey"); // DocumentType changes should be ignored.
350         result.setPredicateDisplayName("updated-" + relationCommon.getPredicateDisplayName());
351                 
352                 return result;
353         }
354
355         @Override
356         protected void compareUpdatedInstances(RelationsCommon original,
357                         RelationsCommon updated) throws Exception {
358         final String msg =
359                 "Data in updated object did not match submitted data.";
360         final String msg2 =
361                 "Data in updated object was not correctly computed.";
362         Assert.assertEquals(
363                         updated.getSubjectCsid(), original.getSubjectCsid(), msg);
364         Assert.assertEquals(
365                         updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
366         Assert.assertEquals(
367                         updated.getObjectCsid(), original.getObjectCsid(), msg);
368         Assert.assertEquals(
369                         updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
370         Assert.assertEquals(
371                         updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);
372         }
373 }