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