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;
50 import org.jboss.resteasy.client.ClientResponse;
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<AbstractCommonList> {
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 getCommonList(Response response) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100 // ---------------------------------------------------------------
101 // CRUD tests : CREATE tests
102 // ---------------------------------------------------------------
104 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105 public void createWithAuthRefs(String testName) throws Exception {
107 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
109 // Submit the request to the service and store the response.
110 String identifier = createIdentifier();
112 // Create all the person refs and entities
115 PoxPayloadOut multipart = createAcquisitionInstance(
117 acquisitionAuthorizerRefName,
118 acquisitionFundingSourcesRefNames,
120 acquisitionSourcesRefNames);
122 AcquisitionClient acquisitionClient = new AcquisitionClient();
123 Response res = acquisitionClient.create(multipart);
125 int statusCode = res.getStatus();
127 // Check the status code of the response: does it match
128 // the expected response(s)?
131 // Does it fall within the set of valid status codes?
132 // Does it exactly match the expected status code?
133 if(logger.isDebugEnabled()){
134 logger.debug(testName + ": status = " + statusCode);
136 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
137 invalidStatusCodeMessage(testRequestType, statusCode));
138 Assert.assertEquals(statusCode, testExpectedStatusCode);
140 // Store the ID returned from the first resource created
141 // for additional tests below.
142 if (knownResourceId == null){
143 knownResourceId = extractId(res);
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149 // Store the IDs from every resource created by tests,
150 // so they can be deleted after tests have been run.
151 acquisitionIdsCreated.add(extractId(res));
154 protected void createPersonRefs(){
155 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
156 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
157 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
158 Response res = personAuthClient.create(multipart);
159 int statusCode = res.getStatus();
161 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
162 invalidStatusCodeMessage(testRequestType, statusCode));
163 Assert.assertEquals(statusCode, STATUS_CREATED);
164 personAuthCSID = extractId(res);
166 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
168 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
169 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
170 personIdsCreated.add(csid);
173 csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
174 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
175 personIdsCreated.add(csid);
177 csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
178 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
179 personIdsCreated.add(csid);
182 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
183 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
184 personIdsCreated.add(csid);
186 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
187 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
188 personIdsCreated.add(csid);
190 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
191 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
192 personIdsCreated.add(csid);
194 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
195 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
196 personIdsCreated.add(csid);
199 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
200 Map<String, String> personInfo = new HashMap<String,String>();
201 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
202 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
203 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
204 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
205 PersonTermGroup term = new PersonTermGroup();
206 String termName = firstName + " " + surName;
207 term.setTermDisplayName(termName);
208 term.setTermName(termName);
209 personTerms.add(term);
210 String result = null;
212 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
213 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
214 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
215 Response res = personAuthClient.createItem(personAuthCSID, multipart);
217 int statusCode = res.getStatus();
218 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(testRequestType, statusCode));
220 Assert.assertEquals(statusCode, STATUS_CREATED);
221 result = extractId(res);
230 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
231 dependsOnMethods = {"createWithAuthRefs"})
232 public void readAndCheckAuthRefs(String testName) throws Exception {
234 testSetup(STATUS_OK, ServiceRequestType.READ);
236 // Submit the request to the service and store the response.
237 AcquisitionClient acquisitionClient = new AcquisitionClient();
238 Response res = acquisitionClient.read(knownResourceId);
239 AcquisitionsCommon acquisition = null;
241 // Check the status code of the response: does it match
242 // the expected response(s)?
243 assertStatusCode(res, testName);
244 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
245 acquisition = (AcquisitionsCommon) extractPart(input,
246 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
247 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
285 res = acquisitionClient.getAuthorityRefs(knownResourceId);
286 AuthorityRefList list = null;
288 assertStatusCode(res, testName);
289 list = (AuthorityRefList)res.getEntity();
290 Assert.assertNotNull(list);
297 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
298 int numAuthRefsFound = items.size();
299 if (logger.isDebugEnabled()) {
300 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
301 " authority references, found " + numAuthRefsFound);
303 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
304 "Did not find all expected authority references! " +
305 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
307 // Optionally output additional data about list members for debugging.
308 boolean iterateThroughList = true;
309 if(iterateThroughList && logger.isDebugEnabled()){
311 for(AuthorityRefList.AuthorityRefItem item : items){
312 logger.debug(testName + ": list-item[" + i + "] Field:" +
313 item.getSourceField() + "= " +
314 item.getAuthDisplayName() +
315 item.getItemDisplayName());
316 logger.debug(testName + ": list-item[" + i + "] refName=" +
318 logger.debug(testName + ": list-item[" + i + "] URI=" +
326 // ---------------------------------------------------------------
327 // Cleanup of resources created during testing
328 // ---------------------------------------------------------------
331 * Deletes all resources created by tests, after all tests have been run.
333 * This cleanup method will always be run, even if one or more tests fail.
334 * For this reason, it attempts to remove all resources created
335 * at any point during testing, even if some of those resources
336 * may be expected to be deleted by certain tests.
338 @AfterClass(alwaysRun=true)
339 public void cleanUp() {
340 String noTest = System.getProperty("noTestCleanup");
341 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
342 if (logger.isDebugEnabled()) {
343 logger.debug("Skipping Cleanup phase ...");
347 if (logger.isDebugEnabled()) {
348 logger.debug("Cleaning up temporary resources created for testing ...");
350 AcquisitionClient acquisitionClient = new AcquisitionClient();
351 for (String resourceId : acquisitionIdsCreated) {
352 // Note: Any non-success responses are ignored and not reported.
353 acquisitionClient.delete(resourceId).close();
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 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
361 // Note: Any non-success response is ignored and not reported.
362 personAuthClient.delete(personAuthCSID).close();
365 // ---------------------------------------------------------------
366 // Utility methods used by tests above
367 // ---------------------------------------------------------------
369 public String getServicePathComponent() {
370 return AcquisitionClient.SERVICE_PATH_COMPONENT;
375 protected String getServiceName() {
376 return AcquisitionClient.SERVICE_NAME;
379 private PoxPayloadOut createAcquisitionInstance(
380 String accessionDateDisplayDate,
381 String acquisitionAuthorizer,
382 List<String> acquisitionFundingSources,
383 List<String> acqOwners,
384 List<String> acquisitionSources) {
386 AcquisitionsCommon acquisition = new AcquisitionsCommon();
387 StructuredDateGroup accessionDateGroup = new StructuredDateGroup();
388 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
389 acquisition.setAccessionDateGroup(accessionDateGroup);
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 AcquisitionClient acquisitionClient = new AcquisitionClient();
427 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
428 PayloadOutputPart commonPart =
429 multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
431 if(logger.isDebugEnabled()){
432 logger.debug("to be created, acquisition common");
433 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));