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