]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7d8a018d1fd1f7fdef7e0db85ac5f88a1a26d44c
[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.ConditioncheckClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.client.PayloadOutputPart;
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.jaxb.AbstractCommonList;
43 import org.collectionspace.services.conditioncheck.ConditionchecksCommon;
44 import org.collectionspace.services.person.PersonTermGroup;
45
46 import org.jboss.resteasy.client.ClientResponse;
47
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  * ConditioncheckAuthRefsTest, carries out Authority References tests against a
57  * deployed and running Conditioncheck (aka Condition Checks) Service.
58  */
59 public class ConditioncheckAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
60
61     private final String CLASS_NAME = ConditioncheckAuthRefsTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63     
64     // Instance variables specific to this test.
65     final String SERVICE_NAME = "conditionchecks";
66     final String SERVICE_PATH_COMPONENT = "conditionchecks";
67     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68     private String knownResourceId = null;
69     private List<String> conditioncheckIdsCreated = new ArrayList<String>();
70     private List<String> personIdsCreated = new ArrayList<String>();
71     private String personAuthCSID = null;
72     private String conditionCheckerRefName = null;
73
74     private final int NUM_AUTH_REFS_EXPECTED = 1;
75
76
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79      */
80     @Override
81     protected CollectionSpaceClient getClientInstance() {
82         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
83     }
84     
85     /* (non-Javadoc)
86      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87      */
88     @Override
89     protected AbstractCommonList getCommonList(
90             ClientResponse<AbstractCommonList> response) {
91         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92     }
93
94     // ---------------------------------------------------------------
95     // CRUD tests : CREATE tests
96     // ---------------------------------------------------------------
97     // Success outcomes
98     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
99     public void createWithAuthRefs(String testName) throws Exception {
100         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
101
102         // Submit the request to the service and store the response.
103         String identifier = createIdentifier();
104         
105         // Create all the person refs and entities
106         createPersonRefs();
107
108         // Create a new Condition Check resource.
109         //
110         // One or more fields in this resource will be PersonAuthority
111         // references, and will refer to Person resources by their refNames.
112         ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
113         PoxPayloadOut multipart = createConditioncheckInstance(
114                 "conditionCheckRefNumber-" + identifier,
115                 conditionCheckerRefName);
116         Response response = conditioncheckClient.create(multipart);
117         int statusCode = response.getStatus();
118         try {
119             // Check the status code of the response: does it match
120             // the expected response(s)?
121             //
122             // Specifically:
123             // Does it fall within the set of valid status codes?
124             // Does it exactly match the expected status code?
125             if(logger.isDebugEnabled()){
126                 logger.debug(testName + ": status = " + statusCode);
127             }
128             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
129                     invalidStatusCodeMessage(testRequestType, statusCode));
130             Assert.assertEquals(statusCode, testExpectedStatusCode);
131     
132             // Store the ID returned from the first resource created
133             // for additional tests below.
134             if (knownResourceId == null){
135                 knownResourceId = extractId(response);
136                 if (logger.isDebugEnabled()) {
137                     logger.debug(testName + ": knownResourceId=" + knownResourceId);
138                 }
139             }
140             
141             // Store the IDs from every resource created by tests,
142             // so they can be deleted after tests have been run.
143             conditioncheckIdsCreated.add(extractId(response));
144         } finally {
145             response.close();
146         }
147     }
148     
149     protected void createPersonRefs(){
150         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
151         // Create a temporary PersonAuthority resource, and its corresponding
152         // refName by which it can be identified.
153         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
154             PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
155         Response res = personAuthClient.create(multipart);
156         try {
157                 int statusCode = res.getStatus();
158                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
159                     invalidStatusCodeMessage(testRequestType, statusCode));
160                 Assert.assertEquals(statusCode, STATUS_CREATED);
161                 personAuthCSID = extractId(res);
162         } finally {
163                 res.close();
164         }
165
166         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
167         // Create temporary Person resources, and their corresponding refNames
168         // by which they can be identified.
169         String csid = createPerson("Carrie", "ConditionChecker1", "carrieConditionChecker", authRefName);
170         personIdsCreated.add(csid);
171         conditionCheckerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
172     }
173     
174     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
175         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
176         Map<String, String> personInfo = new HashMap<String,String>();
177         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
178         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
179         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
180         List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
181         PersonTermGroup term = new PersonTermGroup();
182         String termName = firstName + " " + surName;
183         term.setTermDisplayName(termName);
184         term.setTermName(termName);
185         personTerms.add(term);
186         PoxPayloadOut multipart =
187             PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
188                     authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
189         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
190         int statusCode = res.getStatus();
191
192         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
193                 invalidStatusCodeMessage(testRequestType, statusCode));
194         Assert.assertEquals(statusCode, STATUS_CREATED);
195         return extractId(res);
196     }
197
198     // Success outcomes
199     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
200         dependsOnMethods = {"createWithAuthRefs"})
201     public void readAndCheckAuthRefs(String testName) throws Exception {
202         // Perform setup.
203         testSetup(STATUS_OK, ServiceRequestType.READ);
204
205         // Submit the request to the service and store the response.
206         ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
207         ClientResponse<String> res = conditioncheckClient.read(knownResourceId);
208         ConditionchecksCommon conditioncheckCommon = null;
209         try {
210             assertStatusCode(res, testName);
211             // Extract the common part from the response.
212             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
213             conditioncheckCommon = (ConditionchecksCommon) extractPart(input,
214                 conditioncheckClient.getCommonPartName(), ConditionchecksCommon.class);
215             Assert.assertNotNull(conditioncheckCommon);
216             if(logger.isDebugEnabled()){
217                 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
218             }
219         } finally {
220             if (res != null) {
221                 res.releaseConnection();
222             }
223         }
224         //
225         // Check a couple of fields
226         Assert.assertEquals(conditioncheckCommon.getConditionChecker(), conditionCheckerRefName);
227         
228         // Get the auth refs and check them
229         ClientResponse<AuthorityRefList> res2 = conditioncheckClient.getAuthorityRefs(knownResourceId);
230         AuthorityRefList list = null;
231         try {
232             assertStatusCode(res2, testName);
233             list = res2.getEntity();
234             Assert.assertNotNull(list);
235         } finally {
236             if (res2 != null) {
237                 res2.releaseConnection();
238             }
239         }
240         
241         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
242         int numAuthRefsFound = items.size();
243         if(logger.isDebugEnabled()){
244             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
245                 " authority references, found " + numAuthRefsFound);
246         }
247
248         // Optionally output additional data about list members for debugging.
249         boolean iterateThroughList = true;
250         if(iterateThroughList && logger.isDebugEnabled()){
251             int i = 0;
252             for(AuthorityRefList.AuthorityRefItem item : items){
253                 logger.debug(testName + ": list-item[" + i + "] Field:" +
254                         item.getSourceField() + "= " +
255                         item.getAuthDisplayName() +
256                         item.getItemDisplayName());
257                 logger.debug(testName + ": list-item[" + i + "] refName=" +
258                         item.getRefName());
259                 logger.debug(testName + ": list-item[" + i + "] URI=" +
260                         item.getUri());
261                 i++;
262             }
263         }
264
265         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
266             "Did not find all expected authority references! " +
267             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
268
269     }
270
271
272     // ---------------------------------------------------------------
273     // Cleanup of resources created during testing
274     // ---------------------------------------------------------------
275
276     /**
277      * Deletes all resources created by tests, after all tests have been run.
278      *
279      * This cleanup method will always be run, even if one or more tests fail.
280      * For this reason, it attempts to remove all resources created
281      * at any point during testing, even if some of those resources
282      * may be expected to be deleted by certain tests.
283      */
284     @AfterClass(alwaysRun=true)
285     public void cleanUp() {
286         String noTest = System.getProperty("noTestCleanup");
287         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
288             if (logger.isDebugEnabled()) {
289                 logger.debug("Skipping Cleanup phase ...");
290             }
291             return;
292         }
293         if (logger.isDebugEnabled()) {
294             logger.debug("Cleaning up temporary resources created for testing ...");
295         }
296         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
297         // Delete Person resource(s) (before PersonAuthority resources).
298         
299         for (String resourceId : personIdsCreated) {
300             // Note: Any non-success responses are ignored and not reported.
301             ClientResponse<Response> response = 
302                 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
303             response.releaseConnection();
304         }
305         
306         // Delete PersonAuthority resource(s).
307         // Note: Any non-success response is ignored and not reported.
308         if (personAuthCSID != null) {
309             personAuthClient.delete(personAuthCSID);
310             // Delete Condition Checks resource(s).
311             ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
312             ClientResponse<Response> response = null;
313             for (String resourceId : conditioncheckIdsCreated) {
314                 // Note: Any non-success responses are ignored and not reported.
315                 response = conditioncheckClient.delete(resourceId); // alternative to conditioncheckClient.delete(resourceId).releaseConnection();
316                 response.releaseConnection();
317             }
318         }
319     }
320
321     // ---------------------------------------------------------------
322     // Utility methods used by tests above
323     // ---------------------------------------------------------------
324     public String getServiceName() {
325         return SERVICE_NAME;
326     }
327
328     @Override
329     public String getServicePathComponent() {
330         return SERVICE_PATH_COMPONENT;
331     }
332
333     private PoxPayloadOut createConditioncheckInstance(String conditionCheckRefNumber,
334             String conditionChecker) {
335         ConditionchecksCommon conditioncheckCommon = new ConditionchecksCommon();
336
337         conditioncheckCommon.setConditionCheckRefNumber(conditionCheckRefNumber);
338         conditioncheckCommon.setConditionChecker(conditionChecker);
339
340         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
341         PayloadOutputPart commonPart =
342                 multipart.addPart(new ConditioncheckClient().getCommonPartName(), conditioncheckCommon);
343
344         if(logger.isDebugEnabled()){
345             logger.debug("to be created, conditioncheck common");
346             logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
347         }
348
349         return multipart;
350     }
351
352     @Override
353     protected Class<AbstractCommonList> getCommonListType() {
354         return AbstractCommonList.class;
355     }
356 }