]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c2c377d1862b1f35a3b8b9b776c76ebee45b9172
[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.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
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.common.datetime.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
44 import org.collectionspace.services.intake.IntakesCommon;
45 import org.collectionspace.services.intake.InsurerList;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
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 {
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 getAbstractCommonList(
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
117         if (logger.isDebugEnabled()) {
118             logger.debug(testBanner(testName, CLASS_NAME));
119         }
120         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
121
122         // Submit the request to the service and store the response.
123         String identifier = createIdentifier();
124         
125         // Create all the person refs and entities
126         createPersonRefs();
127         
128         // Submit the request to the service and store the response.
129         IntakeClient intakeClient = new IntakeClient();
130         PoxPayloadOut multipart = createIntakeInstance(
131                 "entryNumber-" + identifier,
132                 CURRENT_DATE_UTC,
133                 currentOwnerRefName,
134                 depositorRefName,
135                 conditionCheckerOrAssessorRefName,
136                 insurerRefName,
137                 valuerRefName );
138         ClientResponse<Response> res = intakeClient.create(multipart);
139
140         int statusCode = res.getStatus();
141
142         // Check the status code of the response: does it match
143         // the expected response(s)?
144         //
145         // Specifically:
146         // Does it fall within the set of valid status codes?
147         // Does it exactly match the expected status code?
148         if(logger.isDebugEnabled()){
149             logger.debug(testName + ": status = " + statusCode);
150         }
151         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154
155         // Store the ID returned from the first resource created
156         // for additional tests below.
157         if (knownResourceId == null){
158             knownResourceId = extractId(res);
159             if (logger.isDebugEnabled()) {
160                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
161             }
162         }
163         
164         // Store the IDs from every resource created by tests,
165         // so they can be deleted after tests have been run.
166         intakeIdsCreated.add(extractId(res));
167     }
168     
169     protected void createPersonRefs(){
170         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
171         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
172                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
173         ClientResponse<Response> res = personAuthClient.create(multipart);
174         int statusCode = res.getStatus();
175
176         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178         Assert.assertEquals(statusCode, STATUS_CREATED);
179         personAuthCSID = extractId(res);
180         
181         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
182         
183         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
184         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
185         personIdsCreated.add(csid);
186         
187         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
188         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189         personIdsCreated.add(csid);
190         
191         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
192         conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193         personIdsCreated.add(csid);
194         
195         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
196         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197         personIdsCreated.add(csid);
198         
199         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
200         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201         personIdsCreated.add(csid);
202     }
203     
204     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
205         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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         PoxPayloadOut multipart = 
211                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
212                                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
213         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
214         int statusCode = res.getStatus();
215
216         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218         Assert.assertEquals(statusCode, STATUS_CREATED);
219         return extractId(res);
220     }
221
222     // Success outcomes
223     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
224         dependsOnMethods = {"createWithAuthRefs"})
225     public void readAndCheckAuthRefs(String testName) throws Exception {
226
227         if (logger.isDebugEnabled()) {
228             logger.debug(testBanner(testName, CLASS_NAME));
229         }
230         // Perform setup.
231         testSetup(STATUS_OK, ServiceRequestType.READ);
232
233         // Submit the request to the service and store the response.
234         IntakeClient intakeClient = new IntakeClient();
235         ClientResponse<String> res = intakeClient.read(knownResourceId);
236         int statusCode = res.getStatus();
237
238         // Check the status code of the response: does it match
239         // the expected response(s)?
240         if(logger.isDebugEnabled()){
241             logger.debug(testName + ".read: status = " + statusCode);
242         }
243         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
244                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
245         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
246
247         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
248         IntakesCommon intake = (IntakesCommon) extractPart(input,
249                         intakeClient.getCommonPartName(), IntakesCommon.class);
250         Assert.assertNotNull(intake);
251         // Check a couple of fields
252         Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
253         Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
254         Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
255         
256         // Get the auth refs and check them
257         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
258         statusCode = res2.getStatus();
259
260         if(logger.isDebugEnabled()){
261             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
262         }
263         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266         AuthorityRefList list = res2.getEntity();
267         
268         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
269         int numAuthRefsFound = items.size();
270         if(logger.isDebugEnabled()){
271             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
272                 " authority references, found " + numAuthRefsFound);
273         }
274         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
275             "Did not find all expected authority references! " +
276             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
277
278         // Optionally output additional data about list members for debugging.
279         boolean iterateThroughList = true;
280         if(iterateThroughList && logger.isDebugEnabled()){
281             int i = 0;
282             for(AuthorityRefList.AuthorityRefItem item : items){
283                 logger.debug(testName + ": list-item[" + i + "] Field:" +
284                                 item.getSourceField() + "= " +
285                         item.getAuthDisplayName() +
286                         item.getItemDisplayName());
287                 logger.debug(testName + ": list-item[" + i + "] refName=" +
288                         item.getRefName());
289                 logger.debug(testName + ": list-item[" + i + "] URI=" +
290                         item.getUri());
291                 i++;
292             }
293         }
294     }
295
296
297     // ---------------------------------------------------------------
298     // Cleanup of resources created during testing
299     // ---------------------------------------------------------------
300
301     /**
302      * Deletes all resources created by tests, after all tests have been run.
303      *
304      * This cleanup method will always be run, even if one or more tests fail.
305      * For this reason, it attempts to remove all resources created
306      * at any point during testing, even if some of those resources
307      * may be expected to be deleted by certain tests.
308      */
309     @AfterClass(alwaysRun=true)
310     public void cleanUp() {
311         String noTest = System.getProperty("noTestCleanup");
312         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
313             if (logger.isDebugEnabled()) {
314                 logger.debug("Skipping Cleanup phase ...");
315             }
316             return;
317         }
318         if (logger.isDebugEnabled()) {
319             logger.debug("Cleaning up temporary resources created for testing ...");
320         }
321         IntakeClient intakeClient = new IntakeClient();
322         // Note: Any non-success responses are ignored and not reported.
323         for (String resourceId : intakeIdsCreated) {
324             intakeClient.delete(resourceId).releaseConnection();
325         }
326         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
327         // Delete persons before PersonAuth
328         for (String resourceId : personIdsCreated) {
329             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
330         }
331         personAuthClient.delete(personAuthCSID).releaseConnection();
332     }
333
334     // ---------------------------------------------------------------
335     // Utility methods used by tests above
336     // ---------------------------------------------------------------
337     @Override
338     public String getServicePathComponent() {
339         return SERVICE_PATH_COMPONENT;
340     }
341
342    private PoxPayloadOut createIntakeInstance(String entryNumber,
343                 String entryDate,
344                                 String currentOwner,
345                                 String depositor,
346                                 String conditionCheckerAssessor,
347                                 String insurer,
348                                 String Valuer ) {
349         IntakesCommon intake = new IntakesCommon();
350         intake.setEntryNumber(entryNumber);
351         intake.setEntryDate(entryDate);
352         intake.setCurrentOwner(currentOwner);
353         intake.setDepositor(depositor);
354         intake.setValuer(Valuer);
355
356         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
357         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
358         checkersOrAssessors.add(conditionCheckerAssessor);
359         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
360
361         InsurerList insurerList = new InsurerList();
362         List<String> insurers = insurerList.getInsurer();
363         insurers.add(insurer);
364         intake.setInsurers(insurerList);
365
366         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
367         PayloadOutputPart commonPart =
368             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
369         commonPart.setLabel(new IntakeClient().getCommonPartName());
370
371         if(logger.isDebugEnabled()){
372             logger.debug("to be created, intake common");
373             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
374         }
375
376         return multipart;
377     }
378 }