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