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.IntakeClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
38 import org.collectionspace.services.intake.IntakesCommon;
39 import org.collectionspace.services.intake.IntakesCommonList;
41 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * PersonAuthRefDocsTest, carries out tests against a
55 * deployed and running Person Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class PersonAuthRefDocsTest extends BaseServiceTest {
62 private final Logger logger =
63 LoggerFactory.getLogger(PersonAuthRefDocsTest.class);
65 // Instance variables specific to this test.
66 private IntakeClient intakeClient = new IntakeClient();
67 private PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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();
72 private List<String> personIdsCreated = new ArrayList();
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;
85 // ---------------------------------------------------------------
86 // CRUD tests : CREATE tests
87 // ---------------------------------------------------------------
89 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
90 public void createIntakeWithAuthRefs(String testName) throws Exception {
92 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
94 // Submit the request to the service and store the response.
95 String identifier = createIdentifier();
97 // Create all the person refs and entities
100 MultipartOutput multipart = createIntakeInstance(
101 "entryNumber-" + identifier,
102 "entryDate-" + identifier,
105 conditionCheckAssesorRefName,
107 fieldCollectorRefName,
110 ClientResponse<Response> res = intakeClient.create(multipart);
112 int statusCode = res.getStatus();
114 // Check the status code of the response: does it match
115 // the expected response(s)?
118 // Does it fall within the set of valid status codes?
119 // Does it exactly match the expected status code?
120 if(logger.isDebugEnabled()){
121 logger.debug(testName + ": status = " + statusCode);
123 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
124 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
125 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
127 // Store the ID returned from the first resource created
128 // for additional tests below.
129 if (knownIntakeId == null){
130 knownIntakeId = extractId(res);
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
136 // Store the IDs from every resource created by tests,
137 // so they can be deleted after tests have been run.
138 intakeIdsCreated.add(extractId(res));
141 protected void createPersonRefs(){
143 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
144 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
145 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
146 ClientResponse<Response> res = personAuthClient.create(multipart);
147 int statusCode = res.getStatus();
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, CREATED_STATUS);
152 personAuthCSID = extractId(res);
154 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
155 authRefName, "Olivier Owner", true);
156 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
157 personIdsCreated.add(currentOwnerPersonCSID);
159 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
160 authRefName, "Debbie Depositor", true);
161 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
163 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
164 authRefName, "Andrew Assessor", true);
165 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
167 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
168 authRefName, "Ingrid Insurer", true);
169 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
171 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
172 authRefName, "Connie Collector", true);
173 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
175 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
176 authRefName, "Vince Valuer", true);
177 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
182 protected String createPerson(String firstName, String surName, String refName ) {
183 Map<String, String> personInfo = new HashMap<String,String>();
184 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
185 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
186 MultipartOutput multipart =
187 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
188 refName, personInfo, personAuthClient.getItemCommonPartName());
189 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
190 int statusCode = res.getStatus();
192 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194 Assert.assertEquals(statusCode, CREATED_STATUS);
195 return extractId(res);
199 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
200 dependsOnMethods = {"createIntakeWithAuthRefs"})
201 public void readAndCheckAuthRefDocs(String testName) throws Exception {
204 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
206 // Get the auth ref docs and check them
207 ClientResponse<AuthorityRefDocList> refDocListResp =
208 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
210 int statusCode = refDocListResp.getStatus();
212 if(logger.isDebugEnabled()){
213 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
218 AuthorityRefDocList list = refDocListResp.getEntity();
220 // Optionally output additional data about list members for debugging.
221 boolean iterateThroughList = true;
222 boolean fFoundIntake = false;
223 if(iterateThroughList && logger.isDebugEnabled()){
224 List<AuthorityRefDocList.AuthorityRefDocItem> items =
225 list.getAuthorityRefDocItem();
227 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
228 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
229 logger.debug(testName + ": list-item[" + i + "] " +
230 item.getDocType() + "(" +
231 item.getDocId() + ") Name:[" +
232 item.getDocName() + "] Number:[" +
233 item.getDocNumber() + "] in field:[" +
234 item.getSourceField() + "]");
235 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
240 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
245 // ---------------------------------------------------------------
246 // Cleanup of resources created during testing
247 // ---------------------------------------------------------------
250 * Deletes all resources created by tests, after all tests have been run.
252 * This cleanup method will always be run, even if one or more tests fail.
253 * For this reason, it attempts to remove all resources created
254 * at any point during testing, even if some of those resources
255 * may be expected to be deleted by certain tests.
257 @AfterClass(alwaysRun=true)
258 public void cleanUp() {
259 String noTest = System.getProperty("noTestCleanup");
260 if("true".equalsIgnoreCase(noTest)) {
261 if (logger.isDebugEnabled()) {
262 logger.debug("Skipping Cleanup phase");
266 if (logger.isDebugEnabled()) {
267 logger.debug("Cleaning up temporary resources created for testing ...");
269 // Note: Any non-success responses are ignored and not reported.
270 for (String resourceId : intakeIdsCreated) {
271 ClientResponse<Response> res = intakeClient.delete(resourceId);
273 // Delete persons before PersonAuth
274 for (String resourceId : personIdsCreated) {
275 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
277 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
280 // ---------------------------------------------------------------
281 // Utility methods used by tests above
282 // ---------------------------------------------------------------
284 public String getServicePathComponent() {
285 return SERVICE_PATH_COMPONENT;
288 private MultipartOutput createIntakeInstance(String entryNumber,
292 String conditionCheckAssesor,
294 String fieldCollector,
296 IntakesCommon intake = new IntakesCommon();
297 intake.setEntryNumber(entryNumber);
298 intake.setEntryDate(entryDate);
299 intake.setCurrentOwner(currentOwner);
300 intake.setDepositor(depositor);
301 intake.setConditionCheckAssesor(conditionCheckAssesor);
302 intake.setInsurer(insurer);
303 intake.setFieldCollector(fieldCollector);
304 intake.setValuer(Valuer);
305 MultipartOutput multipart = new MultipartOutput();
306 OutputPart commonPart =
307 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
308 commonPart.getHeaders().add("label", intakeClient.getCommonPartName());
310 if(logger.isDebugEnabled()){
311 logger.debug("to be created, intake common");
312 logger.debug(objectAsXmlString(intake, IntakesCommon.class));