]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d6e5277674cae5b5704db7558695217a4b8c6c53
[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.CollectionSpaceClient;
34 import org.collectionspace.services.client.IntakeClient;
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.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.intake.InsurerList;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.person.PersonTermGroup;
47
48 import org.jboss.resteasy.client.ClientResponse;
49
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
53 import org.testng.Assert;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.Test;
56
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * IntakeAuthRefsTest, carries out tests against a
62  * deployed and running Intake Service.
63  *
64  * $LastChangedRevision: 1327 $
65  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
66  */
67 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
68
69     private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
70     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71
72     // Instance variables specific to this test.
73     final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
74     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
75     private String knownResourceId = null;
76     private List<String> intakeIdsCreated = new ArrayList<String>();
77     private List<String> personIdsCreated = new ArrayList<String>();
78     private String personAuthCSID = null; 
79     private String currentOwnerRefName = null;
80     private String depositorRefName = null;
81     private String conditionCheckerOrAssessorRefName = null;
82     private String insurerRefName = null;
83     private String valuerRefName = null;
84     private final int NUM_AUTH_REFS_EXPECTED = 5;
85     private final static String CURRENT_DATE_UTC =
86             GregorianCalendarDateTimeUtils.currentDateUTC();
87
88         @Override
89         protected String getServiceName() {
90                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
91         }
92     
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
95      */
96     @Override
97     protected CollectionSpaceClient getClientInstance() {
98         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99     }
100     
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
103      */
104     @Override
105         protected AbstractCommonList getCommonList(
106                         ClientResponse<AbstractCommonList> response) {
107         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
108     }
109
110     // ---------------------------------------------------------------
111     // CRUD tests : CREATE tests
112     // ---------------------------------------------------------------
113     // Success outcomes
114     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
115     public void createWithAuthRefs(String testName) throws Exception {
116         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117
118         // Submit the request to the service and store the response.
119         String identifier = createIdentifier();
120         
121         // Create all the person refs and entities
122         createPersonRefs();
123         
124         // Submit the request to the service and store the response.
125         IntakeClient intakeClient = new IntakeClient();
126         PoxPayloadOut multipart = createIntakeInstance(
127                 "entryNumber-" + identifier,
128                 CURRENT_DATE_UTC,
129                 currentOwnerRefName,
130                 depositorRefName,
131                 conditionCheckerOrAssessorRefName,
132                 insurerRefName,
133                 valuerRefName );
134
135         String newId = null;
136         Response res = intakeClient.create(multipart);
137         try {
138                 int statusCode = res.getStatus();
139                 // Check the status code of the response: does it match
140                 // the expected response(s)?
141                 //
142                 // Specifically:
143                 // Does it fall within the set of valid status codes?
144                 // Does it exactly match the expected status code?
145                 if(logger.isDebugEnabled()){
146                     logger.debug(testName + ": status = " + statusCode);
147                 }
148                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
149                         invalidStatusCodeMessage(testRequestType, statusCode));
150                 Assert.assertEquals(statusCode, testExpectedStatusCode);
151                 newId = extractId(res);
152         } finally {
153                 res.close();
154         }
155
156         // Store the ID returned from the first resource created
157         // for additional tests below.
158         if (knownResourceId == null){
159             knownResourceId = newId;
160             if (logger.isDebugEnabled()) {
161                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
162             }
163         }
164         
165         // Store the IDs from every resource created by tests,
166         // so they can be deleted after tests have been run.
167         intakeIdsCreated.add(newId);
168     }
169     
170     protected void createPersonRefs() {
171         //
172         // First, create a new person authority
173         //
174         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
175         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
176                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
177         Response res = personAuthClient.create(multipart);
178         try {
179                 int statusCode = res.getStatus();
180                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
181                         invalidStatusCodeMessage(testRequestType, statusCode));
182                 Assert.assertEquals(statusCode, STATUS_CREATED);
183                 personAuthCSID = extractId(res);
184         } finally {
185                 res.close();
186         }
187         
188         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
189         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
190         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
191         personIdsCreated.add(csid);
192         
193         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
194         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195         personIdsCreated.add(csid);
196         
197         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
198         conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199         personIdsCreated.add(csid);
200         
201         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
202         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
203         personIdsCreated.add(csid);
204         
205         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
206         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
207         personIdsCreated.add(csid);
208     }
209     
210     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
211         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
212         Map<String, String> personInfo = new HashMap<String,String>();
213         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
214         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
215         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
216         List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
217         PersonTermGroup term = new PersonTermGroup();
218         String termName = firstName + " " + surName;
219         term.setTermDisplayName(termName);
220         term.setTermName(termName);
221         personTerms.add(term);
222         PoxPayloadOut multipart = 
223                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
224                                 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
225         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
226         int statusCode = res.getStatus();
227
228         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
229                 invalidStatusCodeMessage(testRequestType, statusCode));
230         Assert.assertEquals(statusCode, STATUS_CREATED);
231         return extractId(res);
232     }
233
234     // Success outcomes
235     @Test(dataProvider="testName",
236         dependsOnMethods = {"createWithAuthRefs"})
237     public void readAndCheckAuthRefs(String testName) throws Exception {
238         // Perform setup.
239         testSetup(STATUS_OK, ServiceRequestType.READ);
240
241         // Submit the request to the service and store the response.
242         IntakeClient intakeClient = new IntakeClient();
243         ClientResponse<String> res = intakeClient.read(knownResourceId);
244         try {
245                 assertStatusCode(res, testName);
246                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
247                 IntakesCommon intake = (IntakesCommon) extractPart(input,
248                                 intakeClient.getCommonPartName(), IntakesCommon.class);
249                 Assert.assertNotNull(intake);
250                 // Check a couple of fields
251                 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
252                 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
253                 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
254         } finally {
255                 if (res != null) {
256                 res.releaseConnection();
257             }
258         }
259         
260         // Get the auth refs and check them
261         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
262         AuthorityRefList list = null;
263         try {
264                 assertStatusCode(res2, testName);
265                 list = res2.getEntity();
266         } finally {
267                 if (res2 != null) {
268                         res2.releaseConnection();
269             }
270         }
271         
272         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
273         int numAuthRefsFound = items.size();
274         if(logger.isDebugEnabled()){
275             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
276                 " authority references, found " + numAuthRefsFound);
277         }
278         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
279             "Did not find all expected authority references! " +
280             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
281
282         // Optionally output additional data about list members for debugging.
283         boolean iterateThroughList = true;
284         if(iterateThroughList && logger.isDebugEnabled()){
285             int i = 0;
286             for(AuthorityRefList.AuthorityRefItem item : items){
287                 logger.debug(testName + ": list-item[" + i + "] Field:" +
288                                 item.getSourceField() + "= " +
289                         item.getAuthDisplayName() +
290                         item.getItemDisplayName());
291                 logger.debug(testName + ": list-item[" + i + "] refName=" +
292                         item.getRefName());
293                 logger.debug(testName + ": list-item[" + i + "] URI=" +
294                         item.getUri());
295                 i++;
296             }
297         }
298     }
299
300
301     // ---------------------------------------------------------------
302     // Cleanup of resources created during testing
303     // ---------------------------------------------------------------
304
305     /**
306      * Deletes all resources created by tests, after all tests have been run.
307      *
308      * This cleanup method will always be run, even if one or more tests fail.
309      * For this reason, it attempts to remove all resources created
310      * at any point during testing, even if some of those resources
311      * may be expected to be deleted by certain tests.
312      */
313     @AfterClass(alwaysRun=true)
314     public void cleanUp() {
315         String noTest = System.getProperty("noTestCleanup");
316         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
317             if (logger.isDebugEnabled()) {
318                 logger.debug("Skipping Cleanup phase ...");
319             }
320             return;
321         }
322         if (logger.isDebugEnabled()) {
323             logger.debug("Cleaning up temporary resources created for testing ...");
324         }
325         IntakeClient intakeClient = new IntakeClient();
326         // Note: Any non-success responses are ignored and not reported.
327         for (String resourceId : intakeIdsCreated) {
328             intakeClient.delete(resourceId).releaseConnection();
329         }
330         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
331         // Delete persons before PersonAuth
332         for (String resourceId : personIdsCreated) {
333             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
334         }
335         personAuthClient.delete(personAuthCSID).releaseConnection();
336     }
337
338     // ---------------------------------------------------------------
339     // Utility methods used by tests above
340     // ---------------------------------------------------------------
341     @Override
342     public String getServicePathComponent() {
343         return SERVICE_PATH_COMPONENT;
344     }
345
346    private PoxPayloadOut createIntakeInstance(String entryNumber,
347                 String entryDate,
348                                 String currentOwner,
349                                 String depositor,
350                                 String conditionCheckerAssessor,
351                                 String insurer,
352                                 String Valuer ) {
353         IntakesCommon intake = new IntakesCommon();
354         intake.setEntryNumber(entryNumber);
355         intake.setEntryDate(entryDate);
356         intake.setCurrentOwner(currentOwner);
357         intake.setDepositor(depositor);
358         intake.setValuer(Valuer);
359
360         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
361         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
362         checkersOrAssessors.add(conditionCheckerAssessor);
363         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
364
365         InsurerList insurerList = new InsurerList();
366         List<String> insurers = insurerList.getInsurer();
367         insurers.add(insurer);
368         intake.setInsurers(insurerList);
369
370         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
371         PayloadOutputPart commonPart =
372             multipart.addPart(new IntakeClient().getCommonPartName(), intake);
373
374         if(logger.isDebugEnabled()){
375             logger.debug("to be created, intake common");
376             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
377         }
378
379         return multipart;
380     }
381 }