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.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 {
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;
86 protected String getServiceName() {
87 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
94 protected CollectionSpaceClient getClientInstance() {
95 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
102 protected AbstractCommonList getAbstractCommonList(
103 ClientResponse<AbstractCommonList> response) {
104 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
107 // ---------------------------------------------------------------
108 // CRUD tests : CREATE tests
109 // ---------------------------------------------------------------
111 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
112 public void createWithAuthRefs(String testName) throws Exception {
114 if (logger.isDebugEnabled()) {
115 logger.debug(testBanner(testName, CLASS_NAME));
117 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
119 // Submit the request to the service and store the response.
120 String identifier = createIdentifier();
122 // Create all the person refs and entities
125 // Submit the request to the service and store the response.
126 IntakeClient intakeClient = new IntakeClient();
127 PoxPayloadOut multipart = createIntakeInstance(
128 "entryNumber-" + identifier,
129 "entryDate-" + identifier,
132 conditionCheckerOrAssessorRefName,
135 ClientResponse<Response> res = intakeClient.create(multipart);
137 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(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
152 // Store the ID returned from the first resource created
153 // for additional tests below.
154 if (knownResourceId == null){
155 knownResourceId = extractId(res);
156 if (logger.isDebugEnabled()) {
157 logger.debug(testName + ": knownResourceId=" + knownResourceId);
161 // Store the IDs from every resource created by tests,
162 // so they can be deleted after tests have been run.
163 intakeIdsCreated.add(extractId(res));
166 protected void createPersonRefs(){
167 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
168 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
169 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
170 ClientResponse<Response> res = personAuthClient.create(multipart);
171 int statusCode = res.getStatus();
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, STATUS_CREATED);
176 personAuthCSID = extractId(res);
178 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
180 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
181 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
182 personIdsCreated.add(csid);
184 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
185 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 personIdsCreated.add(csid);
188 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
189 conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 personIdsCreated.add(csid);
192 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
193 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
194 personIdsCreated.add(csid);
196 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
197 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
198 personIdsCreated.add(csid);
201 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
202 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
203 Map<String, String> personInfo = new HashMap<String,String>();
204 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
205 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
206 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
207 PoxPayloadOut multipart =
208 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
209 authRefName, personInfo, personAuthClient.getItemCommonPartName());
210 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
211 int statusCode = res.getStatus();
213 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
214 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
215 Assert.assertEquals(statusCode, STATUS_CREATED);
216 return extractId(res);
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
221 dependsOnMethods = {"createWithAuthRefs"})
222 public void readAndCheckAuthRefs(String testName) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug(testBanner(testName, CLASS_NAME));
228 testSetup(STATUS_OK, ServiceRequestType.READ);
230 // Submit the request to the service and store the response.
231 IntakeClient intakeClient = new IntakeClient();
232 ClientResponse<String> res = intakeClient.read(knownResourceId);
233 int statusCode = res.getStatus();
235 // Check the status code of the response: does it match
236 // the expected response(s)?
237 if(logger.isDebugEnabled()){
238 logger.debug(testName + ".read: status = " + statusCode);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
244 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
245 IntakesCommon intake = (IntakesCommon) extractPart(input,
246 intakeClient.getCommonPartName(), IntakesCommon.class);
247 Assert.assertNotNull(intake);
248 // Check a couple of fields
249 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
250 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
251 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
253 // Get the auth refs and check them
254 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
255 statusCode = res2.getStatus();
257 if(logger.isDebugEnabled()){
258 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
263 AuthorityRefList list = res2.getEntity();
265 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
266 int numAuthRefsFound = items.size();
267 if(logger.isDebugEnabled()){
268 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
269 " authority references, found " + numAuthRefsFound);
271 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
272 "Did not find all expected authority references! " +
273 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
275 // Optionally output additional data about list members for debugging.
276 boolean iterateThroughList = true;
277 if(iterateThroughList && logger.isDebugEnabled()){
279 for(AuthorityRefList.AuthorityRefItem item : items){
280 logger.debug(testName + ": list-item[" + i + "] Field:" +
281 item.getSourceField() + "= " +
282 item.getAuthDisplayName() +
283 item.getItemDisplayName());
284 logger.debug(testName + ": list-item[" + i + "] refName=" +
286 logger.debug(testName + ": list-item[" + i + "] URI=" +
294 // ---------------------------------------------------------------
295 // Cleanup of resources created during testing
296 // ---------------------------------------------------------------
299 * Deletes all resources created by tests, after all tests have been run.
301 * This cleanup method will always be run, even if one or more tests fail.
302 * For this reason, it attempts to remove all resources created
303 * at any point during testing, even if some of those resources
304 * may be expected to be deleted by certain tests.
306 @AfterClass(alwaysRun=true)
307 public void cleanUp() {
308 String noTest = System.getProperty("noTestCleanup");
309 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
310 if (logger.isDebugEnabled()) {
311 logger.debug("Skipping Cleanup phase ...");
315 if (logger.isDebugEnabled()) {
316 logger.debug("Cleaning up temporary resources created for testing ...");
318 IntakeClient intakeClient = new IntakeClient();
319 // Note: Any non-success responses are ignored and not reported.
320 for (String resourceId : intakeIdsCreated) {
321 intakeClient.delete(resourceId).releaseConnection();
323 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
324 // Delete persons before PersonAuth
325 for (String resourceId : personIdsCreated) {
326 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
328 personAuthClient.delete(personAuthCSID).releaseConnection();
331 // ---------------------------------------------------------------
332 // Utility methods used by tests above
333 // ---------------------------------------------------------------
335 public String getServicePathComponent() {
336 return SERVICE_PATH_COMPONENT;
339 private PoxPayloadOut createIntakeInstance(String entryNumber,
343 String conditionCheckerAssessor,
346 IntakesCommon intake = new IntakesCommon();
347 intake.setEntryNumber(entryNumber);
348 intake.setEntryDate(entryDate);
349 intake.setCurrentOwner(currentOwner);
350 intake.setDepositor(depositor);
351 intake.setValuer(Valuer);
353 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
354 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
355 checkersOrAssessors.add(conditionCheckerAssessor);
356 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
358 InsurerList insurerList = new InsurerList();
359 List<String> insurers = insurerList.getInsurer();
360 insurers.add(insurer);
361 intake.setInsurers(insurerList);
363 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
364 PayloadOutputPart commonPart =
365 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
366 commonPart.setLabel(new IntakeClient().getCommonPartName());
368 if(logger.isDebugEnabled()){
369 logger.debug("to be created, intake common");
370 logger.debug(objectAsXmlString(intake, IntakesCommon.class));