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();
167 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
168 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
169 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
170 ClientResponse<Response> res = personAuthClient.create(multipart);
171 int statusCode = res.getStatus();
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, STATUS_CREATED);
176 personAuthCSID = extractId(res);
178 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
179 authRefName, "Olivier Owner", true);
180 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
181 personIdsCreated.add(currentOwnerPersonCSID);
183 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
184 authRefName, "Debbie Depositor", true);
185 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
187 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.createPersonRefName(
188 authRefName, "Andrew Checker-Assessor", true);
189 personIdsCreated.add(createPerson("Andrew", "Checker-Assessor", conditionCheckerAssessorRefName));
191 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
192 authRefName, "Ingrid Insurer", true);
193 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
195 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
196 authRefName, "Vince Valuer", true);
197 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
202 protected String createPerson(String firstName, String surName, String refName ) {
203 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
204 Map<String, String> personInfo = new HashMap<String,String>();
205 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
206 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
207 MultipartOutput multipart =
208 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
209 refName, personInfo, personAuthClient.getItemCommonPartName());
210 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
211 int statusCode = res.getStatus();
213 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
214 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
215 Assert.assertEquals(statusCode, STATUS_CREATED);
216 return extractId(res);
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
221 dependsOnMethods = {"createIntakeWithAuthRefs"})
222 public void readAndCheckAuthRefDocs(String testName) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug(testBanner(testName, CLASS_NAME));
228 testSetup(STATUS_OK, ServiceRequestType.READ);
230 // Get the auth ref docs and check them
231 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
232 ClientResponse<AuthorityRefDocList> refDocListResp =
233 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
235 int statusCode = refDocListResp.getStatus();
237 if(logger.isDebugEnabled()){
238 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243 AuthorityRefDocList list = refDocListResp.getEntity();
245 // Optionally output additional data about list members for debugging.
246 boolean iterateThroughList = true;
247 boolean fFoundIntake = false;
248 if(iterateThroughList && logger.isDebugEnabled()){
249 List<AuthorityRefDocList.AuthorityRefDocItem> items =
250 list.getAuthorityRefDocItem();
252 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
253 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
254 logger.debug(testName + ": list-item[" + i + "] " +
255 item.getDocType() + "(" +
256 item.getDocId() + ") Name:[" +
257 item.getDocName() + "] Number:[" +
258 item.getDocNumber() + "] in field:[" +
259 item.getSourceField() + "]");
260 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
265 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
270 // ---------------------------------------------------------------
271 // Cleanup of resources created during testing
272 // ---------------------------------------------------------------
275 * Deletes all resources created by tests, after all tests have been run.
277 * This cleanup method will always be run, even if one or more tests fail.
278 * For this reason, it attempts to remove all resources created
279 * at any point during testing, even if some of those resources
280 * may be expected to be deleted by certain tests.
282 @AfterClass(alwaysRun=true)
283 public void cleanUp() {
284 String noTest = System.getProperty("noTestCleanup");
285 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
286 if (logger.isDebugEnabled()) {
287 logger.debug("Skipping Cleanup phase ...");
291 if (logger.isDebugEnabled()) {
292 logger.debug("Cleaning up temporary resources created for testing ...");
294 IntakeClient intakeClient = new IntakeClient();
295 // Note: Any non-success responses are ignored and not reported.
296 for (String resourceId : intakeIdsCreated) {
297 ClientResponse<Response> res = intakeClient.delete(resourceId);
298 res.releaseConnection();
300 // Delete persons before PersonAuth
301 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
302 for (String resourceId : personIdsCreated) {
303 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
304 res.releaseConnection();
306 if (personAuthCSID != null) {
307 personAuthClient.delete(personAuthCSID).releaseConnection();
311 // ---------------------------------------------------------------
312 // Utility methods used by tests above
313 // ---------------------------------------------------------------
315 public String getServicePathComponent() {
316 return SERVICE_PATH_COMPONENT;
319 private MultipartOutput createIntakeInstance(String entryNumber,
323 String conditionCheckerAssessor,
326 IntakesCommon intake = new IntakesCommon();
327 intake.setEntryNumber(entryNumber);
328 intake.setEntryDate(entryDate);
329 intake.setCurrentOwner(currentOwner);
330 intake.setDepositor(depositor);
331 intake.setConditionCheckerAssessor(conditionCheckerAssessor);
332 intake.setInsurer(insurer);
333 intake.setValuer(Valuer);
334 MultipartOutput multipart = new MultipartOutput();
335 OutputPart commonPart =
336 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
337 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
339 if(logger.isDebugEnabled()){
340 logger.debug("to be created, intake common");
341 logger.debug(objectAsXmlString(intake, IntakesCommon.class));