]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1e64da1c2f841b76e0d75fe0194779c37a746c42
[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 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * AcquisitionAuthRefsTest, carries out tests against a
58  * deployed and running Acquisition Service.
59  *
60  * $LastChangedRevision: 1327 $
61  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62  */
63 public class AcquisitionAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
64
65         private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
66         private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67
68         // Instance variables specific to this test.
69         //    final String SERVICE_PATH_COMPONENT = AcquisitionClient.SERVICE_PATH_COMPONENT;//"acquisitions";
70         final String PERSON_AUTHORITY_NAME = "TestPersonAuthForAquisitionTest";
71         private String knownResourceId = null;
72         private List<String> acquisitionIdsCreated = new ArrayList<String>();
73         private List<String> personIdsCreated = new ArrayList<String>();
74         private String personAuthCSID = null; 
75         private String acquisitionAuthorizerRefName = null;
76         private List<String> acquisitionFundingSourcesRefNames = new ArrayList<String>();
77         private List<String> ownersRefNames = new ArrayList<String>();
78         private List<String> acquisitionSourcesRefNames = new ArrayList<String>();
79         private final int NUM_AUTH_REFS_EXPECTED = 5;
80
81         /* (non-Javadoc)
82          * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
83          */
84         @Override
85         protected CollectionSpaceClient getClientInstance() {
86                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
87         }
88         
89
90         @Override
91         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
92                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93         }       
94
95         /* (non-Javadoc)
96          * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
97          */
98         @Override
99         protected AbstractCommonList getCommonList(Response response) {
100                 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101         }
102
103         // ---------------------------------------------------------------
104         // CRUD tests : CREATE tests
105         // ---------------------------------------------------------------
106         // Success outcomes
107         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
108         public void createWithAuthRefs(String testName) throws Exception {
109                 // Perform setup.
110                 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
111
112                 // Submit the request to the service and store the response.
113                 String identifier = createIdentifier();
114
115                 // Create all the person refs and entities
116                 createPersonRefs();
117
118                 PoxPayloadOut multipart = createAcquisitionInstance(
119                 "April 1, 2010",
120                                 acquisitionAuthorizerRefName,
121                                 acquisitionFundingSourcesRefNames,
122                                 ownersRefNames,
123                                 acquisitionSourcesRefNames);
124
125                 AcquisitionClient acquisitionClient = new AcquisitionClient();
126                 Response res = acquisitionClient.create(multipart);
127
128                 int statusCode = res.getStatus();
129
130                 // Check the status code of the response: does it match
131                 // the expected response(s)?
132                 //
133                 // Specifically:
134                 // Does it fall within the set of valid status codes?
135                 // Does it exactly match the expected status code?
136                 if(logger.isDebugEnabled()){
137                         logger.debug(testName + ": status = " + statusCode);
138                 }
139                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
140                                 invalidStatusCodeMessage(testRequestType, statusCode));
141                 Assert.assertEquals(statusCode, testExpectedStatusCode);
142
143                 // Store the ID returned from the first resource created
144                 // for additional tests below.
145                 if (knownResourceId == null){
146                         knownResourceId = extractId(res);
147                         if (logger.isDebugEnabled()) {
148                                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149                         }
150                 }
151
152                 // Store the IDs from every resource created by tests,
153                 // so they can be deleted after tests have been run.
154                 acquisitionIdsCreated.add(extractId(res));
155         }
156
157         protected void createPersonRefs(){
158                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
159                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
160                                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
161                 Response res = personAuthClient.create(multipart);
162                 int statusCode = res.getStatus();
163
164                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
165                                 invalidStatusCodeMessage(testRequestType, statusCode));
166                 Assert.assertEquals(statusCode, STATUS_CREATED);
167                 personAuthCSID = extractId(res);
168
169                 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
170
171                 String csid = createPerson("Annie", "Authorizer", "annieAuth", authRefName);
172                 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
173                 personIdsCreated.add(csid);
174
175                 /*
176         csid = createPerson("Fran", "Funding-SourceOne", "franFundingSourceOne", authRefName);
177         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
178         personIdsCreated.add(csid);
179
180         csid = createPerson("Fahd", "Funding-SourceTwo", "fahdFundingSourceTwo", authRefName);
181         acquisitionFundingSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
182         personIdsCreated.add(csid);
183                  */
184
185                 csid = createPerson("Owen", "OwnerOne", "owenOwnerOne", authRefName);
186                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
187                 personIdsCreated.add(csid);
188
189                 csid = createPerson("Orelia", "OwnerTwo", "oreliaOwnerTwo", authRefName);
190                 ownersRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
191                 personIdsCreated.add(csid);
192
193                 csid = createPerson("Sammy", "SourceOne", "sammySourceOne", authRefName);
194                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
195                 personIdsCreated.add(csid);
196
197                 csid = createPerson("Serena", "SourceTwo", "serenaSourceTwo", authRefName);
198                 acquisitionSourcesRefNames.add(PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null));
199                 personIdsCreated.add(csid);
200         }
201
202         protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
203                 Map<String, String> personInfo = new HashMap<String,String>();
204                 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
205                 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
206                 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
207                 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
208                 PersonTermGroup term = new PersonTermGroup();
209                 String termName = firstName + " " + surName;
210                 term.setTermDisplayName(termName);
211                 term.setTermName(termName);
212                 personTerms.add(term);
213         String result = null;
214         
215                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
216                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
217                                         authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
218                 Response res = personAuthClient.createItem(personAuthCSID, multipart);
219                 try {
220                         int statusCode = res.getStatus();
221                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
222                                         invalidStatusCodeMessage(testRequestType, statusCode));
223                         Assert.assertEquals(statusCode, STATUS_CREATED);
224                         result = extractId(res);
225                 } finally {
226                         res.close();
227                 }
228                 
229                 return result;
230         }
231
232         // Success outcomes
233         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
234                         dependsOnMethods = {"createWithAuthRefs"})
235                         public void readAndCheckAuthRefs(String testName) throws Exception {
236                 // Perform setup.
237                 testSetup(STATUS_OK, ServiceRequestType.READ);
238
239                 // Submit the request to the service and store the response.
240                 AcquisitionClient acquisitionClient = new AcquisitionClient();
241                 Response res = acquisitionClient.read(knownResourceId);
242                 AcquisitionsCommon acquisition = null;
243                 try {
244                         // Check the status code of the response: does it match
245                         // the expected response(s)?
246                         assertStatusCode(res, testName);
247                         PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
248                         acquisition = (AcquisitionsCommon) extractPart(input,
249                                         acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
250                         Assert.assertNotNull(acquisition);
251                 } finally {
252                         if (res != null) {
253                 res.close();
254             }
255                 }
256
257                 // Check a couple of fields
258                 // Scalar fields
259                 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
260
261                 // In repeatable groups of fields
262                 /*
263         AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
264         List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
265         List<String> acqFundingSourceRefNamesFound = new ArrayList();
266         for (AcquisitionFunding acqFunding : acqFundings) {
267             String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
268             acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
269         }
270         Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
271                  */
272
273                 // In scalar repeatable fields
274                 OwnerList ownersList = acquisition.getOwners();
275                 List<String> owners = ownersList.getOwner();
276                 for (String refName : owners) {
277                         Assert.assertTrue(ownersRefNames.contains(refName));
278                 }
279
280                 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
281                 List<String> sources = acquisitionSources.getAcquisitionSource();
282                 for (String refName : sources) {
283                         Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
284                 }
285                 //
286                 // Get the auth refs and check them
287                 //
288                 res = acquisitionClient.getAuthorityRefs(knownResourceId);
289                 AuthorityRefList list = null;
290                 try {
291                         assertStatusCode(res, testName);
292                         list = res.readEntity(AuthorityRefList.class);
293                         Assert.assertNotNull(list);
294                 } finally {
295                         if (res != null) {
296                                 res.close();
297             }
298                 }
299
300                 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
301                 int numAuthRefsFound = items.size();
302                 if (logger.isDebugEnabled()) {
303                         logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
304                                         " authority references, found " + numAuthRefsFound);
305                 }
306                 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
307                                 "Did not find all expected authority references! " +
308                                 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
309
310                 // Optionally output additional data about list members for debugging.
311                 boolean iterateThroughList = true;
312                 if(iterateThroughList && logger.isDebugEnabled()){
313                         int i = 0;
314                         for(AuthorityRefList.AuthorityRefItem item : items){
315                                 logger.debug(testName + ": list-item[" + i + "] Field:" +
316                                                 item.getSourceField() + "= " +
317                                                 item.getAuthDisplayName() +
318                                                 item.getItemDisplayName());
319                                 logger.debug(testName + ": list-item[" + i + "] refName=" +
320                                                 item.getRefName());
321                                 logger.debug(testName + ": list-item[" + i + "] URI=" +
322                                                 item.getUri());
323                                 i++;
324                         }
325                 }
326         }
327
328
329         // ---------------------------------------------------------------
330         // Cleanup of resources created during testing
331         // ---------------------------------------------------------------
332
333         /**
334          * Deletes all resources created by tests, after all tests have been run.
335          *
336          * This cleanup method will always be run, even if one or more tests fail.
337          * For this reason, it attempts to remove all resources created
338          * at any point during testing, even if some of those resources
339          * may be expected to be deleted by certain tests.
340          */
341         @AfterClass(alwaysRun=true)
342         public void cleanUp() {
343                 String noTest = System.getProperty("noTestCleanup");
344                 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
345                         if (logger.isDebugEnabled()) {
346                                 logger.debug("Skipping Cleanup phase ...");
347                         }
348                         return;
349                 }
350                 if (logger.isDebugEnabled()) {
351                         logger.debug("Cleaning up temporary resources created for testing ...");
352                 }
353                 AcquisitionClient acquisitionClient = new AcquisitionClient();
354                 for (String resourceId : acquisitionIdsCreated) {
355                         // Note: Any non-success responses are ignored and not reported.
356                         acquisitionClient.delete(resourceId).close();
357                 }
358                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
359                 // Delete persons before PersonAuth
360                 for (String resourceId : personIdsCreated) {
361                         // Note: Any non-success responses are ignored and not reported.
362                         personAuthClient.deleteItem(personAuthCSID, resourceId).close();
363                 }
364                 // Note: Any non-success response is ignored and not reported.
365                 personAuthClient.delete(personAuthCSID).close();
366         }
367
368         // ---------------------------------------------------------------
369         // Utility methods used by tests above
370         // ---------------------------------------------------------------
371         @Override
372         public String getServicePathComponent() {
373                 return AcquisitionClient.SERVICE_PATH_COMPONENT;
374         }
375
376
377         @Override
378         protected String getServiceName() {
379                 return AcquisitionClient.SERVICE_NAME;
380         }
381
382         private PoxPayloadOut createAcquisitionInstance(
383             String accessionDateDisplayDate,
384                         String acquisitionAuthorizer,
385                         List<String> acquisitionFundingSources,
386                         List<String> acqOwners,
387                         List<String> acquisitionSources) {
388
389                 AcquisitionsCommon acquisition = new AcquisitionsCommon();
390                 StructuredDateGroup accessionDateGroup = new StructuredDateGroup(); 
391                 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
392         acquisition.setAccessionDateGroup(accessionDateGroup);
393                 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
394
395                 // AcquisitionFunding-related authrefs fields are *not* currently
396                 // enabled, as described in issue CSPACE-2818
397
398                 /*
399         AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
400         List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
401         int i = 0;
402         for (String acqFundingSource: acquisitionFundingSources) {
403             i++;
404             AcquisitionFunding acqFunding = new AcquisitionFunding();
405             acqFunding.setAcquisitionFundingSource(acqFundingSource);
406             acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
407             acqFundings.add(acqFunding);
408         }
409         AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
410         addtlAcqFunding.setAcquisitionFundingCurrency("USD");
411         acqFundings.add(addtlAcqFunding);
412         acquisition.setAcquisitionFundingList(acqFundingsList);
413                  */
414
415                 OwnerList ownersList = new OwnerList();
416                 List<String> owners = ownersList.getOwner();
417                 for (String owner: acqOwners) {
418                         owners.add(owner);
419                 }
420                 acquisition.setOwners(ownersList);
421
422                 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
423                 List<String> acqSources = acqSourcesList.getAcquisitionSource();
424                 for (String acqSource: acquisitionSources) {
425                         acqSources.add(acqSource);
426                 }
427                 acquisition.setAcquisitionSources(acqSourcesList);
428
429                 AcquisitionClient acquisitionClient = new AcquisitionClient();
430                 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
431                 PayloadOutputPart commonPart =
432                         multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
433
434                 if(logger.isDebugEnabled()){
435                         logger.debug("to be created, acquisition common");
436                         logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
437                 }
438
439                 return multipart;
440         }
441 }