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;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * AcquisitionAuthRefsTest, carries out tests against a
59 * deployed and running Acquisition Service.
61 * $LastChangedRevision: 1327 $
62 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
64 public class AcquisitionAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
66 private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
67 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69 // Instance variables specific to this test.
70 // final String SERVICE_PATH_COMPONENT = AcquisitionClient.SERVICE_PATH_COMPONENT;//"acquisitions";
71 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
72 private String knownResourceId = null;
73 private List<String> acquisitionIdsCreated = new ArrayList<String>();
74 private List<String> personIdsCreated = new ArrayList<String>();
75 private String personAuthCSID = null;
76 private String acquisitionAuthorizerRefName = null;
77 private List<String> acquisitionFundingSourcesRefNames = new ArrayList<String>();
78 private List<String> ownersRefNames = new ArrayList<String>();
79 private List<String> acquisitionSourcesRefNames = new ArrayList<String>();
80 private final int NUM_AUTH_REFS_EXPECTED = 5;
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
87 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94 protected AbstractCommonList getCommonList(
95 ClientResponse<AbstractCommonList> response) {
96 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99 // ---------------------------------------------------------------
100 // CRUD tests : CREATE tests
101 // ---------------------------------------------------------------
103 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
104 public void createWithAuthRefs(String testName) throws Exception {
106 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
108 // Submit the request to the service and store the response.
109 String identifier = createIdentifier();
111 // Create all the person refs and entities
114 PoxPayloadOut multipart = createAcquisitionInstance(
115 acquisitionAuthorizerRefName,
116 acquisitionFundingSourcesRefNames,
118 acquisitionSourcesRefNames);
121 AcquisitionClient acquisitionClient = new AcquisitionClient();
122 ClientResponse<Response> res = acquisitionClient.create(multipart);
124 int statusCode = res.getStatus();
126 // Check the status code of the response: does it match
127 // the expected response(s)?
130 // Does it fall within the set of valid status codes?
131 // Does it exactly match the expected status code?
132 if(logger.isDebugEnabled()){
133 logger.debug(testName + ": status = " + statusCode);
135 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
136 invalidStatusCodeMessage(testRequestType, statusCode));
137 Assert.assertEquals(statusCode, testExpectedStatusCode);
139 // Store the ID returned from the first resource created
140 // for additional tests below.
141 if (knownResourceId == null){
142 knownResourceId = extractId(res);
143 if (logger.isDebugEnabled()) {
144 logger.debug(testName + ": knownResourceId=" + knownResourceId);
148 // Store the IDs from every resource created by tests,
149 // so they can be deleted after tests have been run.
150 acquisitionIdsCreated.add(extractId(res));
153 protected void createPersonRefs(){
154 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
155 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
156 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
157 ClientResponse<Response> res = personAuthClient.create(multipart);
158 int statusCode = res.getStatus();
160 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
161 invalidStatusCodeMessage(testRequestType, statusCode));
162 Assert.assertEquals(statusCode, STATUS_CREATED);
163 personAuthCSID = extractId(res);
165 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
167 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
168 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
169 personIdsCreated.add(csid);
172 csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
173 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
174 personIdsCreated.add(csid);
176 csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
177 acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
178 personIdsCreated.add(csid);
181 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
182 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
183 personIdsCreated.add(csid);
185 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
186 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
187 personIdsCreated.add(csid);
189 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
190 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
191 personIdsCreated.add(csid);
193 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
194 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
195 personIdsCreated.add(csid);
198 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
199 Map<String, String> personInfo = new HashMap<String,String>();
200 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
201 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
202 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
203 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
204 PoxPayloadOut multipart =
205 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
206 authRefName, personInfo, personAuthClient.getItemCommonPartName());
207 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
208 int statusCode = res.getStatus();
210 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(testRequestType, statusCode));
212 Assert.assertEquals(statusCode, STATUS_CREATED);
213 return extractId(res);
217 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
218 dependsOnMethods = {"createWithAuthRefs"})
219 public void readAndCheckAuthRefs(String testName) throws Exception {
221 testSetup(STATUS_OK, ServiceRequestType.READ);
223 // Submit the request to the service and store the response.
224 AcquisitionClient acquisitionClient = new AcquisitionClient();
225 ClientResponse<String> res = acquisitionClient.read(knownResourceId);
226 AcquisitionsCommon acquisition = null;
228 // Check the status code of the response: does it match
229 // the expected response(s)?
230 assertStatusCode(res, testName);
231 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
232 acquisition = (AcquisitionsCommon) extractPart(input,
233 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
234 Assert.assertNotNull(acquisition);
237 res.releaseConnection();
241 // Check a couple of fields
243 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
245 // In repeatable groups of fields
247 AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
248 List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
249 List<String> acqFundingSourceRefNamesFound = new ArrayList();
250 for (AcquisitionFunding acqFunding : acqFundings) {
251 String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
252 acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
254 Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
257 // In scalar repeatable fields
258 OwnerList ownersList = acquisition.getOwners();
259 List<String> owners = ownersList.getOwner();
260 for (String refName : owners) {
261 Assert.assertTrue(ownersRefNames.contains(refName));
264 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
265 List<String> sources = acquisitionSources.getAcquisitionSource();
266 for (String refName : sources) {
267 Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
270 // Get the auth refs and check them
272 ClientResponse<AuthorityRefList> res2 = acquisitionClient.getAuthorityRefs(knownResourceId);
273 AuthorityRefList list = null;
275 assertStatusCode(res2, testName);
276 list = res2.getEntity();
277 Assert.assertNotNull(list);
280 res2.releaseConnection();
284 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
285 int numAuthRefsFound = items.size();
286 if (logger.isDebugEnabled()){
287 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
288 " authority references, found " + numAuthRefsFound);
290 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
291 "Did not find all expected authority references! " +
292 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
294 // Optionally output additional data about list members for debugging.
295 boolean iterateThroughList = true;
296 if(iterateThroughList && logger.isDebugEnabled()){
298 for(AuthorityRefList.AuthorityRefItem item : items){
299 logger.debug(testName + ": list-item[" + i + "] Field:" +
300 item.getSourceField() + "= " +
301 item.getAuthDisplayName() +
302 item.getItemDisplayName());
303 logger.debug(testName + ": list-item[" + i + "] refName=" +
305 logger.debug(testName + ": list-item[" + i + "] URI=" +
313 // ---------------------------------------------------------------
314 // Cleanup of resources created during testing
315 // ---------------------------------------------------------------
318 * Deletes all resources created by tests, after all tests have been run.
320 * This cleanup method will always be run, even if one or more tests fail.
321 * For this reason, it attempts to remove all resources created
322 * at any point during testing, even if some of those resources
323 * may be expected to be deleted by certain tests.
325 @AfterClass(alwaysRun=true)
326 public void cleanUp() {
327 String noTest = System.getProperty("noTestCleanup");
328 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
329 if (logger.isDebugEnabled()) {
330 logger.debug("Skipping Cleanup phase ...");
334 if (logger.isDebugEnabled()) {
335 logger.debug("Cleaning up temporary resources created for testing ...");
337 AcquisitionClient acquisitionClient = new AcquisitionClient();
338 for (String resourceId : acquisitionIdsCreated) {
339 // Note: Any non-success responses are ignored and not reported.
340 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
341 res.releaseConnection();
343 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
344 // Delete persons before PersonAuth
345 for (String resourceId : personIdsCreated) {
346 // Note: Any non-success responses are ignored and not reported.
347 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
348 res.releaseConnection();
350 // Note: Any non-success response is ignored and not reported.
351 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
352 res.releaseConnection();
355 // ---------------------------------------------------------------
356 // Utility methods used by tests above
357 // ---------------------------------------------------------------
359 public String getServicePathComponent() {
360 return AcquisitionClient.SERVICE_PATH_COMPONENT;
365 protected String getServiceName() {
366 return AcquisitionClient.SERVICE_NAME;
369 private PoxPayloadOut createAcquisitionInstance(
370 String acquisitionAuthorizer,
371 List<String> acquisitionFundingSources,
372 List<String> acqOwners,
373 List<String> acquisitionSources) {
375 AcquisitionsCommon acquisition = new AcquisitionsCommon();
376 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
378 // AcquisitionFunding-related authrefs fields are *not* currently
379 // enabled, as described in issue CSPACE-2818
382 AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
383 List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
385 for (String acqFundingSource: acquisitionFundingSources) {
387 AcquisitionFunding acqFunding = new AcquisitionFunding();
388 acqFunding.setAcquisitionFundingSource(acqFundingSource);
389 acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
390 acqFundings.add(acqFunding);
392 AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
393 addtlAcqFunding.setAcquisitionFundingCurrency("USD");
394 acqFundings.add(addtlAcqFunding);
395 acquisition.setAcquisitionFundingList(acqFundingsList);
398 OwnerList ownersList = new OwnerList();
399 List<String> owners = ownersList.getOwner();
400 for (String owner: acqOwners) {
403 acquisition.setOwners(ownersList);
405 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
406 List<String> acqSources = acqSourcesList.getAcquisitionSource();
407 for (String acqSource: acquisitionSources) {
408 acqSources.add(acqSource);
410 acquisition.setAcquisitionSources(acqSourcesList);
412 AcquisitionClient acquisitionClient = new AcquisitionClient();
413 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
414 PayloadOutputPart commonPart =
415 multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
417 if(logger.isDebugEnabled()){
418 logger.debug("to be created, acquisition common");
419 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));