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.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
44 import org.collectionspace.services.intake.IntakesCommon;
45 import org.collectionspace.services.intake.InsurerList;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
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 {
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 getAbstractCommonList(
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 {
117 if (logger.isDebugEnabled()) {
118 logger.debug(testBanner(testName, CLASS_NAME));
120 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
122 // Submit the request to the service and store the response.
123 String identifier = createIdentifier();
125 // Create all the person refs and entities
128 // Submit the request to the service and store the response.
129 IntakeClient intakeClient = new IntakeClient();
130 PoxPayloadOut multipart = createIntakeInstance(
131 "entryNumber-" + identifier,
135 conditionCheckerOrAssessorRefName,
138 ClientResponse<Response> res = intakeClient.create(multipart);
140 int statusCode = res.getStatus();
142 // Check the status code of the response: does it match
143 // the expected response(s)?
146 // Does it fall within the set of valid status codes?
147 // Does it exactly match the expected status code?
148 if(logger.isDebugEnabled()){
149 logger.debug(testName + ": status = " + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 // Store the ID returned from the first resource created
156 // for additional tests below.
157 if (knownResourceId == null){
158 knownResourceId = extractId(res);
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(extractId(res));
169 protected void createPersonRefs(){
170 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
171 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
172 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
173 ClientResponse<Response> res = personAuthClient.create(multipart);
174 int statusCode = res.getStatus();
176 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178 Assert.assertEquals(statusCode, STATUS_CREATED);
179 personAuthCSID = extractId(res);
181 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
183 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
184 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
185 personIdsCreated.add(csid);
187 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
188 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189 personIdsCreated.add(csid);
191 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
192 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193 personIdsCreated.add(csid);
195 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
196 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197 personIdsCreated.add(csid);
199 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
200 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201 personIdsCreated.add(csid);
204 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
205 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
206 Map<String, String> personInfo = new HashMap<String,String>();
207 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
208 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
209 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
210 PoxPayloadOut multipart =
211 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
212 authRefName, personInfo, personAuthClient.getItemCommonPartName());
213 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
214 int statusCode = res.getStatus();
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, STATUS_CREATED);
219 return extractId(res);
223 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
224 dependsOnMethods = {"createWithAuthRefs"})
225 public void readAndCheckAuthRefs(String testName) throws Exception {
227 if (logger.isDebugEnabled()) {
228 logger.debug(testBanner(testName, CLASS_NAME));
231 testSetup(STATUS_OK, ServiceRequestType.READ);
233 // Submit the request to the service and store the response.
234 IntakeClient intakeClient = new IntakeClient();
235 ClientResponse<String> res = intakeClient.read(knownResourceId);
236 int statusCode = res.getStatus();
238 // Check the status code of the response: does it match
239 // the expected response(s)?
240 if(logger.isDebugEnabled()){
241 logger.debug(testName + ".read: status = " + statusCode);
243 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
244 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
245 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
247 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
248 IntakesCommon intake = (IntakesCommon) extractPart(input,
249 intakeClient.getCommonPartName(), IntakesCommon.class);
250 Assert.assertNotNull(intake);
251 // Check a couple of fields
252 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
253 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
254 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
256 // Get the auth refs and check them
257 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
258 statusCode = res2.getStatus();
260 if(logger.isDebugEnabled()){
261 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
263 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 AuthorityRefList list = res2.getEntity();
268 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
269 int numAuthRefsFound = items.size();
270 if(logger.isDebugEnabled()){
271 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
272 " authority references, found " + numAuthRefsFound);
274 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
275 "Did not find all expected authority references! " +
276 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
278 // Optionally output additional data about list members for debugging.
279 boolean iterateThroughList = true;
280 if(iterateThroughList && logger.isDebugEnabled()){
282 for(AuthorityRefList.AuthorityRefItem item : items){
283 logger.debug(testName + ": list-item[" + i + "] Field:" +
284 item.getSourceField() + "= " +
285 item.getAuthDisplayName() +
286 item.getItemDisplayName());
287 logger.debug(testName + ": list-item[" + i + "] refName=" +
289 logger.debug(testName + ": list-item[" + i + "] URI=" +
297 // ---------------------------------------------------------------
298 // Cleanup of resources created during testing
299 // ---------------------------------------------------------------
302 * Deletes all resources created by tests, after all tests have been run.
304 * This cleanup method will always be run, even if one or more tests fail.
305 * For this reason, it attempts to remove all resources created
306 * at any point during testing, even if some of those resources
307 * may be expected to be deleted by certain tests.
309 @AfterClass(alwaysRun=true)
310 public void cleanUp() {
311 String noTest = System.getProperty("noTestCleanup");
312 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
313 if (logger.isDebugEnabled()) {
314 logger.debug("Skipping Cleanup phase ...");
318 if (logger.isDebugEnabled()) {
319 logger.debug("Cleaning up temporary resources created for testing ...");
321 IntakeClient intakeClient = new IntakeClient();
322 // Note: Any non-success responses are ignored and not reported.
323 for (String resourceId : intakeIdsCreated) {
324 intakeClient.delete(resourceId).releaseConnection();
326 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
327 // Delete persons before PersonAuth
328 for (String resourceId : personIdsCreated) {
329 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
331 personAuthClient.delete(personAuthCSID).releaseConnection();
334 // ---------------------------------------------------------------
335 // Utility methods used by tests above
336 // ---------------------------------------------------------------
338 public String getServicePathComponent() {
339 return SERVICE_PATH_COMPONENT;
342 private PoxPayloadOut createIntakeInstance(String entryNumber,
346 String conditionCheckerAssessor,
349 IntakesCommon intake = new IntakesCommon();
350 intake.setEntryNumber(entryNumber);
351 intake.setEntryDate(entryDate);
352 intake.setCurrentOwner(currentOwner);
353 intake.setDepositor(depositor);
354 intake.setValuer(Valuer);
356 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
357 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
358 checkersOrAssessors.add(conditionCheckerAssessor);
359 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
361 InsurerList insurerList = new InsurerList();
362 List<String> insurers = insurerList.getInsurer();
363 insurers.add(insurer);
364 intake.setInsurers(insurerList);
366 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
367 PayloadOutputPart commonPart =
368 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
369 commonPart.setLabel(new IntakeClient().getCommonPartName());
371 if(logger.isDebugEnabled()){
372 logger.debug("to be created, intake common");
373 logger.debug(objectAsXmlString(intake, IntakesCommon.class));