]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2666d6781c191dbf7c220ab4d31001f504cf24cc
[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     final String SERVICE_PATH_COMPONENT = "intakes";
67     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68     private String knownIntakeId = null;
69     private List<String> intakeIdsCreated = new ArrayList();
70     private List<String> personIdsCreated = new ArrayList();
71     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
72     private int OK_STATUS = Response.Status.OK.getStatusCode();
73     private String personAuthCSID = null; 
74     private String currentOwnerPersonCSID = null; 
75     private String currentOwnerRefName = null;
76     private String depositorRefName = null;
77     private String conditionCheckAssesorRefName = null;
78     private String insurerRefName = null;
79     private String fieldCollectorRefName = null;
80     private String valuerRefName = null;
81     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
82
83     // ---------------------------------------------------------------
84     // CRUD tests : CREATE tests
85     // ---------------------------------------------------------------
86     // Success outcomes
87     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
88     public void createIntakeWithAuthRefs(String testName) throws Exception {
89
90         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
91
92         // Submit the request to the service and store the response.
93         String identifier = createIdentifier();
94         
95         // Create all the person refs and entities
96         createPersonRefs();
97
98         IntakeClient intakeClient = new IntakeClient();
99         MultipartOutput multipart = createIntakeInstance(
100                 "entryNumber-" + identifier,
101                 "entryDate-" + identifier,
102                                                                 currentOwnerRefName,
103                                                                 depositorRefName,
104                                                                 conditionCheckAssesorRefName,
105                                                                 insurerRefName,
106                                                                 fieldCollectorRefName,
107                                                                 valuerRefName );
108
109         ClientResponse<Response> res = intakeClient.create(multipart);
110
111         int statusCode = res.getStatus();
112
113         // Check the status code of the response: does it match
114         // the expected response(s)?
115         //
116         // Specifically:
117         // Does it fall within the set of valid status codes?
118         // Does it exactly match the expected status code?
119         if(logger.isDebugEnabled()){
120             logger.debug(testName + ": status = " + statusCode);
121         }
122         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
125
126         // Store the ID returned from the first resource created
127         // for additional tests below.
128         if (knownIntakeId == null){
129             knownIntakeId = extractId(res);
130             if (logger.isDebugEnabled()) {
131                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
132             }
133         }
134         
135         // Store the IDs from every resource created by tests,
136         // so they can be deleted after tests have been run.
137         intakeIdsCreated.add(extractId(res));
138     }
139     
140     protected void createPersonRefs(){
141         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
184         Map<String, String> personInfo = new HashMap<String,String>();
185         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
186         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
187         MultipartOutput multipart = 
188                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
189                                 refName, personInfo, personAuthClient.getItemCommonPartName());
190         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
191         int statusCode = res.getStatus();
192
193         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195         Assert.assertEquals(statusCode, CREATED_STATUS);
196         return extractId(res);
197     }
198
199     // Success outcomes
200     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
201         dependsOnMethods = {"createIntakeWithAuthRefs"})
202     public void readAndCheckAuthRefDocs(String testName) throws Exception {
203
204         // Perform setup.
205         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
206         
207         // Get the auth ref docs and check them
208        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
209        ClientResponse<AuthorityRefDocList> refDocListResp =
210                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
211
212         int statusCode = refDocListResp.getStatus();
213
214         if(logger.isDebugEnabled()){
215             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
216         }
217         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
218                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
219         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220         AuthorityRefDocList list = refDocListResp.getEntity();
221
222         // Optionally output additional data about list members for debugging.
223         boolean iterateThroughList = true;
224         boolean fFoundIntake = false;
225         if(iterateThroughList && logger.isDebugEnabled()){
226             List<AuthorityRefDocList.AuthorityRefDocItem> items =
227                     list.getAuthorityRefDocItem();
228             int i = 0;
229             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
230             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
231                 logger.debug(testName + ": list-item[" + i + "] " +
232                                 item.getDocType() + "(" +
233                                 item.getDocId() + ") Name:[" +
234                                 item.getDocName() + "] Number:[" +
235                                 item.getDocNumber() + "] in field:[" +
236                                 item.getSourceField() + "]");
237                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
238                         fFoundIntake = true;
239                 }
240                 i++;
241             }
242             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
243         }
244     }
245
246
247     // ---------------------------------------------------------------
248     // Cleanup of resources created during testing
249     // ---------------------------------------------------------------
250
251     /**
252      * Deletes all resources created by tests, after all tests have been run.
253      *
254      * This cleanup method will always be run, even if one or more tests fail.
255      * For this reason, it attempts to remove all resources created
256      * at any point during testing, even if some of those resources
257      * may be expected to be deleted by certain tests.
258      */
259     @AfterClass(alwaysRun=true)
260     public void cleanUp() {
261         String noTest = System.getProperty("noTestCleanup");
262         if("true".equalsIgnoreCase(noTest)) {
263             if (logger.isDebugEnabled()) {
264                 logger.debug("Skipping Cleanup phase");
265             }
266             return;
267         }
268         IntakeClient intakeClient = new IntakeClient();
269         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
270         if (logger.isDebugEnabled()) {
271             logger.debug("Cleaning up temporary resources created for testing ...");
272         }
273         // Note: Any non-success responses are ignored and not reported.
274         for (String resourceId : intakeIdsCreated) {
275             ClientResponse<Response> res = intakeClient.delete(resourceId);
276         }
277         // Delete persons before PersonAuth
278         for (String resourceId : personIdsCreated) {
279             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
280         }
281         ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
282     }
283
284     // ---------------------------------------------------------------
285     // Utility methods used by tests above
286     // ---------------------------------------------------------------
287     @Override
288     public String getServicePathComponent() {
289         return SERVICE_PATH_COMPONENT;
290     }
291
292    private MultipartOutput createIntakeInstance(String entryNumber,
293                 String entryDate,
294                                 String currentOwner,
295                                 String depositor,
296                                 String conditionCheckAssesor,
297                                 String insurer,
298                                 String fieldCollector,
299                                 String Valuer ) {
300         IntakesCommon intake = new IntakesCommon();
301         intake.setEntryNumber(entryNumber);
302         intake.setEntryDate(entryDate);
303         intake.setCurrentOwner(currentOwner);
304         intake.setDepositor(depositor);
305         intake.setConditionCheckAssesor(conditionCheckAssesor);
306         intake.setInsurer(insurer);
307         intake.setFieldCollector(fieldCollector);
308         intake.setValuer(Valuer);
309         MultipartOutput multipart = new MultipartOutput();
310         OutputPart commonPart =
311             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
312         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
313
314         if(logger.isDebugEnabled()){
315             logger.debug("to be created, intake common");
316             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
317         }
318
319         return multipart;
320     }
321 }