]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b690bce9738d9f09f470980e20a116afdd5a01fb
[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  
24 package org.collectionspace.services.client.test;
25
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
29
30 import org.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.commons.httpclient.methods.PostMethod;
32 import org.apache.commons.httpclient.methods.PutMethod;
33 import org.apache.commons.httpclient.methods.StringRequestEntity;
34
35 import org.collectionspace.services.client.CollectionObjectClient;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.collectionspace.services.collectionobject.CollectionObject;
38 import org.collectionspace.services.collectionobject.CollectionObjectList;
39
40 import org.jboss.resteasy.client.ClientResponse;
41
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
44
45 /**
46  * CollectionObjectServiceTest, carries out tests against a
47  * deployed and running CollectionObject Service.
48  * 
49  * $LastChangedRevision$
50  * $LastChangedDate$
51  */
52 public class CollectionObjectServiceTest extends AbstractServiceTest {
53
54     // Instance variables specific to this test.
55     private CollectionObjectClient client = new CollectionObjectClient();
56
57     // Instance variables common to all entity service test classes.
58     private final String NON_EXISTENT_ID = createNonExistentIdentifier();
59     private String knownObjectId = null;
60  
61     // ---------------------------------------------------------------
62     // CRUD tests : CREATE tests
63     // ---------------------------------------------------------------
64
65     // Success outcomes
66     
67     @Override
68     @Test
69     public void create() {
70
71         // Perform setup, such as initializing the type of service request
72         // and its valid and expected status codes.
73         super.setupCreate();
74
75         // Submit the request to the service and store the response.
76         String identifier = createIdentifier();
77         CollectionObject collectionObject = createCollectionObject(identifier);
78         ClientResponse<Response> res = client.createCollectionObject(collectionObject);
79         int statusCode = res.getStatus();
80
81         // Check the status code of the response: does it match the expected response(s)?
82         //
83         // Does it fall within the set of valid status codes?
84         // Does it exactly match the expected status code?
85         verbose("create: status = " + statusCode);
86         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
87             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
88         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
89
90         // Store the ID returned from this create operation for additional tests below.
91         knownObjectId = extractId(res);
92     }
93
94     @Override
95     @Test(dependsOnMethods = {"create"})
96     public void createMultiple() {
97         for(int i = 0; i < 3; i++){
98             create   ();
99         }
100     }
101
102     // Failure outcomes
103
104     @Override
105     @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
106     public void createNull() {
107         ClientResponse<Response> res = client.createCollectionObject(null);
108     }
109     
110     @Override
111     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112     public void createWithMalformedXml() {
113     
114         // Perform setup.
115         super.setupCreateWithMalformedXml();
116
117         // Submit the request to the service and store the response.
118         String url = getServiceRootURL();
119         PostMethod method = new PostMethod(url);
120         final String MALFORMED_XML_DATA =
121             "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
122         StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
123         int statusCode = submitRequest(method, entity);
124         
125         // Check the status code of the response: does it match the expected response(s)?
126         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
127         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
130     }
131
132     @Override
133     @Test(dependsOnMethods = {"create", "testSubmitRequest"}) //, "createWithMalformedXml"})
134     public void createWithWrongXmlSchema() {
135     
136         // Perform setup.
137         super.setupCreateWithWrongXmlSchema();
138      
139         // Submit the request to the service and store the response.
140         String url = getServiceRootURL();
141         PostMethod method = new PostMethod(url);
142         final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
143         StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
144         int statusCode = submitRequest(method, entity);
145         
146         // Check the status code of the response: does it match the expected response(s)?
147         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
148         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151     }
152
153     // ---------------------------------------------------------------
154     // CRUD tests : READ tests
155     // ---------------------------------------------------------------
156
157     // Success outcomes
158
159     @Override
160     @Test(dependsOnMethods = {"create"})
161     public void read() {
162     
163         // Perform setup.
164         super.setupRead();
165
166         // Submit the request to the service and store the response.
167         ClientResponse<CollectionObject> res = 
168             client.getCollectionObject(knownObjectId);
169         int statusCode = res.getStatus();
170             
171         // Check the status code of the response: does it match the expected response(s)?
172         verbose("read: status = " + statusCode);
173         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176     }
177
178     @Override
179     @Test(dependsOnMethods = {"read"})
180     public void readNonExistent() {
181
182         // Perform setup.
183         super.setupReadNonExistent();
184         
185         // Submit the request to the service and store the response.
186         ClientResponse<CollectionObject> res = 
187             client.getCollectionObject(NON_EXISTENT_ID);
188         int statusCode = res.getStatus();
189
190         // Check the status code of the response: does it match the expected response(s)?
191         verbose("readNonExistent: status = " + res.getStatus());
192         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
195     }
196
197
198     // ---------------------------------------------------------------
199     // CRUD tests : READ (list, or multiple) tests
200     // ---------------------------------------------------------------
201
202     // Success outcomes
203
204     @Override
205     @Test(dependsOnMethods = {"createMultiple"})
206     public void readList() {
207     
208         // Perform setup.
209         super.setupReadList();
210
211         // Submit the request to the service and store the response.
212         ClientResponse<CollectionObjectList> res = client.getCollectionObjectList();
213         CollectionObjectList coList = res.getEntity();
214         int statusCode = res.getStatus();
215
216         // Check the status code of the response: does it match the expected response(s)?
217         verbose("readList: status = " + res.getStatus());
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221
222         // Optionally output additional data about list members for debugging.
223         boolean iterateThroughList = false;
224         if (iterateThroughList && logger.isDebugEnabled()) {
225             List<CollectionObjectList.CollectionObjectListItem> coItemList =
226                 coList.getCollectionObjectListItem();
227             int i = 0;
228             for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
229                 verbose("readList: list-item[" + i + "] csid=" + pli.getCsid());
230                 verbose("readList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
231                 verbose("readList: list-item[" + i + "] URI=" + pli.getUri());
232                 i++;
233             }
234         }
235         
236     }
237
238     // Failure outcomes
239     
240     // None at present.
241
242
243     // ---------------------------------------------------------------
244     // CRUD tests : UPDATE tests
245     // ---------------------------------------------------------------
246
247     // Success outcomes
248
249     @Override
250     @Test(dependsOnMethods = {"create"})
251     public void update() {
252     
253         // Perform setup.
254         super.setupUpdate();
255
256         // Retrieve an existing resource that we can update.
257         ClientResponse<CollectionObject> res = 
258             client.getCollectionObject(knownObjectId);
259         verbose("read: status = " + res.getStatus());
260         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
261         CollectionObject collectionObject = res.getEntity();
262         verbose("Got object to update with ID: " + knownObjectId,
263                 collectionObject, CollectionObject.class);
264
265         // Update the content of this resource.
266         //collectionObject.setCsid("updated-" + knownObjectId);
267         collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
268         collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
269
270         // Submit the request to the service and store the response.
271         res = client.updateCollectionObject(knownObjectId, collectionObject);
272         int statusCode = res.getStatus();
273         CollectionObject updatedCollectionObject = res.getEntity();
274
275         // Check the status code of the response: does it match the expected response(s)?
276         verbose("update: status = " + res.getStatus());
277         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
278             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
279         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
280         
281         // Check the contents of the response: does it match what was submitted?
282         verbose("update: ", updatedCollectionObject, CollectionObject.class);
283         Assert.assertEquals(updatedCollectionObject.getObjectName(), 
284             collectionObject.getObjectName(), "Data in updated object did not match submitted data.");
285     }
286
287     @Override
288     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
289     public void updateWithMalformedXml() {
290
291         // Perform setup.
292         super.setupUpdateWithMalformedXml();
293
294         // Submit the request to the service and store the response.
295         String url = getResourceURL(knownObjectId);
296         PutMethod method = new PutMethod(url);
297         final String MALFORMED_XML_DATA =
298             "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
299         StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
300         int statusCode = submitRequest(method, entity);
301         
302         // Check the status code of the response: does it match the expected response(s)?
303         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
304         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
305             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
306         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
307     }
308
309     @Test(dependsOnMethods = {"create", "testSubmitRequest"}) // , "createWithMalformedXml"})
310     public void updateWithWrongXmlSchema() {
311     
312         // Perform setup.
313         super.setupUpdateWithWrongXmlSchema();
314         
315         // Submit the request to the service and store the response.
316         String url = getResourceURL(knownObjectId);
317         PutMethod method = new PutMethod(url);
318         final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
319         StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
320         int statusCode = submitRequest(method, entity);
321         
322         // Check the status code of the response: does it match the expected response(s)?
323         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
324         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
325             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
326         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
327     }
328
329     @Test(dependsOnMethods = {"update"})
330     public void updateNonExistent() {
331
332         // Perform setup.
333         super.setupUpdateNonExistent();
334
335         // Submit the request to the service and store the response.
336         // Note: The ID used in this 'create' call may be arbitrary.
337         // The only relevant ID may be the one used in updateCollectionObject(), below.
338         CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
339         ClientResponse<CollectionObject> res =
340             client.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
341         int statusCode = res.getStatus();
342
343         // Check the status code of the response: does it match the expected response(s)?
344         verbose("updateNonExistent: status = " + res.getStatus());
345         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
346             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
347         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
348     }
349
350
351     // ---------------------------------------------------------------
352     // CRUD tests : DELETE tests
353     // ---------------------------------------------------------------
354
355     // Success outcomes
356
357     @Test(dependsOnMethods = 
358         {"create", "read", "testSubmitRequest", "update"})
359     public void delete() {
360
361         // Perform setup.
362         super.setupDelete();
363
364         // Submit the request to the service and store the response.
365         ClientResponse<Response> res = client.deleteCollectionObject(knownObjectId);
366         int statusCode = res.getStatus();
367
368         // Check the status code of the response: does it match the expected response(s)?
369         verbose("delete: status = " + res.getStatus());
370         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
371             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
372         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373     }
374
375     // Failure outcomes
376
377     @Test(dependsOnMethods = {"delete"})
378     public void deleteNonExistent() {
379
380         // Perform setup.
381         super.setupDeleteNonExistent();
382
383         // Submit the request to the service and store the response.
384         ClientResponse<Response> res =
385             client.deleteCollectionObject(NON_EXISTENT_ID);
386         int statusCode = res.getStatus();
387
388         // Check the status code of the response: does it match the expected response(s)?
389         verbose("deleteNonExistent: status = " + res.getStatus());
390         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
393     }
394
395
396     // ---------------------------------------------------------------
397     // Utility tests : tests of code used in tests above
398     // ---------------------------------------------------------------
399
400     /**
401      * Tests the HttpClient-based code used to submit data, in various methods below.
402      */
403     @Test(dependsOnMethods = {"create", "read"})
404     public void testSubmitRequest() {
405
406         // Expected status code: 200 OK
407         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
408
409         // Submit the request to the service and store the response.
410         String url = getResourceURL(knownObjectId);
411         GetMethod method = new GetMethod(url);
412         int statusCode = submitRequest(method);
413         
414         // Check the status code of the response: does it match the expected response(s)?
415         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
416         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417
418     }
419                 
420
421     // ---------------------------------------------------------------
422     // Utility methods used by tests above
423     // ---------------------------------------------------------------
424     
425     private CollectionObject createCollectionObject(String identifier) {
426         CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
427                 "objectName-" + identifier);
428         return collectionObject;
429     }
430
431     private CollectionObject createCollectionObject(String objectNumber, String objectName) {
432         CollectionObject collectionObject = new CollectionObject();
433         collectionObject.setObjectNumber(objectNumber);
434         collectionObject.setObjectName(objectName);
435         return collectionObject;
436     }
437     
438     @Override
439     public String getServicePathComponent() {
440         // @TODO Determine if it is possible to obtain this value programmatically.
441         // We set this in an annotation in the CollectionObjectProxy interface, for instance.
442         // We also set service-specific constants in each service module.
443         final String SERVICE_PATH_COMPONENT = "collectionobjects";
444         return SERVICE_PATH_COMPONENT;
445     }
446     
447 }