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.jboss.resteasy.client.ClientResponse;
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
53 import org.testng.Assert;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.Test;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * IntakeAuthRefsTest, carries out tests against a
62 * deployed and running Intake Service.
64 * $LastChangedRevision: 1327 $
65 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
67 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
69 private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
70 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
72 // Instance variables specific to this test.
73 final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
74 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
75 private String knownResourceId = null;
76 private List<String> intakeIdsCreated = new ArrayList<String>();
77 private List<String> personIdsCreated = new ArrayList<String>();
78 private String personAuthCSID = null;
79 private String currentOwnerRefName = null;
80 private String depositorRefName = null;
81 private String conditionCheckerOrAssessorRefName = null;
82 private String insurerRefName = null;
83 private String valuerRefName = null;
84 private final int NUM_AUTH_REFS_EXPECTED = 5;
85 private final static String CURRENT_DATE_UTC =
86 GregorianCalendarDateTimeUtils.currentDateUTC();
89 protected String getServiceName() {
90 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
97 protected CollectionSpaceClient getClientInstance() {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
102 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
105 protected AbstractCommonList getCommonList(Response response) {
106 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
109 // ---------------------------------------------------------------
110 // CRUD tests : CREATE tests
111 // ---------------------------------------------------------------
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
114 public void createWithAuthRefs(String testName) throws Exception {
115 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117 // Submit the request to the service and store the response.
118 String identifier = createIdentifier();
120 // Create all the person refs and entities
123 // Submit the request to the service and store the response.
124 IntakeClient intakeClient = new IntakeClient();
125 PoxPayloadOut multipart = createIntakeInstance(
126 "entryNumber-" + identifier,
130 conditionCheckerOrAssessorRefName,
135 Response res = intakeClient.create(multipart);
137 int statusCode = res.getStatus();
138 // Check the status code of the response: does it match
139 // the expected response(s)?
142 // Does it fall within the set of valid status codes?
143 // Does it exactly match the expected status code?
144 if(logger.isDebugEnabled()){
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(testRequestType, statusCode));
149 Assert.assertEquals(statusCode, testExpectedStatusCode);
150 newId = extractId(res);
155 // Store the ID returned from the first resource created
156 // for additional tests below.
157 if (knownResourceId == null){
158 knownResourceId = newId;
159 if (logger.isDebugEnabled()) {
160 logger.debug(testName + ": knownResourceId=" + knownResourceId);
164 // Store the IDs from every resource created by tests,
165 // so they can be deleted after tests have been run.
166 intakeIdsCreated.add(newId);
169 protected void createPersonRefs() {
171 // First, create a new person authority
173 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
174 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
175 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
176 Response res = personAuthClient.create(multipart);
178 int statusCode = res.getStatus();
179 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(testRequestType, statusCode));
181 Assert.assertEquals(statusCode, STATUS_CREATED);
182 personAuthCSID = extractId(res);
187 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
188 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
189 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 personIdsCreated.add(csid);
192 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
193 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
194 personIdsCreated.add(csid);
196 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
197 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
198 personIdsCreated.add(csid);
200 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
201 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
202 personIdsCreated.add(csid);
204 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
205 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 personIdsCreated.add(csid);
209 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
210 String result = null;
212 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
213 Map<String, String> personInfo = new HashMap<String,String>();
214 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
215 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
216 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
217 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
218 PersonTermGroup term = new PersonTermGroup();
219 String termName = firstName + " " + surName;
220 term.setTermDisplayName(termName);
221 term.setTermName(termName);
222 personTerms.add(term);
223 PoxPayloadOut multipart =
224 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
225 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
227 Response res = personAuthClient.createItem(personAuthCSID, multipart);
229 int statusCode = res.getStatus();
230 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(testRequestType, statusCode));
232 Assert.assertEquals(statusCode, STATUS_CREATED);
233 result = extractId(res);
242 @Test(dataProvider="testName",
243 dependsOnMethods = {"createWithAuthRefs"})
244 public void readAndCheckAuthRefs(String testName) throws Exception {
246 testSetup(STATUS_OK, ServiceRequestType.READ);
248 // Submit the request to the service and store the response.
249 IntakeClient intakeClient = new IntakeClient();
250 Response res = intakeClient.read(knownResourceId);
252 assertStatusCode(res, testName);
253 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
254 IntakesCommon intake = (IntakesCommon) extractPart(input,
255 intakeClient.getCommonPartName(), IntakesCommon.class);
256 Assert.assertNotNull(intake);
257 // Check a couple of fields
258 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
259 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
260 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
267 // Get the auth refs and check them
268 res = intakeClient.getAuthorityRefs(knownResourceId);
269 AuthorityRefList list = null;
271 assertStatusCode(res, testName);
272 list = (AuthorityRefList)res.getEntity();
279 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
280 int numAuthRefsFound = items.size();
281 if(logger.isDebugEnabled()){
282 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
283 " authority references, found " + numAuthRefsFound);
285 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
286 "Did not find all expected authority references! " +
287 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
289 // Optionally output additional data about list members for debugging.
290 boolean iterateThroughList = true;
291 if(iterateThroughList && logger.isDebugEnabled()){
293 for(AuthorityRefList.AuthorityRefItem item : items){
294 logger.debug(testName + ": list-item[" + i + "] Field:" +
295 item.getSourceField() + "= " +
296 item.getAuthDisplayName() +
297 item.getItemDisplayName());
298 logger.debug(testName + ": list-item[" + i + "] refName=" +
300 logger.debug(testName + ": list-item[" + i + "] URI=" +
308 // ---------------------------------------------------------------
309 // Cleanup of resources created during testing
310 // ---------------------------------------------------------------
313 * Deletes all resources created by tests, after all tests have been run.
315 * This cleanup method will always be run, even if one or more tests fail.
316 * For this reason, it attempts to remove all resources created
317 * at any point during testing, even if some of those resources
318 * may be expected to be deleted by certain tests.
320 @AfterClass(alwaysRun=true)
321 public void cleanUp() {
322 String noTest = System.getProperty("noTestCleanup");
323 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
324 if (logger.isDebugEnabled()) {
325 logger.debug("Skipping Cleanup phase ...");
329 if (logger.isDebugEnabled()) {
330 logger.debug("Cleaning up temporary resources created for testing ...");
332 IntakeClient intakeClient = new IntakeClient();
333 // Note: Any non-success responses are ignored and not reported.
334 for (String resourceId : intakeIdsCreated) {
335 intakeClient.delete(resourceId).close();
337 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
338 // Delete persons before PersonAuth
339 for (String resourceId : personIdsCreated) {
340 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
342 personAuthClient.delete(personAuthCSID).close();
345 // ---------------------------------------------------------------
346 // Utility methods used by tests above
347 // ---------------------------------------------------------------
349 public String getServicePathComponent() {
350 return SERVICE_PATH_COMPONENT;
353 private PoxPayloadOut createIntakeInstance(String entryNumber,
357 String conditionCheckerAssessor,
360 IntakesCommon intake = new IntakesCommon();
361 intake.setEntryNumber(entryNumber);
362 intake.setEntryDate(entryDate);
363 intake.setCurrentOwner(currentOwner);
364 intake.setDepositor(depositor);
365 intake.setValuer(Valuer);
367 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
368 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
369 checkersOrAssessors.add(conditionCheckerAssessor);
370 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
372 InsurerList insurerList = new InsurerList();
373 List<String> insurers = insurerList.getInsurer();
374 insurers.add(insurer);
375 intake.setInsurers(insurerList);
377 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
378 PayloadOutputPart commonPart =
379 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
381 if(logger.isDebugEnabled()){
382 logger.debug("to be created, intake common");
383 logger.debug(objectAsXmlString(intake, IntakesCommon.class));