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.authorityref.AuthorityRefList;
41 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
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;
47 import org.jboss.resteasy.client.ClientResponse;
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
51 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.Test;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * IntakeAuthRefsTest, carries out tests against a
61 * deployed and running Intake Service.
63 * $LastChangedRevision: 1327 $
64 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
66 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
68 private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
73 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
74 private String knownResourceId = null;
75 private List<String> intakeIdsCreated = new ArrayList<String>();
76 private List<String> personIdsCreated = new ArrayList<String>();
77 private String personAuthCSID = null;
78 private String currentOwnerRefName = null;
79 private String depositorRefName = null;
80 private String conditionCheckerOrAssessorRefName = null;
81 private String insurerRefName = null;
82 private String valuerRefName = null;
83 private final int NUM_AUTH_REFS_EXPECTED = 5;
84 private final static String CURRENT_DATE_UTC =
85 GregorianCalendarDateTimeUtils.currentDateUTC();
88 protected String getServiceName() {
89 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
93 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
96 protected CollectionSpaceClient getClientInstance() {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
104 protected AbstractCommonList getCommonList(
105 ClientResponse<AbstractCommonList> 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,
133 ClientResponse<Response> res = intakeClient.create(multipart);
135 int statusCode = res.getStatus();
137 // Check the status code of the response: does it match
138 // the expected response(s)?
141 // Does it fall within the set of valid status codes?
142 // Does it exactly match the expected status code?
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ": status = " + statusCode);
146 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(testRequestType, statusCode));
148 Assert.assertEquals(statusCode, testExpectedStatusCode);
150 // Store the ID returned from the first resource created
151 // for additional tests below.
152 if (knownResourceId == null){
153 knownResourceId = extractId(res);
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ": knownResourceId=" + knownResourceId);
159 // Store the IDs from every resource created by tests,
160 // so they can be deleted after tests have been run.
161 intakeIdsCreated.add(extractId(res));
164 protected void createPersonRefs(){
165 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
166 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
167 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
168 ClientResponse<Response> res = personAuthClient.create(multipart);
169 int statusCode = res.getStatus();
171 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(testRequestType, statusCode));
173 Assert.assertEquals(statusCode, STATUS_CREATED);
174 personAuthCSID = extractId(res);
176 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
178 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
179 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
180 personIdsCreated.add(csid);
182 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
183 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
184 personIdsCreated.add(csid);
186 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
187 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188 personIdsCreated.add(csid);
190 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
191 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192 personIdsCreated.add(csid);
194 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
195 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
196 personIdsCreated.add(csid);
199 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
200 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
201 Map<String, String> personInfo = new HashMap<String,String>();
202 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
203 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
204 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
205 PoxPayloadOut multipart =
206 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
207 authRefName, personInfo, null, personAuthClient.getItemCommonPartName());
208 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
209 int statusCode = res.getStatus();
211 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
212 invalidStatusCodeMessage(testRequestType, statusCode));
213 Assert.assertEquals(statusCode, STATUS_CREATED);
214 return extractId(res);
218 @Test(dataProvider="testName",
219 dependsOnMethods = {"createWithAuthRefs"})
220 public void readAndCheckAuthRefs(String testName) throws Exception {
222 testSetup(STATUS_OK, ServiceRequestType.READ);
224 // Submit the request to the service and store the response.
225 IntakeClient intakeClient = new IntakeClient();
226 ClientResponse<String> res = intakeClient.read(knownResourceId);
228 assertStatusCode(res, testName);
229 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
230 IntakesCommon intake = (IntakesCommon) extractPart(input,
231 intakeClient.getCommonPartName(), IntakesCommon.class);
232 Assert.assertNotNull(intake);
233 // Check a couple of fields
234 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
235 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
236 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
239 res.releaseConnection();
243 // Get the auth refs and check them
244 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
245 AuthorityRefList list = null;
247 assertStatusCode(res2, testName);
248 list = res2.getEntity();
251 res2.releaseConnection();
255 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
256 int numAuthRefsFound = items.size();
257 if(logger.isDebugEnabled()){
258 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
259 " authority references, found " + numAuthRefsFound);
261 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
262 "Did not find all expected authority references! " +
263 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
265 // Optionally output additional data about list members for debugging.
266 boolean iterateThroughList = true;
267 if(iterateThroughList && logger.isDebugEnabled()){
269 for(AuthorityRefList.AuthorityRefItem item : items){
270 logger.debug(testName + ": list-item[" + i + "] Field:" +
271 item.getSourceField() + "= " +
272 item.getAuthDisplayName() +
273 item.getItemDisplayName());
274 logger.debug(testName + ": list-item[" + i + "] refName=" +
276 logger.debug(testName + ": list-item[" + i + "] URI=" +
284 // ---------------------------------------------------------------
285 // Cleanup of resources created during testing
286 // ---------------------------------------------------------------
289 * Deletes all resources created by tests, after all tests have been run.
291 * This cleanup method will always be run, even if one or more tests fail.
292 * For this reason, it attempts to remove all resources created
293 * at any point during testing, even if some of those resources
294 * may be expected to be deleted by certain tests.
296 @AfterClass(alwaysRun=true)
297 public void cleanUp() {
298 String noTest = System.getProperty("noTestCleanup");
299 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
300 if (logger.isDebugEnabled()) {
301 logger.debug("Skipping Cleanup phase ...");
305 if (logger.isDebugEnabled()) {
306 logger.debug("Cleaning up temporary resources created for testing ...");
308 IntakeClient intakeClient = new IntakeClient();
309 // Note: Any non-success responses are ignored and not reported.
310 for (String resourceId : intakeIdsCreated) {
311 intakeClient.delete(resourceId).releaseConnection();
313 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
314 // Delete persons before PersonAuth
315 for (String resourceId : personIdsCreated) {
316 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
318 personAuthClient.delete(personAuthCSID).releaseConnection();
321 // ---------------------------------------------------------------
322 // Utility methods used by tests above
323 // ---------------------------------------------------------------
325 public String getServicePathComponent() {
326 return SERVICE_PATH_COMPONENT;
329 private PoxPayloadOut createIntakeInstance(String entryNumber,
333 String conditionCheckerAssessor,
336 IntakesCommon intake = new IntakesCommon();
337 intake.setEntryNumber(entryNumber);
338 intake.setEntryDate(entryDate);
339 intake.setCurrentOwner(currentOwner);
340 intake.setDepositor(depositor);
341 intake.setValuer(Valuer);
343 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
344 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
345 checkersOrAssessors.add(conditionCheckerAssessor);
346 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
348 InsurerList insurerList = new InsurerList();
349 List<String> insurers = insurerList.getInsurer();
350 insurers.add(insurer);
351 intake.setInsurers(insurerList);
353 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
354 PayloadOutputPart commonPart =
355 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
357 if(logger.isDebugEnabled()){
358 logger.debug("to be created, intake common");
359 logger.debug(objectAsXmlString(intake, IntakesCommon.class));