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