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 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;
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 conditionCheckAssesorRefName,
126 fieldCollectorRefName,
129 ClientResponse<Response> res = intakeClient.create(multipart);
131 int statusCode = res.getStatus();
133 // Check the status code of the response: does it match
134 // the expected response(s)?
137 // Does it fall within the set of valid status codes?
138 // Does it exactly match the expected status code?
139 if(logger.isDebugEnabled()){
140 logger.debug(testName + ": status = " + statusCode);
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
146 res.releaseConnection();
149 // Store the ID returned from the first resource created
150 // for additional tests below.
151 if (knownIntakeId == null){
152 knownIntakeId = extractId(res);
153 if (logger.isDebugEnabled()) {
154 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
158 // Store the IDs from every resource created by tests,
159 // so they can be deleted after tests have been run.
160 intakeIdsCreated.add(extractId(res));
164 * Creates the person refs.
166 protected void createPersonRefs(){
167 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
169 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
170 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
171 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
172 ClientResponse<Response> res = personAuthClient.create(multipart);
173 int statusCode = res.getStatus();
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, STATUS_CREATED);
178 personAuthCSID = extractId(res);
180 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
181 authRefName, "Olivier Owner", true);
182 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
183 personIdsCreated.add(currentOwnerPersonCSID);
185 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
186 authRefName, "Debbie Depositor", true);
187 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
189 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
190 authRefName, "Andrew Assessor", true);
191 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
193 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
194 authRefName, "Ingrid Insurer", true);
195 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
197 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
198 authRefName, "Connie Collector", true);
199 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
201 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
202 authRefName, "Vince Valuer", true);
203 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
208 protected String createPerson(String firstName, String surName, String refName ) {
209 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
210 Map<String, String> personInfo = new HashMap<String,String>();
211 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
212 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
213 MultipartOutput multipart =
214 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
215 refName, personInfo, personAuthClient.getItemCommonPartName());
216 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
217 int statusCode = res.getStatus();
219 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
220 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
221 Assert.assertEquals(statusCode, STATUS_CREATED);
222 return extractId(res);
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
227 dependsOnMethods = {"createIntakeWithAuthRefs"})
228 public void readAndCheckAuthRefDocs(String testName) throws Exception {
230 if (logger.isDebugEnabled()) {
231 logger.debug(testBanner(testName, CLASS_NAME));
234 testSetup(STATUS_OK, ServiceRequestType.READ);
236 // Get the auth ref docs and check them
237 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
238 ClientResponse<AuthorityRefDocList> refDocListResp =
239 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
241 int statusCode = refDocListResp.getStatus();
243 if(logger.isDebugEnabled()){
244 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 AuthorityRefDocList list = refDocListResp.getEntity();
251 // Optionally output additional data about list members for debugging.
252 boolean iterateThroughList = true;
253 boolean fFoundIntake = false;
254 if(iterateThroughList && logger.isDebugEnabled()){
255 List<AuthorityRefDocList.AuthorityRefDocItem> items =
256 list.getAuthorityRefDocItem();
258 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
259 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
260 logger.debug(testName + ": list-item[" + i + "] " +
261 item.getDocType() + "(" +
262 item.getDocId() + ") Name:[" +
263 item.getDocName() + "] Number:[" +
264 item.getDocNumber() + "] in field:[" +
265 item.getSourceField() + "]");
266 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
271 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
276 // ---------------------------------------------------------------
277 // Cleanup of resources created during testing
278 // ---------------------------------------------------------------
281 * Deletes all resources created by tests, after all tests have been run.
283 * This cleanup method will always be run, even if one or more tests fail.
284 * For this reason, it attempts to remove all resources created
285 * at any point during testing, even if some of those resources
286 * may be expected to be deleted by certain tests.
288 @AfterClass(alwaysRun=true)
289 public void cleanUp() {
290 String noTest = System.getProperty("noTestCleanup");
291 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
292 if (logger.isDebugEnabled()) {
293 logger.debug("Skipping Cleanup phase ...");
297 if (logger.isDebugEnabled()) {
298 logger.debug("Cleaning up temporary resources created for testing ...");
300 IntakeClient intakeClient = new IntakeClient();
301 // Note: Any non-success responses are ignored and not reported.
302 for (String resourceId : intakeIdsCreated) {
303 ClientResponse<Response> res = intakeClient.delete(resourceId);
304 res.releaseConnection();
306 // Delete persons before PersonAuth
307 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
308 for (String resourceId : personIdsCreated) {
309 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
310 res.releaseConnection();
312 if (personAuthCSID != null) {
313 personAuthClient.delete(personAuthCSID).releaseConnection();
317 // ---------------------------------------------------------------
318 // Utility methods used by tests above
319 // ---------------------------------------------------------------
321 public String getServicePathComponent() {
322 return SERVICE_PATH_COMPONENT;
325 private MultipartOutput createIntakeInstance(String entryNumber,
329 String conditionCheckAssesor,
331 String fieldCollector,
333 IntakesCommon intake = new IntakesCommon();
334 intake.setEntryNumber(entryNumber);
335 intake.setEntryDate(entryDate);
336 intake.setCurrentOwner(currentOwner);
337 intake.setDepositor(depositor);
338 intake.setConditionCheckAssesor(conditionCheckAssesor);
339 intake.setInsurer(insurer);
340 intake.setFieldCollector(fieldCollector);
341 intake.setValuer(Valuer);
342 MultipartOutput multipart = new MultipartOutput();
343 OutputPart commonPart =
344 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
345 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
347 if(logger.isDebugEnabled()){
348 logger.debug("to be created, intake common");
349 logger.debug(objectAsXmlString(intake, IntakesCommon.class));