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.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
39 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
40 import org.collectionspace.services.intake.IntakesCommon;
41 import org.collectionspace.services.intake.InsurerList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.jboss.resteasy.client.ClientResponse;
46 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * PersonAuthRefDocsTest, carries out tests against a
58 * deployed and running Person Service.
60 * $LastChangedRevision: 1327 $
61 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
63 public class PersonAuthRefDocsTest extends BaseServiceTest {
65 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 final String SERVICE_PATH_COMPONENT = "intakes";
70 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
71 private String knownIntakeId = null;
72 private List<String> intakeIdsCreated = new ArrayList<String>();
73 private List<String> personIdsCreated = new ArrayList<String>();
74 private String personAuthCSID = null;
75 private String currentOwnerPersonCSID = null;
76 private String currentOwnerRefName = null;
77 private String depositorRefName = null;
78 private String conditionCheckerAssessorRefName = null;
79 private String insurerRefName = null;
80 private String valuerRefName = null;
81 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
87 protected CollectionSpaceClient getClientInstance() {
88 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
95 protected AbstractCommonList getAbstractCommonList(
96 ClientResponse<AbstractCommonList> response) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100 // ---------------------------------------------------------------
101 // CRUD tests : CREATE tests
102 // ---------------------------------------------------------------
104 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105 public void createIntakeWithAuthRefs(String testName) throws Exception {
107 if (logger.isDebugEnabled()) {
108 logger.debug(testBanner(testName, CLASS_NAME));
110 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
112 // Submit the request to the service and store the response.
113 String identifier = createIdentifier();
115 // Create all the person refs and entities
118 IntakeClient intakeClient = new IntakeClient();
119 MultipartOutput multipart = createIntakeInstance(
120 "entryNumber-" + identifier,
121 "entryDate-" + identifier,
124 conditionCheckerAssessorRefName,
128 ClientResponse<Response> res = intakeClient.create(multipart);
130 int statusCode = res.getStatus();
132 // Check the status code of the response: does it match
133 // the expected response(s)?
136 // Does it fall within the set of valid status codes?
137 // Does it exactly match the expected status code?
138 if(logger.isDebugEnabled()){
139 logger.debug(testName + ": status = " + statusCode);
141 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
142 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
143 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
145 res.releaseConnection();
148 // Store the ID returned from the first resource created
149 // for additional tests below.
150 if (knownIntakeId == null){
151 knownIntakeId = extractId(res);
152 if (logger.isDebugEnabled()) {
153 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
157 // Store the IDs from every resource created by tests,
158 // so they can be deleted after tests have been run.
159 intakeIdsCreated.add(extractId(res));
163 * Creates the person refs.
165 protected void createPersonRefs(){
166 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
167 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
168 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
169 ClientResponse<Response> res = personAuthClient.create(multipart);
170 int statusCode = res.getStatus();
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, STATUS_CREATED);
175 personAuthCSID = extractId(res);
177 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
179 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
180 Assert.assertNotNull(csid);
181 currentOwnerPersonCSID = csid;
182 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
183 Assert.assertNotNull(currentOwnerRefName);
184 personIdsCreated.add(csid);
186 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
187 Assert.assertNotNull(csid);
188 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189 Assert.assertNotNull(depositorRefName);
190 personIdsCreated.add(csid);
192 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
193 Assert.assertNotNull(csid);
194 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195 Assert.assertNotNull(conditionCheckerAssessorRefName);
196 personIdsCreated.add(csid);
198 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
199 Assert.assertNotNull(csid);
200 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201 Assert.assertNotNull(insurerRefName);
202 personIdsCreated.add(csid);
204 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
205 Assert.assertNotNull(csid);
206 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
207 Assert.assertNotNull(valuerRefName);
208 personIdsCreated.add(csid);
212 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
213 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
214 Map<String, String> personInfo = new HashMap<String,String>();
215 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
216 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
217 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
218 MultipartOutput multipart =
219 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
220 authRefName, personInfo, personAuthClient.getItemCommonPartName());
221 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
222 int statusCode = res.getStatus();
224 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226 Assert.assertEquals(statusCode, STATUS_CREATED);
227 return extractId(res);
232 // FIXME: Uncomment @Test annotation after CSPACE-2577 is fixed.
234 // @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
235 // dependsOnMethods = {"createIntakeWithAuthRefs"})
236 public void readAndCheckAuthRefDocs(String testName) throws Exception {
238 if (logger.isDebugEnabled()) {
239 logger.debug(testBanner(testName, CLASS_NAME));
242 testSetup(STATUS_OK, ServiceRequestType.READ);
244 // Get the auth ref docs and check them
245 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
246 ClientResponse<AuthorityRefDocList> refDocListResp =
247 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
249 int statusCode = refDocListResp.getStatus();
251 if(logger.isDebugEnabled()){
252 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
254 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
257 AuthorityRefDocList list = refDocListResp.getEntity();
259 // Optionally output additional data about list members for debugging.
260 boolean iterateThroughList = true;
261 boolean fFoundIntake = false;
262 if(iterateThroughList && logger.isDebugEnabled()){
263 List<AuthorityRefDocList.AuthorityRefDocItem> items =
264 list.getAuthorityRefDocItem();
266 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
267 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
268 logger.debug(testName + ": list-item[" + i + "] " +
269 item.getDocType() + "(" +
270 item.getDocId() + ") Name:[" +
271 item.getDocName() + "] Number:[" +
272 item.getDocNumber() + "] in field:[" +
273 item.getSourceField() + "]");
274 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
279 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
284 // ---------------------------------------------------------------
285 // Cleanup of resources created during testing
286 // ---------------------------------------------------------------
289 * Deletes all resources created by tests, after all tests have been run.
291 * This cleanup method will always be run, even if one or more tests fail.
292 * For this reason, it attempts to remove all resources created
293 * at any point during testing, even if some of those resources
294 * may be expected to be deleted by certain tests.
296 @AfterClass(alwaysRun=true)
297 public void cleanUp() {
298 String noTest = System.getProperty("noTestCleanup");
299 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
300 if (logger.isDebugEnabled()) {
301 logger.debug("Skipping Cleanup phase ...");
305 if (logger.isDebugEnabled()) {
306 logger.debug("Cleaning up temporary resources created for testing ...");
308 IntakeClient intakeClient = new IntakeClient();
309 // Note: Any non-success responses are ignored and not reported.
310 for (String resourceId : intakeIdsCreated) {
311 ClientResponse<Response> res = intakeClient.delete(resourceId);
312 res.releaseConnection();
314 // Delete persons before PersonAuth
315 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
316 for (String resourceId : personIdsCreated) {
317 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
318 res.releaseConnection();
320 if (personAuthCSID != null) {
321 personAuthClient.delete(personAuthCSID).releaseConnection();
325 // ---------------------------------------------------------------
326 // Utility methods used by tests above
327 // ---------------------------------------------------------------
329 public String getServicePathComponent() {
330 return SERVICE_PATH_COMPONENT;
333 private MultipartOutput createIntakeInstance(String entryNumber,
337 String conditionCheckerAssessor,
340 IntakesCommon intake = new IntakesCommon();
341 intake.setEntryNumber(entryNumber);
342 intake.setEntryDate(entryDate);
343 intake.setCurrentOwner(currentOwner);
344 intake.setDepositor(depositor);
345 intake.setValuer(Valuer);
347 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
348 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
349 checkersOrAssessors.add(conditionCheckerAssessor);
350 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
352 InsurerList insurerList = new InsurerList();
353 List<String> insurers = insurerList.getInsurer();
354 insurers.add(insurer);
355 intake.setInsurers(insurerList);
357 MultipartOutput multipart = new MultipartOutput();
358 OutputPart commonPart =
359 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
360 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
362 if(logger.isDebugEnabled()){
363 logger.debug("to be created, intake common");
364 logger.debug(objectAsXmlString(intake, IntakesCommon.class));