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(
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 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
110 // Submit the request to the service and store the response.
111 String identifier = createIdentifier();
113 // Create all the person refs and entities
116 PoxPayloadOut multipart = createAcquisitionInstance(
118 acquisitionAuthorizerRefName,
119 acquisitionFundingSourcesRefNames,
121 acquisitionSourcesRefNames);
123 AcquisitionClient acquisitionClient = new AcquisitionClient();
124 Response res = acquisitionClient.create(multipart);
126 int statusCode = res.getStatus();
128 // Check the status code of the response: does it match
129 // the expected response(s)?
132 // Does it fall within the set of valid status codes?
133 // Does it exactly match the expected status code?
134 if(logger.isDebugEnabled()){
135 logger.debug(testName + ": status = " + statusCode);
137 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(testRequestType, statusCode));
139 Assert.assertEquals(statusCode, testExpectedStatusCode);
141 // Store the ID returned from the first resource created
142 // for additional tests below.
143 if (knownResourceId == null){
144 knownResourceId = extractId(res);
145 if (logger.isDebugEnabled()) {
146 logger.debug(testName + ": knownResourceId=" + knownResourceId);
150 // Store the IDs from every resource created by tests,
151 // so they can be deleted after tests have been run.
152 acquisitionIdsCreated.add(extractId(res));
155 protected void createPersonRefs(){
156 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
157 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
158 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
159 Response res = personAuthClient.create(multipart);
160 int statusCode = res.getStatus();
162 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
163 invalidStatusCodeMessage(testRequestType, statusCode));
164 Assert.assertEquals(statusCode, STATUS_CREATED);
165 personAuthCSID = extractId(res);
167 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
169 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
170 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
171 personIdsCreated.add(csid);
174 csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
175 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
176 personIdsCreated.add(csid);
178 csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
179 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
180 personIdsCreated.add(csid);
183 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
184 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
185 personIdsCreated.add(csid);
187 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
188 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
189 personIdsCreated.add(csid);
191 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
192 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
193 personIdsCreated.add(csid);
195 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
196 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
197 personIdsCreated.add(csid);
200 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
201 Map<String, String> personInfo = new HashMap<String,String>();
202 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
203 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
204 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
205 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
206 PersonTermGroup term = new PersonTermGroup();
207 String termName = firstName + " " + surName;
208 term.setTermDisplayName(termName);
209 term.setTermName(termName);
210 personTerms.add(term);
211 String result = null;
213 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
214 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
215 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
216 Response res = personAuthClient.createItem(personAuthCSID, multipart);
218 int statusCode = res.getStatus();
219 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
220 invalidStatusCodeMessage(testRequestType, statusCode));
221 Assert.assertEquals(statusCode, STATUS_CREATED);
222 result = extractId(res);
231 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
232 dependsOnMethods = {"createWithAuthRefs"})
233 public void readAndCheckAuthRefs(String testName) throws Exception {
235 testSetup(STATUS_OK, ServiceRequestType.READ);
237 // Submit the request to the service and store the response.
238 AcquisitionClient acquisitionClient = new AcquisitionClient();
239 Response res = acquisitionClient.read(knownResourceId);
240 AcquisitionsCommon acquisition = null;
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 assertStatusCode(res, testName);
245 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
246 acquisition = (AcquisitionsCommon) extractPart(input,
247 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
248 Assert.assertNotNull(acquisition);
255 // Check a couple of fields
257 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
259 // In repeatable groups of fields
261 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
262 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
263 List<String> acqFundingSourceRefNamesFound = new ArrayList();
264 for (AcquisitionFunding acqFunding : acqFundings) {
265 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
266 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
268 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
271 // In scalar repeatable fields
272 OwnerList ownersList = acquisition.getOwners();
273 List<String> owners = ownersList.getOwner();
274 for (String refName : owners) {
275 Assert.assertTrue(ownersRefNames.contains(refName));
278 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
279 List<String> sources = acquisitionSources.getAcquisitionSource();
280 for (String refName : sources) {
281 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
284 // Get the auth refs and check them
286 res = acquisitionClient.getAuthorityRefs(knownResourceId);
287 AuthorityRefList list = null;
289 assertStatusCode(res, testName);
290 list = (AuthorityRefList)res.getEntity();
291 Assert.assertNotNull(list);
298 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
299 int numAuthRefsFound = items.size();
300 if (logger.isDebugEnabled()) {
301 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
302 " authority references, found " + numAuthRefsFound);
304 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
305 "Did not find all expected authority references! " +
306 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
308 // Optionally output additional data about list members for debugging.
309 boolean iterateThroughList = true;
310 if(iterateThroughList && logger.isDebugEnabled()){
312 for(AuthorityRefList.AuthorityRefItem item : items){
313 logger.debug(testName + ": list-item[" + i + "] Field:" +
314 item.getSourceField() + "= " +
315 item.getAuthDisplayName() +
316 item.getItemDisplayName());
317 logger.debug(testName + ": list-item[" + i + "] refName=" +
319 logger.debug(testName + ": list-item[" + i + "] URI=" +
327 // ---------------------------------------------------------------
328 // Cleanup of resources created during testing
329 // ---------------------------------------------------------------
332 * Deletes all resources created by tests, after all tests have been run.
334 * This cleanup method will always be run, even if one or more tests fail.
335 * For this reason, it attempts to remove all resources created
336 * at any point during testing, even if some of those resources
337 * may be expected to be deleted by certain tests.
339 @AfterClass(alwaysRun=true)
340 public void cleanUp() {
341 String noTest = System.getProperty("noTestCleanup");
342 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
343 if (logger.isDebugEnabled()) {
344 logger.debug("Skipping Cleanup phase ...");
348 if (logger.isDebugEnabled()) {
349 logger.debug("Cleaning up temporary resources created for testing ...");
351 AcquisitionClient acquisitionClient = new AcquisitionClient();
352 for (String resourceId : acquisitionIdsCreated) {
353 // Note: Any non-success responses are ignored and not reported.
354 acquisitionClient.delete(resourceId).close();
356 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
357 // Delete persons before PersonAuth
358 for (String resourceId : personIdsCreated) {
359 // Note: Any non-success responses are ignored and not reported.
360 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
362 // Note: Any non-success response is ignored and not reported.
363 personAuthClient.delete(personAuthCSID).close();
366 // ---------------------------------------------------------------
367 // Utility methods used by tests above
368 // ---------------------------------------------------------------
370 public String getServicePathComponent() {
371 return AcquisitionClient.SERVICE_PATH_COMPONENT;
376 protected String getServiceName() {
377 return AcquisitionClient.SERVICE_NAME;
380 private PoxPayloadOut createAcquisitionInstance(
381 String accessionDateDisplayDate,
382 String acquisitionAuthorizer,
383 List<String> acquisitionFundingSources,
384 List<String> acqOwners,
385 List<String> acquisitionSources) {
387 AcquisitionsCommon acquisition = new AcquisitionsCommon();
388 StructuredDateGroup accessionDateGroup = new StructuredDateGroup();
389 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
390 acquisition.setAccessionDateGroup(accessionDateGroup);
391 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
393 // AcquisitionFunding-related authrefs fields are *not* currently
394 // enabled, as described in issue CSPACE-2818
397 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
398 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
400 for (String acqFundingSource: acquisitionFundingSources) {
402 AcquisitionFunding acqFunding = new AcquisitionFunding();
403 acqFunding.setAcquisitionFundingSource(acqFundingSource);
404 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
405 acqFundings.add(acqFunding);
407 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
408 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
409 acqFundings.add(addtlAcqFunding);
410 acquisition.setAcquisitionFundingList(acqFundingsList);
413 OwnerList ownersList = new OwnerList();
414 List<String> owners = ownersList.getOwner();
415 for (String owner: acqOwners) {
418 acquisition.setOwners(ownersList);
420 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
421 List<String> acqSources = acqSourcesList.getAcquisitionSource();
422 for (String acqSource: acquisitionSources) {
423 acqSources.add(acqSource);
425 acquisition.setAcquisitionSources(acqSourcesList);
427 AcquisitionClient acquisitionClient = new AcquisitionClient();
428 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
429 PayloadOutputPart commonPart =
430 multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
432 if(logger.isDebugEnabled()){
433 logger.debug("to be created, acquisition common");
434 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));