]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
22bc8372856860f0e8d2c5b8319aac4155cb450a
[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 Logger logger =
65        LoggerFactory.getLogger(PersonAuthRefDocsTest.class);
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();
72     private List<String> personIdsCreated = new ArrayList();
73     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
74     private int OK_STATUS = Response.Status.OK.getStatusCode();
75     private String personAuthCSID = null; 
76     private String currentOwnerPersonCSID = null; 
77     private String currentOwnerRefName = null;
78     private String depositorRefName = null;
79     private String conditionCheckAssesorRefName = null;
80     private String insurerRefName = null;
81     private String fieldCollectorRefName = null;
82     private String valuerRefName = null;
83     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
84
85     /* (non-Javadoc)
86      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
87      */
88     @Override
89     protected CollectionSpaceClient getClientInstance() {
90         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91     }
92     
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
95      */
96     @Override
97         protected AbstractCommonList getAbstractCommonList(
98                         ClientResponse<AbstractCommonList> response) {
99         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100     }
101
102     // ---------------------------------------------------------------
103     // CRUD tests : CREATE tests
104     // ---------------------------------------------------------------
105     // Success outcomes
106     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
107     public void createIntakeWithAuthRefs(String testName) throws Exception {
108
109         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
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                                                                 conditionCheckAssesorRefName,
124                                                                 insurerRefName,
125                                                                 fieldCollectorRefName,
126                                                                 valuerRefName );
127
128         ClientResponse<Response> res = intakeClient.create(multipart);
129
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
145         // Store the ID returned from the first resource created
146         // for additional tests below.
147         if (knownIntakeId == null){
148             knownIntakeId = extractId(res);
149             if (logger.isDebugEnabled()) {
150                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
151             }
152         }
153         
154         // Store the IDs from every resource created by tests,
155         // so they can be deleted after tests have been run.
156         intakeIdsCreated.add(extractId(res));
157     }
158     
159     protected void createPersonRefs(){
160         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
161         String authRefName = 
162                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
163         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
164                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
165         ClientResponse<Response> res = personAuthClient.create(multipart);
166         int statusCode = res.getStatus();
167
168         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170         Assert.assertEquals(statusCode, CREATED_STATUS);
171         personAuthCSID = extractId(res);
172         
173         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
174                                                                 authRefName, "Olivier Owner", true);
175                                 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
176         personIdsCreated.add(currentOwnerPersonCSID);
177         
178         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
179                                                                         authRefName, "Debbie Depositor", true);
180         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
181         
182         conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
183                                                                         authRefName, "Andrew Assessor", true);
184         personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
185         
186         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
187                                                                         authRefName, "Ingrid Insurer", true);
188         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
189         
190         fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
191                                                                         authRefName, "Connie Collector", true);
192         personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
193         
194         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
195                                                                         authRefName, "Vince Valuer", true);
196         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
197         
198
199     }
200     
201     protected String createPerson(String firstName, String surName, String refName ) {
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         MultipartOutput multipart = 
207                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
208                                 refName, personInfo, personAuthClient.getItemCommonPartName());
209         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
210         int statusCode = res.getStatus();
211
212         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
213                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
214         Assert.assertEquals(statusCode, CREATED_STATUS);
215         return extractId(res);
216     }
217
218     // Success outcomes
219     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
220         dependsOnMethods = {"createIntakeWithAuthRefs"})
221     public void readAndCheckAuthRefDocs(String testName) throws Exception {
222
223         // Perform setup.
224         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
225         
226         // Get the auth ref docs and check them
227        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
228        ClientResponse<AuthorityRefDocList> refDocListResp =
229                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
230
231         int statusCode = refDocListResp.getStatus();
232
233         if(logger.isDebugEnabled()){
234             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
235         }
236         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239         AuthorityRefDocList list = refDocListResp.getEntity();
240
241         // Optionally output additional data about list members for debugging.
242         boolean iterateThroughList = true;
243         boolean fFoundIntake = false;
244         if(iterateThroughList && logger.isDebugEnabled()){
245             List<AuthorityRefDocList.AuthorityRefDocItem> items =
246                     list.getAuthorityRefDocItem();
247             int i = 0;
248             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
249             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
250                 logger.debug(testName + ": list-item[" + i + "] " +
251                                 item.getDocType() + "(" +
252                                 item.getDocId() + ") Name:[" +
253                                 item.getDocName() + "] Number:[" +
254                                 item.getDocNumber() + "] in field:[" +
255                                 item.getSourceField() + "]");
256                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
257                         fFoundIntake = true;
258                 }
259                 i++;
260             }
261             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
262         }
263     }
264
265
266     // ---------------------------------------------------------------
267     // Cleanup of resources created during testing
268     // ---------------------------------------------------------------
269
270     /**
271      * Deletes all resources created by tests, after all tests have been run.
272      *
273      * This cleanup method will always be run, even if one or more tests fail.
274      * For this reason, it attempts to remove all resources created
275      * at any point during testing, even if some of those resources
276      * may be expected to be deleted by certain tests.
277      */
278     @AfterClass(alwaysRun=true)
279     public void cleanUp() {
280         String noTest = System.getProperty("noTestCleanup");
281         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
282             if (logger.isDebugEnabled()) {
283                 logger.debug("Skipping Cleanup phase ...");
284             }
285             return;
286         }
287         if (logger.isDebugEnabled()) {
288             logger.debug("Cleaning up temporary resources created for testing ...");
289         }
290         IntakeClient intakeClient = new IntakeClient();
291         // Note: Any non-success responses are ignored and not reported.
292         for (String resourceId : intakeIdsCreated) {
293             ClientResponse<Response> res = intakeClient.delete(resourceId);
294         }
295         // Delete persons before PersonAuth
296         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
297         for (String resourceId : personIdsCreated) {
298             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
299         }
300         if (personAuthCSID != null) {
301                 personAuthClient.delete(personAuthCSID);
302         }
303     }
304
305     // ---------------------------------------------------------------
306     // Utility methods used by tests above
307     // ---------------------------------------------------------------
308     @Override
309     public String getServicePathComponent() {
310         return SERVICE_PATH_COMPONENT;
311     }
312
313    private MultipartOutput createIntakeInstance(String entryNumber,
314                 String entryDate,
315                                 String currentOwner,
316                                 String depositor,
317                                 String conditionCheckAssesor,
318                                 String insurer,
319                                 String fieldCollector,
320                                 String Valuer ) {
321         IntakesCommon intake = new IntakesCommon();
322         intake.setEntryNumber(entryNumber);
323         intake.setEntryDate(entryDate);
324         intake.setCurrentOwner(currentOwner);
325         intake.setDepositor(depositor);
326         intake.setConditionCheckAssesor(conditionCheckAssesor);
327         intake.setInsurer(insurer);
328         intake.setFieldCollector(fieldCollector);
329         intake.setValuer(Valuer);
330         MultipartOutput multipart = new MultipartOutput();
331         OutputPart commonPart =
332             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
333         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
334
335         if(logger.isDebugEnabled()){
336             logger.debug("to be created, intake common");
337             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
338         }
339
340         return multipart;
341     }
342 }