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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.Response;
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;
50 * RelationServiceTest, carries out tests against a
51 * deployed and running Relation Service.
53 * $LastChangedRevision$
56 public class RelationServiceTest extends AbstractPoxServiceTestImpl<RelationsCommonList, RelationsCommon> {
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>();
64 private static final String UNINITIALIZED_CSID = "-1";
65 private static final String UNINITIALIZED_REFNAME = "null";
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;
73 private String personAuthCSID = null;
74 private String personAuthShortId = PERSON_AUTHORITY_NAME + System.currentTimeMillis();
75 private String personAuthDisplayName = personAuthShortId;
78 /** The SERVICE path component. */
79 final String SERVICE_PATH_COMPONENT = "relations";
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85 protected CollectionSpaceClient getClientInstance() throws Exception {
86 return new RelationClient();
90 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
91 return new RelationClient(clientPropertiesFilename);
94 protected Class<RelationsCommonList> getCommonListType() {
95 return (Class<RelationsCommonList>)RelationsCommonList.class;
99 * Creates the person refs as a precondition for running the tests in this class.
103 private void createPersonRefs() throws Exception {
106 PersonClient personAuthClient = new PersonClient();
107 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
108 personAuthDisplayName, personAuthShortId, personAuthClient.getCommonPartName());
109 Response res = personAuthClient.create(multipart);
111 int statusCode = res.getStatus();
113 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
114 invalidStatusCodeMessage(testRequestType, statusCode));
115 Assert.assertEquals(statusCode, STATUS_CREATED);
116 personAuthCSID = extractId(res);
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);
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);
138 private void deletePersonRefs() throws Exception {
139 PersonClient personAuthClient = new PersonClient();
140 for (String csid:personIdsCreated) {
141 Response res = personAuthClient.deleteItem(personAuthCSID, csid);
143 int statusCode = res.getStatus();
145 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
146 invalidStatusCodeMessage(testRequestType, statusCode));
147 Assert.assertEquals(statusCode, this.STATUS_OK);
153 // Now delete the container (the parent)
155 Response res = personAuthClient.delete(personAuthCSID);
157 int statusCode = res.getStatus();
159 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
160 invalidStatusCodeMessage(testRequestType, statusCode));
161 Assert.assertEquals(statusCode, this.STATUS_OK);
168 private String createPerson(String firstName, String surName, String shortId, String authRefName) throws Exception {
169 String result = null;
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);
181 int statusCode = res.getStatus();
183 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(testRequestType, statusCode));
185 Assert.assertEquals(statusCode, STATUS_CREATED);
186 result = extractId(res);
194 @Test(dataProvider="testName",
195 dependsOnMethods = {"create"})
196 public void createWithSelfRelation(String testName) throws Exception {
197 // Test result codes setup
198 setupCreateWithInvalidBody();
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);
210 statusCode = res.getStatus();
215 // Check the status code of the response: does it match
216 // the expected response(s)?
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);
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.
229 * This method is called by the base class method (test) readList().
234 protected void printList(String testName, RelationsCommonList list) {
235 List<RelationsCommonList.RelationListItem> items =
236 list.getRelationListItem();
238 for(RelationsCommonList.RelationListItem item : items){
239 logger.debug(testName + ": list-item[" + i + "] csid=" +
241 logger.debug(testName + ": list-item[" + i + "] URI=" +
247 // ---------------------------------------------------------------
248 // Utility methods used by tests above
249 // ---------------------------------------------------------------
251 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
254 public String getServicePathComponent() {
255 return SERVICE_PATH_COMPONENT;
258 private RelationsCommon createRelationsCommon(String identifier) {
259 RelationsCommon relationCommon = new RelationsCommon();
260 fillRelation(relationCommon, identifier);
261 return relationCommon;
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));
276 * Creates the relation instance.
278 * @param identifier the identifier
279 * @return the multipart output
282 private PoxPayloadOut createRelationInstance(String identifier) throws Exception {
283 RelationsCommon relation = createRelationsCommon(identifier);
284 PoxPayloadOut result = createRelationInstance(relation);
289 * Fills the relation.
291 * @param relationCommon the relation
292 * @param identifier the identifier
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);
301 * Fills the relation.
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
310 private void fillRelation(RelationsCommon relationCommon,
311 String subjectCsid, String subjectDocumentType,
312 String objectCsid, String objectDocumentType,
314 String rtDisplayName) {
315 relationCommon.setSubjectCsid(subjectCsid);
316 relationCommon.setSubjectDocumentType(subjectDocumentType);
317 relationCommon.setObjectCsid(objectCsid);
318 relationCommon.setObjectDocumentType(objectDocumentType);
320 relationCommon.setRelationshipType(rt);
321 relationCommon.setPredicateDisplayName(rtDisplayName);
325 protected String getServiceName() {
326 return RelationClient.SERVICE_NAME;
330 public void CRUDTests(String testName) {
331 // TODO Auto-generated method stub
336 protected PoxPayloadOut createInstance(String commonPartName,
337 String identifier) throws Exception {
338 return createRelationInstance(identifier);
342 protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
343 RelationsCommon result = new RelationsCommon();
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());
356 protected void compareUpdatedInstances(RelationsCommon original,
357 RelationsCommon updated) throws Exception {
359 "Data in updated object did not match submitted data.";
361 "Data in updated object was not correctly computed.";
363 updated.getSubjectCsid(), original.getSubjectCsid(), msg);
365 updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
367 updated.getObjectCsid(), original.getObjectCsid(), msg);
369 updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
371 updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);