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