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