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.AcquisitionClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
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.jaxb.AbstractCommonList;
43 import org.collectionspace.services.acquisition.AcquisitionsCommon;
44 import org.collectionspace.services.acquisition.AcquisitionFunding;
45 import org.collectionspace.services.acquisition.AcquisitionFundingList;
46 import org.collectionspace.services.acquisition.AcquisitionSourceList;
47 import org.collectionspace.services.acquisition.OwnerList;
49 import org.jboss.resteasy.client.ClientResponse;
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 * AcquisitionAuthRefsTest, carries out tests against a
61 * deployed and running Acquisition Service.
63 * $LastChangedRevision: 1327 $
64 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
66 public class AcquisitionAuthRefsTest extends BaseServiceTest {
68 private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 // final String SERVICE_PATH_COMPONENT = AcquisitionClient.SERVICE_PATH_COMPONENT;//"acquisitions";
73 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
74 private String knownResourceId = null;
75 private List<String> acquisitionIdsCreated = new ArrayList<String>();
76 private List<String> personIdsCreated = new ArrayList<String>();
77 private String personAuthCSID = null;
78 private String acquisitionAuthorizerRefName = null;
79 private List<String> acquisitionFundingSourcesRefNames = new ArrayList<String>();
80 private List<String> ownersRefNames = new ArrayList<String>();
81 private List<String> acquisitionSourcesRefNames = new ArrayList<String>();
82 private final int NUM_AUTH_REFS_EXPECTED = 5;
85 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88 protected CollectionSpaceClient getClientInstance() {
89 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
96 protected AbstractCommonList getAbstractCommonList(
97 ClientResponse<AbstractCommonList> response) {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 // ---------------------------------------------------------------
102 // CRUD tests : CREATE tests
103 // ---------------------------------------------------------------
105 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
106 public void createWithAuthRefs(String testName) throws Exception {
108 if (logger.isDebugEnabled()) {
109 logger.debug(testBanner(testName, CLASS_NAME));
113 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
115 // Submit the request to the service and store the response.
116 String identifier = createIdentifier();
118 // Create all the person refs and entities
121 PoxPayloadOut multipart = createAcquisitionInstance(
123 acquisitionAuthorizerRefName,
124 acquisitionFundingSourcesRefNames,
126 acquisitionSourcesRefNames);
128 AcquisitionClient acquisitionClient = new AcquisitionClient();
129 ClientResponse<Response> res = acquisitionClient.create(multipart);
131 int statusCode = res.getStatus();
133 // Check the status code of the response: does it match
134 // the expected response(s)?
137 // Does it fall within the set of valid status codes?
138 // Does it exactly match the expected status code?
139 if(logger.isDebugEnabled()){
140 logger.debug(testName + ": status = " + statusCode);
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
146 // Store the ID returned from the first resource created
147 // for additional tests below.
148 if (knownResourceId == null){
149 knownResourceId = extractId(res);
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": knownResourceId=" + knownResourceId);
155 // Store the IDs from every resource created by tests,
156 // so they can be deleted after tests have been run.
157 acquisitionIdsCreated.add(extractId(res));
160 protected void createPersonRefs(){
161 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
162 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
163 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
164 ClientResponse<Response> res = personAuthClient.create(multipart);
165 int statusCode = res.getStatus();
167 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169 Assert.assertEquals(statusCode, STATUS_CREATED);
170 personAuthCSID = extractId(res);
172 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
174 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
175 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
176 personIdsCreated.add(csid);
179 csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
180 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
181 personIdsCreated.add(csid);
183 csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
184 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
185 personIdsCreated.add(csid);
188 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
189 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
190 personIdsCreated.add(csid);
192 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
193 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
194 personIdsCreated.add(csid);
196 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
197 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
198 personIdsCreated.add(csid);
200 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
201 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
202 personIdsCreated.add(csid);
205 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
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 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
211 PoxPayloadOut multipart =
212 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
213 authRefName, personInfo, personAuthClient.getItemCommonPartName());
214 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
215 int statusCode = res.getStatus();
217 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
218 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
219 Assert.assertEquals(statusCode, STATUS_CREATED);
220 return extractId(res);
224 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
225 dependsOnMethods = {"createWithAuthRefs"})
226 public void readAndCheckAuthRefs(String testName) throws Exception {
228 if (logger.isDebugEnabled()) {
229 logger.debug(testBanner(testName, CLASS_NAME));
233 testSetup(STATUS_OK, ServiceRequestType.READ);
235 // Submit the request to the service and store the response.
236 AcquisitionClient acquisitionClient = new AcquisitionClient();
237 ClientResponse<String> res = acquisitionClient.read(knownResourceId);
238 int statusCode = res.getStatus();
240 // Check the status code of the response: does it match
241 // the expected response(s)?
242 if(logger.isDebugEnabled()){
243 logger.debug(testName + ".read: status = " + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
250 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
251 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
252 Assert.assertNotNull(acquisition);
254 // Check a couple of fields
256 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
258 // In repeatable groups of fields
260 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
261 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
262 List<String> acqFundingSourceRefNamesFound = new ArrayList();
263 for (AcquisitionFunding acqFunding : acqFundings) {
264 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
265 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
267 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
270 // In scalar repeatable fields
271 OwnerList ownersList = acquisition.getOwners();
272 List<String> owners = ownersList.getOwner();
273 for (String refName : owners) {
274 Assert.assertTrue(ownersRefNames.contains(refName));
277 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
278 List<String> sources = acquisitionSources.getAcquisitionSource();
279 for (String refName : sources) {
280 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
283 // Get the auth refs and check them
284 ClientResponse<AuthorityRefList> res2 =
285 acquisitionClient.getAuthorityRefs(knownResourceId);
286 statusCode = res2.getStatus();
288 if(logger.isDebugEnabled()){
289 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294 AuthorityRefList list = res2.getEntity();
296 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
297 int numAuthRefsFound = items.size();
298 if(logger.isDebugEnabled()){
299 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
300 " authority references, found " + numAuthRefsFound);
302 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
303 "Did not find all expected authority references! " +
304 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
306 // Optionally output additional data about list members for debugging.
307 boolean iterateThroughList = true;
308 if(iterateThroughList && logger.isDebugEnabled()){
310 for(AuthorityRefList.AuthorityRefItem item : items){
311 logger.debug(testName + ": list-item[" + i + "] Field:" +
312 item.getSourceField() + "= " +
313 item.getAuthDisplayName() +
314 item.getItemDisplayName());
315 logger.debug(testName + ": list-item[" + i + "] refName=" +
317 logger.debug(testName + ": list-item[" + i + "] URI=" +
325 // ---------------------------------------------------------------
326 // Cleanup of resources created during testing
327 // ---------------------------------------------------------------
330 * Deletes all resources created by tests, after all tests have been run.
332 * This cleanup method will always be run, even if one or more tests fail.
333 * For this reason, it attempts to remove all resources created
334 * at any point during testing, even if some of those resources
335 * may be expected to be deleted by certain tests.
337 @AfterClass(alwaysRun=true)
338 public void cleanUp() {
339 String noTest = System.getProperty("noTestCleanup");
340 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("Skipping Cleanup phase ...");
346 if (logger.isDebugEnabled()) {
347 logger.debug("Cleaning up temporary resources created for testing ...");
349 AcquisitionClient acquisitionClient = new AcquisitionClient();
350 for (String resourceId : acquisitionIdsCreated) {
351 // Note: Any non-success responses are ignored and not reported.
352 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
353 res.releaseConnection();
355 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
356 // Delete persons before PersonAuth
357 for (String resourceId : personIdsCreated) {
358 // Note: Any non-success responses are ignored and not reported.
359 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
360 res.releaseConnection();
362 // Note: Any non-success response is ignored and not reported.
363 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
364 res.releaseConnection();
367 // ---------------------------------------------------------------
368 // Utility methods used by tests above
369 // ---------------------------------------------------------------
371 public String getServicePathComponent() {
372 return AcquisitionClient.SERVICE_PATH_COMPONENT;
377 protected String getServiceName() {
378 return AcquisitionClient.SERVICE_NAME;
381 private PoxPayloadOut createAcquisitionInstance(
382 String accessionDate,
383 String acquisitionAuthorizer,
384 List<String> acquisitionFundingSources,
385 List<String> acqOwners,
386 List<String> acquisitionSources) {
388 AcquisitionsCommon acquisition = new AcquisitionsCommon();
389 acquisition.setAccessionDate(accessionDate);
390 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
392 // AcquisitionFunding-related authrefs fields are *not* currently
393 // enabled, as described in issue CSPACE-2818
396 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
397 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
399 for (String acqFundingSource: acquisitionFundingSources) {
401 AcquisitionFunding acqFunding = new AcquisitionFunding();
402 acqFunding.setAcquisitionFundingSource(acqFundingSource);
403 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
404 acqFundings.add(acqFunding);
406 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
407 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
408 acqFundings.add(addtlAcqFunding);
409 acquisition.setAcquisitionFundingList(acqFundingsList);
412 OwnerList ownersList = new OwnerList();
413 List<String> owners = ownersList.getOwner();
414 for (String owner: acqOwners) {
417 acquisition.setOwners(ownersList);
419 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
420 List<String> acqSources = acqSourcesList.getAcquisitionSource();
421 for (String acqSource: acquisitionSources) {
422 acqSources.add(acqSource);
424 acquisition.setAcquisitionSources(acqSourcesList);
426 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
427 PayloadOutputPart commonPart =
428 multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
429 AcquisitionClient acquisitionClient = new AcquisitionClient();
430 commonPart.setLabel(acquisitionClient.getCommonPartName());
432 if(logger.isDebugEnabled()){
433 logger.debug("to be created, acquisition common");
434 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));