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 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
212 PoxPayloadOut multipart =
213 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
214 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
215 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
216 int statusCode = res.getStatus();
218 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(testRequestType, statusCode));
220 Assert.assertEquals(statusCode, STATUS_CREATED);
221 return extractId(res);
225 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
226 dependsOnMethods = {"createWithAuthRefs"})
227 public void readAndCheckAuthRefs(String testName) throws Exception {
229 testSetup(STATUS_OK, ServiceRequestType.READ);
231 // Submit the request to the service and store the response.
232 AcquisitionClient acquisitionClient = new AcquisitionClient();
233 ClientResponse<String> res = acquisitionClient.read(knownResourceId);
234 AcquisitionsCommon acquisition = null;
236 // Check the status code of the response: does it match
237 // the expected response(s)?
238 assertStatusCode(res, testName);
239 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
240 acquisition = (AcquisitionsCommon) extractPart(input,
241 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
242 Assert.assertNotNull(acquisition);
245 res.releaseConnection();
249 // Check a couple of fields
251 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
253 // In repeatable groups of fields
255 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
256 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
257 List<String> acqFundingSourceRefNamesFound = new ArrayList();
258 for (AcquisitionFunding acqFunding : acqFundings) {
259 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
260 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
262 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
265 // In scalar repeatable fields
266 OwnerList ownersList = acquisition.getOwners();
267 List<String> owners = ownersList.getOwner();
268 for (String refName : owners) {
269 Assert.assertTrue(ownersRefNames.contains(refName));
272 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
273 List<String> sources = acquisitionSources.getAcquisitionSource();
274 for (String refName : sources) {
275 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
278 // Get the auth refs and check them
280 ClientResponse<AuthorityRefList> res2 = acquisitionClient.getAuthorityRefs(knownResourceId);
281 AuthorityRefList list = null;
283 assertStatusCode(res2, testName);
284 list = res2.getEntity();
285 Assert.assertNotNull(list);
288 res2.releaseConnection();
292 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
293 int numAuthRefsFound = items.size();
294 if (logger.isDebugEnabled()){
295 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
296 " authority references, found " + numAuthRefsFound);
298 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
299 "Did not find all expected authority references! " +
300 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
302 // Optionally output additional data about list members for debugging.
303 boolean iterateThroughList = true;
304 if(iterateThroughList && logger.isDebugEnabled()){
306 for(AuthorityRefList.AuthorityRefItem item : items){
307 logger.debug(testName + ": list-item[" + i + "] Field:" +
308 item.getSourceField() + "= " +
309 item.getAuthDisplayName() +
310 item.getItemDisplayName());
311 logger.debug(testName + ": list-item[" + i + "] refName=" +
313 logger.debug(testName + ": list-item[" + i + "] URI=" +
321 // ---------------------------------------------------------------
322 // Cleanup of resources created during testing
323 // ---------------------------------------------------------------
326 * Deletes all resources created by tests, after all tests have been run.
328 * This cleanup method will always be run, even if one or more tests fail.
329 * For this reason, it attempts to remove all resources created
330 * at any point during testing, even if some of those resources
331 * may be expected to be deleted by certain tests.
333 @AfterClass(alwaysRun=true)
334 public void cleanUp() {
335 String noTest = System.getProperty("noTestCleanup");
336 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
337 if (logger.isDebugEnabled()) {
338 logger.debug("Skipping Cleanup phase ...");
342 if (logger.isDebugEnabled()) {
343 logger.debug("Cleaning up temporary resources created for testing ...");
345 AcquisitionClient acquisitionClient = new AcquisitionClient();
346 for (String resourceId : acquisitionIdsCreated) {
347 // Note: Any non-success responses are ignored and not reported.
348 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
349 res.releaseConnection();
351 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
352 // Delete persons before PersonAuth
353 for (String resourceId : personIdsCreated) {
354 // Note: Any non-success responses are ignored and not reported.
355 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
356 res.releaseConnection();
358 // Note: Any non-success response is ignored and not reported.
359 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
360 res.releaseConnection();
363 // ---------------------------------------------------------------
364 // Utility methods used by tests above
365 // ---------------------------------------------------------------
367 public String getServicePathComponent() {
368 return AcquisitionClient.SERVICE_PATH_COMPONENT;
373 protected String getServiceName() {
374 return AcquisitionClient.SERVICE_NAME;
377 private PoxPayloadOut createAcquisitionInstance(
378 String accessionDateDisplayDate,
379 String acquisitionAuthorizer,
380 List<String> acquisitionFundingSources,
381 List<String> acqOwners,
382 List<String> acquisitionSources) {
384 AcquisitionsCommon acquisition = new AcquisitionsCommon();
385 StructuredDateGroup accessionDateGroup = new StructuredDateGroup();
386 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
387 acquisition.setAccessionDateGroup(accessionDateGroup);
388 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
390 // AcquisitionFunding-related authrefs fields are *not* currently
391 // enabled, as described in issue CSPACE-2818
394 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
395 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
397 for (String acqFundingSource: acquisitionFundingSources) {
399 AcquisitionFunding acqFunding = new AcquisitionFunding();
400 acqFunding.setAcquisitionFundingSource(acqFundingSource);
401 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
402 acqFundings.add(acqFunding);
404 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
405 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
406 acqFundings.add(addtlAcqFunding);
407 acquisition.setAcquisitionFundingList(acqFundingsList);
410 OwnerList ownersList = new OwnerList();
411 List<String> owners = ownersList.getOwner();
412 for (String owner: acqOwners) {
415 acquisition.setOwners(ownersList);
417 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
418 List<String> acqSources = acqSourcesList.getAcquisitionSource();
419 for (String acqSource: acquisitionSources) {
420 acqSources.add(acqSource);
422 acquisition.setAcquisitionSources(acqSourcesList);
424 AcquisitionClient acquisitionClient = new AcquisitionClient();
425 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
426 PayloadOutputPart commonPart =
427 multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
429 if(logger.isDebugEnabled()){
430 logger.debug("to be created, acquisition common");
431 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));