]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f0f9737e8ff094194770b0d831464f1a0717ea76
[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 Logger logger =
66        LoggerFactory.getLogger(CollectionObjectAuthRefsTest.class);
67
68     // Instance variables specific to this test.
69     /** The SERVIC e_ pat h_ component. */
70     final String SERVICE_PATH_COMPONENT = "collectionobjects";
71     
72     /** The PERSO n_ authorit y_ 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 CREATE d_ status. */
85     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
86     
87     /** The O k_ status. */
88     private int OK_STATUS = Response.Status.OK.getStatusCode();
89     
90     /** The person auth csid. */
91     private String personAuthCSID = null; 
92     
93     /** The content organization ref name. */
94     private String contentOrganizationRefName = null;
95     
96     /** The content people ref name. */
97     private String contentPeopleRefName = null;
98     
99     /** The content person ref name. */
100     private String contentPersonRefName = null;
101     
102     /** The inscriber ref name. */
103     private String inscriberRefName = null;
104     
105     /** The NU m_ aut h_ ref s_ expected. */
106     private final int NUM_AUTH_REFS_EXPECTED = 4;
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
110      */
111     @Override
112     protected CollectionSpaceClient getClientInstance() {
113         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
114     }
115     
116     /* (non-Javadoc)
117      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
118      */
119     @Override
120         protected AbstractCommonList getAbstractCommonList(
121                         ClientResponse<AbstractCommonList> response) {
122         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
123     }
124
125     // ---------------------------------------------------------------
126     // CRUD tests : CREATE tests
127     // ---------------------------------------------------------------
128     // Success outcomes
129     /**
130      * Creates the with auth refs.
131      *
132      * @param testName the test name
133      * @throws Exception the exception
134      */
135     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
136     public void createWithAuthRefs(String testName) throws Exception {
137
138         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
139
140         // Submit the request to the service and store the response.
141         String identifier = createIdentifier();
142         
143         // Create all the person refs and entities
144         createPersonRefs();
145         
146         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
147         MultipartOutput multipart = createCollectionObjectInstance(
148                                                         "Obj Title",
149                                                         "ObjNum-1234",
150                                                                 contentOrganizationRefName,
151                                                                 contentPeopleRefName,
152                                                                 contentPersonRefName,
153                                                                 inscriberRefName );
154
155         ClientResponse<Response> res = collectionObjectClient.create(multipart);
156
157         int statusCode = res.getStatus();
158
159         // Check the status code of the response: does it match
160         // the expected response(s)?
161         //
162         // Specifically:
163         // Does it fall within the set of valid status codes?
164         // Does it exactly match the expected status code?
165         if(logger.isDebugEnabled()){
166             logger.debug(testName + ": status = " + statusCode);
167         }
168         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
171
172         // Store the ID returned from the first resource created
173         // for additional tests below.
174         if (knownResourceId == null){
175             knownResourceId = extractId(res);
176             if (logger.isDebugEnabled()) {
177                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
178             }
179         }
180         
181         // Store the IDs from every resource created by tests,
182         // so they can be deleted after tests have been run.
183         collectionObjectIdsCreated.add(extractId(res));
184     }
185     
186     /**
187      * Creates the person refs.
188      */
189     protected void createPersonRefs(){
190         String authRefName = 
191                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
192         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
193         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
194                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
195         ClientResponse<Response> res = personAuthClient.create(multipart);
196         int statusCode = res.getStatus();
197
198         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200         Assert.assertEquals(statusCode, CREATED_STATUS);
201         personAuthCSID = extractId(res);
202         
203         contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
204                                                                 authRefName, "Omni Org", true);
205         personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
206         
207         contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
208                                                                         authRefName, "Pushy People", true);
209         personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
210         
211         contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
212                                                                         authRefName, "Connie ContactPerson", true);
213         personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
214         
215         inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
216                                                                         authRefName, "Ingrid Inscriber", true);
217         personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
218     }
219     
220     /**
221      * Creates the person.
222      *
223      * @param firstName the first name
224      * @param surName the sur name
225      * @param refName the ref name
226      * @return the string
227      */
228     protected String createPerson(String firstName, String surName, String refName ) {
229         Map<String, String> personInfo = new HashMap<String,String>();
230         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
231         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
232         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
233         MultipartOutput multipart = 
234                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
235                                 refName, personInfo, personAuthClient.getItemCommonPartName());
236         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
237         int statusCode = res.getStatus();
238
239         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241         Assert.assertEquals(statusCode, CREATED_STATUS);
242         return extractId(res);
243     }
244
245     // Success outcomes
246     /**
247      * Read and check auth refs.
248      *
249      * @param testName the test name
250      * @throws Exception the exception
251      */
252     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
253         dependsOnMethods = {"createWithAuthRefs"})
254     public void readAndCheckAuthRefs(String testName) throws Exception {
255
256         // Perform setup.
257         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
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 }