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