]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8ea4c8488b9ba2ce807bf4b689bc702ddd06b421
[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.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30
31 import org.collectionspace.services.client.CollectionObjectClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.collectionobject.CollectionObject;
34 import org.collectionspace.services.collectionobject.CollectionObjectList;
35
36 import org.jboss.resteasy.client.ClientResponse;
37
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 /**
42  * CollectionObjectServiceTest, carries out tests against a
43  * deployed and running CollectionObject Service.
44  * 
45  * $LastChangedRevision$
46  * $LastChangedDate$
47  */
48 public class CollectionObjectServiceTest extends AbstractServiceTest {
49
50     // Instance variables specific to this test.
51     private CollectionObjectClient client = new CollectionObjectClient();
52     final String SERVICE_PATH_COMPONENT = "collectionobjects";
53     private String knownResourceId = null; 
54  
55     // ---------------------------------------------------------------
56     // CRUD tests : CREATE tests
57     // ---------------------------------------------------------------
58
59     // Success outcomes
60     
61     @Override
62     @Test
63     public void create() {
64
65         // Perform setup, such as initializing the type of service request
66         // (e.g. CREATE, DELETE), its valid and expected status codes, and
67         // its associated HTTP method name (e.g. POST, DELETE).
68         setupCreate();
69
70         // Submit the request to the service and store the response.
71         String identifier = createIdentifier();
72         CollectionObject collectionObject =
73           createCollectionObjectInstance(identifier);
74         ClientResponse<Response> res = client.create(collectionObject);
75         int statusCode = res.getStatus();
76
77         // Check the status code of the response: does it match
78         // the expected response(s)?
79         //
80         // Specifically:
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
89         // for additional tests below.
90         knownResourceId = extractId(res);
91     }
92
93     @Override
94     @Test(dependsOnMethods = {"create"})
95     public void createList() {
96         for(int i = 0; i < 3; i++){
97             create();
98         }
99     }
100
101     // Failure outcomes
102
103     // Placeholders until the three tests below can be uncommented.
104     // See Issue CSPACE-401.
105     public void createWithEmptyEntityBody() {}
106     public void createWithMalformedXml() {}
107     public void createWithWrongXmlSchema() {}
108
109 /*
110     @Override
111     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112     public void createWithEmptyEntityBody() {
113     
114         // Perform setup.
115         setupCreateWithEmptyEntityBody();
116
117         // Submit the request to the service and store the response.
118         String method = REQUEST_TYPE.httpMethodName();
119         String url = getServiceRootURL();
120         String mediaType = MediaType.APPLICATION_XML;
121         final String entity = "";
122         int statusCode = submitRequest(method, url, mediaType, entity);
123         
124         // Check the status code of the response: does it match
125         // the expected response(s)?
126         verbose("createWithEmptyEntityBody 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"})
134     public void createWithMalformedXml() {
135     
136         // Perform setup.
137         setupCreateWithMalformedXml();
138
139         // Submit the request to the service and store the response.
140         String method = REQUEST_TYPE.httpMethodName();
141         String url = getServiceRootURL();
142         String mediaType = MediaType.APPLICATION_XML;
143         final String entity = MALFORMED_XML_DATA; // Constant from base class.
144         int statusCode = submitRequest(method, url, mediaType, entity);
145         
146         // Check the status code of the response: does it match
147         // the expected response(s)?
148         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
149         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
152     }
153
154     @Override
155     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
156     public void createWithWrongXmlSchema() {
157     
158         // Perform setup.
159         setupCreateWithWrongXmlSchema();
160      
161         // Submit the request to the service and store the response.
162         String method = REQUEST_TYPE.httpMethodName();
163         String url = getServiceRootURL();
164         String mediaType = MediaType.APPLICATION_XML;
165         final String entity = WRONG_XML_SCHEMA_DATA;
166         int statusCode = submitRequest(method, url, mediaType, entity);
167         
168         // Check the status code of the response: does it match
169         // the expected response(s)?
170         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
171         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
174     }
175 */
176
177     // ---------------------------------------------------------------
178     // CRUD tests : READ tests
179     // ---------------------------------------------------------------
180
181     // Success outcomes
182
183     @Override
184     @Test(dependsOnMethods = {"create"})
185     public void read() {
186     
187         // Perform setup.
188         setupRead();
189
190         // Submit the request to the service and store the response.
191         ClientResponse<CollectionObject> res = client.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         verbose("read: status = " + statusCode);
197         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
200     }
201     
202     // Failure outcomes
203
204     @Override
205     @Test(dependsOnMethods = {"read"})
206     public void readNonExistent() {
207
208         // Perform setup.
209         setupReadNonExistent();
210         
211         // Submit the request to the service and store the response.
212         ClientResponse<CollectionObject> res = client.read(NON_EXISTENT_ID);
213         int statusCode = res.getStatus();
214
215         // Check the status code of the response: does it match
216         // the expected response(s)?
217         verbose("readNonExistent: status = " + res.getStatus());
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221     }
222
223
224     // ---------------------------------------------------------------
225     // CRUD tests : READ_LIST tests
226     // ---------------------------------------------------------------
227
228     // Success outcomes
229
230     @Override
231     @Test(dependsOnMethods = {"createList"})
232     public void readList() {
233     
234         // Perform setup.
235         setupReadList();
236
237         // Submit the request to the service and store the response.
238         ClientResponse<CollectionObjectList> res = client.readList();
239         CollectionObjectList list = res.getEntity();
240         int statusCode = res.getStatus();
241
242         // Check the status code of the response: does it match
243         // the expected response(s)?
244         verbose("readList: status = " + res.getStatus());
245         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
248
249         // Optionally output additional data about list members for debugging.
250         boolean iterateThroughList = false;
251         if (iterateThroughList && logger.isDebugEnabled()) {
252             List<CollectionObjectList.CollectionObjectListItem> items =
253                 list.getCollectionObjectListItem();
254             int i = 0;
255             for(CollectionObjectList.CollectionObjectListItem item : items){
256                 verbose("readList: list-item[" + i + "] csid=" +
257                     item.getCsid());
258                 verbose("readList: list-item[" + i + "] objectNumber=" +
259                     item.getObjectNumber());
260                 verbose("readList: list-item[" + i + "] URI=" +
261                     item.getUri());
262                 i++;
263             }
264         }
265         
266     }
267
268     // Failure outcomes
269     
270     // None at present.
271
272
273     // ---------------------------------------------------------------
274     // CRUD tests : UPDATE tests
275     // ---------------------------------------------------------------
276
277     // Success outcomes
278
279     @Override
280     @Test(dependsOnMethods = {"create"})
281     public void update() {
282     
283         // Perform setup.
284         setupUpdate();
285
286         // Retrieve an existing resource that we can update.
287         ClientResponse<CollectionObject> res = client.read(knownResourceId);
288         verbose("read: status = " + res.getStatus());
289         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290         CollectionObject collectionObject = res.getEntity();
291         verbose("Got object to update with ID: " + knownResourceId,
292                 collectionObject, CollectionObject.class);
293
294         // Update the content of this resource.
295         collectionObject.setObjectNumber("updated-" +
296           collectionObject.getObjectNumber());
297         collectionObject.setObjectName("updated-" +
298           collectionObject.getObjectName());
299
300         // Submit the request to the service and store the response.
301         res = client.update(knownResourceId, collectionObject);
302         int statusCode = res.getStatus();
303
304         // Check the status code of the response: does it match
305         // the expected response(s)?
306         verbose("update: status = " + res.getStatus());
307         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
310         
311         // Check the contents of the response: does it match
312         // what was submitted?
313         CollectionObject updatedObject = res.getEntity();
314         verbose("update: ", updatedObject, CollectionObject.class);
315         Assert.assertEquals(updatedObject.getObjectName(), 
316             collectionObject.getObjectName(), 
317             "Data in updated object did not match submitted data.");
318     }
319     
320     // Failure outcomes
321
322     // Placeholders until the three tests below can be uncommented.
323     // See Issue CSPACE-401.
324     public void updateWithEmptyEntityBody() {}
325     public void updateWithMalformedXml() {}
326     public void updateWithWrongXmlSchema() {}
327
328 /*
329     @Override
330     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
331     public void updateWithEmptyEntityBody() {
332     
333         // Perform setup.
334         setupUpdateWithEmptyEntityBody();
335
336         // Submit the request to the service and store the response.
337         String method = REQUEST_TYPE.httpMethodName();
338         String url = getResourceURL(knownResourceId);
339         String mediaType = MediaType.APPLICATION_XML;
340         final String entity = "";
341         int statusCode = submitRequest(method, url, mediaType, entity);
342         
343         // Check the status code of the response: does it match
344         // the expected response(s)?
345         verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
346         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349     }
350
351     @Override
352     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
353     public void updateWithMalformedXml() {
354
355         // Perform setup.
356         setupUpdateWithMalformedXml();
357
358         // Submit the request to the service and store the response.
359         String method = REQUEST_TYPE.httpMethodName();
360         String url = getResourceURL(knownResourceId);
361         final String entity = MALFORMED_XML_DATA;
362         String mediaType = MediaType.APPLICATION_XML;
363         int statusCode = submitRequest(method, url, mediaType, entity);
364         
365         // Check the status code of the response: does it match
366         // the expected response(s)?
367         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
368         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371     }
372
373     @Override
374     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
375     public void updateWithWrongXmlSchema() {
376     
377         // Perform setup.
378         setupUpdateWithWrongXmlSchema();
379         
380         // Submit the request to the service and store the response.
381         String method = REQUEST_TYPE.httpMethodName();
382         String url = getResourceURL(knownResourceId);
383         String mediaType = MediaType.APPLICATION_XML;
384         final String entity = WRONG_XML_SCHEMA_DATA;
385         int statusCode = submitRequest(method, url, mediaType, entity);
386         
387         // Check the status code of the response: does it match
388         // the expected response(s)?
389         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
390         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
393     }
394 */
395
396     @Override
397     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
398     public void updateNonExistent() {
399
400         // Perform setup.
401         setupUpdateNonExistent();
402
403         // Submit the request to the service and store the response.
404         // Note: The ID used in this 'create' call may be arbitrary.
405         // The only relevant ID may be the one used in update(), below.
406         CollectionObject collectionObject = createCollectionObjectInstance(NON_EXISTENT_ID);
407         ClientResponse<CollectionObject> res =
408           client.update(NON_EXISTENT_ID, collectionObject);
409         int statusCode = res.getStatus();
410
411         // Check the status code of the response: does it match
412         // the expected response(s)?
413         verbose("updateNonExistent: status = " + res.getStatus());
414         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417     }
418
419     // ---------------------------------------------------------------
420     // CRUD tests : DELETE tests
421     // ---------------------------------------------------------------
422
423     // Success outcomes
424
425     @Override
426     @Test(dependsOnMethods = 
427         {"create", "read", "update"})
428     public void delete() {
429
430         // Perform setup.
431         setupDelete();
432
433         // Submit the request to the service and store the response.
434         ClientResponse<Response> res = client.delete(knownResourceId);
435         int statusCode = res.getStatus();
436
437         // Check the status code of the response: does it match
438         // the expected response(s)?
439         verbose("delete: status = " + res.getStatus());
440         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
441             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
442         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
443     }
444
445     // Failure outcomes
446
447     @Override
448     @Test(dependsOnMethods = {"delete"})
449     public void deleteNonExistent() {
450
451         // Perform setup.
452         setupDeleteNonExistent();
453
454         // Submit the request to the service and store the response.
455         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
456         int statusCode = res.getStatus();
457
458         // Check the status code of the response: does it match
459         // the expected response(s)?
460         verbose("deleteNonExistent: status = " + res.getStatus());
461         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464     }
465
466
467     // ---------------------------------------------------------------
468     // Utility tests : tests of code used in tests above
469     // ---------------------------------------------------------------
470
471     /**
472      * Tests the code for manually submitting data that is used by several
473      * of the methods above.
474      */
475     @Test(dependsOnMethods = {"create", "read"})
476     public void testSubmitRequest() {
477
478         // Expected status code: 200 OK
479         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
480
481         // Submit the request to the service and store the response.
482         String method = ServiceRequestType.READ.httpMethodName();
483         String url = getResourceURL(knownResourceId);
484         int statusCode = submitRequest(method, url);
485         
486         // Check the status code of the response: does it match
487         // the expected response(s)?
488         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
489         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490
491     }           
492
493     // ---------------------------------------------------------------
494     // Utility methods used by tests above
495     // ---------------------------------------------------------------
496
497     @Override
498     public String getServicePathComponent() {
499         // @TODO Determine if it is possible to obtain this
500         // value programmatically.
501         //
502         // We set this in an annotation in the CollectionObjectProxy
503         // interface, for instance.  We also set service-specific
504         // constants in each service module, which might also
505         // return this value.
506         return SERVICE_PATH_COMPONENT;
507     }
508     
509     private CollectionObject createCollectionObjectInstance(String identifier) {
510         CollectionObject collectionObject =
511           createCollectionObjectInstance(
512               "objectNumber-" + identifier,
513               "objectName-" + identifier);
514         return collectionObject;
515     }
516
517     private CollectionObject createCollectionObjectInstance(
518       String objectNumber, String objectName) {
519         CollectionObject collectionObject = new CollectionObject();
520         collectionObject.setObjectNumber(objectNumber);
521         collectionObject.setObjectName(objectName);
522         return collectionObject;
523     }
524     
525     
526 }