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