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