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