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