]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
24e28c8d921ff8f7a9e8fe5d34f39fae9377c049
[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.IntakeClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.intake.Intake;
33 import org.collectionspace.services.intake.IntakeList;
34
35 import org.jboss.resteasy.client.ClientResponse;
36
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39
40 /**
41  * IntakeServiceTest, carries out tests against a
42  * deployed and running Intake Service.
43  * 
44  * $LastChangedRevision$
45  * $LastChangedDate$
46  */
47 public class IntakeServiceTest extends AbstractServiceTest {
48
49     // Instance variables specific to this test.
50     private IntakeClient client = new IntakeClient();
51     final String SERVICE_PATH_COMPONENT = "intakes";
52     private String knownResourceId = null;
53
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         Intake intake = createIntakeInstance(identifier);
73         ClientResponse<Response> res = client.create(intake);
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 for
88         // 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<Intake> 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<Intake> 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<IntakeList> res = client.readList();
220         IntakeList 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<IntakeList.IntakeListItem> items =
234                 list.getIntakeListItem();
235             int i = 0;
236             for(IntakeList.IntakeListItem item : items){
237                 verbose("readList: list-item[" + i + "] csid=" +
238                     item.getCsid());
239                 verbose("readList: list-item[" + i + "] objectNumber=" +
240                     item.getEntryNumber());
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<Intake> res = client.read(knownResourceId);
269         verbose("read: status = " + res.getStatus());
270         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
271         Intake intake = res.getEntity();
272         verbose("Got object to update with ID: " + knownResourceId,
273                 intake, Intake.class);
274
275         // Update the content of this resource.
276         intake.setEntryNumber("updated-" + intake.getEntryNumber());
277         intake.setEntryDate("updated-" + intake.getEntryDate());
278     
279         // Submit the request to the service and store the response.
280         res = client.update(knownResourceId, intake);
281         int statusCode = res.getStatus();
282         Intake updatedObject = res.getEntity();
283
284         // Check the status code of the response: does it match
285         // the expected response(s)?
286         verbose("update: status = " + res.getStatus());
287         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
290         
291         // Check the contents of the response: does it match
292         // what was submitted?
293         verbose("update: ", updatedObject, Intake.class);
294         Assert.assertEquals(updatedObject.getEntryDate(), 
295             intake.getEntryDate(), 
296             "Data in updated object did not match submitted data.");
297     }
298
299     // Failure outcomes
300
301     // Placeholders until the two tests below can be uncommented.
302     // See Issue CSPACE-401.
303     public void updateWithMalformedXml() {}
304     public void updateWithWrongXmlSchema() {}
305
306 /*
307     @Override
308     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
309     public void updateWithMalformedXml() {
310
311         // Perform setup.
312         setupUpdateWithMalformedXml();
313
314         // Submit the request to the service and store the response.
315         String method = REQUEST_TYPE.httpMethodName();
316         String url = getResourceURL(knownResourceId);
317         final String entity = MALFORMED_XML_DATA;
318         int statusCode = submitRequest(method, url, entity);
319         
320         // Check the status code of the response: does it match
321         // the expected response(s)?
322         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
323         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
324             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
325         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
326     }
327
328     @Override
329     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
330     public void updateWithWrongXmlSchema() {
331     
332         // Perform setup.
333         setupUpdateWithWrongXmlSchema();
334         
335         // Submit the request to the service and store the response.
336         String method = REQUEST_TYPE.httpMethodName();
337         String url = getResourceURL(knownResourceId);
338         final String entity = WRONG_XML_SCHEMA_DATA;
339         int statusCode = submitRequest(method, url, 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         Intake intake = createIntakeInstance(NON_EXISTENT_ID);
361         ClientResponse<Intake> res =
362           client.update(NON_EXISTENT_ID, intake);
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         return SERVICE_PATH_COMPONENT;
454     }
455     
456     private Intake createIntakeInstance(String identifier) {
457         Intake intake =
458             createIntakeInstance(
459                 "entryNumber-" + identifier,
460                 "entryDate-" + identifier);
461         return intake;
462     }
463     
464     private Intake createIntakeInstance(String entryNumber, String entryDate) {
465         Intake intake = new Intake();
466         intake.setEntryNumber(entryNumber);
467         intake.setEntryDate(entryDate);
468         return intake;
469     }
470
471 }