]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
09ff900c4e536b3bf327fe075afd61573037659f
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32
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;
48
49 import org.jboss.resteasy.client.ClientResponse;
50
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;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * AcquisitionAuthRefsTest, carries out tests against a
61  * deployed and running Acquisition Service.
62  *
63  * $LastChangedRevision: 1327 $
64  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
65  */
66 public class AcquisitionAuthRefsTest extends BaseServiceTest {
67
68         private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
69         private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70
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;
83
84         /* (non-Javadoc)
85          * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86          */
87         @Override
88         protected CollectionSpaceClient getClientInstance() {
89                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
90         }
91
92         /* (non-Javadoc)
93          * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94          */
95         @Override
96         protected AbstractCommonList getAbstractCommonList(
97                         ClientResponse<AbstractCommonList> response) {
98                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99         }
100
101         // ---------------------------------------------------------------
102         // CRUD tests : CREATE tests
103         // ---------------------------------------------------------------
104         // Success outcomes
105         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
106         public void createWithAuthRefs(String testName) throws Exception {
107
108                 if (logger.isDebugEnabled()) {
109                         logger.debug(testBanner(testName, CLASS_NAME));
110                 };
111
112                 // Perform setup.
113                 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
114
115                 // Submit the request to the service and store the response.
116                 String identifier = createIdentifier();
117
118                 // Create all the person refs and entities
119                 createPersonRefs();
120
121                 PoxPayloadOut multipart = createAcquisitionInstance(
122                                 "April 1, 2010",
123                                 acquisitionAuthorizerRefName,
124                                 acquisitionFundingSourcesRefNames,
125                                 ownersRefNames,
126                                 acquisitionSourcesRefNames);
127
128                 AcquisitionClient acquisitionClient = new AcquisitionClient();
129                 ClientResponse<Response> res = acquisitionClient.create(multipart);
130
131                 int statusCode = res.getStatus();
132
133                 // Check the status code of the response: does it match
134                 // the expected response(s)?
135                 //
136                 // Specifically:
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);
141                 }
142                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
145
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);
152                         }
153                 }
154
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));
158         }
159
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();
166
167                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169                 Assert.assertEquals(statusCode, STATUS_CREATED);
170                 personAuthCSID = extractId(res);
171
172                 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
173
174                 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
175                 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
176                 personIdsCreated.add(csid);
177
178                 /*
179         csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
180         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
181         personIdsCreated.add(csid);
182
183         csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
184         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
185         personIdsCreated.add(csid);
186                  */
187
188                 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
189                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
190                 personIdsCreated.add(csid);
191
192                 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
193                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
194                 personIdsCreated.add(csid);
195
196                 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
197                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
198                 personIdsCreated.add(csid);
199
200                 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
201                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
202                 personIdsCreated.add(csid);
203         }
204
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();
216
217                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
218                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
219                 Assert.assertEquals(statusCode, STATUS_CREATED);
220                 return extractId(res);
221         }
222
223         // Success outcomes
224         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
225                         dependsOnMethods = {"createWithAuthRefs"})
226                         public void readAndCheckAuthRefs(String testName) throws Exception {
227
228                 if (logger.isDebugEnabled()) {
229                         logger.debug(testBanner(testName, CLASS_NAME));
230                 };
231
232                 // Perform setup.
233                 testSetup(STATUS_OK, ServiceRequestType.READ);
234
235                 // Submit the request to the service and store the response.
236                 AcquisitionClient acquisitionClient = new AcquisitionClient();
237                 ClientResponse<String> res = acquisitionClient.read(knownResourceId);
238                 int statusCode = res.getStatus();
239
240                 // Check the status code of the response: does it match
241                 // the expected response(s)?
242                 if(logger.isDebugEnabled()){
243                         logger.debug(testName + ".read: status = " + statusCode);
244                 }
245                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
248
249                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
250                 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
251                                 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
252                 Assert.assertNotNull(acquisition);
253
254                 // Check a couple of fields
255                 // Scalar fields
256                 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
257
258                 // In repeatable groups of fields
259                 /*
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);
266         }
267         Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
268                  */
269
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));
275                 }
276
277                 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
278                 List<String> sources = acquisitionSources.getAcquisitionSource();
279                 for (String refName : sources) {
280                         Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
281                 }
282
283                 // Get the auth refs and check them
284                 ClientResponse<AuthorityRefList> res2 =
285                         acquisitionClient.getAuthorityRefs(knownResourceId);
286                 statusCode = res2.getStatus();
287
288                 if(logger.isDebugEnabled()){
289                         logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
290                 }
291                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294                 AuthorityRefList list = res2.getEntity();
295
296                 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
297                 int numAuthRefsFound = items.size();
298                 if(logger.isDebugEnabled()){
299                         logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
300                                         " authority references, found " + numAuthRefsFound);
301                 }
302                 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
303                                 "Did not find all expected authority references! " +
304                                 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
305
306                 // Optionally output additional data about list members for debugging.
307                 boolean iterateThroughList = true;
308                 if(iterateThroughList && logger.isDebugEnabled()){
309                         int i = 0;
310                         for(AuthorityRefList.AuthorityRefItem item : items){
311                                 logger.debug(testName + ": list-item[" + i + "] Field:" +
312                                                 item.getSourceField() + "= " +
313                                                 item.getAuthDisplayName() +
314                                                 item.getItemDisplayName());
315                                 logger.debug(testName + ": list-item[" + i + "] refName=" +
316                                                 item.getRefName());
317                                 logger.debug(testName + ": list-item[" + i + "] URI=" +
318                                                 item.getUri());
319                                 i++;
320                         }
321                 }
322         }
323
324
325         // ---------------------------------------------------------------
326         // Cleanup of resources created during testing
327         // ---------------------------------------------------------------
328
329         /**
330          * Deletes all resources created by tests, after all tests have been run.
331          *
332          * This cleanup method will always be run, even if one or more tests fail.
333          * For this reason, it attempts to remove all resources created
334          * at any point during testing, even if some of those resources
335          * may be expected to be deleted by certain tests.
336          */
337         @AfterClass(alwaysRun=true)
338         public void cleanUp() {
339                 String noTest = System.getProperty("noTestCleanup");
340                 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
341                         if (logger.isDebugEnabled()) {
342                                 logger.debug("Skipping Cleanup phase ...");
343                         }
344                         return;
345                 }
346                 if (logger.isDebugEnabled()) {
347                         logger.debug("Cleaning up temporary resources created for testing ...");
348                 }
349                 AcquisitionClient acquisitionClient = new AcquisitionClient();
350                 for (String resourceId : acquisitionIdsCreated) {
351                         // Note: Any non-success responses are ignored and not reported.
352                         ClientResponse<Response> res = acquisitionClient.delete(resourceId);
353                         res.releaseConnection();
354                 }
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                         ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
360                         res.releaseConnection();
361                 }
362                 // Note: Any non-success response is ignored and not reported.
363                 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
364                 res.releaseConnection();
365         }
366
367         // ---------------------------------------------------------------
368         // Utility methods used by tests above
369         // ---------------------------------------------------------------
370         @Override
371         public String getServicePathComponent() {
372                 return AcquisitionClient.SERVICE_PATH_COMPONENT;
373         }
374
375
376         @Override
377         protected String getServiceName() {
378                 return AcquisitionClient.SERVICE_NAME;
379         }
380
381         private PoxPayloadOut createAcquisitionInstance(
382                         String accessionDate,
383                         String acquisitionAuthorizer,
384                         List<String> acquisitionFundingSources,
385                         List<String> acqOwners,
386                         List<String> acquisitionSources) {
387
388                 AcquisitionsCommon acquisition = new AcquisitionsCommon();
389                 acquisition.setAccessionDate(accessionDate);
390                 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
391
392                 // AcquisitionFunding-related authrefs fields are *not* currently
393                 // enabled, as described in issue CSPACE-2818
394
395                 /*
396         AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
397         List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
398         int i = 0;
399         for (String acqFundingSource: acquisitionFundingSources) {
400             i++;
401             AcquisitionFunding acqFunding = new AcquisitionFunding();
402             acqFunding.setAcquisitionFundingSource(acqFundingSource);
403             acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
404             acqFundings.add(acqFunding);
405         }
406         AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
407         addtlAcqFunding.setAcquisitionFundingCurrency("USD");
408         acqFundings.add(addtlAcqFunding);
409         acquisition.setAcquisitionFundingList(acqFundingsList);
410                  */
411
412                 OwnerList ownersList = new OwnerList();
413                 List<String> owners = ownersList.getOwner();
414                 for (String owner: acqOwners) {
415                         owners.add(owner);
416                 }
417                 acquisition.setOwners(ownersList);
418
419                 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
420                 List<String> acqSources = acqSourcesList.getAcquisitionSource();
421                 for (String acqSource: acquisitionSources) {
422                         acqSources.add(acqSource);
423                 }
424                 acquisition.setAcquisitionSources(acqSourcesList);
425
426                 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
427                 PayloadOutputPart commonPart =
428                         multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
429                 AcquisitionClient acquisitionClient = new AcquisitionClient();
430                 commonPart.setLabel(acquisitionClient.getCommonPartName());
431
432                 if(logger.isDebugEnabled()){
433                         logger.debug("to be created, acquisition common");
434                         logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
435                 }
436
437                 return multipart;
438         }
439 }