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.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;
43 import org.testng.Assert;
44 import org.testng.annotations.AfterSuite;
45 import org.testng.annotations.BeforeSuite;
46 import org.testng.annotations.Test;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * RelationServiceTest, carries out tests against a
52 * deployed and running Relation Service.
54 * $LastChangedRevision$
57 public class RelationServiceTest extends AbstractPoxServiceTestImpl<RelationsCommonList, RelationsCommon> {
60 private final String CLASS_NAME = RelationServiceTest.class.getName();
61 private final String PERSON_AUTHORITY_NAME = "TestPersonAuthForRelationTest";
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63 private List<String> personIdsCreated = new ArrayList<String>();
65 private static final String UNINITIALIZED_CSID = "-1";
66 private static final String UNINITIALIZED_REFNAME = "null";
68 private static final String PERSONS_DOCUMENT_TYPE = "Person";
69 private String samSubjectPersonCSID = UNINITIALIZED_CSID;
70 private String oliveObjectPersonCSID = UNINITIALIZED_REFNAME;
71 private String samSubjectRefName = UNINITIALIZED_CSID;
72 private String oliveObjectRefName = UNINITIALIZED_REFNAME;
74 private String personAuthCSID = null;
75 private String personAuthShortId = PERSON_AUTHORITY_NAME + System.currentTimeMillis();
76 private String personAuthDisplayName = personAuthShortId;
79 /** The SERVICE path component. */
80 final String SERVICE_PATH_COMPONENT = "relations";
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
87 return new RelationClient();
91 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
92 return new RelationClient(clientPropertiesFilename);
95 protected Class<RelationsCommonList> getCommonListType() {
96 return (Class<RelationsCommonList>)RelationsCommonList.class;
100 * Creates the person refs as a precondition for running the tests in this class.
103 private void createPersonRefs() {
106 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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() {
139 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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) {
169 String result = null;
171 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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) {
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
281 private PoxPayloadOut createRelationInstance(String identifier) {
282 RelationsCommon relation = createRelationsCommon(identifier);
283 PoxPayloadOut result = createRelationInstance(relation);
288 * Fills the relation.
290 * @param relationCommon the relation
291 * @param identifier the identifier
293 private void fillRelation(RelationsCommon relationCommon, String identifier) {
294 fillRelation(relationCommon, samSubjectPersonCSID, null, oliveObjectPersonCSID, null,
295 RelationshipType.COLLECTIONOBJECT_INTAKE.toString(),
296 RelationshipType.COLLECTIONOBJECT_INTAKE + ".displayName-" + identifier);
300 * Fills the relation.
302 * @param relationCommon the relation
303 * @param subjectCsid the subject document id
304 * @param subjectDocumentType the subject document type
305 * @param objectCsid the object document id
306 * @param objectDocumentType the object document type
309 private void fillRelation(RelationsCommon relationCommon,
310 String subjectCsid, String subjectDocumentType,
311 String objectCsid, String objectDocumentType,
313 String rtDisplayName) {
314 relationCommon.setSubjectCsid(subjectCsid);
315 relationCommon.setSubjectDocumentType(subjectDocumentType);
316 relationCommon.setObjectCsid(objectCsid);
317 relationCommon.setObjectDocumentType(objectDocumentType);
319 relationCommon.setRelationshipType(rt);
320 relationCommon.setPredicateDisplayName(rtDisplayName);
324 protected String getServiceName() {
325 return RelationClient.SERVICE_NAME;
329 public void CRUDTests(String testName) {
330 // TODO Auto-generated method stub
335 protected PoxPayloadOut createInstance(String commonPartName,
337 return createRelationInstance(identifier);
341 protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
342 RelationsCommon result = new RelationsCommon();
344 // Update the content of this resource, inverting subject and object
345 result.setSubjectCsid(relationCommon.getObjectCsid());
346 result.setSubjectDocumentType("Hooey"); // DocumentType changes should be ignored.
347 result.setObjectCsid(relationCommon.getSubjectCsid());
348 result.setObjectDocumentType("Fooey"); // DocumentType changes should be ignored.
349 result.setPredicateDisplayName("updated-" + relationCommon.getPredicateDisplayName());
355 protected void compareUpdatedInstances(RelationsCommon original,
356 RelationsCommon updated) throws Exception {
358 "Data in updated object did not match submitted data.";
360 "Data in updated object was not correctly computed.";
362 updated.getSubjectCsid(), original.getSubjectCsid(), msg);
364 updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
366 updated.getObjectCsid(), original.getObjectCsid(), msg);
368 updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
370 updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);