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;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * IntakeAuthRefsTest, carries out tests against a
55 * deployed and running Intake Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
62 private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
63 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65 // Instance variables specific to this test.
66 final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
67 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68 private String knownResourceId = null;
69 private List<String> intakeIdsCreated = new ArrayList<String>();
70 private List<String> personIdsCreated = new ArrayList<String>();
71 private String personAuthCSID = null;
72 private String currentOwnerRefName = null;
73 private String depositorRefName = null;
74 private String conditionCheckerOrAssessorRefName = null;
75 private String insurerRefName = null;
76 private String valuerRefName = null;
77 private final int NUM_AUTH_REFS_EXPECTED = 5;
78 private final static String CURRENT_DATE_UTC =
79 GregorianCalendarDateTimeUtils.currentDateUTC();
82 protected String getServiceName() {
83 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
90 protected CollectionSpaceClient getClientInstance() {
91 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
95 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
96 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
103 protected AbstractCommonList getCommonList(Response response) {
104 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
107 // ---------------------------------------------------------------
108 // CRUD tests : CREATE tests
109 // ---------------------------------------------------------------
111 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
112 public void createWithAuthRefs(String testName) throws Exception {
113 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
115 // Submit the request to the service and store the response.
116 String identifier = createIdentifier();
118 // Create all the person refs and entities
121 // Submit the request to the service and store the response.
122 IntakeClient intakeClient = new IntakeClient();
123 PoxPayloadOut multipart = createIntakeInstance(
124 "entryNumber-" + identifier,
128 conditionCheckerOrAssessorRefName,
133 Response res = intakeClient.create(multipart);
135 int statusCode = res.getStatus();
136 // Check the status code of the response: does it match
137 // the expected response(s)?
140 // Does it fall within the set of valid status codes?
141 // Does it exactly match the expected status code?
142 if(logger.isDebugEnabled()){
143 logger.debug(testName + ": status = " + statusCode);
145 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
146 invalidStatusCodeMessage(testRequestType, statusCode));
147 Assert.assertEquals(statusCode, testExpectedStatusCode);
148 newId = extractId(res);
153 // Store the ID returned from the first resource created
154 // for additional tests below.
155 if (knownResourceId == null){
156 knownResourceId = newId;
157 if (logger.isDebugEnabled()) {
158 logger.debug(testName + ": knownResourceId=" + knownResourceId);
162 // Store the IDs from every resource created by tests,
163 // so they can be deleted after tests have been run.
164 intakeIdsCreated.add(newId);
167 protected void createPersonRefs() throws Exception {
169 // First, create a new person authority
171 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
172 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
173 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
174 Response res = personAuthClient.create(multipart);
176 int statusCode = res.getStatus();
177 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(testRequestType, statusCode));
179 Assert.assertEquals(statusCode, STATUS_CREATED);
180 personAuthCSID = extractId(res);
185 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
186 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
187 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188 personIdsCreated.add(csid);
190 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
191 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192 personIdsCreated.add(csid);
194 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
195 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
196 personIdsCreated.add(csid);
198 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
199 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
200 personIdsCreated.add(csid);
202 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
203 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
204 personIdsCreated.add(csid);
207 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) throws Exception {
208 String result = null;
210 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
211 Map<String, String> personInfo = new HashMap<String,String>();
212 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
213 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
214 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
215 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
216 PersonTermGroup term = new PersonTermGroup();
217 String termName = firstName + " " + surName;
218 term.setTermDisplayName(termName);
219 term.setTermName(termName);
220 personTerms.add(term);
221 PoxPayloadOut multipart =
222 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
223 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
225 Response res = personAuthClient.createItem(personAuthCSID, multipart);
227 int statusCode = res.getStatus();
228 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(testRequestType, statusCode));
230 Assert.assertEquals(statusCode, STATUS_CREATED);
231 result = extractId(res);
240 @Test(dataProvider="testName",
241 dependsOnMethods = {"createWithAuthRefs"})
242 public void readAndCheckAuthRefs(String testName) throws Exception {
244 testSetup(STATUS_OK, ServiceRequestType.READ);
246 // Submit the request to the service and store the response.
247 IntakeClient intakeClient = new IntakeClient();
248 Response res = intakeClient.read(knownResourceId);
250 assertStatusCode(res, testName);
251 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
252 IntakesCommon intake = (IntakesCommon) extractPart(input,
253 intakeClient.getCommonPartName(), IntakesCommon.class);
254 Assert.assertNotNull(intake);
255 // Check a couple of fields
256 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
257 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
258 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
265 // Get the auth refs and check them
266 res = intakeClient.getAuthorityRefs(knownResourceId);
267 AuthorityRefList list = null;
269 assertStatusCode(res, testName);
270 list = res.readEntity(AuthorityRefList.class);
277 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
278 int numAuthRefsFound = items.size();
279 if(logger.isDebugEnabled()){
280 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
281 " authority references, found " + numAuthRefsFound);
283 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
284 "Did not find all expected authority references! " +
285 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
287 // Optionally output additional data about list members for debugging.
288 boolean iterateThroughList = true;
289 if(iterateThroughList && logger.isDebugEnabled()){
291 for(AuthorityRefList.AuthorityRefItem item : items){
292 logger.debug(testName + ": list-item[" + i + "] Field:" +
293 item.getSourceField() + "= " +
294 item.getAuthDisplayName() +
295 item.getItemDisplayName());
296 logger.debug(testName + ": list-item[" + i + "] refName=" +
298 logger.debug(testName + ": list-item[" + i + "] URI=" +
306 // ---------------------------------------------------------------
307 // Cleanup of resources created during testing
308 // ---------------------------------------------------------------
311 * Deletes all resources created by tests, after all tests have been run.
313 * This cleanup method will always be run, even if one or more tests fail.
314 * For this reason, it attempts to remove all resources created
315 * at any point during testing, even if some of those resources
316 * may be expected to be deleted by certain tests.
319 @AfterClass(alwaysRun=true)
320 public void cleanUp() throws Exception {
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,
358 String Valuer ) throws Exception {
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));