]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
970bbbe0312788d2d52f19e8b416b15381136c31
[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     private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
67     private PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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     // ---------------------------------------------------------------
83     // CRUD tests : CREATE tests
84     // ---------------------------------------------------------------
85     // Success outcomes
86     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87     public void createWithAuthRefs(String testName) throws Exception {
88
89         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
90
91         // Submit the request to the service and store the response.
92         String identifier = createIdentifier();
93         
94         // Create all the person refs and entities
95         createPersonRefs();
96         
97         MultipartOutput multipart = createCollectionObjectInstance(
98                                                         "Obj Title",
99                                                         "ObjNum-1234",
100                                                                 contentOrganizationRefName,
101                                                                 contentPeopleRefName,
102                                                                 contentPersonRefName,
103                                                                 inscriberRefName );
104
105         ClientResponse<Response> res = collectionObjectClient.create(multipart);
106
107         int statusCode = res.getStatus();
108
109         // Check the status code of the response: does it match
110         // the expected response(s)?
111         //
112         // Specifically:
113         // Does it fall within the set of valid status codes?
114         // Does it exactly match the expected status code?
115         if(logger.isDebugEnabled()){
116             logger.debug(testName + ": status = " + statusCode);
117         }
118         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
119                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
120         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
121
122         // Store the ID returned from the first resource created
123         // for additional tests below.
124         if (knownResourceId == null){
125             knownResourceId = extractId(res);
126             if (logger.isDebugEnabled()) {
127                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
128             }
129         }
130         
131         // Store the IDs from every resource created by tests,
132         // so they can be deleted after tests have been run.
133         collectionObjectIdsCreated.add(extractId(res));
134     }
135     
136     protected void createPersonRefs(){
137         String authRefName = 
138                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
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         MultipartOutput multipart = 
171                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
172                                 refName, personInfo, personAuthClient.getItemCommonPartName());
173         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
174         int statusCode = res.getStatus();
175
176         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178         Assert.assertEquals(statusCode, CREATED_STATUS);
179         return extractId(res);
180     }
181
182     // Success outcomes
183     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
184         dependsOnMethods = {"createWithAuthRefs"})
185     public void readAndCheckAuthRefs(String testName) throws Exception {
186
187         // Perform setup.
188         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
189
190         // Submit the request to the service and store the response.
191         ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
192         int statusCode = res.getStatus();
193
194         // Check the status code of the response: does it match
195         // the expected response(s)?
196         if(logger.isDebugEnabled()){
197             logger.debug(testName + ".read: status = " + statusCode);
198         }
199         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
202
203         MultipartInput input = (MultipartInput) res.getEntity();
204         CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
205                         collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
206         Assert.assertNotNull(collectionObject);
207         // Check a couple of fields
208         Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
209         Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
210         
211         // Get the auth refs and check them
212         ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
213         statusCode = res2.getStatus();
214
215         if(logger.isDebugEnabled()){
216             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
217         }
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221         AuthorityRefList list = res2.getEntity();
222
223         // Optionally output additional data about list members for debugging.
224         boolean iterateThroughList = true;
225         if(iterateThroughList && logger.isDebugEnabled()){
226             List<AuthorityRefList.AuthorityRefItem> items =
227                     list.getAuthorityRefItem();
228             int i = 0;
229             for(AuthorityRefList.AuthorityRefItem item : items){
230                 logger.debug(testName + ": list-item[" + i + "] Field:" +
231                                 item.getSourceField() + "= " +
232                         item.getAuthDisplayName() +
233                         item.getItemDisplayName());
234                 logger.debug(testName + ": list-item[" + i + "] refName=" +
235                         item.getRefName());
236                 logger.debug(testName + ": list-item[" + i + "] URI=" +
237                         item.getUri());
238                 i++;
239             }
240             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
241         }
242     }
243
244
245     // ---------------------------------------------------------------
246     // Cleanup of resources created during testing
247     // ---------------------------------------------------------------
248
249     /**
250      * Deletes all resources created by tests, after all tests have been run.
251      *
252      * This cleanup method will always be run, even if one or more tests fail.
253      * For this reason, it attempts to remove all resources created
254      * at any point during testing, even if some of those resources
255      * may be expected to be deleted by certain tests.
256      */
257     // @AfterClass(alwaysRun=true)
258     public void cleanUp() {
259         if (logger.isDebugEnabled()) {
260             logger.debug("Cleaning up temporary resources created for testing ...");
261         }
262         // Note: Any non-success responses are ignored and not reported.
263         for (String resourceId : collectionObjectIdsCreated) {
264             ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
265         }
266         // Delete persons before PersonAuth
267         for (String resourceId : personIdsCreated) {
268             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
269         }
270         ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
271     }
272
273     // ---------------------------------------------------------------
274     // Utility methods used by tests above
275     // ---------------------------------------------------------------
276     @Override
277     public String getServicePathComponent() {
278         return SERVICE_PATH_COMPONENT;
279     }
280
281    private MultipartOutput createCollectionObjectInstance(
282                                 String title,
283                                 String objNum,
284                                 String contentOrganization,
285                                 String contentPeople,
286                                 String contentPerson,
287                                 String inscriber ) {
288         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
289         collectionObject.setTitle(title);
290         collectionObject.setObjectNumber(objNum);
291         collectionObject.setContentOrganization(contentOrganization);
292         collectionObject.setContentPeople(contentPeople);
293         collectionObject.setContentPerson(contentPerson);
294         collectionObject.setInscriber(inscriber);
295         MultipartOutput multipart = new MultipartOutput();
296         OutputPart commonPart =
297             multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
298         commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
299
300         if(logger.isDebugEnabled()){
301             logger.debug("to be created, collectionObject common");
302             logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));
303         }
304
305         return multipart;
306     }
307 }