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 = "TestPersonAuth";
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 personShortId = PERSON_AUTHORITY_NAME;
81 /** The SERVICE path component. */
82 final String SERVICE_PATH_COMPONENT = "relations";
85 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88 protected CollectionSpaceClient getClientInstance() {
89 return new RelationClient();
92 protected Class<RelationsCommonList> getCommonListType() {
93 return (Class<RelationsCommonList>)RelationsCommonList.class;
97 * Creates the person refs as a precondition for running the tests in this class.
100 private void createPersonRefs() {
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);
108 int statusCode = res.getStatus();
110 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
111 invalidStatusCodeMessage(testRequestType, statusCode));
112 Assert.assertEquals(statusCode, STATUS_CREATED);
113 personAuthCSID = extractId(res);
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);
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);
135 private void deletePersonRefs() {
137 // Delete all the persons we created for the tests
141 private String createPerson(String firstName, String surName, String shortId, String authRefName) {
142 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
143 Map<String, String> personInfo = new HashMap<String, String>();
144 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
145 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
146 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
147 PoxPayloadOut multipart =
148 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
149 authRefName, personInfo, null, personAuthClient.getItemCommonPartName());
150 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
151 int statusCode = res.getStatus();
153 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
154 invalidStatusCodeMessage(testRequestType, statusCode));
155 Assert.assertEquals(statusCode, STATUS_CREATED);
156 return extractId(res);
159 @Test(dataProvider="testName",
160 dependsOnMethods = {"create"})
161 public void createWithSelfRelation(String testName) throws Exception {
162 // Test result codes setup
163 setupCreateWithInvalidBody();
165 // Submit the request to the service and store the response.
166 RelationClient client = new RelationClient();
167 String identifier = createIdentifier();
168 RelationsCommon relationsCommon = createRelationsCommon(identifier);
169 // Make the subject ID equal to the object ID
170 relationsCommon.setSubjectCsid(relationsCommon.getObjectCsid());
171 PoxPayloadOut multipart = createRelationInstance(relationsCommon);
172 Response res = client.create(multipart);
175 statusCode = res.getStatus();
180 // Check the status code of the response: does it match
181 // the expected response(s)?
183 // Does it fall within the set of valid status codes?
184 // Does it exactly match the expected status code?
185 if(logger.isDebugEnabled()){
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(testRequestType, statusCode));
190 Assert.assertEquals(statusCode, STATUS_BAD_REQUEST); //should be an error: same objectID and subjectID are not allowed by validator.
194 * This method is called by the base class method (test) readList().
199 protected void printList(String testName, RelationsCommonList list) {
200 List<RelationsCommonList.RelationListItem> items =
201 list.getRelationListItem();
203 for(RelationsCommonList.RelationListItem item : items){
204 logger.debug(testName + ": list-item[" + i + "] csid=" +
206 logger.debug(testName + ": list-item[" + i + "] URI=" +
212 // ---------------------------------------------------------------
213 // Utility methods used by tests above
214 // ---------------------------------------------------------------
216 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
219 public String getServicePathComponent() {
220 return SERVICE_PATH_COMPONENT;
223 private RelationsCommon createRelationsCommon(String identifier) {
224 RelationsCommon relationCommon = new RelationsCommon();
225 fillRelation(relationCommon, identifier);
226 return relationCommon;
229 private PoxPayloadOut createRelationInstance(RelationsCommon relation) {
230 PoxPayloadOut result = new PoxPayloadOut(this.getServicePathComponent());
231 PayloadOutputPart commonPart =
232 result.addPart(new RelationClient().getCommonPartName(), relation);
233 if(logger.isDebugEnabled()){
234 logger.debug("to be created, relation common");
235 logger.debug(objectAsXmlString(relation, RelationsCommon.class));
241 * Creates the relation instance.
243 * @param identifier the identifier
244 * @return the multipart output
246 private PoxPayloadOut createRelationInstance(String identifier) {
247 RelationsCommon relation = createRelationsCommon(identifier);
248 PoxPayloadOut result = createRelationInstance(relation);
253 * Fills the relation.
255 * @param relationCommon the relation
256 * @param identifier the identifier
258 private void fillRelation(RelationsCommon relationCommon, String identifier) {
259 fillRelation(relationCommon, samSubjectPersonCSID, null, oliveObjectPersonCSID, null,
260 RelationshipType.COLLECTIONOBJECT_INTAKE.toString(),
261 RelationshipType.COLLECTIONOBJECT_INTAKE + ".displayName-" + identifier);
265 * Fills the relation.
267 * @param relationCommon the relation
268 * @param subjectCsid the subject document id
269 * @param subjectDocumentType the subject document type
270 * @param objectCsid the object document id
271 * @param objectDocumentType the object document type
274 private void fillRelation(RelationsCommon relationCommon,
275 String subjectCsid, String subjectDocumentType,
276 String objectCsid, String objectDocumentType,
278 String rtDisplayName) {
279 relationCommon.setSubjectCsid(subjectCsid);
280 relationCommon.setSubjectDocumentType(subjectDocumentType);
281 relationCommon.setObjectCsid(objectCsid);
282 relationCommon.setObjectDocumentType(objectDocumentType);
284 relationCommon.setRelationshipType(rt);
285 relationCommon.setPredicateDisplayName(rtDisplayName);
289 protected String getServiceName() {
290 return RelationClient.SERVICE_NAME;
294 public void CRUDTests(String testName) {
295 // TODO Auto-generated method stub
300 protected PoxPayloadOut createInstance(String commonPartName,
302 return createRelationInstance(identifier);
306 protected RelationsCommon updateInstance(RelationsCommon relationCommon) {
307 RelationsCommon result = new RelationsCommon();
309 // Update the content of this resource, inverting subject and object
310 result.setSubjectCsid(relationCommon.getObjectCsid());
311 result.setSubjectDocumentType("Hooey"); // DocumentType changes should be ignored.
312 result.setObjectCsid(relationCommon.getSubjectCsid());
313 result.setObjectDocumentType("Fooey"); // DocumentType changes should be ignored.
314 result.setPredicateDisplayName("updated-" + relationCommon.getPredicateDisplayName());
320 protected void compareUpdatedInstances(RelationsCommon original,
321 RelationsCommon updated) throws Exception {
323 "Data in updated object did not match submitted data.";
325 "Data in updated object was not correctly computed.";
327 updated.getSubjectCsid(), original.getSubjectCsid(), msg);
329 updated.getSubjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
331 updated.getObjectCsid(), original.getObjectCsid(), msg);
333 updated.getObjectDocumentType(), PERSONS_DOCUMENT_TYPE, msg2); // DocumentType changes should have been ignored.
335 updated.getPredicateDisplayName(), original.getPredicateDisplayName(), msg);