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(
106 ClientResponse<AbstractCommonList> response) {
107 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
110 // ---------------------------------------------------------------
111 // CRUD tests : CREATE tests
112 // ---------------------------------------------------------------
114 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
115 public void createWithAuthRefs(String testName) throws Exception {
116 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
118 // Submit the request to the service and store the response.
119 String identifier = createIdentifier();
121 // Create all the person refs and entities
124 // Submit the request to the service and store the response.
125 IntakeClient intakeClient = new IntakeClient();
126 PoxPayloadOut multipart = createIntakeInstance(
127 "entryNumber-" + identifier,
131 conditionCheckerOrAssessorRefName,
136 Response res = intakeClient.create(multipart);
138 int statusCode = res.getStatus();
139 // Check the status code of the response: does it match
140 // the expected response(s)?
143 // Does it fall within the set of valid status codes?
144 // Does it exactly match the expected status code?
145 if(logger.isDebugEnabled()){
146 logger.debug(testName + ": status = " + statusCode);
148 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(testRequestType, statusCode));
150 Assert.assertEquals(statusCode, testExpectedStatusCode);
151 newId = extractId(res);
156 // Store the ID returned from the first resource created
157 // for additional tests below.
158 if (knownResourceId == null){
159 knownResourceId = newId;
160 if (logger.isDebugEnabled()) {
161 logger.debug(testName + ": knownResourceId=" + knownResourceId);
165 // Store the IDs from every resource created by tests,
166 // so they can be deleted after tests have been run.
167 intakeIdsCreated.add(newId);
170 protected void createPersonRefs() {
172 // First, create a new person authority
174 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
175 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
176 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
177 Response res = personAuthClient.create(multipart);
179 int statusCode = res.getStatus();
180 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
181 invalidStatusCodeMessage(testRequestType, statusCode));
182 Assert.assertEquals(statusCode, STATUS_CREATED);
183 personAuthCSID = extractId(res);
188 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
189 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
190 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
191 personIdsCreated.add(csid);
193 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
194 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195 personIdsCreated.add(csid);
197 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
198 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199 personIdsCreated.add(csid);
201 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
202 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
203 personIdsCreated.add(csid);
205 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
206 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
207 personIdsCreated.add(csid);
210 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
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());
225 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
226 int statusCode = res.getStatus();
228 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(testRequestType, statusCode));
230 Assert.assertEquals(statusCode, STATUS_CREATED);
231 return extractId(res);
235 @Test(dataProvider="testName",
236 dependsOnMethods = {"createWithAuthRefs"})
237 public void readAndCheckAuthRefs(String testName) throws Exception {
239 testSetup(STATUS_OK, ServiceRequestType.READ);
241 // Submit the request to the service and store the response.
242 IntakeClient intakeClient = new IntakeClient();
243 ClientResponse<String> res = intakeClient.read(knownResourceId);
245 assertStatusCode(res, testName);
246 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
247 IntakesCommon intake = (IntakesCommon) extractPart(input,
248 intakeClient.getCommonPartName(), IntakesCommon.class);
249 Assert.assertNotNull(intake);
250 // Check a couple of fields
251 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
252 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
253 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
256 res.releaseConnection();
260 // Get the auth refs and check them
261 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
262 AuthorityRefList list = null;
264 assertStatusCode(res2, testName);
265 list = res2.getEntity();
268 res2.releaseConnection();
272 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
273 int numAuthRefsFound = items.size();
274 if(logger.isDebugEnabled()){
275 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
276 " authority references, found " + numAuthRefsFound);
278 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
279 "Did not find all expected authority references! " +
280 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
282 // Optionally output additional data about list members for debugging.
283 boolean iterateThroughList = true;
284 if(iterateThroughList && logger.isDebugEnabled()){
286 for(AuthorityRefList.AuthorityRefItem item : items){
287 logger.debug(testName + ": list-item[" + i + "] Field:" +
288 item.getSourceField() + "= " +
289 item.getAuthDisplayName() +
290 item.getItemDisplayName());
291 logger.debug(testName + ": list-item[" + i + "] refName=" +
293 logger.debug(testName + ": list-item[" + i + "] URI=" +
301 // ---------------------------------------------------------------
302 // Cleanup of resources created during testing
303 // ---------------------------------------------------------------
306 * Deletes all resources created by tests, after all tests have been run.
308 * This cleanup method will always be run, even if one or more tests fail.
309 * For this reason, it attempts to remove all resources created
310 * at any point during testing, even if some of those resources
311 * may be expected to be deleted by certain tests.
313 @AfterClass(alwaysRun=true)
314 public void cleanUp() {
315 String noTest = System.getProperty("noTestCleanup");
316 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
317 if (logger.isDebugEnabled()) {
318 logger.debug("Skipping Cleanup phase ...");
322 if (logger.isDebugEnabled()) {
323 logger.debug("Cleaning up temporary resources created for testing ...");
325 IntakeClient intakeClient = new IntakeClient();
326 // Note: Any non-success responses are ignored and not reported.
327 for (String resourceId : intakeIdsCreated) {
328 intakeClient.delete(resourceId).releaseConnection();
330 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
331 // Delete persons before PersonAuth
332 for (String resourceId : personIdsCreated) {
333 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
335 personAuthClient.delete(personAuthCSID).releaseConnection();
338 // ---------------------------------------------------------------
339 // Utility methods used by tests above
340 // ---------------------------------------------------------------
342 public String getServicePathComponent() {
343 return SERVICE_PATH_COMPONENT;
346 private PoxPayloadOut createIntakeInstance(String entryNumber,
350 String conditionCheckerAssessor,
353 IntakesCommon intake = new IntakesCommon();
354 intake.setEntryNumber(entryNumber);
355 intake.setEntryDate(entryDate);
356 intake.setCurrentOwner(currentOwner);
357 intake.setDepositor(depositor);
358 intake.setValuer(Valuer);
360 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
361 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
362 checkersOrAssessors.add(conditionCheckerAssessor);
363 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
365 InsurerList insurerList = new InsurerList();
366 List<String> insurers = insurerList.getInsurer();
367 insurers.add(insurer);
368 intake.setInsurers(insurerList);
370 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
371 PayloadOutputPart commonPart =
372 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
374 if(logger.isDebugEnabled()){
375 logger.debug("to be created, intake common");
376 logger.debug(objectAsXmlString(intake, IntakesCommon.class));