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