]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d79938806ba268a5d17871e73cfebd0ffb6ed26f
[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(Response response) {
97                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98         }
99
100         // ---------------------------------------------------------------
101         // CRUD tests : CREATE tests
102         // ---------------------------------------------------------------
103         // Success outcomes
104         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105         public void createWithAuthRefs(String testName) throws Exception {
106                 // Perform setup.
107                 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
108
109                 // Submit the request to the service and store the response.
110                 String identifier = createIdentifier();
111
112                 // Create all the person refs and entities
113                 createPersonRefs();
114
115                 PoxPayloadOut multipart = createAcquisitionInstance(
116                 "April 1, 2010",
117                                 acquisitionAuthorizerRefName,
118                                 acquisitionFundingSourcesRefNames,
119                                 ownersRefNames,
120                                 acquisitionSourcesRefNames);
121
122                 AcquisitionClient acquisitionClient = new AcquisitionClient();
123                 Response res = acquisitionClient.create(multipart);
124
125                 int statusCode = res.getStatus();
126
127                 // Check the status code of the response: does it match
128                 // the expected response(s)?
129                 //
130                 // Specifically:
131                 // Does it fall within the set of valid status codes?
132                 // Does it exactly match the expected status code?
133                 if(logger.isDebugEnabled()){
134                         logger.debug(testName + ": status = " + statusCode);
135                 }
136                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
137                                 invalidStatusCodeMessage(testRequestType, statusCode));
138                 Assert.assertEquals(statusCode, testExpectedStatusCode);
139
140                 // Store the ID returned from the first resource created
141                 // for additional tests below.
142                 if (knownResourceId == null){
143                         knownResourceId = extractId(res);
144                         if (logger.isDebugEnabled()) {
145                                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
146                         }
147                 }
148
149                 // Store the IDs from every resource created by tests,
150                 // so they can be deleted after tests have been run.
151                 acquisitionIdsCreated.add(extractId(res));
152         }
153
154         protected void createPersonRefs(){
155                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
156                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
157                                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
158                 Response res = personAuthClient.create(multipart);
159                 int statusCode = res.getStatus();
160
161                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
162                                 invalidStatusCodeMessage(testRequestType, statusCode));
163                 Assert.assertEquals(statusCode, STATUS_CREATED);
164                 personAuthCSID = extractId(res);
165
166                 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
167
168                 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
169                 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
170                 personIdsCreated.add(csid);
171
172                 /*
173         csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
174         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
175         personIdsCreated.add(csid);
176
177         csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
178         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
179         personIdsCreated.add(csid);
180                  */
181
182                 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
183                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
184                 personIdsCreated.add(csid);
185
186                 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
187                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
188                 personIdsCreated.add(csid);
189
190                 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
191                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
192                 personIdsCreated.add(csid);
193
194                 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
195                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
196                 personIdsCreated.add(csid);
197         }
198
199         protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
200                 Map<String, String> personInfo = new HashMap<String,String>();
201                 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
202                 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
203                 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
204                 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
205                 PersonTermGroup term = new PersonTermGroup();
206                 String termName = firstName + " " + surName;
207                 term.setTermDisplayName(termName);
208                 term.setTermName(termName);
209                 personTerms.add(term);
210         String result = null;
211         
212                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
213                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
214                                         authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
215                 Response res = personAuthClient.createItem(personAuthCSID, multipart);
216                 try {
217                         int statusCode = res.getStatus();
218                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219                                         invalidStatusCodeMessage(testRequestType, statusCode));
220                         Assert.assertEquals(statusCode, STATUS_CREATED);
221                         result = extractId(res);
222                 } finally {
223                         res.close();
224                 }
225                 
226                 return result;
227         }
228
229         // Success outcomes
230         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
231                         dependsOnMethods = {"createWithAuthRefs"})
232                         public void readAndCheckAuthRefs(String testName) throws Exception {
233                 // Perform setup.
234                 testSetup(STATUS_OK, ServiceRequestType.READ);
235
236                 // Submit the request to the service and store the response.
237                 AcquisitionClient acquisitionClient = new AcquisitionClient();
238                 Response res = acquisitionClient.read(knownResourceId);
239                 AcquisitionsCommon acquisition = null;
240                 try {
241                         // Check the status code of the response: does it match
242                         // the expected response(s)?
243                         assertStatusCode(res, testName);
244                         PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
245                         acquisition = (AcquisitionsCommon) extractPart(input,
246                                         acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
247                         Assert.assertNotNull(acquisition);
248                 } finally {
249                         if (res != null) {
250                 res.close();
251             }
252                 }
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                 //
285                 res =   acquisitionClient.getAuthorityRefs(knownResourceId);
286                 AuthorityRefList list = null;
287                 try {
288                         assertStatusCode(res, testName);
289                         list = (AuthorityRefList)res.getEntity();
290                         Assert.assertNotNull(list);
291                 } finally {
292                         if (res != null) {
293                                 res.close();
294             }
295                 }
296
297                 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
298                 int numAuthRefsFound = items.size();
299                 if (logger.isDebugEnabled()) {
300                         logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
301                                         " authority references, found " + numAuthRefsFound);
302                 }
303                 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
304                                 "Did not find all expected authority references! " +
305                                 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
306
307                 // Optionally output additional data about list members for debugging.
308                 boolean iterateThroughList = true;
309                 if(iterateThroughList && logger.isDebugEnabled()){
310                         int i = 0;
311                         for(AuthorityRefList.AuthorityRefItem item : items){
312                                 logger.debug(testName + ": list-item[" + i + "] Field:" +
313                                                 item.getSourceField() + "= " +
314                                                 item.getAuthDisplayName() +
315                                                 item.getItemDisplayName());
316                                 logger.debug(testName + ": list-item[" + i + "] refName=" +
317                                                 item.getRefName());
318                                 logger.debug(testName + ": list-item[" + i + "] URI=" +
319                                                 item.getUri());
320                                 i++;
321                         }
322                 }
323         }
324
325
326         // ---------------------------------------------------------------
327         // Cleanup of resources created during testing
328         // ---------------------------------------------------------------
329
330         /**
331          * Deletes all resources created by tests, after all tests have been run.
332          *
333          * This cleanup method will always be run, even if one or more tests fail.
334          * For this reason, it attempts to remove all resources created
335          * at any point during testing, even if some of those resources
336          * may be expected to be deleted by certain tests.
337          */
338         @AfterClass(alwaysRun=true)
339         public void cleanUp() {
340                 String noTest = System.getProperty("noTestCleanup");
341                 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
342                         if (logger.isDebugEnabled()) {
343                                 logger.debug("Skipping Cleanup phase ...");
344                         }
345                         return;
346                 }
347                 if (logger.isDebugEnabled()) {
348                         logger.debug("Cleaning up temporary resources created for testing ...");
349                 }
350                 AcquisitionClient acquisitionClient = new AcquisitionClient();
351                 for (String resourceId : acquisitionIdsCreated) {
352                         // Note: Any non-success responses are ignored and not reported.
353                         acquisitionClient.delete(resourceId).close();
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                         personAuthClient.deleteItem(personAuthCSID, resourceId).close();
360                 }
361                 // Note: Any non-success response is ignored and not reported.
362                 personAuthClient.delete(personAuthCSID).close();
363         }
364
365         // ---------------------------------------------------------------
366         // Utility methods used by tests above
367         // ---------------------------------------------------------------
368         @Override
369         public String getServicePathComponent() {
370                 return AcquisitionClient.SERVICE_PATH_COMPONENT;
371         }
372
373
374         @Override
375         protected String getServiceName() {
376                 return AcquisitionClient.SERVICE_NAME;
377         }
378
379         private PoxPayloadOut createAcquisitionInstance(
380             String accessionDateDisplayDate,
381                         String acquisitionAuthorizer,
382                         List<String> acquisitionFundingSources,
383                         List<String> acqOwners,
384                         List<String> acquisitionSources) {
385
386                 AcquisitionsCommon acquisition = new AcquisitionsCommon();
387                 StructuredDateGroup accessionDateGroup = new StructuredDateGroup(); 
388                 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
389         acquisition.setAccessionDateGroup(accessionDateGroup);
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                 AcquisitionClient acquisitionClient = new AcquisitionClient();
427                 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
428                 PayloadOutputPart commonPart =
429                         multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
430
431                 if(logger.isDebugEnabled()){
432                         logger.debug("to be created, acquisition common");
433                         logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
434                 }
435
436                 return multipart;
437         }
438 }