]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9e4e1d006f8e1ba365e39710bc41b3d2577d97bb
[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 contentInscriberRefName = 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                                                                 contentInscriberRefName );
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 =
201             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Omni Org", true);
202         personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
203         
204         contentPeopleRefName =
205             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Pushy People", true);
206         personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
207         
208         contentPersonRefName =
209                 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Connie ContactPerson", true);
210         personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
211         
212         contentInscriberRefName =
213             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Ingrid Inscriber", true);
214         personIdsCreated.add(createPerson("Ingrid", "Inscriber", contentInscriberRefName));
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.getInscriptionContentInscriber(), contentInscriberRefName);
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         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
294         int numAuthRefsFound = items.size();
295         if(logger.isDebugEnabled()){
296             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
297                 " authority references, found " + numAuthRefsFound);
298         }
299         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
300             "Did not find all expected authority references! " +
301             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
302
303         // Optionally output additional data about list members for debugging.
304         boolean iterateThroughList = true;
305         if(iterateThroughList && logger.isDebugEnabled()){;
306             int i = 0;
307             for(AuthorityRefList.AuthorityRefItem item : items){
308                 logger.debug(testName + ": list-item[" + i + "] Field:" +
309                                 item.getSourceField() + "= " +
310                         item.getAuthDisplayName() +
311                         item.getItemDisplayName());
312                 logger.debug(testName + ": list-item[" + i + "] refName=" +
313                         item.getRefName());
314                 logger.debug(testName + ": list-item[" + i + "] URI=" +
315                         item.getUri());
316                 i++;
317             }
318         }
319     }
320
321
322     // ---------------------------------------------------------------
323     // Cleanup of resources created during testing
324     // ---------------------------------------------------------------
325
326     /**
327      * Deletes all resources created by tests, after all tests have been run.
328      *
329      * This cleanup method will always be run, even if one or more tests fail.
330      * For this reason, it attempts to remove all resources created
331      * at any point during testing, even if some of those resources
332      * may be expected to be deleted by certain tests.
333      */
334     @AfterClass(alwaysRun=true)
335     public void cleanUp() {
336         String noTest = System.getProperty("noTestCleanup");
337         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
338             if (logger.isDebugEnabled()) {
339                 logger.debug("Skipping Cleanup phase ...");
340             }
341             return;
342         }
343         if (logger.isDebugEnabled()) {
344             logger.debug("Cleaning up temporary resources created for testing ...");
345         }
346         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
347         for (String resourceId : collectionObjectIdsCreated) {
348             // Note: Any non-success responses are ignored and not reported.
349             collectionObjectClient.delete(resourceId).releaseConnection();
350         }
351         // Note: Any non-success response is ignored and not reported.
352         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
353         // Delete persons before PersonAuth
354         for (String resourceId : personIdsCreated) {
355             // Note: Any non-success responses are ignored and not reported.
356             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
357         }
358         personAuthClient.delete(personAuthCSID).releaseConnection();
359     }
360
361     // ---------------------------------------------------------------
362     // Utility methods used by tests above
363     // ---------------------------------------------------------------
364     /* (non-Javadoc)
365      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
366      */
367     @Override
368     public String getServicePathComponent() {
369         return SERVICE_PATH_COMPONENT;
370     }
371
372    /**
373     * Creates the collection object instance.
374     *
375     * @param title the title
376     * @param objNum the obj num
377     * @param contentOrganization the content organization
378     * @param contentPeople the content people
379     * @param contentPerson the content person
380     * @param inscriber the inscriber
381     * @return the multipart output
382     */
383    private MultipartOutput createCollectionObjectInstance(
384                                 String title,
385                                 String objNum,
386                                 String contentOrganization,
387                                 String contentPeople,
388                                 String contentPerson,
389                                 String inscriber ) {
390         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
391         collectionObject.setTitle(title);
392         collectionObject.setObjectNumber(objNum);
393         collectionObject.setContentOrganization(contentOrganization);
394         collectionObject.setContentPeople(contentPeople);
395         collectionObject.setContentPerson(contentPerson);
396         collectionObject.setInscriptionContentInscriber(inscriber);
397         MultipartOutput multipart = new MultipartOutput();
398         OutputPart commonPart =
399             multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
400         commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
401
402         if(logger.isDebugEnabled()){
403             logger.debug("to be created, collectionObject common");
404             logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));
405         }
406
407         return multipart;
408     }
409 }