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.jboss.resteasy.client.ClientResponse;
45 import org.testng.Assert;
46 import org.testng.annotations.AfterSuite;
47 import org.testng.annotations.BeforeSuite;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * RelationServiceTest, carries out tests against a
55 * deployed and running Relation Service.
57 * $LastChangedRevision$
60 public class RelationServiceTest extends AbstractPoxServiceTestImpl<RelationsCommonList, RelationsCommon> {
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>();
68 private static final String UNINITIALIZED_CSID = "-1";
69 private static final String UNINITIALIZED_REFNAME = "null";
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;
77 private String personAuthCSID = null;
78 private String personAuthShortId = PERSON_AUTHORITY_NAME + System.currentTimeMillis();
79 private String personAuthDisplayName = personAuthShortId;
82 /** The SERVICE path component. */
83 final String SERVICE_PATH_COMPONENT = "relations";
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
89 protected CollectionSpaceClient getClientInstance() {
90 return new RelationClient();
93 protected Class<RelationsCommonList> getCommonListType() {
94 return (Class<RelationsCommonList>)RelationsCommonList.class;
98 * Creates the person refs as a precondition for running the tests in this class.
101 private void createPersonRefs() {
104 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
105 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
106 personAuthDisplayName, personAuthShortId, personAuthClient.getCommonPartName());
107 Response res = personAuthClient.create(multipart);
109 int statusCode = res.getStatus();
111 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
112 invalidStatusCodeMessage(testRequestType, statusCode));
113 Assert.assertEquals(statusCode, STATUS_CREATED);
114 personAuthCSID = extractId(res);
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);
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);
136 private void deletePersonRefs() {
138 // Delete all the persons we created for the tests
142 private String createPerson(String firstName, String surName, String shortId, String authRefName) {
143 String result = null;
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);
155 int statusCode = res.getStatus();
157 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
158 invalidStatusCodeMessage(testRequestType, statusCode));
159 Assert.assertEquals(statusCode, STATUS_CREATED);
160 result = extractId(res);
168 @Test(dataProvider="testName",
169 dependsOnMethods = {"create"})
170 public void createWithSelfRelation(String testName) throws Exception {
171 // Test result codes setup
172 setupCreateWithInvalidBody();
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);
184 statusCode = res.getStatus();
189 // Check the status code of the response: does it match
190 // the expected response(s)?
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);
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.
203 * This method is called by the base class method (test) readList().
208 protected void printList(String testName, RelationsCommonList list) {
209 List<RelationsCommonList.RelationListItem> items =
210 list.getRelationListItem();
212 for(RelationsCommonList.RelationListItem item : items){
213 logger.debug(testName + ": list-item[" + i + "] csid=" +
215 logger.debug(testName + ": list-item[" + i + "] URI=" +
221 // ---------------------------------------------------------------
222 // Utility methods used by tests above
223 // ---------------------------------------------------------------
225 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
228 public String getServicePathComponent() {
229 return SERVICE_PATH_COMPONENT;
232 private RelationsCommon createRelationsCommon(String identifier) {
233 RelationsCommon relationCommon = new RelationsCommon();
234 fillRelation(relationCommon, identifier);
235 return relationCommon;
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));
250 * Creates the relation instance.
252 * @param identifier the identifier
253 * @return the multipart output
255 private PoxPayloadOut createRelationInstance(String identifier) {
256 RelationsCommon relation = createRelationsCommon(identifier);
257 PoxPayloadOut result = createRelationInstance(relation);
262 * Fills the relation.
264 * @param relationCommon the relation
265 * @param identifier the identifier
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);
274 * Fills the relation.
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
283 private void fillRelation(RelationsCommon relationCommon,
284 String subjectCsid, String subjectDocumentType,
285 String objectCsid, String objectDocumentType,
287 String rtDisplayName) {
288 relationCommon.setSubjectCsid(subjectCsid);
289 relationCommon.setSubjectDocumentType(subjectDocumentType);
290 relationCommon.setObjectCsid(objectCsid);
291 relationCommon.setObjectDocumentType(objectDocumentType);
293 relationCommon.setRelationshipType(rt);
294 relationCommon.setPredicateDisplayName(rtDisplayName);
298 protected String getServiceName() {
299 return RelationClient.SERVICE_NAME;
303 public void CRUDTests(String testName) {
304 // TODO Auto-generated method stub
309 protected PoxPayloadOut createInstance(String commonPartName,
311 return createRelationInstance(identifier);
315 protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
316 RelationsCommon result = new RelationsCommon();
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());
329 protected void compareUpdatedInstances(RelationsCommon original,
330 RelationsCommon updated) throws Exception {
332 "Data in updated object did not match submitted data.";
334 "Data in updated object was not correctly computed.";
336 updated.getSubjectCsid(), original.getSubjectCsid(), msg);
338 updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
340 updated.getObjectCsid(), original.getObjectCsid(), msg);
342 updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
344 updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);