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 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
233 dependsOnMethods = {"createIntakeWithAuthRefs"})
234 public void readAndCheckAuthRefDocs(String testName) throws Exception {
236 if (logger.isDebugEnabled()) {
237 logger.debug(testBanner(testName, CLASS_NAME));
240 testSetup(STATUS_OK, ServiceRequestType.READ);
242 // Get the auth ref docs and check them
244 // Single scalar field
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 // Repeatable scalar field
261 // FIXME: Add an appropriate test here, or preferably a new test case.
263 // Optionally output additional data about list members for debugging.
264 boolean iterateThroughList = true;
265 boolean fFoundIntake = false;
266 if(iterateThroughList && logger.isDebugEnabled()){
267 List<AuthorityRefDocList.AuthorityRefDocItem> items =
268 list.getAuthorityRefDocItem();
270 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
271 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
272 logger.debug(testName + ": list-item[" + i + "] " +
273 item.getDocType() + "(" +
274 item.getDocId() + ") Name:[" +
275 item.getDocName() + "] Number:[" +
276 item.getDocNumber() + "] in field:[" +
277 item.getSourceField() + "]");
278 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
283 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
288 // ---------------------------------------------------------------
289 // Cleanup of resources created during testing
290 // ---------------------------------------------------------------
293 * Deletes all resources created by tests, after all tests have been run.
295 * This cleanup method will always be run, even if one or more tests fail.
296 * For this reason, it attempts to remove all resources created
297 * at any point during testing, even if some of those resources
298 * may be expected to be deleted by certain tests.
300 @AfterClass(alwaysRun=true)
301 public void cleanUp() {
302 String noTest = System.getProperty("noTestCleanup");
303 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
304 if (logger.isDebugEnabled()) {
305 logger.debug("Skipping Cleanup phase ...");
309 if (logger.isDebugEnabled()) {
310 logger.debug("Cleaning up temporary resources created for testing ...");
312 IntakeClient intakeClient = new IntakeClient();
313 // Note: Any non-success responses are ignored and not reported.
314 for (String resourceId : intakeIdsCreated) {
315 ClientResponse<Response> res = intakeClient.delete(resourceId);
316 res.releaseConnection();
318 // Delete persons before PersonAuth
319 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
320 for (String resourceId : personIdsCreated) {
321 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
322 res.releaseConnection();
324 if (personAuthCSID != null) {
325 personAuthClient.delete(personAuthCSID).releaseConnection();
329 // ---------------------------------------------------------------
330 // Utility methods used by tests above
331 // ---------------------------------------------------------------
333 public String getServicePathComponent() {
334 return SERVICE_PATH_COMPONENT;
337 private MultipartOutput createIntakeInstance(String entryNumber,
341 String conditionCheckerAssessor,
344 IntakesCommon intake = new IntakesCommon();
345 intake.setEntryNumber(entryNumber);
346 intake.setEntryDate(entryDate);
347 intake.setCurrentOwner(currentOwner);
348 intake.setDepositor(depositor);
349 intake.setValuer(Valuer);
351 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
352 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
353 checkersOrAssessors.add(conditionCheckerAssessor);
354 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
356 InsurerList insurerList = new InsurerList();
357 List<String> insurers = insurerList.getInsurer();
358 insurers.add(insurer);
359 intake.setInsurers(insurerList);
361 MultipartOutput multipart = new MultipartOutput();
362 OutputPart commonPart =
363 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
364 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
366 if(logger.isDebugEnabled()){
367 logger.debug("to be created, intake common");
368 logger.debug(objectAsXmlString(intake, IntakesCommon.class));