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.IntakesCommon;
40 //import org.collectionspace.services.intake.IntakesCommonList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
45 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * PersonAuthRefDocsTest, carries out tests against a
57 * deployed and running Person Service.
59 * $LastChangedRevision: 1327 $
60 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62 public class PersonAuthRefDocsTest extends BaseServiceTest {
64 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
65 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67 // Instance variables specific to this test.
68 final String SERVICE_PATH_COMPONENT = "intakes";
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownIntakeId = null;
71 private List<String> intakeIdsCreated = new ArrayList<String>();
72 private List<String> personIdsCreated = new ArrayList<String>();
73 private String personAuthCSID = null;
74 private String currentOwnerPersonCSID = null;
75 private String currentOwnerRefName = null;
76 private String depositorRefName = null;
77 private String conditionCheckerAssessorRefName = null;
78 private String insurerRefName = null;
79 private String valuerRefName = null;
80 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
87 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94 protected AbstractCommonList getAbstractCommonList(
95 ClientResponse<AbstractCommonList> response) {
96 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99 // ---------------------------------------------------------------
100 // CRUD tests : CREATE tests
101 // ---------------------------------------------------------------
103 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
104 public void createIntakeWithAuthRefs(String testName) throws Exception {
106 if (logger.isDebugEnabled()) {
107 logger.debug(testBanner(testName, CLASS_NAME));
109 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
111 // Submit the request to the service and store the response.
112 String identifier = createIdentifier();
114 // Create all the person refs and entities
117 IntakeClient intakeClient = new IntakeClient();
118 MultipartOutput multipart = createIntakeInstance(
119 "entryNumber-" + identifier,
120 "entryDate-" + identifier,
123 conditionCheckerAssessorRefName,
127 ClientResponse<Response> res = intakeClient.create(multipart);
129 int statusCode = res.getStatus();
131 // Check the status code of the response: does it match
132 // the expected response(s)?
135 // Does it fall within the set of valid status codes?
136 // Does it exactly match the expected status code?
137 if(logger.isDebugEnabled()){
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144 res.releaseConnection();
147 // Store the ID returned from the first resource created
148 // for additional tests below.
149 if (knownIntakeId == null){
150 knownIntakeId = extractId(res);
151 if (logger.isDebugEnabled()) {
152 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
156 // Store the IDs from every resource created by tests,
157 // so they can be deleted after tests have been run.
158 intakeIdsCreated.add(extractId(res));
162 * Creates the person refs.
164 protected void createPersonRefs(){
165 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
166 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
167 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
168 ClientResponse<Response> res = personAuthClient.create(multipart);
169 int statusCode = res.getStatus();
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, STATUS_CREATED);
174 personAuthCSID = extractId(res);
176 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
178 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
179 Assert.assertNotNull(csid);
180 currentOwnerPersonCSID = csid;
181 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
182 Assert.assertNotNull(currentOwnerRefName);
183 personIdsCreated.add(csid);
185 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
186 Assert.assertNotNull(csid);
187 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188 Assert.assertNotNull(depositorRefName);
189 personIdsCreated.add(csid);
191 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
192 Assert.assertNotNull(csid);
193 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
194 Assert.assertNotNull(conditionCheckerAssessorRefName);
195 personIdsCreated.add(csid);
197 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
198 Assert.assertNotNull(csid);
199 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
200 Assert.assertNotNull(insurerRefName);
201 personIdsCreated.add(csid);
203 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
204 Assert.assertNotNull(csid);
205 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 Assert.assertNotNull(valuerRefName);
207 personIdsCreated.add(csid);
211 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
212 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
213 Map<String, String> personInfo = new HashMap<String,String>();
214 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
215 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
216 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
217 MultipartOutput multipart =
218 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
219 authRefName, personInfo, personAuthClient.getItemCommonPartName());
220 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
221 int statusCode = res.getStatus();
223 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225 Assert.assertEquals(statusCode, STATUS_CREATED);
226 return extractId(res);
230 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
231 dependsOnMethods = {"createIntakeWithAuthRefs"})
232 public void readAndCheckAuthRefDocs(String testName) throws Exception {
234 if (logger.isDebugEnabled()) {
235 logger.debug(testBanner(testName, CLASS_NAME));
238 testSetup(STATUS_OK, ServiceRequestType.READ);
240 // Get the auth ref docs and check them
241 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
242 ClientResponse<AuthorityRefDocList> refDocListResp =
243 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
245 int statusCode = refDocListResp.getStatus();
247 if(logger.isDebugEnabled()){
248 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
250 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 AuthorityRefDocList list = refDocListResp.getEntity();
255 // Optionally output additional data about list members for debugging.
256 boolean iterateThroughList = true;
257 boolean fFoundIntake = false;
258 if(iterateThroughList && logger.isDebugEnabled()){
259 List<AuthorityRefDocList.AuthorityRefDocItem> items =
260 list.getAuthorityRefDocItem();
262 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
263 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
264 logger.debug(testName + ": list-item[" + i + "] " +
265 item.getDocType() + "(" +
266 item.getDocId() + ") Name:[" +
267 item.getDocName() + "] Number:[" +
268 item.getDocNumber() + "] in field:[" +
269 item.getSourceField() + "]");
270 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
275 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
280 // ---------------------------------------------------------------
281 // Cleanup of resources created during testing
282 // ---------------------------------------------------------------
285 * Deletes all resources created by tests, after all tests have been run.
287 * This cleanup method will always be run, even if one or more tests fail.
288 * For this reason, it attempts to remove all resources created
289 * at any point during testing, even if some of those resources
290 * may be expected to be deleted by certain tests.
292 @AfterClass(alwaysRun=true)
293 public void cleanUp() {
294 String noTest = System.getProperty("noTestCleanup");
295 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
296 if (logger.isDebugEnabled()) {
297 logger.debug("Skipping Cleanup phase ...");
301 if (logger.isDebugEnabled()) {
302 logger.debug("Cleaning up temporary resources created for testing ...");
304 IntakeClient intakeClient = new IntakeClient();
305 // Note: Any non-success responses are ignored and not reported.
306 for (String resourceId : intakeIdsCreated) {
307 ClientResponse<Response> res = intakeClient.delete(resourceId);
308 res.releaseConnection();
310 // Delete persons before PersonAuth
311 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
312 for (String resourceId : personIdsCreated) {
313 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
314 res.releaseConnection();
316 if (personAuthCSID != null) {
317 personAuthClient.delete(personAuthCSID).releaseConnection();
321 // ---------------------------------------------------------------
322 // Utility methods used by tests above
323 // ---------------------------------------------------------------
325 public String getServicePathComponent() {
326 return SERVICE_PATH_COMPONENT;
329 private MultipartOutput createIntakeInstance(String entryNumber,
333 String conditionCheckerAssessor,
336 IntakesCommon intake = new IntakesCommon();
337 intake.setEntryNumber(entryNumber);
338 intake.setEntryDate(entryDate);
339 intake.setCurrentOwner(currentOwner);
340 intake.setDepositor(depositor);
341 intake.setConditionCheckerAssessor(conditionCheckerAssessor);
342 intake.setInsurer(insurer);
343 intake.setValuer(Valuer);
344 MultipartOutput multipart = new MultipartOutput();
345 OutputPart commonPart =
346 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
347 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
349 if(logger.isDebugEnabled()){
350 logger.debug("to be created, intake common");
351 logger.debug(objectAsXmlString(intake, IntakesCommon.class));