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 Logger logger =
65 LoggerFactory.getLogger(PersonAuthRefDocsTest.class);
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 int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
74 private int OK_STATUS = Response.Status.OK.getStatusCode();
75 private String personAuthCSID = null;
76 private String currentOwnerPersonCSID = null;
77 private String currentOwnerRefName = null;
78 private String depositorRefName = null;
79 private String conditionCheckAssesorRefName = null;
80 private String insurerRefName = null;
81 private String fieldCollectorRefName = null;
82 private String valuerRefName = null;
83 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
89 protected CollectionSpaceClient getClientInstance() {
90 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
97 protected AbstractCommonList getAbstractCommonList(
98 ClientResponse<AbstractCommonList> response) {
99 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
102 // ---------------------------------------------------------------
103 // CRUD tests : CREATE tests
104 // ---------------------------------------------------------------
106 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
107 public void createIntakeWithAuthRefs(String testName) throws Exception {
109 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
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 conditionCheckAssesorRefName,
125 fieldCollectorRefName,
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();
168 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
169 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
170 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
171 ClientResponse<Response> res = personAuthClient.create(multipart);
172 int statusCode = res.getStatus();
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, CREATED_STATUS);
177 personAuthCSID = extractId(res);
179 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
180 authRefName, "Olivier Owner", true);
181 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
182 personIdsCreated.add(currentOwnerPersonCSID);
184 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
185 authRefName, "Debbie Depositor", true);
186 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
188 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
189 authRefName, "Andrew Assessor", true);
190 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
192 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
193 authRefName, "Ingrid Insurer", true);
194 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
196 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
197 authRefName, "Connie Collector", true);
198 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
200 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
201 authRefName, "Vince Valuer", true);
202 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
207 protected String createPerson(String firstName, String surName, String refName ) {
208 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
209 Map<String, String> personInfo = new HashMap<String,String>();
210 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
211 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
212 MultipartOutput multipart =
213 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
214 refName, personInfo, personAuthClient.getItemCommonPartName());
215 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
216 int statusCode = res.getStatus();
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, CREATED_STATUS);
221 return extractId(res);
225 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
226 dependsOnMethods = {"createIntakeWithAuthRefs"})
227 public void readAndCheckAuthRefDocs(String testName) throws Exception {
230 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
232 // Get the auth ref docs and check them
233 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
234 ClientResponse<AuthorityRefDocList> refDocListResp =
235 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
237 int statusCode = refDocListResp.getStatus();
239 if(logger.isDebugEnabled()){
240 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
242 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
243 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
244 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
245 AuthorityRefDocList list = refDocListResp.getEntity();
247 // Optionally output additional data about list members for debugging.
248 boolean iterateThroughList = true;
249 boolean fFoundIntake = false;
250 if(iterateThroughList && logger.isDebugEnabled()){
251 List<AuthorityRefDocList.AuthorityRefDocItem> items =
252 list.getAuthorityRefDocItem();
254 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
255 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
256 logger.debug(testName + ": list-item[" + i + "] " +
257 item.getDocType() + "(" +
258 item.getDocId() + ") Name:[" +
259 item.getDocName() + "] Number:[" +
260 item.getDocNumber() + "] in field:[" +
261 item.getSourceField() + "]");
262 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
267 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
272 // ---------------------------------------------------------------
273 // Cleanup of resources created during testing
274 // ---------------------------------------------------------------
277 * Deletes all resources created by tests, after all tests have been run.
279 * This cleanup method will always be run, even if one or more tests fail.
280 * For this reason, it attempts to remove all resources created
281 * at any point during testing, even if some of those resources
282 * may be expected to be deleted by certain tests.
284 @AfterClass(alwaysRun=true)
285 public void cleanUp() {
286 String noTest = System.getProperty("noTestCleanup");
287 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
288 if (logger.isDebugEnabled()) {
289 logger.debug("Skipping Cleanup phase ...");
293 if (logger.isDebugEnabled()) {
294 logger.debug("Cleaning up temporary resources created for testing ...");
296 IntakeClient intakeClient = new IntakeClient();
297 // Note: Any non-success responses are ignored and not reported.
298 for (String resourceId : intakeIdsCreated) {
299 ClientResponse<Response> res = intakeClient.delete(resourceId);
300 res.releaseConnection();
302 // Delete persons before PersonAuth
303 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
304 for (String resourceId : personIdsCreated) {
305 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
306 res.releaseConnection();
308 if (personAuthCSID != null) {
309 personAuthClient.delete(personAuthCSID).releaseConnection();
313 // ---------------------------------------------------------------
314 // Utility methods used by tests above
315 // ---------------------------------------------------------------
317 public String getServicePathComponent() {
318 return SERVICE_PATH_COMPONENT;
321 private MultipartOutput createIntakeInstance(String entryNumber,
325 String conditionCheckAssesor,
327 String fieldCollector,
329 IntakesCommon intake = new IntakesCommon();
330 intake.setEntryNumber(entryNumber);
331 intake.setEntryDate(entryDate);
332 intake.setCurrentOwner(currentOwner);
333 intake.setDepositor(depositor);
334 intake.setConditionCheckAssesor(conditionCheckAssesor);
335 intake.setInsurer(insurer);
336 intake.setFieldCollector(fieldCollector);
337 intake.setValuer(Valuer);
338 MultipartOutput multipart = new MultipartOutput();
339 OutputPart commonPart =
340 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
341 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
343 if(logger.isDebugEnabled()){
344 logger.debug("to be created, intake common");
345 logger.debug(objectAsXmlString(intake, IntakesCommon.class));