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.Response;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.IntakeClient;
35 import org.collectionspace.services.client.PayloadOutputPart;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.intake.InsurerList;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.person.PersonTermGroup;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * IntakeAuthRefsTest, carries out tests against a
56 * deployed and running Intake Service.
58 * $LastChangedRevision: 1327 $
59 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
63 private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66 // Instance variables specific to this test.
67 final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
68 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69 private String knownResourceId = null;
70 private List<String> intakeIdsCreated = new ArrayList<String>();
71 private List<String> personIdsCreated = new ArrayList<String>();
72 private String personAuthCSID = null;
73 private String currentOwnerRefName = null;
74 private String depositorRefName = null;
75 private String conditionCheckerOrAssessorRefName = null;
76 private String insurerRefName = null;
77 private String valuerRefName = null;
78 private final int NUM_AUTH_REFS_EXPECTED = 5;
79 private final static String CURRENT_DATE_UTC =
80 GregorianCalendarDateTimeUtils.currentDateUTC();
83 protected String getServiceName() {
84 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
88 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
91 protected CollectionSpaceClient getClientInstance() {
92 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
104 protected AbstractCommonList getCommonList(Response response) {
105 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
108 // ---------------------------------------------------------------
109 // CRUD tests : CREATE tests
110 // ---------------------------------------------------------------
112 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
113 public void createWithAuthRefs(String testName) throws Exception {
114 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
116 // Submit the request to the service and store the response.
117 String identifier = createIdentifier();
119 // Create all the person refs and entities
122 // Submit the request to the service and store the response.
123 IntakeClient intakeClient = new IntakeClient();
124 PoxPayloadOut multipart = createIntakeInstance(
125 "entryNumber-" + identifier,
129 conditionCheckerOrAssessorRefName,
134 Response res = intakeClient.create(multipart);
136 int statusCode = res.getStatus();
137 // Check the status code of the response: does it match
138 // the expected response(s)?
141 // Does it fall within the set of valid status codes?
142 // Does it exactly match the expected status code?
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ": status = " + statusCode);
146 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(testRequestType, statusCode));
148 Assert.assertEquals(statusCode, testExpectedStatusCode);
149 newId = extractId(res);
154 // Store the ID returned from the first resource created
155 // for additional tests below.
156 if (knownResourceId == null){
157 knownResourceId = newId;
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": knownResourceId=" + knownResourceId);
163 // Store the IDs from every resource created by tests,
164 // so they can be deleted after tests have been run.
165 intakeIdsCreated.add(newId);
168 protected void createPersonRefs() {
170 // First, create a new person authority
172 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
173 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
174 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
175 Response res = personAuthClient.create(multipart);
177 int statusCode = res.getStatus();
178 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(testRequestType, statusCode));
180 Assert.assertEquals(statusCode, STATUS_CREATED);
181 personAuthCSID = extractId(res);
186 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
187 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
188 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189 personIdsCreated.add(csid);
191 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
192 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193 personIdsCreated.add(csid);
195 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
196 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197 personIdsCreated.add(csid);
199 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
200 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201 personIdsCreated.add(csid);
203 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
204 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
205 personIdsCreated.add(csid);
208 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
209 String result = null;
211 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
212 Map<String, String> personInfo = new HashMap<String,String>();
213 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
214 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
215 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
216 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
217 PersonTermGroup term = new PersonTermGroup();
218 String termName = firstName + " " + surName;
219 term.setTermDisplayName(termName);
220 term.setTermName(termName);
221 personTerms.add(term);
222 PoxPayloadOut multipart =
223 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
224 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
226 Response res = personAuthClient.createItem(personAuthCSID, multipart);
228 int statusCode = res.getStatus();
229 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
230 invalidStatusCodeMessage(testRequestType, statusCode));
231 Assert.assertEquals(statusCode, STATUS_CREATED);
232 result = extractId(res);
241 @Test(dataProvider="testName",
242 dependsOnMethods = {"createWithAuthRefs"})
243 public void readAndCheckAuthRefs(String testName) throws Exception {
245 testSetup(STATUS_OK, ServiceRequestType.READ);
247 // Submit the request to the service and store the response.
248 IntakeClient intakeClient = new IntakeClient();
249 Response res = intakeClient.read(knownResourceId);
251 assertStatusCode(res, testName);
252 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
253 IntakesCommon intake = (IntakesCommon) extractPart(input,
254 intakeClient.getCommonPartName(), IntakesCommon.class);
255 Assert.assertNotNull(intake);
256 // Check a couple of fields
257 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
258 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
259 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
266 // Get the auth refs and check them
267 res = intakeClient.getAuthorityRefs(knownResourceId);
268 AuthorityRefList list = null;
270 assertStatusCode(res, testName);
271 list = res.readEntity(AuthorityRefList.class);
278 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
279 int numAuthRefsFound = items.size();
280 if(logger.isDebugEnabled()){
281 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
282 " authority references, found " + numAuthRefsFound);
284 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
285 "Did not find all expected authority references! " +
286 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
288 // Optionally output additional data about list members for debugging.
289 boolean iterateThroughList = true;
290 if(iterateThroughList && logger.isDebugEnabled()){
292 for(AuthorityRefList.AuthorityRefItem item : items){
293 logger.debug(testName + ": list-item[" + i + "] Field:" +
294 item.getSourceField() + "= " +
295 item.getAuthDisplayName() +
296 item.getItemDisplayName());
297 logger.debug(testName + ": list-item[" + i + "] refName=" +
299 logger.debug(testName + ": list-item[" + i + "] URI=" +
307 // ---------------------------------------------------------------
308 // Cleanup of resources created during testing
309 // ---------------------------------------------------------------
312 * Deletes all resources created by tests, after all tests have been run.
314 * This cleanup method will always be run, even if one or more tests fail.
315 * For this reason, it attempts to remove all resources created
316 * at any point during testing, even if some of those resources
317 * may be expected to be deleted by certain tests.
319 @AfterClass(alwaysRun=true)
320 public void cleanUp() {
321 String noTest = System.getProperty("noTestCleanup");
322 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
323 if (logger.isDebugEnabled()) {
324 logger.debug("Skipping Cleanup phase ...");
328 if (logger.isDebugEnabled()) {
329 logger.debug("Cleaning up temporary resources created for testing ...");
331 IntakeClient intakeClient = new IntakeClient();
332 // Note: Any non-success responses are ignored and not reported.
333 for (String resourceId : intakeIdsCreated) {
334 intakeClient.delete(resourceId).close();
336 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
337 // Delete persons before PersonAuth
338 for (String resourceId : personIdsCreated) {
339 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
341 personAuthClient.delete(personAuthCSID).close();
344 // ---------------------------------------------------------------
345 // Utility methods used by tests above
346 // ---------------------------------------------------------------
348 public String getServicePathComponent() {
349 return SERVICE_PATH_COMPONENT;
352 private PoxPayloadOut createIntakeInstance(String entryNumber,
356 String conditionCheckerAssessor,
359 IntakesCommon intake = new IntakesCommon();
360 intake.setEntryNumber(entryNumber);
361 intake.setEntryDate(entryDate);
362 intake.setCurrentOwner(currentOwner);
363 intake.setDepositor(depositor);
364 intake.setValuer(Valuer);
366 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
367 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
368 checkersOrAssessors.add(conditionCheckerAssessor);
369 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
371 InsurerList insurerList = new InsurerList();
372 List<String> insurers = insurerList.getInsurer();
373 insurers.add(insurer);
374 intake.setInsurers(insurerList);
376 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
377 PayloadOutputPart commonPart =
378 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
380 if(logger.isDebugEnabled()){
381 logger.debug("to be created, intake common");
382 logger.debug(objectAsXmlString(intake, IntakesCommon.class));