]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b600372d15f36fd6b80b6fdc079832f851d3234e
[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         String authRefName = 
167                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
168         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
169                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
170         ClientResponse<Response> res = personAuthClient.create(multipart);
171         int statusCode = res.getStatus();
172
173         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175         Assert.assertEquals(statusCode, STATUS_CREATED);
176         personAuthCSID = extractId(res);
177         
178         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
179                                                                 authRefName, "Olivier Owner", true);
180                                 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
181         personIdsCreated.add(currentOwnerPersonCSID);
182         
183         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
184                                                                         authRefName, "Debbie Depositor", true);
185         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
186         
187         conditionCheckerAssessorRefName = PersonAuthorityClientUtils.createPersonRefName(
188                                                                         authRefName, "Andrew Checker-Assessor", true);
189         personIdsCreated.add(createPerson("Andrew", "Checker-Assessor", conditionCheckerAssessorRefName));
190         
191         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
192                                                                         authRefName, "Ingrid Insurer", true);
193         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
194         
195         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
196                                                                         authRefName, "Vince Valuer", true);
197         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
198         
199
200     }
201     
202     protected String createPerson(String firstName, String surName, String refName ) {
203         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
204         Map<String, String> personInfo = new HashMap<String,String>();
205         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
206         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
207         MultipartOutput multipart = 
208                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
209                                 refName, personInfo, personAuthClient.getItemCommonPartName());
210         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
211         int statusCode = res.getStatus();
212
213         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
214                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
215         Assert.assertEquals(statusCode, STATUS_CREATED);
216         return extractId(res);
217     }
218
219     // Success outcomes
220     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
221         dependsOnMethods = {"createIntakeWithAuthRefs"})
222     public void readAndCheckAuthRefDocs(String testName) throws Exception {
223
224         if (logger.isDebugEnabled()) {
225             logger.debug(testBanner(testName, CLASS_NAME));
226         }
227         // Perform setup.
228         testSetup(STATUS_OK, ServiceRequestType.READ);
229         
230         // Get the auth ref docs and check them
231        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
232        ClientResponse<AuthorityRefDocList> refDocListResp =
233                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
234
235         int statusCode = refDocListResp.getStatus();
236
237         if(logger.isDebugEnabled()){
238             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
239         }
240         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243         AuthorityRefDocList list = refDocListResp.getEntity();
244
245         // Optionally output additional data about list members for debugging.
246         boolean iterateThroughList = true;
247         boolean fFoundIntake = false;
248         if(iterateThroughList && logger.isDebugEnabled()){
249             List<AuthorityRefDocList.AuthorityRefDocItem> items =
250                     list.getAuthorityRefDocItem();
251             int i = 0;
252             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
253             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
254                 logger.debug(testName + ": list-item[" + i + "] " +
255                                 item.getDocType() + "(" +
256                                 item.getDocId() + ") Name:[" +
257                                 item.getDocName() + "] Number:[" +
258                                 item.getDocNumber() + "] in field:[" +
259                                 item.getSourceField() + "]");
260                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
261                         fFoundIntake = true;
262                 }
263                 i++;
264             }
265             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
266         }
267     }
268
269
270     // ---------------------------------------------------------------
271     // Cleanup of resources created during testing
272     // ---------------------------------------------------------------
273
274     /**
275      * Deletes all resources created by tests, after all tests have been run.
276      *
277      * This cleanup method will always be run, even if one or more tests fail.
278      * For this reason, it attempts to remove all resources created
279      * at any point during testing, even if some of those resources
280      * may be expected to be deleted by certain tests.
281      */
282     @AfterClass(alwaysRun=true)
283     public void cleanUp() {
284         String noTest = System.getProperty("noTestCleanup");
285         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
286             if (logger.isDebugEnabled()) {
287                 logger.debug("Skipping Cleanup phase ...");
288             }
289             return;
290         }
291         if (logger.isDebugEnabled()) {
292             logger.debug("Cleaning up temporary resources created for testing ...");
293         }
294         IntakeClient intakeClient = new IntakeClient();
295         // Note: Any non-success responses are ignored and not reported.
296         for (String resourceId : intakeIdsCreated) {
297             ClientResponse<Response> res = intakeClient.delete(resourceId);
298             res.releaseConnection();
299         }
300         // Delete persons before PersonAuth
301         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
302         for (String resourceId : personIdsCreated) {
303             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
304             res.releaseConnection();
305         }
306         if (personAuthCSID != null) {
307                 personAuthClient.delete(personAuthCSID).releaseConnection();
308         }
309     }
310
311     // ---------------------------------------------------------------
312     // Utility methods used by tests above
313     // ---------------------------------------------------------------
314     @Override
315     public String getServicePathComponent() {
316         return SERVICE_PATH_COMPONENT;
317     }
318
319    private MultipartOutput createIntakeInstance(String entryNumber,
320                 String entryDate,
321                                 String currentOwner,
322                                 String depositor,
323                                 String conditionCheckerAssessor,
324                                 String insurer,
325                                 String Valuer ) {
326         IntakesCommon intake = new IntakesCommon();
327         intake.setEntryNumber(entryNumber);
328         intake.setEntryDate(entryDate);
329         intake.setCurrentOwner(currentOwner);
330         intake.setDepositor(depositor);
331         intake.setConditionCheckerAssessor(conditionCheckerAssessor);
332         intake.setInsurer(insurer);
333         intake.setValuer(Valuer);
334         MultipartOutput multipart = new MultipartOutput();
335         OutputPart commonPart =
336             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
337         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
338
339         if(logger.isDebugEnabled()){
340             logger.debug("to be created, intake common");
341             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
342         }
343
344         return multipart;
345     }
346 }