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();
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;
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 // Store the ID returned from the first resource created
146 // for additional tests below.
147 if (knownIntakeId == null){
148 knownIntakeId = extractId(res);
149 if (logger.isDebugEnabled()) {
150 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
154 // Store the IDs from every resource created by tests,
155 // so they can be deleted after tests have been run.
156 intakeIdsCreated.add(extractId(res));
159 protected void createPersonRefs(){
160 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
162 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
163 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
164 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
165 ClientResponse<Response> res = personAuthClient.create(multipart);
166 int statusCode = res.getStatus();
168 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170 Assert.assertEquals(statusCode, CREATED_STATUS);
171 personAuthCSID = extractId(res);
173 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
174 authRefName, "Olivier Owner", true);
175 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
176 personIdsCreated.add(currentOwnerPersonCSID);
178 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
179 authRefName, "Debbie Depositor", true);
180 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
182 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
183 authRefName, "Andrew Assessor", true);
184 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
186 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
187 authRefName, "Ingrid Insurer", true);
188 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
190 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
191 authRefName, "Connie Collector", true);
192 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
194 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
195 authRefName, "Vince Valuer", true);
196 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
201 protected String createPerson(String firstName, String surName, String refName ) {
202 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
203 Map<String, String> personInfo = new HashMap<String,String>();
204 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
205 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
206 MultipartOutput multipart =
207 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
208 refName, personInfo, personAuthClient.getItemCommonPartName());
209 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
210 int statusCode = res.getStatus();
212 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
213 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
214 Assert.assertEquals(statusCode, CREATED_STATUS);
215 return extractId(res);
219 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
220 dependsOnMethods = {"createIntakeWithAuthRefs"})
221 public void readAndCheckAuthRefDocs(String testName) throws Exception {
224 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
226 // Get the auth ref docs and check them
227 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
228 ClientResponse<AuthorityRefDocList> refDocListResp =
229 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
231 int statusCode = refDocListResp.getStatus();
233 if(logger.isDebugEnabled()){
234 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
236 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239 AuthorityRefDocList list = refDocListResp.getEntity();
241 // Optionally output additional data about list members for debugging.
242 boolean iterateThroughList = true;
243 boolean fFoundIntake = false;
244 if(iterateThroughList && logger.isDebugEnabled()){
245 List<AuthorityRefDocList.AuthorityRefDocItem> items =
246 list.getAuthorityRefDocItem();
248 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
249 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
250 logger.debug(testName + ": list-item[" + i + "] " +
251 item.getDocType() + "(" +
252 item.getDocId() + ") Name:[" +
253 item.getDocName() + "] Number:[" +
254 item.getDocNumber() + "] in field:[" +
255 item.getSourceField() + "]");
256 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
261 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
266 // ---------------------------------------------------------------
267 // Cleanup of resources created during testing
268 // ---------------------------------------------------------------
271 * Deletes all resources created by tests, after all tests have been run.
273 * This cleanup method will always be run, even if one or more tests fail.
274 * For this reason, it attempts to remove all resources created
275 * at any point during testing, even if some of those resources
276 * may be expected to be deleted by certain tests.
278 @AfterClass(alwaysRun=true)
279 public void cleanUp() {
280 String noTest = System.getProperty("noTestCleanup");
281 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
282 if (logger.isDebugEnabled()) {
283 logger.debug("Skipping Cleanup phase ...");
287 if (logger.isDebugEnabled()) {
288 logger.debug("Cleaning up temporary resources created for testing ...");
290 IntakeClient intakeClient = new IntakeClient();
291 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
292 // Note: Any non-success responses are ignored and not reported.
293 for (String resourceId : intakeIdsCreated) {
294 ClientResponse<Response> res = intakeClient.delete(resourceId);
296 // Delete persons before PersonAuth
297 for (String resourceId : personIdsCreated) {
298 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
300 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
303 // ---------------------------------------------------------------
304 // Utility methods used by tests above
305 // ---------------------------------------------------------------
307 public String getServicePathComponent() {
308 return SERVICE_PATH_COMPONENT;
311 private MultipartOutput createIntakeInstance(String entryNumber,
315 String conditionCheckAssesor,
317 String fieldCollector,
319 IntakesCommon intake = new IntakesCommon();
320 intake.setEntryNumber(entryNumber);
321 intake.setEntryDate(entryDate);
322 intake.setCurrentOwner(currentOwner);
323 intake.setDepositor(depositor);
324 intake.setConditionCheckAssesor(conditionCheckAssesor);
325 intake.setInsurer(insurer);
326 intake.setFieldCollector(fieldCollector);
327 intake.setValuer(Valuer);
328 MultipartOutput multipart = new MultipartOutput();
329 OutputPart commonPart =
330 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
331 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
333 if(logger.isDebugEnabled()){
334 logger.debug("to be created, intake common");
335 logger.debug(objectAsXmlString(intake, IntakesCommon.class));