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 assertStatusCode(res, testName);
238 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
239 IntakesCommon intake = (IntakesCommon) extractPart(input,
240 intakeClient.getCommonPartName(), IntakesCommon.class);
241 Assert.assertNotNull(intake);
242 // Check a couple of fields
243 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
244 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
245 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
247 // Get the auth refs and check them
248 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
249 assertStatusCode(res2, testName);
251 AuthorityRefList list = res2.getEntity();
253 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
254 int numAuthRefsFound = items.size();
255 if(logger.isDebugEnabled()){
256 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
257 " authority references, found " + numAuthRefsFound);
259 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
260 "Did not find all expected authority references! " +
261 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
263 // Optionally output additional data about list members for debugging.
264 boolean iterateThroughList = true;
265 if(iterateThroughList && logger.isDebugEnabled()){
267 for(AuthorityRefList.AuthorityRefItem item : items){
268 logger.debug(testName + ": list-item[" + i + "] Field:" +
269 item.getSourceField() + "= " +
270 item.getAuthDisplayName() +
271 item.getItemDisplayName());
272 logger.debug(testName + ": list-item[" + i + "] refName=" +
274 logger.debug(testName + ": list-item[" + i + "] URI=" +
282 // ---------------------------------------------------------------
283 // Cleanup of resources created during testing
284 // ---------------------------------------------------------------
287 * Deletes all resources created by tests, after all tests have been run.
289 * This cleanup method will always be run, even if one or more tests fail.
290 * For this reason, it attempts to remove all resources created
291 * at any point during testing, even if some of those resources
292 * may be expected to be deleted by certain tests.
294 @AfterClass(alwaysRun=true)
295 public void cleanUp() {
296 String noTest = System.getProperty("noTestCleanup");
297 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
298 if (logger.isDebugEnabled()) {
299 logger.debug("Skipping Cleanup phase ...");
303 if (logger.isDebugEnabled()) {
304 logger.debug("Cleaning up temporary resources created for testing ...");
306 IntakeClient intakeClient = new IntakeClient();
307 // Note: Any non-success responses are ignored and not reported.
308 for (String resourceId : intakeIdsCreated) {
309 intakeClient.delete(resourceId).releaseConnection();
311 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
312 // Delete persons before PersonAuth
313 for (String resourceId : personIdsCreated) {
314 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
316 personAuthClient.delete(personAuthCSID).releaseConnection();
319 // ---------------------------------------------------------------
320 // Utility methods used by tests above
321 // ---------------------------------------------------------------
323 public String getServicePathComponent() {
324 return SERVICE_PATH_COMPONENT;
327 private PoxPayloadOut createIntakeInstance(String entryNumber,
331 String conditionCheckerAssessor,
334 IntakesCommon intake = new IntakesCommon();
335 intake.setEntryNumber(entryNumber);
336 intake.setEntryDate(entryDate);
337 intake.setCurrentOwner(currentOwner);
338 intake.setDepositor(depositor);
339 intake.setValuer(Valuer);
341 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
342 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
343 checkersOrAssessors.add(conditionCheckerAssessor);
344 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
346 InsurerList insurerList = new InsurerList();
347 List<String> insurers = insurerList.getInsurer();
348 insurers.add(insurer);
349 intake.setInsurers(insurerList);
351 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
352 PayloadOutputPart commonPart =
353 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
354 commonPart.setLabel(new IntakeClient().getCommonPartName());
356 if(logger.isDebugEnabled()){
357 logger.debug("to be created, intake common");
358 logger.debug(objectAsXmlString(intake, IntakesCommon.class));