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