]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d5b42d41979766f49c750ddbfcc5c4f78203facb
[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.CollectionObjectClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
40 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
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  * CollectionObjectAuthRefsTest, carries out tests against a
57  * deployed and running CollectionObject Service.
58  *
59  * $LastChangedRevision: 1327 $
60  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61  */
62 public class CollectionObjectAuthRefsTest extends BaseServiceTest {
63
64    /** The logger. */
65     private final String CLASS_NAME = CollectionObjectAuthRefsTest.class.getName();
66     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67
68     // Instance variables specific to this test.
69     /** The service path component. */
70     final String SERVICE_PATH_COMPONENT = "collectionobjects";
71     
72     /** The person authority name. */
73     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
74     
75     /** The known resource id. */
76     private String knownResourceId = null;
77     
78     /** The collection object ids created. */
79     private List<String> collectionObjectIdsCreated = new ArrayList<String>();
80     
81     /** The person ids created. */
82     private List<String> personIdsCreated = new ArrayList<String>();
83     
84     /** The person auth csid. */
85     private String personAuthCSID = null; 
86     
87     /** The content organization ref name. */
88     private String contentOrganizationRefName = null;
89     
90     /** The content people ref name. */
91     private String contentPeopleRefName = null;
92     
93     /** The content person ref name. */
94     private String contentPersonRefName = null;
95     
96     /** The inscriber ref name. */
97     private String inscriberRefName = null;
98     
99     /** The number of authority references expected. */
100     private final int NUM_AUTH_REFS_EXPECTED = 4;
101
102     /* (non-Javadoc)
103      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
104      */
105     @Override
106     protected CollectionSpaceClient getClientInstance() {
107         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
108     }
109     
110     /* (non-Javadoc)
111      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
112      */
113     @Override
114         protected AbstractCommonList getAbstractCommonList(
115                         ClientResponse<AbstractCommonList> response) {
116         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
117     }
118
119     // ---------------------------------------------------------------
120     // CRUD tests : CREATE tests
121     // ---------------------------------------------------------------
122     // Success outcomes
123     /**
124      * Creates the with auth refs.
125      *
126      * @param testName the test name
127      * @throws Exception the exception
128      */
129     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
130     public void createWithAuthRefs(String testName) throws Exception {
131
132         if (logger.isDebugEnabled()) {
133             logger.debug(testBanner(testName, CLASS_NAME));
134         }
135         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
136
137         // Submit the request to the service and store the response.
138         String identifier = createIdentifier();
139         
140         // Create all the person refs and entities
141         createPersonRefs();
142         
143         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
144         MultipartOutput multipart = createCollectionObjectInstance(
145                                                         "Obj Title",
146                                                         "ObjNum-1234",
147                                                                 contentOrganizationRefName,
148                                                                 contentPeopleRefName,
149                                                                 contentPersonRefName,
150                                                                 inscriberRefName );
151
152         ClientResponse<Response> res = collectionObjectClient.create(multipart);
153
154         int statusCode = res.getStatus();
155
156         // Check the status code of the response: does it match
157         // the expected response(s)?
158         //
159         // Specifically:
160         // Does it fall within the set of valid status codes?
161         // Does it exactly match the expected status code?
162         if(logger.isDebugEnabled()){
163             logger.debug(testName + ": status = " + statusCode);
164         }
165         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
166                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
167         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
168
169         // Store the ID returned from the first resource created
170         // for additional tests below.
171         if (knownResourceId == null){
172             knownResourceId = extractId(res);
173             if (logger.isDebugEnabled()) {
174                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
175             }
176         }
177         
178         // Store the IDs from every resource created by tests,
179         // so they can be deleted after tests have been run.
180         collectionObjectIdsCreated.add(extractId(res));
181     }
182     
183     /**
184      * Creates the person refs.
185      */
186     protected void createPersonRefs(){
187         String authRefName = 
188                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
189         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
190         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
191                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
192         ClientResponse<Response> res = personAuthClient.create(multipart);
193         int statusCode = res.getStatus();
194
195         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197         Assert.assertEquals(statusCode, STATUS_CREATED);
198         personAuthCSID = extractId(res);
199         
200         contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
201                                                                 authRefName, "Omni Org", true);
202         personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
203         
204         contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
205                                                                         authRefName, "Pushy People", true);
206         personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
207         
208         contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
209                                                                         authRefName, "Connie ContactPerson", true);
210         personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
211         
212         inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
213                                                                         authRefName, "Ingrid Inscriber", true);
214         personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
215     }
216     
217     /**
218      * Creates the person.
219      *
220      * @param firstName the first name
221      * @param surName the sur name
222      * @param refName the ref name
223      * @return the string
224      */
225     protected String createPerson(String firstName, String surName, String refName ) {
226         Map<String, String> personInfo = new HashMap<String,String>();
227         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
228         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
229         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
230         MultipartOutput multipart = 
231                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
232                                 refName, personInfo, personAuthClient.getItemCommonPartName());
233         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
234         int statusCode = res.getStatus();
235
236         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238         Assert.assertEquals(statusCode, STATUS_CREATED);
239         return extractId(res);
240     }
241
242     // Success outcomes
243     /**
244      * Read and check auth refs.
245      *
246      * @param testName the test name
247      * @throws Exception the exception
248      */
249     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
250         dependsOnMethods = {"createWithAuthRefs"})
251     public void readAndCheckAuthRefs(String testName) throws Exception {
252
253         if (logger.isDebugEnabled()) {
254             logger.debug(testBanner(testName, CLASS_NAME));
255         }
256         // Perform setup.
257         testSetup(STATUS_OK, ServiceRequestType.READ);
258
259         // Submit the request to the service and store the response.
260         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
261         ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
262         int statusCode = res.getStatus();
263
264         // Check the status code of the response: does it match
265         // the expected response(s)?
266         if(logger.isDebugEnabled()){
267             logger.debug(testName + ".read: status = " + statusCode);
268         }
269         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272
273         MultipartInput input = (MultipartInput) res.getEntity();
274         CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
275                         collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
276         Assert.assertNotNull(collectionObject);
277         // Check a couple of fields
278         Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
279         Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
280         
281         // Get the auth refs and check them
282         ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
283         statusCode = res2.getStatus();
284
285         if(logger.isDebugEnabled()){
286             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
287         }
288         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291         AuthorityRefList list = res2.getEntity();
292
293         // Optionally output additional data about list members for debugging.
294         boolean iterateThroughList = true;
295         if(iterateThroughList && logger.isDebugEnabled()){
296             List<AuthorityRefList.AuthorityRefItem> items =
297                     list.getAuthorityRefItem();
298             int i = 0;
299             for(AuthorityRefList.AuthorityRefItem item : items){
300                 logger.debug(testName + ": list-item[" + i + "] Field:" +
301                                 item.getSourceField() + "= " +
302                         item.getAuthDisplayName() +
303                         item.getItemDisplayName());
304                 logger.debug(testName + ": list-item[" + i + "] refName=" +
305                         item.getRefName());
306                 logger.debug(testName + ": list-item[" + i + "] URI=" +
307                         item.getUri());
308                 i++;
309             }
310             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
311         }
312     }
313
314
315     // ---------------------------------------------------------------
316     // Cleanup of resources created during testing
317     // ---------------------------------------------------------------
318
319     /**
320      * Deletes all resources created by tests, after all tests have been run.
321      *
322      * This cleanup method will always be run, even if one or more tests fail.
323      * For this reason, it attempts to remove all resources created
324      * at any point during testing, even if some of those resources
325      * may be expected to be deleted by certain tests.
326      */
327     @AfterClass(alwaysRun=true)
328     public void cleanUp() {
329         String noTest = System.getProperty("noTestCleanup");
330         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
331             if (logger.isDebugEnabled()) {
332                 logger.debug("Skipping Cleanup phase ...");
333             }
334             return;
335         }
336         if (logger.isDebugEnabled()) {
337             logger.debug("Cleaning up temporary resources created for testing ...");
338         }
339         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
340         for (String resourceId : collectionObjectIdsCreated) {
341             // Note: Any non-success responses are ignored and not reported.
342             collectionObjectClient.delete(resourceId).releaseConnection();
343         }
344         // Note: Any non-success response is ignored and not reported.
345         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
346         // Delete persons before PersonAuth
347         for (String resourceId : personIdsCreated) {
348             // Note: Any non-success responses are ignored and not reported.
349             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
350         }
351         personAuthClient.delete(personAuthCSID).releaseConnection();
352     }
353
354     // ---------------------------------------------------------------
355     // Utility methods used by tests above
356     // ---------------------------------------------------------------
357     /* (non-Javadoc)
358      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
359      */
360     @Override
361     public String getServicePathComponent() {
362         return SERVICE_PATH_COMPONENT;
363     }
364
365    /**
366     * Creates the collection object instance.
367     *
368     * @param title the title
369     * @param objNum the obj num
370     * @param contentOrganization the content organization
371     * @param contentPeople the content people
372     * @param contentPerson the content person
373     * @param inscriber the inscriber
374     * @return the multipart output
375     */
376    private MultipartOutput createCollectionObjectInstance(
377                                 String title,
378                                 String objNum,
379                                 String contentOrganization,
380                                 String contentPeople,
381                                 String contentPerson,
382                                 String inscriber ) {
383         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
384         collectionObject.setTitle(title);
385         collectionObject.setObjectNumber(objNum);
386         collectionObject.setContentOrganization(contentOrganization);
387         collectionObject.setContentPeople(contentPeople);
388         collectionObject.setContentPerson(contentPerson);
389         collectionObject.setInscriber(inscriber);
390         MultipartOutput multipart = new MultipartOutput();
391         OutputPart commonPart =
392             multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
393         commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
394
395         if(logger.isDebugEnabled()){
396             logger.debug("to be created, collectionObject common");
397             logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));
398         }
399
400         return multipart;
401     }
402 }