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.AcquisitionClient;
34 import org.collectionspace.services.client.CollectionSpaceClient;
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.jaxb.AbstractCommonList;
42 import org.collectionspace.services.acquisition.AcquisitionsCommon;
43 import org.collectionspace.services.acquisition.AcquisitionFunding;
44 import org.collectionspace.services.acquisition.AcquisitionFundingList;
45 import org.collectionspace.services.acquisition.AcquisitionSourceList;
46 import org.collectionspace.services.acquisition.OwnerList;
47 import org.collectionspace.services.acquisition.StructuredDateGroup;
48 import org.collectionspace.services.person.PersonTermGroup;
49 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * AcquisitionAuthRefsTest, carries out tests against a
58 * deployed and running Acquisition Service.
60 * $LastChangedRevision: 1327 $
61 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
63 public class AcquisitionAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
65 private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 // final String SERVICE_PATH_COMPONENT = AcquisitionClient.SERVICE_PATH_COMPONENT;//"acquisitions";
70 final String PERSON_AUTHORITY_NAME = "TestPersonAuthForAquisitionTest";
71 private String knownResourceId = null;
72 private List<String> acquisitionIdsCreated = new ArrayList<String>();
73 private List<String> personIdsCreated = new ArrayList<String>();
74 private String personAuthCSID = null;
75 private String acquisitionAuthorizerRefName = null;
76 private List<String> acquisitionFundingSourcesRefNames = new ArrayList<String>();
77 private List<String> ownersRefNames = new ArrayList<String>();
78 private List<String> acquisitionSourcesRefNames = new ArrayList<String>();
79 private final int NUM_AUTH_REFS_EXPECTED = 5;
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85 protected CollectionSpaceClient getClientInstance() {
86 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
92 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
99 protected AbstractCommonList getCommonList(Response response) {
100 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
103 // ---------------------------------------------------------------
104 // CRUD tests : CREATE tests
105 // ---------------------------------------------------------------
107 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
108 public void createWithAuthRefs(String testName) throws Exception {
110 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
112 // Submit the request to the service and store the response.
113 String identifier = createIdentifier();
115 // Create all the person refs and entities
118 PoxPayloadOut multipart = createAcquisitionInstance(
120 acquisitionAuthorizerRefName,
121 acquisitionFundingSourcesRefNames,
123 acquisitionSourcesRefNames);
125 AcquisitionClient acquisitionClient = new AcquisitionClient();
126 Response res = acquisitionClient.create(multipart);
128 int statusCode = res.getStatus();
130 // Check the status code of the response: does it match
131 // the expected response(s)?
134 // Does it fall within the set of valid status codes?
135 // Does it exactly match the expected status code?
136 if(logger.isDebugEnabled()){
137 logger.debug(testName + ": status = " + statusCode);
139 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
140 invalidStatusCodeMessage(testRequestType, statusCode));
141 Assert.assertEquals(statusCode, testExpectedStatusCode);
143 // Store the ID returned from the first resource created
144 // for additional tests below.
145 if (knownResourceId == null){
146 knownResourceId = extractId(res);
147 if (logger.isDebugEnabled()) {
148 logger.debug(testName + ": knownResourceId=" + knownResourceId);
152 // Store the IDs from every resource created by tests,
153 // so they can be deleted after tests have been run.
154 acquisitionIdsCreated.add(extractId(res));
157 protected void createPersonRefs(){
158 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
159 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
160 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
161 Response res = personAuthClient.create(multipart);
162 int statusCode = res.getStatus();
164 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
165 invalidStatusCodeMessage(testRequestType, statusCode));
166 Assert.assertEquals(statusCode, STATUS_CREATED);
167 personAuthCSID = extractId(res);
169 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
171 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
172 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
173 personIdsCreated.add(csid);
176 csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
177 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
178 personIdsCreated.add(csid);
180 csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
181 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
182 personIdsCreated.add(csid);
185 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
186 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
187 personIdsCreated.add(csid);
189 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
190 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
191 personIdsCreated.add(csid);
193 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
194 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
195 personIdsCreated.add(csid);
197 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
198 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
199 personIdsCreated.add(csid);
202 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
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 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
208 PersonTermGroup term = new PersonTermGroup();
209 String termName = firstName + " " + surName;
210 term.setTermDisplayName(termName);
211 term.setTermName(termName);
212 personTerms.add(term);
213 String result = null;
215 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
216 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
217 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
218 Response res = personAuthClient.createItem(personAuthCSID, multipart);
220 int statusCode = res.getStatus();
221 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
222 invalidStatusCodeMessage(testRequestType, statusCode));
223 Assert.assertEquals(statusCode, STATUS_CREATED);
224 result = extractId(res);
233 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
234 dependsOnMethods = {"createWithAuthRefs"})
235 public void readAndCheckAuthRefs(String testName) throws Exception {
237 testSetup(STATUS_OK, ServiceRequestType.READ);
239 // Submit the request to the service and store the response.
240 AcquisitionClient acquisitionClient = new AcquisitionClient();
241 Response res = acquisitionClient.read(knownResourceId);
242 AcquisitionsCommon acquisition = null;
244 // Check the status code of the response: does it match
245 // the expected response(s)?
246 assertStatusCode(res, testName);
247 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
248 acquisition = (AcquisitionsCommon) extractPart(input,
249 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
250 Assert.assertNotNull(acquisition);
257 // Check a couple of fields
259 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
261 // In repeatable groups of fields
263 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
264 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
265 List<String> acqFundingSourceRefNamesFound = new ArrayList();
266 for (AcquisitionFunding acqFunding : acqFundings) {
267 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
268 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
270 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
273 // In scalar repeatable fields
274 OwnerList ownersList = acquisition.getOwners();
275 List<String> owners = ownersList.getOwner();
276 for (String refName : owners) {
277 Assert.assertTrue(ownersRefNames.contains(refName));
280 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
281 List<String> sources = acquisitionSources.getAcquisitionSource();
282 for (String refName : sources) {
283 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
286 // Get the auth refs and check them
288 res = acquisitionClient.getAuthorityRefs(knownResourceId);
289 AuthorityRefList list = null;
291 assertStatusCode(res, testName);
292 list = res.readEntity(AuthorityRefList.class);
293 Assert.assertNotNull(list);
300 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
301 int numAuthRefsFound = items.size();
302 if (logger.isDebugEnabled()) {
303 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
304 " authority references, found " + numAuthRefsFound);
306 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
307 "Did not find all expected authority references! " +
308 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
310 // Optionally output additional data about list members for debugging.
311 boolean iterateThroughList = true;
312 if(iterateThroughList && logger.isDebugEnabled()){
314 for(AuthorityRefList.AuthorityRefItem item : items){
315 logger.debug(testName + ": list-item[" + i + "] Field:" +
316 item.getSourceField() + "= " +
317 item.getAuthDisplayName() +
318 item.getItemDisplayName());
319 logger.debug(testName + ": list-item[" + i + "] refName=" +
321 logger.debug(testName + ": list-item[" + i + "] URI=" +
329 // ---------------------------------------------------------------
330 // Cleanup of resources created during testing
331 // ---------------------------------------------------------------
334 * Deletes all resources created by tests, after all tests have been run.
336 * This cleanup method will always be run, even if one or more tests fail.
337 * For this reason, it attempts to remove all resources created
338 * at any point during testing, even if some of those resources
339 * may be expected to be deleted by certain tests.
341 @AfterClass(alwaysRun=true)
342 public void cleanUp() {
343 String noTest = System.getProperty("noTestCleanup");
344 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
345 if (logger.isDebugEnabled()) {
346 logger.debug("Skipping Cleanup phase ...");
350 if (logger.isDebugEnabled()) {
351 logger.debug("Cleaning up temporary resources created for testing ...");
353 AcquisitionClient acquisitionClient = new AcquisitionClient();
354 for (String resourceId : acquisitionIdsCreated) {
355 // Note: Any non-success responses are ignored and not reported.
356 acquisitionClient.delete(resourceId).close();
358 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
359 // Delete persons before PersonAuth
360 for (String resourceId : personIdsCreated) {
361 // Note: Any non-success responses are ignored and not reported.
362 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
364 // Note: Any non-success response is ignored and not reported.
365 personAuthClient.delete(personAuthCSID).close();
368 // ---------------------------------------------------------------
369 // Utility methods used by tests above
370 // ---------------------------------------------------------------
372 public String getServicePathComponent() {
373 return AcquisitionClient.SERVICE_PATH_COMPONENT;
378 protected String getServiceName() {
379 return AcquisitionClient.SERVICE_NAME;
382 private PoxPayloadOut createAcquisitionInstance(
383 String accessionDateDisplayDate,
384 String acquisitionAuthorizer,
385 List<String> acquisitionFundingSources,
386 List<String> acqOwners,
387 List<String> acquisitionSources) {
389 AcquisitionsCommon acquisition = new AcquisitionsCommon();
390 StructuredDateGroup accessionDateGroup = new StructuredDateGroup();
391 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
392 acquisition.setAccessionDateGroup(accessionDateGroup);
393 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
395 // AcquisitionFunding-related authrefs fields are *not* currently
396 // enabled, as described in issue CSPACE-2818
399 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
400 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
402 for (String acqFundingSource: acquisitionFundingSources) {
404 AcquisitionFunding acqFunding = new AcquisitionFunding();
405 acqFunding.setAcquisitionFundingSource(acqFundingSource);
406 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
407 acqFundings.add(acqFunding);
409 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
410 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
411 acqFundings.add(addtlAcqFunding);
412 acquisition.setAcquisitionFundingList(acqFundingsList);
415 OwnerList ownersList = new OwnerList();
416 List<String> owners = ownersList.getOwner();
417 for (String owner: acqOwners) {
420 acquisition.setOwners(ownersList);
422 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
423 List<String> acqSources = acqSourcesList.getAcquisitionSource();
424 for (String acqSource: acquisitionSources) {
425 acqSources.add(acqSource);
427 acquisition.setAcquisitionSources(acqSourcesList);
429 AcquisitionClient acquisitionClient = new AcquisitionClient();
430 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
431 PayloadOutputPart commonPart =
432 multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
434 if(logger.isDebugEnabled()){
435 logger.debug("to be created, acquisition common");
436 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));