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