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