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.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.IntakeClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
38 import org.collectionspace.services.intake.IntakesCommon;
39 import org.collectionspace.services.intake.IntakesCommonList;
41 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * PersonAuthRefDocsTest, carries out tests against a
55 * deployed and running Person Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class PersonAuthRefDocsTest extends BaseServiceTest {
62 private final Logger logger =
63 LoggerFactory.getLogger(PersonAuthRefDocsTest.class);
65 // Instance variables specific to this test.
66 final String SERVICE_PATH_COMPONENT = "intakes";
67 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68 private String knownIntakeId = null;
69 private List<String> intakeIdsCreated = new ArrayList();
70 private List<String> personIdsCreated = new ArrayList();
71 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
72 private int OK_STATUS = Response.Status.OK.getStatusCode();
73 private String personAuthCSID = null;
74 private String currentOwnerPersonCSID = null;
75 private String currentOwnerRefName = null;
76 private String depositorRefName = null;
77 private String conditionCheckAssesorRefName = null;
78 private String insurerRefName = null;
79 private String fieldCollectorRefName = null;
80 private String valuerRefName = null;
81 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
83 // ---------------------------------------------------------------
84 // CRUD tests : CREATE tests
85 // ---------------------------------------------------------------
87 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
88 public void createIntakeWithAuthRefs(String testName) throws Exception {
90 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
92 // Submit the request to the service and store the response.
93 String identifier = createIdentifier();
95 // Create all the person refs and entities
98 IntakeClient intakeClient = new IntakeClient();
99 MultipartOutput multipart = createIntakeInstance(
100 "entryNumber-" + identifier,
101 "entryDate-" + identifier,
104 conditionCheckAssesorRefName,
106 fieldCollectorRefName,
109 ClientResponse<Response> res = intakeClient.create(multipart);
111 int statusCode = res.getStatus();
113 // Check the status code of the response: does it match
114 // the expected response(s)?
117 // Does it fall within the set of valid status codes?
118 // Does it exactly match the expected status code?
119 if(logger.isDebugEnabled()){
120 logger.debug(testName + ": status = " + statusCode);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
126 // Store the ID returned from the first resource created
127 // for additional tests below.
128 if (knownIntakeId == null){
129 knownIntakeId = extractId(res);
130 if (logger.isDebugEnabled()) {
131 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
135 // Store the IDs from every resource created by tests,
136 // so they can be deleted after tests have been run.
137 intakeIdsCreated.add(extractId(res));
140 protected void createPersonRefs(){
141 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
143 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
144 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
145 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
146 ClientResponse<Response> res = personAuthClient.create(multipart);
147 int statusCode = res.getStatus();
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, CREATED_STATUS);
152 personAuthCSID = extractId(res);
154 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
155 authRefName, "Olivier Owner", true);
156 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
157 personIdsCreated.add(currentOwnerPersonCSID);
159 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
160 authRefName, "Debbie Depositor", true);
161 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
163 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
164 authRefName, "Andrew Assessor", true);
165 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
167 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
168 authRefName, "Ingrid Insurer", true);
169 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
171 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
172 authRefName, "Connie Collector", true);
173 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
175 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
176 authRefName, "Vince Valuer", true);
177 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
182 protected String createPerson(String firstName, String surName, String refName ) {
183 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
184 Map<String, String> personInfo = new HashMap<String,String>();
185 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
186 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
187 MultipartOutput multipart =
188 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
189 refName, personInfo, personAuthClient.getItemCommonPartName());
190 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
191 int statusCode = res.getStatus();
193 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195 Assert.assertEquals(statusCode, CREATED_STATUS);
196 return extractId(res);
200 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
201 dependsOnMethods = {"createIntakeWithAuthRefs"})
202 public void readAndCheckAuthRefDocs(String testName) throws Exception {
205 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
207 // Get the auth ref docs and check them
208 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
209 ClientResponse<AuthorityRefDocList> refDocListResp =
210 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
212 int statusCode = refDocListResp.getStatus();
214 if(logger.isDebugEnabled()){
215 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
217 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
218 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
219 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 AuthorityRefDocList list = refDocListResp.getEntity();
222 // Optionally output additional data about list members for debugging.
223 boolean iterateThroughList = true;
224 boolean fFoundIntake = false;
225 if(iterateThroughList && logger.isDebugEnabled()){
226 List<AuthorityRefDocList.AuthorityRefDocItem> items =
227 list.getAuthorityRefDocItem();
229 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
230 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
231 logger.debug(testName + ": list-item[" + i + "] " +
232 item.getDocType() + "(" +
233 item.getDocId() + ") Name:[" +
234 item.getDocName() + "] Number:[" +
235 item.getDocNumber() + "] in field:[" +
236 item.getSourceField() + "]");
237 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
242 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
247 // ---------------------------------------------------------------
248 // Cleanup of resources created during testing
249 // ---------------------------------------------------------------
252 * Deletes all resources created by tests, after all tests have been run.
254 * This cleanup method will always be run, even if one or more tests fail.
255 * For this reason, it attempts to remove all resources created
256 * at any point during testing, even if some of those resources
257 * may be expected to be deleted by certain tests.
259 @AfterClass(alwaysRun=true)
260 public void cleanUp() {
261 String noTest = System.getProperty("noTestCleanup");
262 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("Skipping Cleanup phase ...");
268 if (logger.isDebugEnabled()) {
269 logger.debug("Cleaning up temporary resources created for testing ...");
271 IntakeClient intakeClient = new IntakeClient();
272 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
273 // Note: Any non-success responses are ignored and not reported.
274 for (String resourceId : intakeIdsCreated) {
275 ClientResponse<Response> res = intakeClient.delete(resourceId);
277 // Delete persons before PersonAuth
278 for (String resourceId : personIdsCreated) {
279 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
281 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
284 // ---------------------------------------------------------------
285 // Utility methods used by tests above
286 // ---------------------------------------------------------------
288 public String getServicePathComponent() {
289 return SERVICE_PATH_COMPONENT;
292 private MultipartOutput createIntakeInstance(String entryNumber,
296 String conditionCheckAssesor,
298 String fieldCollector,
300 IntakesCommon intake = new IntakesCommon();
301 intake.setEntryNumber(entryNumber);
302 intake.setEntryDate(entryDate);
303 intake.setCurrentOwner(currentOwner);
304 intake.setDepositor(depositor);
305 intake.setConditionCheckAssesor(conditionCheckAssesor);
306 intake.setInsurer(insurer);
307 intake.setFieldCollector(fieldCollector);
308 intake.setValuer(Valuer);
309 MultipartOutput multipart = new MultipartOutput();
310 OutputPart commonPart =
311 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
312 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
314 if(logger.isDebugEnabled()){
315 logger.debug("to be created, intake common");
316 logger.debug(objectAsXmlString(intake, IntakesCommon.class));