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