]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4a6ebbc9a73aa31ba8a91241c2e774728bb85aac
[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.Response;
31
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;
49
50 import org.jboss.resteasy.client.ClientResponse;
51
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<AbstractCommonList> {
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 getCommonList(
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                 // Perform setup.
108                 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
109
110                 // Submit the request to the service and store the response.
111                 String identifier = createIdentifier();
112
113                 // Create all the person refs and entities
114                 createPersonRefs();
115
116                 PoxPayloadOut multipart = createAcquisitionInstance(
117                 "April 1, 2010",
118                                 acquisitionAuthorizerRefName,
119                                 acquisitionFundingSourcesRefNames,
120                                 ownersRefNames,
121                                 acquisitionSourcesRefNames);
122
123                 AcquisitionClient acquisitionClient = new AcquisitionClient();
124                 ClientResponse<Response> res = acquisitionClient.create(multipart);
125
126                 int statusCode = res.getStatus();
127
128                 // Check the status code of the response: does it match
129                 // the expected response(s)?
130                 //
131                 // Specifically:
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);
136                 }
137                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138                                 invalidStatusCodeMessage(testRequestType, statusCode));
139                 Assert.assertEquals(statusCode, testExpectedStatusCode);
140
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);
147                         }
148                 }
149
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));
153         }
154
155         protected void createPersonRefs(){
156                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
157                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
158                                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
159                 ClientResponse<Response> res = personAuthClient.create(multipart);
160                 int statusCode = res.getStatus();
161
162                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
163                                 invalidStatusCodeMessage(testRequestType, statusCode));
164                 Assert.assertEquals(statusCode, STATUS_CREATED);
165                 personAuthCSID = extractId(res);
166
167                 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
168
169                 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
170                 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
171                 personIdsCreated.add(csid);
172
173                 /*
174         csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
175         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
176         personIdsCreated.add(csid);
177
178         csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
179         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
180         personIdsCreated.add(csid);
181                  */
182
183                 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
184                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
185                 personIdsCreated.add(csid);
186
187                 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
188                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
189                 personIdsCreated.add(csid);
190
191                 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
192                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
193                 personIdsCreated.add(csid);
194
195                 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
196                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
197                 personIdsCreated.add(csid);
198         }
199
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();
217
218                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219                                 invalidStatusCodeMessage(testRequestType, statusCode));
220                 Assert.assertEquals(statusCode, STATUS_CREATED);
221                 return extractId(res);
222         }
223
224         // Success outcomes
225         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
226                         dependsOnMethods = {"createWithAuthRefs"})
227                         public void readAndCheckAuthRefs(String testName) throws Exception {
228                 // Perform setup.
229                 testSetup(STATUS_OK, ServiceRequestType.READ);
230
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;
235                 try {
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);
243                 } finally {
244                         if (res != null) {
245                 res.releaseConnection();
246             }
247                 }
248
249                 // Check a couple of fields
250                 // Scalar fields
251                 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
252
253                 // In repeatable groups of fields
254                 /*
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);
261         }
262         Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
263                  */
264
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));
270                 }
271
272                 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
273                 List<String> sources = acquisitionSources.getAcquisitionSource();
274                 for (String refName : sources) {
275                         Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
276                 }
277                 //
278                 // Get the auth refs and check them
279                 //
280                 ClientResponse<AuthorityRefList> res2 = acquisitionClient.getAuthorityRefs(knownResourceId);
281                 AuthorityRefList list = null;
282                 try {
283                         assertStatusCode(res2, testName);
284                         list = res2.getEntity();
285                         Assert.assertNotNull(list);
286                 } finally {
287                         if (res2 != null) {
288                                 res2.releaseConnection();
289             }
290                 }
291
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);
297                 }
298                 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
299                                 "Did not find all expected authority references! " +
300                                 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
301
302                 // Optionally output additional data about list members for debugging.
303                 boolean iterateThroughList = true;
304                 if(iterateThroughList && logger.isDebugEnabled()){
305                         int i = 0;
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=" +
312                                                 item.getRefName());
313                                 logger.debug(testName + ": list-item[" + i + "] URI=" +
314                                                 item.getUri());
315                                 i++;
316                         }
317                 }
318         }
319
320
321         // ---------------------------------------------------------------
322         // Cleanup of resources created during testing
323         // ---------------------------------------------------------------
324
325         /**
326          * Deletes all resources created by tests, after all tests have been run.
327          *
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.
332          */
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 ...");
339                         }
340                         return;
341                 }
342                 if (logger.isDebugEnabled()) {
343                         logger.debug("Cleaning up temporary resources created for testing ...");
344                 }
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();
350                 }
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();
357                 }
358                 // Note: Any non-success response is ignored and not reported.
359                 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
360                 res.releaseConnection();
361         }
362
363         // ---------------------------------------------------------------
364         // Utility methods used by tests above
365         // ---------------------------------------------------------------
366         @Override
367         public String getServicePathComponent() {
368                 return AcquisitionClient.SERVICE_PATH_COMPONENT;
369         }
370
371
372         @Override
373         protected String getServiceName() {
374                 return AcquisitionClient.SERVICE_NAME;
375         }
376
377         private PoxPayloadOut createAcquisitionInstance(
378             String accessionDateDisplayDate,
379                         String acquisitionAuthorizer,
380                         List<String> acquisitionFundingSources,
381                         List<String> acqOwners,
382                         List<String> acquisitionSources) {
383
384                 AcquisitionsCommon acquisition = new AcquisitionsCommon();
385                 StructuredDateGroup accessionDateGroup = new StructuredDateGroup(); 
386                 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
387         acquisition.setAccessionDateGroup(accessionDateGroup);
388                 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
389
390                 // AcquisitionFunding-related authrefs fields are *not* currently
391                 // enabled, as described in issue CSPACE-2818
392
393                 /*
394         AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
395         List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
396         int i = 0;
397         for (String acqFundingSource: acquisitionFundingSources) {
398             i++;
399             AcquisitionFunding acqFunding = new AcquisitionFunding();
400             acqFunding.setAcquisitionFundingSource(acqFundingSource);
401             acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
402             acqFundings.add(acqFunding);
403         }
404         AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
405         addtlAcqFunding.setAcquisitionFundingCurrency("USD");
406         acqFundings.add(addtlAcqFunding);
407         acquisition.setAcquisitionFundingList(acqFundingsList);
408                  */
409
410                 OwnerList ownersList = new OwnerList();
411                 List<String> owners = ownersList.getOwner();
412                 for (String owner: acqOwners) {
413                         owners.add(owner);
414                 }
415                 acquisition.setOwners(ownersList);
416
417                 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
418                 List<String> acqSources = acqSourcesList.getAcquisitionSource();
419                 for (String acqSource: acquisitionSources) {
420                         acqSources.add(acqSource);
421                 }
422                 acquisition.setAcquisitionSources(acqSourcesList);
423
424                 AcquisitionClient acquisitionClient = new AcquisitionClient();
425                 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
426                 PayloadOutputPart commonPart =
427                         multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
428
429                 if(logger.isDebugEnabled()){
430                         logger.debug("to be created, acquisition common");
431                         logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
432                 }
433
434                 return multipart;
435         }
436 }