]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b71e41a0047b368b038364cd5bb8a9956d4eedba
[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.Response;
31
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.IntakeClient;
35 import org.collectionspace.services.client.PayloadOutputPart;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.intake.InsurerList;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.person.PersonTermGroup;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * IntakeAuthRefsTest, carries out tests against a
55  * deployed and running Intake Service.
56  *
57  * $LastChangedRevision: 1327 $
58  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
59  */
60 public class IntakeAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
61
62     private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
63     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64
65     // Instance variables specific to this test.
66     final String SERVICE_PATH_COMPONENT = IntakeClient.SERVICE_PATH_COMPONENT;//"intakes";
67     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68     private String knownResourceId = null;
69     private List<String> intakeIdsCreated = new ArrayList<String>();
70     private List<String> personIdsCreated = new ArrayList<String>();
71     private String personAuthCSID = null; 
72     private String currentOwnerRefName = null;
73     private String depositorRefName = null;
74     private String conditionCheckerOrAssessorRefName = null;
75     private String insurerRefName = null;
76     private String valuerRefName = null;
77     private final int NUM_AUTH_REFS_EXPECTED = 5;
78     private final static String CURRENT_DATE_UTC =
79             GregorianCalendarDateTimeUtils.currentDateUTC();
80
81         @Override
82         protected String getServiceName() {
83                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
84         }
85     
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88      */
89     @Override
90     protected CollectionSpaceClient getClientInstance() {
91         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92     }
93
94         @Override
95         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
96         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97         }
98     
99     /* (non-Javadoc)
100      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
101      */
102     @Override
103         protected AbstractCommonList getCommonList(Response response) {
104         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
105     }
106
107     // ---------------------------------------------------------------
108     // CRUD tests : CREATE tests
109     // ---------------------------------------------------------------
110     // Success outcomes
111     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
112     public void createWithAuthRefs(String testName) throws Exception {
113         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
114
115         // Submit the request to the service and store the response.
116         String identifier = createIdentifier();
117         
118         // Create all the person refs and entities
119         createPersonRefs();
120         
121         // Submit the request to the service and store the response.
122         IntakeClient intakeClient = new IntakeClient();
123         PoxPayloadOut multipart = createIntakeInstance(
124                 "entryNumber-" + identifier,
125                 CURRENT_DATE_UTC,
126                 currentOwnerRefName,
127                 depositorRefName,
128                 conditionCheckerOrAssessorRefName,
129                 insurerRefName,
130                 valuerRefName );
131
132         String newId = null;
133         Response res = intakeClient.create(multipart);
134         try {
135                 int statusCode = res.getStatus();
136                 // Check the status code of the response: does it match
137                 // the expected response(s)?
138                 //
139                 // Specifically:
140                 // Does it fall within the set of valid status codes?
141                 // Does it exactly match the expected status code?
142                 if(logger.isDebugEnabled()){
143                     logger.debug(testName + ": status = " + statusCode);
144                 }
145                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
146                         invalidStatusCodeMessage(testRequestType, statusCode));
147                 Assert.assertEquals(statusCode, testExpectedStatusCode);
148                 newId = extractId(res);
149         } finally {
150                 res.close();
151         }
152
153         // Store the ID returned from the first resource created
154         // for additional tests below.
155         if (knownResourceId == null){
156             knownResourceId = newId;
157             if (logger.isDebugEnabled()) {
158                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
159             }
160         }
161         
162         // Store the IDs from every resource created by tests,
163         // so they can be deleted after tests have been run.
164         intakeIdsCreated.add(newId);
165     }
166     
167     protected void createPersonRefs() throws Exception {
168         //
169         // First, create a new person authority
170         //
171         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
172         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
173                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
174         Response res = personAuthClient.create(multipart);
175         try {
176                 int statusCode = res.getStatus();
177                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
178                         invalidStatusCodeMessage(testRequestType, statusCode));
179                 Assert.assertEquals(statusCode, STATUS_CREATED);
180                 personAuthCSID = extractId(res);
181         } finally {
182                 res.close();
183         }
184         
185         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
186         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
187         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188         personIdsCreated.add(csid);
189         
190         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
191         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192         personIdsCreated.add(csid);
193         
194         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
195         conditionCheckerOrAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
196         personIdsCreated.add(csid);
197         
198         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
199         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
200         personIdsCreated.add(csid);
201         
202         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
203         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
204         personIdsCreated.add(csid);
205     }
206     
207     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) throws Exception {
208         String result = null;
209         
210         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
211         Map<String, String> personInfo = new HashMap<String,String>();
212         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
213         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
214         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
215         List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
216         PersonTermGroup term = new PersonTermGroup();
217         String termName = firstName + " " + surName;
218         term.setTermDisplayName(termName);
219         term.setTermName(termName);
220         personTerms.add(term);
221         PoxPayloadOut multipart = 
222                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
223                                 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
224         
225         Response res = personAuthClient.createItem(personAuthCSID, multipart);
226         try {
227                 int statusCode = res.getStatus();
228                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
229                         invalidStatusCodeMessage(testRequestType, statusCode));
230                 Assert.assertEquals(statusCode, STATUS_CREATED);
231                 result = extractId(res);
232         } finally {
233                 res.close();
234         }
235         
236         return result;
237     }
238
239     // Success outcomes
240     @Test(dataProvider="testName",
241         dependsOnMethods = {"createWithAuthRefs"})
242     public void readAndCheckAuthRefs(String testName) throws Exception {
243         // Perform setup.
244         testSetup(STATUS_OK, ServiceRequestType.READ);
245
246         // Submit the request to the service and store the response.
247         IntakeClient intakeClient = new IntakeClient();
248         Response res = intakeClient.read(knownResourceId);
249         try {
250                 assertStatusCode(res, testName);
251                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
252                 IntakesCommon intake = (IntakesCommon) extractPart(input,
253                                 intakeClient.getCommonPartName(), IntakesCommon.class);
254                 Assert.assertNotNull(intake);
255                 // Check a couple of fields
256                 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
257                 Assert.assertEquals(intake.getConditionCheckersOrAssessors().getConditionCheckerOrAssessor().get(0), conditionCheckerOrAssessorRefName);
258                 Assert.assertEquals(intake.getInsurers().getInsurer().get(0), insurerRefName);
259         } finally {
260                 if (res != null) {
261                 res.close();
262             }
263         }
264         
265         // Get the auth refs and check them
266         res = intakeClient.getAuthorityRefs(knownResourceId);
267         AuthorityRefList list = null;
268         try {
269                 assertStatusCode(res, testName);
270                 list = res.readEntity(AuthorityRefList.class);
271         } finally {
272                 if (res != null) {
273                         res.close();
274             }
275         }
276         
277         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
278         int numAuthRefsFound = items.size();
279         if(logger.isDebugEnabled()){
280             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
281                 " authority references, found " + numAuthRefsFound);
282         }
283         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
284             "Did not find all expected authority references! " +
285             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
286
287         // Optionally output additional data about list members for debugging.
288         boolean iterateThroughList = true;
289         if(iterateThroughList && logger.isDebugEnabled()){
290             int i = 0;
291             for(AuthorityRefList.AuthorityRefItem item : items){
292                 logger.debug(testName + ": list-item[" + i + "] Field:" +
293                                 item.getSourceField() + "= " +
294                         item.getAuthDisplayName() +
295                         item.getItemDisplayName());
296                 logger.debug(testName + ": list-item[" + i + "] refName=" +
297                         item.getRefName());
298                 logger.debug(testName + ": list-item[" + i + "] URI=" +
299                         item.getUri());
300                 i++;
301             }
302         }
303     }
304
305
306     // ---------------------------------------------------------------
307     // Cleanup of resources created during testing
308     // ---------------------------------------------------------------
309
310     /**
311      * Deletes all resources created by tests, after all tests have been run.
312      *
313      * This cleanup method will always be run, even if one or more tests fail.
314      * For this reason, it attempts to remove all resources created
315      * at any point during testing, even if some of those resources
316      * may be expected to be deleted by certain tests.
317      * @throws Exception 
318      */
319     @AfterClass(alwaysRun=true)
320     public void cleanUp() throws Exception {
321         String noTest = System.getProperty("noTestCleanup");
322         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
323             if (logger.isDebugEnabled()) {
324                 logger.debug("Skipping Cleanup phase ...");
325             }
326             return;
327         }
328         if (logger.isDebugEnabled()) {
329             logger.debug("Cleaning up temporary resources created for testing ...");
330         }
331         IntakeClient intakeClient = new IntakeClient();
332         // Note: Any non-success responses are ignored and not reported.
333         for (String resourceId : intakeIdsCreated) {
334             intakeClient.delete(resourceId).close();
335         }
336         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
337         // Delete persons before PersonAuth
338         for (String resourceId : personIdsCreated) {
339             personAuthClient.deleteItem(personAuthCSID, resourceId).close();
340         }
341         personAuthClient.delete(personAuthCSID).close();
342     }
343
344     // ---------------------------------------------------------------
345     // Utility methods used by tests above
346     // ---------------------------------------------------------------
347     @Override
348     public String getServicePathComponent() {
349         return SERVICE_PATH_COMPONENT;
350     }
351
352    private PoxPayloadOut createIntakeInstance(String entryNumber,
353                 String entryDate,
354                                 String currentOwner,
355                                 String depositor,
356                                 String conditionCheckerAssessor,
357                                 String insurer,
358                                 String Valuer ) throws Exception {
359         IntakesCommon intake = new IntakesCommon();
360         intake.setEntryNumber(entryNumber);
361         intake.setEntryDate(entryDate);
362         intake.setCurrentOwner(currentOwner);
363         intake.setDepositor(depositor);
364         intake.setValuer(Valuer);
365
366         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
367         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
368         checkersOrAssessors.add(conditionCheckerAssessor);
369         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
370
371         InsurerList insurerList = new InsurerList();
372         List<String> insurers = insurerList.getInsurer();
373         insurers.add(insurer);
374         intake.setInsurers(insurerList);
375
376         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
377         PayloadOutputPart commonPart =
378             multipart.addPart(new IntakeClient().getCommonPartName(), intake);
379
380         if(logger.isDebugEnabled()){
381             logger.debug("to be created, intake common");
382             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
383         }
384
385         return multipart;
386     }
387 }