]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
aaecd2c2e5e34de717cfda97d7aacab80f87b108
[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                 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                 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         String result = null;
212         
213                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
214                 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
215                                         authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
216                 Response res = personAuthClient.createItem(personAuthCSID, multipart);
217                 try {
218                         int statusCode = res.getStatus();
219                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
220                                         invalidStatusCodeMessage(testRequestType, statusCode));
221                         Assert.assertEquals(statusCode, STATUS_CREATED);
222                         result = extractId(res);
223                 } finally {
224                         res.close();
225                 }
226                 
227                 return result;
228         }
229
230         // Success outcomes
231         @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
232                         dependsOnMethods = {"createWithAuthRefs"})
233                         public void readAndCheckAuthRefs(String testName) throws Exception {
234                 // Perform setup.
235                 testSetup(STATUS_OK, ServiceRequestType.READ);
236
237                 // Submit the request to the service and store the response.
238                 AcquisitionClient acquisitionClient = new AcquisitionClient();
239                 Response res = acquisitionClient.read(knownResourceId);
240                 AcquisitionsCommon acquisition = null;
241                 try {
242                         // Check the status code of the response: does it match
243                         // the expected response(s)?
244                         assertStatusCode(res, testName);
245                         PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
246                         acquisition = (AcquisitionsCommon) extractPart(input,
247                                         acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
248                         Assert.assertNotNull(acquisition);
249                 } finally {
250                         if (res != null) {
251                 res.close();
252             }
253                 }
254
255                 // Check a couple of fields
256                 // Scalar fields
257                 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
258
259                 // In repeatable groups of fields
260                 /*
261         AcquisitionFundingList acqFundingList = acquisition.getAcquisitionFundingList();
262         List<AcquisitionFunding> acqFundings = acqFundingList.getAcquisitionFunding();
263         List<String> acqFundingSourceRefNamesFound = new ArrayList();
264         for (AcquisitionFunding acqFunding : acqFundings) {
265             String acqFundingSourceRefName = acqFunding.getAcquisitionFundingSource();
266             acqFundingSourceRefNamesFound.add(acqFundingSourceRefName);
267         }
268         Assert.assertTrue(acqFundingSourceRefNamesFound.containsAll(acquisitionFundingSourcesRefNames));
269                  */
270
271                 // In scalar repeatable fields
272                 OwnerList ownersList = acquisition.getOwners();
273                 List<String> owners = ownersList.getOwner();
274                 for (String refName : owners) {
275                         Assert.assertTrue(ownersRefNames.contains(refName));
276                 }
277
278                 AcquisitionSourceList acquisitionSources = acquisition.getAcquisitionSources();
279                 List<String> sources = acquisitionSources.getAcquisitionSource();
280                 for (String refName : sources) {
281                         Assert.assertTrue(acquisitionSourcesRefNames.contains(refName));
282                 }
283                 //
284                 // Get the auth refs and check them
285                 //
286                 res =   acquisitionClient.getAuthorityRefs(knownResourceId);
287                 AuthorityRefList list = null;
288                 try {
289                         assertStatusCode(res, testName);
290                         list = (AuthorityRefList)res.getEntity();
291                         Assert.assertNotNull(list);
292                 } finally {
293                         if (res != null) {
294                                 res.close();
295             }
296                 }
297
298                 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
299                 int numAuthRefsFound = items.size();
300                 if (logger.isDebugEnabled()) {
301                         logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
302                                         " authority references, found " + numAuthRefsFound);
303                 }
304                 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
305                                 "Did not find all expected authority references! " +
306                                 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
307
308                 // Optionally output additional data about list members for debugging.
309                 boolean iterateThroughList = true;
310                 if(iterateThroughList && logger.isDebugEnabled()){
311                         int i = 0;
312                         for(AuthorityRefList.AuthorityRefItem item : items){
313                                 logger.debug(testName + ": list-item[" + i + "] Field:" +
314                                                 item.getSourceField() + "= " +
315                                                 item.getAuthDisplayName() +
316                                                 item.getItemDisplayName());
317                                 logger.debug(testName + ": list-item[" + i + "] refName=" +
318                                                 item.getRefName());
319                                 logger.debug(testName + ": list-item[" + i + "] URI=" +
320                                                 item.getUri());
321                                 i++;
322                         }
323                 }
324         }
325
326
327         // ---------------------------------------------------------------
328         // Cleanup of resources created during testing
329         // ---------------------------------------------------------------
330
331         /**
332          * Deletes all resources created by tests, after all tests have been run.
333          *
334          * This cleanup method will always be run, even if one or more tests fail.
335          * For this reason, it attempts to remove all resources created
336          * at any point during testing, even if some of those resources
337          * may be expected to be deleted by certain tests.
338          */
339         @AfterClass(alwaysRun=true)
340         public void cleanUp() {
341                 String noTest = System.getProperty("noTestCleanup");
342                 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
343                         if (logger.isDebugEnabled()) {
344                                 logger.debug("Skipping Cleanup phase ...");
345                         }
346                         return;
347                 }
348                 if (logger.isDebugEnabled()) {
349                         logger.debug("Cleaning up temporary resources created for testing ...");
350                 }
351                 AcquisitionClient acquisitionClient = new AcquisitionClient();
352                 for (String resourceId : acquisitionIdsCreated) {
353                         // Note: Any non-success responses are ignored and not reported.
354                         acquisitionClient.delete(resourceId).close();
355                 }
356                 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
357                 // Delete persons before PersonAuth
358                 for (String resourceId : personIdsCreated) {
359                         // Note: Any non-success responses are ignored and not reported.
360                         personAuthClient.deleteItem(personAuthCSID, resourceId).close();
361                 }
362                 // Note: Any non-success response is ignored and not reported.
363                 personAuthClient.delete(personAuthCSID).close();
364         }
365
366         // ---------------------------------------------------------------
367         // Utility methods used by tests above
368         // ---------------------------------------------------------------
369         @Override
370         public String getServicePathComponent() {
371                 return AcquisitionClient.SERVICE_PATH_COMPONENT;
372         }
373
374
375         @Override
376         protected String getServiceName() {
377                 return AcquisitionClient.SERVICE_NAME;
378         }
379
380         private PoxPayloadOut createAcquisitionInstance(
381             String accessionDateDisplayDate,
382                         String acquisitionAuthorizer,
383                         List<String> acquisitionFundingSources,
384                         List<String> acqOwners,
385                         List<String> acquisitionSources) {
386
387                 AcquisitionsCommon acquisition = new AcquisitionsCommon();
388                 StructuredDateGroup accessionDateGroup = new StructuredDateGroup(); 
389                 accessionDateGroup.setDateDisplayDate(accessionDateDisplayDate);
390         acquisition.setAccessionDateGroup(accessionDateGroup);
391                 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
392
393                 // AcquisitionFunding-related authrefs fields are *not* currently
394                 // enabled, as described in issue CSPACE-2818
395
396                 /*
397         AcquisitionFundingList acqFundingsList = new AcquisitionFundingList();
398         List<AcquisitionFunding> acqFundings = acqFundingsList.getAcquisitionFunding();
399         int i = 0;
400         for (String acqFundingSource: acquisitionFundingSources) {
401             i++;
402             AcquisitionFunding acqFunding = new AcquisitionFunding();
403             acqFunding.setAcquisitionFundingSource(acqFundingSource);
404             acqFunding.setAcquisitionFundingSourceProvisos("funding source provisos-" + i);
405             acqFundings.add(acqFunding);
406         }
407         AcquisitionFunding addtlAcqFunding = new AcquisitionFunding();
408         addtlAcqFunding.setAcquisitionFundingCurrency("USD");
409         acqFundings.add(addtlAcqFunding);
410         acquisition.setAcquisitionFundingList(acqFundingsList);
411                  */
412
413                 OwnerList ownersList = new OwnerList();
414                 List<String> owners = ownersList.getOwner();
415                 for (String owner: acqOwners) {
416                         owners.add(owner);
417                 }
418                 acquisition.setOwners(ownersList);
419
420                 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
421                 List<String> acqSources = acqSourcesList.getAcquisitionSource();
422                 for (String acqSource: acquisitionSources) {
423                         acqSources.add(acqSource);
424                 }
425                 acquisition.setAcquisitionSources(acqSourcesList);
426
427                 AcquisitionClient acquisitionClient = new AcquisitionClient();
428                 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
429                 PayloadOutputPart commonPart =
430                         multipart.addPart(acquisitionClient.getCommonPartName(), acquisition);
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 }