]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
28c937a9ada2410a117fd99d18a007666663e6c2
[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.IntakeClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.intake.Intake;
34 import org.collectionspace.services.intake.IntakeList;
35
36 import org.jboss.resteasy.client.ClientResponse;
37
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 /**
42  * IntakeServiceTest, carries out tests against a
43  * deployed and running Intake Service.
44  * 
45  * $LastChangedRevision$
46  * $LastChangedDate$
47  */
48 public class IntakeServiceTest extends AbstractServiceTest {
49
50     // Instance variables specific to this test.
51     private IntakeClient client = new IntakeClient();
52     final String SERVICE_PATH_COMPONENT = "intakes";
53     private String knownResourceId = null;
54
55
56     // ---------------------------------------------------------------
57     // CRUD tests : CREATE tests
58     // ---------------------------------------------------------------
59
60     // Success outcomes
61     
62     @Override
63     @Test
64     public void create() {
65
66         // Perform setup, such as initializing the type of service request
67         // (e.g. CREATE, DELETE), its valid and expected status codes, and
68         // its associated HTTP method name (e.g. POST, DELETE).
69         setupCreate();
70
71         // Submit the request to the service and store the response.
72         String identifier = createIdentifier();
73         Intake intake = createIntakeInstance(identifier);
74         ClientResponse<Response> res = client.create(intake);
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 for
89         // 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<Intake> 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<Intake> 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<IntakeList> res = client.readList();
239         IntakeList 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<IntakeList.IntakeListItem> items =
253                 list.getIntakeListItem();
254             int i = 0;
255             for(IntakeList.IntakeListItem item : items){
256                 verbose("readList: list-item[" + i + "] csid=" +
257                     item.getCsid());
258                 verbose("readList: list-item[" + i + "] objectNumber=" +
259                     item.getEntryNumber());
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<Intake> res = client.read(knownResourceId);
288         verbose("read: status = " + res.getStatus());
289         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290         Intake intake = res.getEntity();
291         verbose("Got object to update with ID: " + knownResourceId,
292                 intake, Intake.class);
293
294         // Update the content of this resource.
295         intake.setEntryNumber("updated-" + intake.getEntryNumber());
296         intake.setEntryDate("updated-" + intake.getEntryDate());
297     
298         // Submit the request to the service and store the response.
299         res = client.update(knownResourceId, intake);
300         int statusCode = res.getStatus();
301         Intake updatedObject = res.getEntity();
302
303         // Check the status code of the response: does it match
304         // the expected response(s)?
305         verbose("update: status = " + res.getStatus());
306         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
307             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
308         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
309         
310         // Check the contents of the response: does it match
311         // what was submitted?
312         verbose("update: ", updatedObject, Intake.class);
313         Assert.assertEquals(updatedObject.getEntryDate(), 
314             intake.getEntryDate(), 
315             "Data in updated object did not match submitted data.");
316     }
317
318     // Failure outcomes
319
320     // Placeholders until the three tests below can be uncommented.
321     // See Issue CSPACE-401.
322     public void updateWithEmptyEntityBody() {}
323     public void updateWithMalformedXml() {}
324     public void updateWithWrongXmlSchema() {}
325
326 /*
327     @Override
328     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
329     public void updateWithEmptyEntityBody() {
330     
331         // Perform setup.
332         setupUpdateWithEmptyEntityBody();
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 = "";
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("updateWithEmptyEntityBody 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     @Override
350     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
351     public void updateWithMalformedXml() {
352
353         // Perform setup.
354         setupUpdateWithMalformedXml();
355
356         // Submit the request to the service and store the response.
357         String method = REQUEST_TYPE.httpMethodName();
358         String url = getResourceURL(knownResourceId);
359         String mediaType = MediaType.APPLICATION_XML;
360         final String entity = MALFORMED_XML_DATA;
361         int statusCode = submitRequest(method, url, mediaType, entity);
362         
363         // Check the status code of the response: does it match
364         // the expected response(s)?
365         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
366         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369     }
370
371     @Override
372     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
373     public void updateWithWrongXmlSchema() {
374     
375         // Perform setup.
376         setupUpdateWithWrongXmlSchema();
377         
378         // Submit the request to the service and store the response.
379         String method = REQUEST_TYPE.httpMethodName();
380         String url = getResourceURL(knownResourceId);
381         String mediaType = MediaType.APPLICATION_XML;
382         final String entity = WRONG_XML_SCHEMA_DATA;
383         int statusCode = submitRequest(method, url, mediaType, entity);
384         
385         // Check the status code of the response: does it match
386         // the expected response(s)?
387         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
388         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
391     }
392 */
393
394     @Override
395     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
396     public void updateNonExistent() {
397
398         // Perform setup.
399         setupUpdateNonExistent();
400
401         // Submit the request to the service and store the response.
402         // Note: The ID used in this 'create' call may be arbitrary.
403         // The only relevant ID may be the one used in update(), below.
404         Intake intake = createIntakeInstance(NON_EXISTENT_ID);
405         ClientResponse<Intake> res =
406           client.update(NON_EXISTENT_ID, intake);
407         int statusCode = res.getStatus();
408
409         // Check the status code of the response: does it match
410         // the expected response(s)?
411         verbose("updateNonExistent: status = " + res.getStatus());
412         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
413             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
414         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
415     }
416
417     // ---------------------------------------------------------------
418     // CRUD tests : DELETE tests
419     // ---------------------------------------------------------------
420
421     // Success outcomes
422
423     @Override
424     @Test(dependsOnMethods = 
425         {"create", "read", "update"})
426     public void delete() {
427
428         // Perform setup.
429         setupDelete();
430
431         // Submit the request to the service and store the response.
432         ClientResponse<Response> res = client.delete(knownResourceId);
433         int statusCode = res.getStatus();
434
435         // Check the status code of the response: does it match
436         // the expected response(s)?
437         verbose("delete: status = " + res.getStatus());
438         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
439             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
440         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441     }
442
443     // Failure outcomes
444
445     @Override
446     @Test(dependsOnMethods = {"delete"})
447     public void deleteNonExistent() {
448
449         // Perform setup.
450         setupDeleteNonExistent();
451
452         // Submit the request to the service and store the response.
453         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
454         int statusCode = res.getStatus();
455
456         // Check the status code of the response: does it match
457         // the expected response(s)?
458         verbose("deleteNonExistent: status = " + res.getStatus());
459         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
462     }
463
464
465     // ---------------------------------------------------------------
466     // Utility tests : tests of code used in tests above
467     // ---------------------------------------------------------------
468
469     /**
470      * Tests the code for manually submitting data that is used by several
471      * of the methods above.
472      */
473     @Test(dependsOnMethods = {"create", "read"})
474     public void testSubmitRequest() {
475
476         // Expected status code: 200 OK
477         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
478
479         // Submit the request to the service and store the response.
480         String method = ServiceRequestType.READ.httpMethodName();
481         String url = getResourceURL(knownResourceId);
482         int statusCode = submitRequest(method, url);
483         
484         // Check the status code of the response: does it match
485         // the expected response(s)?
486         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
487         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
488
489     }           
490
491     // ---------------------------------------------------------------
492     // Utility methods used by tests above
493     // ---------------------------------------------------------------
494
495     @Override
496     public String getServicePathComponent() {
497         return SERVICE_PATH_COMPONENT;
498     }
499     
500     private Intake createIntakeInstance(String identifier) {
501         Intake intake =
502             createIntakeInstance(
503                 "entryNumber-" + identifier,
504                 "entryDate-" + identifier);
505         return intake;
506     }
507     
508     private Intake createIntakeInstance(String entryNumber, String entryDate) {
509         Intake intake = new Intake();
510         intake.setEntryNumber(entryNumber);
511         intake.setEntryDate(entryDate);
512         return intake;
513     }
514
515 }