]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dae79c9d1a7c0279c1c010d84c51acecd4ab1636
[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 two tests below can be uncommented.
104     // See Issue CSPACE-401.
105     public void createWithMalformedXml() {}
106     public void createWithWrongXmlSchema() {}
107
108 /*
109     @Override
110     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
111     public void createWithMalformedXml() {
112     
113         // Perform setup.
114         setupCreateWithMalformedXml();
115
116         // Submit the request to the service and store the response.
117         String method = REQUEST_TYPE.httpMethodName();
118         String url = getServiceRootURL();
119         String mediaType = MediaType.APPLICATION_XML;
120         final String entity = MALFORMED_XML_DATA; // Constant from base class.
121         int statusCode = submitRequest(method, url, mediaType, entity);
122         
123         // Check the status code of the response: does it match
124         // the expected response(s)?
125         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
126         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
129     }
130
131     @Override
132     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
133     public void createWithWrongXmlSchema() {
134     
135         // Perform setup.
136         setupCreateWithWrongXmlSchema();
137      
138         // Submit the request to the service and store the response.
139         String method = REQUEST_TYPE.httpMethodName();
140         String url = getServiceRootURL();
141         String mediaType = MediaType.APPLICATION_XML;
142         final String entity = WRONG_XML_SCHEMA_DATA;
143         int statusCode = submitRequest(method, url, mediaType, entity);
144         
145         // Check the status code of the response: does it match
146         // 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     // ---------------------------------------------------------------
155     // CRUD tests : READ tests
156     // ---------------------------------------------------------------
157
158     // Success outcomes
159
160     @Override
161     @Test(dependsOnMethods = {"create"})
162     public void read() {
163     
164         // Perform setup.
165         setupRead();
166
167         // Submit the request to the service and store the response.
168         ClientResponse<CollectionObject> res = client.read(knownResourceId);
169         int statusCode = res.getStatus();
170             
171         // Check the status code of the response: does it match
172         // the expected response(s)?
173         verbose("read: status = " + statusCode);
174         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177     }
178     
179     // Failure outcomes
180
181     @Override
182     @Test(dependsOnMethods = {"read"})
183     public void readNonExistent() {
184
185         // Perform setup.
186         setupReadNonExistent();
187         
188         // Submit the request to the service and store the response.
189         ClientResponse<CollectionObject> res = client.read(NON_EXISTENT_ID);
190         int statusCode = res.getStatus();
191
192         // Check the status code of the response: does it match
193         // 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 tests
203     // ---------------------------------------------------------------
204
205     // Success outcomes
206
207     @Override
208     @Test(dependsOnMethods = {"createList"})
209     public void readList() {
210     
211         // Perform setup.
212         setupReadList();
213
214         // Submit the request to the service and store the response.
215         ClientResponse<CollectionObjectList> res = client.readList();
216         CollectionObjectList list = res.getEntity();
217         int statusCode = res.getStatus();
218
219         // Check the status code of the response: does it match
220         // the expected response(s)?
221         verbose("readList: status = " + res.getStatus());
222         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
225
226         // Optionally output additional data about list members for debugging.
227         boolean iterateThroughList = false;
228         if (iterateThroughList && logger.isDebugEnabled()) {
229             List<CollectionObjectList.CollectionObjectListItem> items =
230                 list.getCollectionObjectListItem();
231             int i = 0;
232             for(CollectionObjectList.CollectionObjectListItem item : items){
233                 verbose("readList: list-item[" + i + "] csid=" +
234                     item.getCsid());
235                 verbose("readList: list-item[" + i + "] objectNumber=" +
236                     item.getObjectNumber());
237                 verbose("readList: list-item[" + i + "] URI=" +
238                     item.getUri());
239                 i++;
240             }
241         }
242         
243     }
244
245     // Failure outcomes
246     
247     // None at present.
248
249
250     // ---------------------------------------------------------------
251     // CRUD tests : UPDATE tests
252     // ---------------------------------------------------------------
253
254     // Success outcomes
255
256     @Override
257     @Test(dependsOnMethods = {"create"})
258     public void update() {
259     
260         // Perform setup.
261         setupUpdate();
262
263         // Retrieve an existing resource that we can update.
264         ClientResponse<CollectionObject> res = client.read(knownResourceId);
265         verbose("read: status = " + res.getStatus());
266         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
267         CollectionObject collectionObject = res.getEntity();
268         verbose("Got object to update with ID: " + knownResourceId,
269                 collectionObject, CollectionObject.class);
270
271         // Update the content of this resource.
272         collectionObject.setObjectNumber("updated-" +
273           collectionObject.getObjectNumber());
274         collectionObject.setObjectName("updated-" +
275           collectionObject.getObjectName());
276
277         // Submit the request to the service and store the response.
278         res = client.update(knownResourceId, collectionObject);
279         int statusCode = res.getStatus();
280
281         // Check the status code of the response: does it match
282         // the expected response(s)?
283         verbose("update: status = " + res.getStatus());
284         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
285             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
286         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
287         
288         // Check the contents of the response: does it match
289         // what was submitted?
290         CollectionObject updatedObject = res.getEntity();
291         verbose("update: ", updatedObject, CollectionObject.class);
292         Assert.assertEquals(updatedObject.getObjectName(), 
293             collectionObject.getObjectName(), 
294             "Data in updated object did not match submitted data.");
295     }
296     
297     // Failure outcomes
298
299     // Placeholders until the two tests below can be uncommented.
300     // See Issue CSPACE-401.
301     public void updateWithMalformedXml() {}
302     public void updateWithWrongXmlSchema() {}
303
304 /*
305     @Override
306     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
307     public void updateWithMalformedXml() {
308
309         // Perform setup.
310         setupUpdateWithMalformedXml();
311
312         // Submit the request to the service and store the response.
313         String method = REQUEST_TYPE.httpMethodName();
314         String url = getResourceURL(knownResourceId);
315         final String entity = MALFORMED_XML_DATA;
316         String mediaType = MediaType.APPLICATION_XML;
317         int statusCode = submitRequest(method, url, mediaType, entity);
318         
319         // Check the status code of the response: does it match
320         // the expected response(s)?
321         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
322         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
325     }
326
327     @Override
328     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
329     public void updateWithWrongXmlSchema() {
330     
331         // Perform setup.
332         setupUpdateWithWrongXmlSchema();
333         
334         // Submit the request to the service and store the response.
335         String method = REQUEST_TYPE.httpMethodName();
336         String url = getResourceURL(knownResourceId);
337         String mediaType = MediaType.APPLICATION_XML;
338         final String entity = WRONG_XML_SCHEMA_DATA;
339         int statusCode = submitRequest(method, url, mediaType, entity);
340         
341         // Check the status code of the response: does it match
342         // the expected response(s)?
343         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
344         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
347     }
348 */
349
350     @Override
351     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
352     public void updateNonExistent() {
353
354         // Perform setup.
355         setupUpdateNonExistent();
356
357         // Submit the request to the service and store the response.
358         // Note: The ID used in this 'create' call may be arbitrary.
359         // The only relevant ID may be the one used in update(), below.
360         CollectionObject collectionObject = createCollectionObjectInstance(NON_EXISTENT_ID);
361         ClientResponse<CollectionObject> res =
362           client.update(NON_EXISTENT_ID, collectionObject);
363         int statusCode = res.getStatus();
364
365         // Check the status code of the response: does it match
366         // the expected response(s)?
367         verbose("updateNonExistent: status = " + res.getStatus());
368         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371     }
372
373     // ---------------------------------------------------------------
374     // CRUD tests : DELETE tests
375     // ---------------------------------------------------------------
376
377     // Success outcomes
378
379     @Override
380     @Test(dependsOnMethods = 
381         {"create", "read", "update"})
382     public void delete() {
383
384         // Perform setup.
385         setupDelete();
386
387         // Submit the request to the service and store the response.
388         ClientResponse<Response> res = client.delete(knownResourceId);
389         int statusCode = res.getStatus();
390
391         // Check the status code of the response: does it match
392         // the expected response(s)?
393         verbose("delete: status = " + res.getStatus());
394         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
397     }
398
399     // Failure outcomes
400
401     @Override
402     @Test(dependsOnMethods = {"delete"})
403     public void deleteNonExistent() {
404
405         // Perform setup.
406         setupDeleteNonExistent();
407
408         // Submit the request to the service and store the response.
409         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
410         int statusCode = res.getStatus();
411
412         // Check the status code of the response: does it match
413         // the expected response(s)?
414         verbose("deleteNonExistent: status = " + res.getStatus());
415         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
418     }
419
420
421     // ---------------------------------------------------------------
422     // Utility tests : tests of code used in tests above
423     // ---------------------------------------------------------------
424
425     /**
426      * Tests the code for manually submitting data that is used by several
427      * of the methods above.
428      */
429     @Test(dependsOnMethods = {"create", "read"})
430     public void testSubmitRequest() {
431
432         // Expected status code: 200 OK
433         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
434
435         // Submit the request to the service and store the response.
436         String method = ServiceRequestType.READ.httpMethodName();
437         String url = getResourceURL(knownResourceId);
438         int statusCode = submitRequest(method, url);
439         
440         // Check the status code of the response: does it match
441         // the expected response(s)?
442         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
443         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
444
445     }           
446
447     // ---------------------------------------------------------------
448     // Utility methods used by tests above
449     // ---------------------------------------------------------------
450
451     @Override
452     public String getServicePathComponent() {
453         // @TODO Determine if it is possible to obtain this
454         // value programmatically.
455         //
456         // We set this in an annotation in the CollectionObjectProxy
457         // interface, for instance.  We also set service-specific
458         // constants in each service module, which might also
459         // return this value.
460         return SERVICE_PATH_COMPONENT;
461     }
462     
463     private CollectionObject createCollectionObjectInstance(String identifier) {
464         CollectionObject collectionObject =
465           createCollectionObjectInstance(
466               "objectNumber-" + identifier,
467               "objectName-" + identifier);
468         return collectionObject;
469     }
470
471     private CollectionObject createCollectionObjectInstance(
472       String objectNumber, String objectName) {
473         CollectionObject collectionObject = new CollectionObject();
474         collectionObject.setObjectNumber(objectNumber);
475         collectionObject.setObjectName(objectName);
476         return collectionObject;
477     }
478     
479     
480 }