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 assertStatusCode(res, testName);
240 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
241 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
242 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
243 Assert.assertNotNull(acquisition);
245 // Check a couple of fields
247 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
249 // In repeatable groups of fields
251 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
252 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
253 List<String> acqFundingSourceRefNamesFound = new ArrayList();
254 for (AcquisitionFunding acqFunding : acqFundings) {
255 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
256 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
258 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
261 // In scalar repeatable fields
262 OwnerList ownersList = acquisition.getOwners();
263 List<String> owners = ownersList.getOwner();
264 for (String refName : owners) {
265 Assert.assertTrue(ownersRefNames.contains(refName));
268 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
269 List<String> sources = acquisitionSources.getAcquisitionSource();
270 for (String refName : sources) {
271 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
274 // Get the auth refs and check them
275 ClientResponse<AuthorityRefList> res2 =
276 acquisitionClient.getAuthorityRefs(knownResourceId);
277 assertStatusCode(res2, testName);
279 AuthorityRefList list = res2.getEntity();
281 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
282 int numAuthRefsFound = items.size();
283 if(logger.isDebugEnabled()){
284 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
285 " authority references, found " + numAuthRefsFound);
287 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
288 "Did not find all expected authority references! " +
289 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
291 // Optionally output additional data about list members for debugging.
292 boolean iterateThroughList = true;
293 if(iterateThroughList && logger.isDebugEnabled()){
295 for(AuthorityRefList.AuthorityRefItem item : items){
296 logger.debug(testName + ": list-item[" + i + "] Field:" +
297 item.getSourceField() + "= " +
298 item.getAuthDisplayName() +
299 item.getItemDisplayName());
300 logger.debug(testName + ": list-item[" + i + "] refName=" +
302 logger.debug(testName + ": list-item[" + i + "] URI=" +
310 // ---------------------------------------------------------------
311 // Cleanup of resources created during testing
312 // ---------------------------------------------------------------
315 * Deletes all resources created by tests, after all tests have been run.
317 * This cleanup method will always be run, even if one or more tests fail.
318 * For this reason, it attempts to remove all resources created
319 * at any point during testing, even if some of those resources
320 * may be expected to be deleted by certain tests.
322 @AfterClass(alwaysRun=true)
323 public void cleanUp() {
324 String noTest = System.getProperty("noTestCleanup");
325 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
326 if (logger.isDebugEnabled()) {
327 logger.debug("Skipping Cleanup phase ...");
331 if (logger.isDebugEnabled()) {
332 logger.debug("Cleaning up temporary resources created for testing ...");
334 AcquisitionClient acquisitionClient = new AcquisitionClient();
335 for (String resourceId : acquisitionIdsCreated) {
336 // Note: Any non-success responses are ignored and not reported.
337 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
338 res.releaseConnection();
340 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
341 // Delete persons before PersonAuth
342 for (String resourceId : personIdsCreated) {
343 // Note: Any non-success responses are ignored and not reported.
344 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
345 res.releaseConnection();
347 // Note: Any non-success response is ignored and not reported.
348 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
349 res.releaseConnection();
352 // ---------------------------------------------------------------
353 // Utility methods used by tests above
354 // ---------------------------------------------------------------
356 public String getServicePathComponent() {
357 return AcquisitionClient.SERVICE_PATH_COMPONENT;
362 protected String getServiceName() {
363 return AcquisitionClient.SERVICE_NAME;
366 private PoxPayloadOut createAcquisitionInstance(
367 String accessionDate,
368 String acquisitionAuthorizer,
369 List<String> acquisitionFundingSources,
370 List<String> acqOwners,
371 List<String> acquisitionSources) {
373 AcquisitionsCommon acquisition = new AcquisitionsCommon();
374 acquisition.setAccessionDate(accessionDate);
375 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
377 // AcquisitionFunding-related authrefs fields are *not* currently
378 // enabled, as described in issue CSPACE-2818
381 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
382 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
384 for (String acqFundingSource: acquisitionFundingSources) {
386 AcquisitionFunding acqFunding = new AcquisitionFunding();
387 acqFunding.setAcquisitionFundingSource(acqFundingSource);
388 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
389 acqFundings.add(acqFunding);
391 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
392 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
393 acqFundings.add(addtlAcqFunding);
394 acquisition.setAcquisitionFundingList(acqFundingsList);
397 OwnerList ownersList = new OwnerList();
398 List<String> owners = ownersList.getOwner();
399 for (String owner: acqOwners) {
402 acquisition.setOwners(ownersList);
404 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
405 List<String> acqSources = acqSourcesList.getAcquisitionSource();
406 for (String acqSource: acquisitionSources) {
407 acqSources.add(acqSource);
409 acquisition.setAcquisitionSources(acqSourcesList);
411 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
412 PayloadOutputPart commonPart =
413 multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
414 AcquisitionClient acquisitionClient = new AcquisitionClient();
415 commonPart.setLabel(acquisitionClient.getCommonPartName());
417 if(logger.isDebugEnabled()){
418 logger.debug("to be created, acquisition common");
419 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));