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,
134 ClientResponse<Response> res = intakeClient.create(multipart);
136 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);
151 // Store the ID returned from the first resource created
152 // for additional tests below.
153 if (knownResourceId == null){
154 knownResourceId = extractId(res);
155 if (logger.isDebugEnabled()) {
156 logger.debug(testName + ": knownResourceId=" + knownResourceId);
160 // Store the IDs from every resource created by tests,
161 // so they can be deleted after tests have been run.
162 intakeIdsCreated.add(extractId(res));
165 protected void createPersonRefs(){
166 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
167 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
168 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
169 ClientResponse<Response> res = personAuthClient.create(multipart);
170 int statusCode = res.getStatus();
172 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(testRequestType, statusCode));
174 Assert.assertEquals(statusCode, STATUS_CREATED);
175 personAuthCSID = extractId(res);
177 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
179 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
180 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
181 personIdsCreated.add(csid);
183 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
184 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
185 personIdsCreated.add(csid);
187 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
188 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189 personIdsCreated.add(csid);
191 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
192 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193 personIdsCreated.add(csid);
195 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
196 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197 personIdsCreated.add(csid);
200 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
201 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
202 Map<String, String> personInfo = new HashMap<String,String>();
203 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
204 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
205 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
206 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
207 PersonTermGroup term = new PersonTermGroup();
208 String termName = firstName + " " + surName;
209 term.setTermDisplayName(termName);
210 term.setTermName(termName);
211 personTerms.add(term);
212 PoxPayloadOut multipart =
213 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
214 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
215 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
216 int statusCode = res.getStatus();
218 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(testRequestType, statusCode));
220 Assert.assertEquals(statusCode, STATUS_CREATED);
221 return extractId(res);
225 @Test(dataProvider="testName",
226 dependsOnMethods = {"createWithAuthRefs"})
227 public void readAndCheckAuthRefs(String testName) throws Exception {
229 testSetup(STATUS_OK, ServiceRequestType.READ);
231 // Submit the request to the service and store the response.
232 IntakeClient intakeClient = new IntakeClient();
233 ClientResponse<String> res = intakeClient.read(knownResourceId);
235 assertStatusCode(res, testName);
236 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
237 IntakesCommon intake = (IntakesCommon) extractPart(input,
238 intakeClient.getCommonPartName(), IntakesCommon.class);
239 Assert.assertNotNull(intake);
240 // Check a couple of fields
241 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
242 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
243 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
246 res.releaseConnection();
250 // Get the auth refs and check them
251 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
252 AuthorityRefList list = null;
254 assertStatusCode(res2, testName);
255 list = res2.getEntity();
258 res2.releaseConnection();
262 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
263 int numAuthRefsFound = items.size();
264 if(logger.isDebugEnabled()){
265 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
266 " authority references, found " + numAuthRefsFound);
268 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
269 "Did not find all expected authority references! " +
270 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
272 // Optionally output additional data about list members for debugging.
273 boolean iterateThroughList = true;
274 if(iterateThroughList && logger.isDebugEnabled()){
276 for(AuthorityRefList.AuthorityRefItem item : items){
277 logger.debug(testName + ": list-item[" + i + "] Field:" +
278 item.getSourceField() + "= " +
279 item.getAuthDisplayName() +
280 item.getItemDisplayName());
281 logger.debug(testName + ": list-item[" + i + "] refName=" +
283 logger.debug(testName + ": list-item[" + i + "] URI=" +
291 // ---------------------------------------------------------------
292 // Cleanup of resources created during testing
293 // ---------------------------------------------------------------
296 * Deletes all resources created by tests, after all tests have been run.
298 * This cleanup method will always be run, even if one or more tests fail.
299 * For this reason, it attempts to remove all resources created
300 * at any point during testing, even if some of those resources
301 * may be expected to be deleted by certain tests.
303 @AfterClass(alwaysRun=true)
304 public void cleanUp() {
305 String noTest = System.getProperty("noTestCleanup");
306 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
307 if (logger.isDebugEnabled()) {
308 logger.debug("Skipping Cleanup phase ...");
312 if (logger.isDebugEnabled()) {
313 logger.debug("Cleaning up temporary resources created for testing ...");
315 IntakeClient intakeClient = new IntakeClient();
316 // Note: Any non-success responses are ignored and not reported.
317 for (String resourceId : intakeIdsCreated) {
318 intakeClient.delete(resourceId).releaseConnection();
320 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
321 // Delete persons before PersonAuth
322 for (String resourceId : personIdsCreated) {
323 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
325 personAuthClient.delete(personAuthCSID).releaseConnection();
328 // ---------------------------------------------------------------
329 // Utility methods used by tests above
330 // ---------------------------------------------------------------
332 public String getServicePathComponent() {
333 return SERVICE_PATH_COMPONENT;
336 private PoxPayloadOut createIntakeInstance(String entryNumber,
340 String conditionCheckerAssessor,
343 IntakesCommon intake = new IntakesCommon();
344 intake.setEntryNumber(entryNumber);
345 intake.setEntryDate(entryDate);
346 intake.setCurrentOwner(currentOwner);
347 intake.setDepositor(depositor);
348 intake.setValuer(Valuer);
350 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
351 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
352 checkersOrAssessors.add(conditionCheckerAssessor);
353 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
355 InsurerList insurerList = new InsurerList();
356 List<String> insurers = insurerList.getInsurer();
357 insurers.add(insurer);
358 intake.setInsurers(insurerList);
360 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
361 PayloadOutputPart commonPart =
362 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
364 if(logger.isDebugEnabled()){
365 logger.debug("to be created, intake common");
366 logger.debug(objectAsXmlString(intake, IntakesCommon.class));