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