]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f064f95dca0e48aa19bdbc331335ad975fae1144
[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 package org.collectionspace.services.client.test;
24
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.IntakeClient;
30 import org.collectionspace.services.intake.IntakesCommon;
31 import org.collectionspace.services.intake.IntakesCommonList;
32
33 import org.jboss.resteasy.client.ClientResponse;
34
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * IntakeServiceTest, carries out tests against a
46  * deployed and running Intake Service.
47  *
48  * $LastChangedRevision$
49  * $LastChangedDate$
50  */
51 public class IntakeServiceTest extends AbstractServiceTest {
52
53    private final Logger logger =
54        LoggerFactory.getLogger(IntakeServiceTest.class);
55
56     // Instance variables specific to this test.
57     private IntakeClient client = new IntakeClient();
58     final String SERVICE_PATH_COMPONENT = "intakes";
59     private String knownResourceId = null;
60
61     // ---------------------------------------------------------------
62     // CRUD tests : CREATE tests
63     // ---------------------------------------------------------------
64     // Success outcomes
65     @Override
66     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
67     public void create(String testName) throws Exception {
68
69         // Perform setup, such as initializing the type of service request
70         // (e.g. CREATE, DELETE), its valid and expected status codes, and
71         // its associated HTTP method name (e.g. POST, DELETE).
72         setupCreate(testName);
73
74         // Submit the request to the service and store the response.
75         String identifier = createIdentifier();
76
77         MultipartOutput multipart = createIntakeInstance(identifier);
78         ClientResponse<Response> res = client.create(multipart);
79
80         int statusCode = res.getStatus();
81
82         // Check the status code of the response: does it match
83         // the expected response(s)?
84         //
85         // Specifically:
86         // Does it fall within the set of valid status codes?
87         // Does it exactly match the expected status code?
88         if(logger.isDebugEnabled()){
89             logger.debug(testName + ": status = " + statusCode);
90         }
91         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
94
95         // Store the ID returned from this create operation
96         // for additional tests below.
97         knownResourceId = extractId(res);
98         if(logger.isDebugEnabled()){
99             logger.debug(testName + ": knownResourceId=" + knownResourceId);
100         }
101     }
102
103     @Override
104     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
105         dependsOnMethods = {"create"})
106     public void createList(String testName) throws Exception {
107         for(int i = 0; i < 3; i++){
108             create(testName);
109         }
110     }
111
112     // Failure outcomes
113     // Placeholders until the three tests below can be uncommented.
114     // See Issue CSPACE-401.
115     @Override
116     public void createWithEmptyEntityBody(String testName) throws Exception {
117     }
118
119     @Override
120     public void createWithMalformedXml(String testName) throws Exception {
121     }
122
123     @Override
124     public void createWithWrongXmlSchema(String testName) throws Exception {
125     }
126
127     /*
128     @Override
129     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
130         dependsOnMethods = {"create", "testSubmitRequest"})
131     public void createWithEmptyEntityBody(String testName) throws Exception {
132
133     // Perform setup.
134     setupCreateWithEmptyEntityBody(testName);
135
136     // Submit the request to the service and store the response.
137     String method = REQUEST_TYPE.httpMethodName();
138     String url = getServiceRootURL();
139     String mediaType = MediaType.APPLICATION_XML;
140     final String entity = "";
141     int statusCode = submitRequest(method, url, mediaType, entity);
142
143     // Check the status code of the response: does it match
144     // the expected response(s)?
145     if(logger.isDebugEnabled()){
146         logger.debug("createWithEmptyEntityBody url=" + url +
147             " status=" + statusCode);
148      }
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(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
156         dependsOnMethods = {"create", "testSubmitRequest"})
157     public void createWithMalformedXml(String testName) throws Exception {
158
159     // Perform setup.
160     setupCreateWithMalformedXml(testName);
161
162     // Submit the request to the service and store the response.
163     String method = REQUEST_TYPE.httpMethodName();
164     String url = getServiceRootURL();
165     String mediaType = MediaType.APPLICATION_XML;
166     final String entity = MALFORMED_XML_DATA; // Constant from base class.
167     int statusCode = submitRequest(method, url, mediaType, entity);
168
169     // Check the status code of the response: does it match
170     // the expected response(s)?
171     if(logger.isDebugEnabled()){
172         logger.debug(testName + ": url=" + url +
173             " status=" + statusCode);
174      }
175     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178     }
179
180     @Override
181     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
182         dependsOnMethods = {"create", "testSubmitRequest"})
183     public void createWithWrongXmlSchema(String testName) throws Exception {
184
185     // Perform setup.
186     setupCreateWithWrongXmlSchema(testName);
187
188     // Submit the request to the service and store the response.
189     String method = REQUEST_TYPE.httpMethodName();
190     String url = getServiceRootURL();
191     String mediaType = MediaType.APPLICATION_XML;
192     final String entity = WRONG_XML_SCHEMA_DATA;
193     int statusCode = submitRequest(method, url, mediaType, entity);
194
195     // Check the status code of the response: does it match
196     // the expected response(s)?
197     if(logger.isDebugEnabled()){
198         logger.debug(testName + ": url=" + url +
199             " status=" + statusCode);
200      }
201     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
204     }
205      */
206
207     // ---------------------------------------------------------------
208     // CRUD tests : READ tests
209     // ---------------------------------------------------------------
210     // Success outcomes
211     @Override
212     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
213         dependsOnMethods = {"create"})
214     public void read(String testName) throws Exception {
215
216         // Perform setup.
217         setupRead(testName);
218
219         // Submit the request to the service and store the response.
220         ClientResponse<MultipartInput> res = client.read(knownResourceId);
221         int statusCode = res.getStatus();
222
223         // Check the status code of the response: does it match
224         // the expected response(s)?
225         if(logger.isDebugEnabled()){
226             logger.debug(testName + ": status = " + statusCode);
227         }
228         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
231
232         MultipartInput input = (MultipartInput) res.getEntity();
233         IntakesCommon intake = (IntakesCommon) extractPart(input,
234                 client.getCommonPartName(), IntakesCommon.class);
235         Assert.assertNotNull(intake);
236     }
237
238     // Failure outcomes
239     @Override
240     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
241         dependsOnMethods = {"read"})
242     public void readNonExistent(String testName) throws Exception {
243
244         // Perform setup.
245         setupReadNonExistent(testName);
246
247         // Submit the request to the service and store the response.
248         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
249         int statusCode = res.getStatus();
250
251         // Check the status code of the response: does it match
252         // the expected response(s)?
253         if(logger.isDebugEnabled()){
254             logger.debug(testName + ": status = " + statusCode);
255         }
256         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259     }
260
261     // ---------------------------------------------------------------
262     // CRUD tests : READ_LIST tests
263     // ---------------------------------------------------------------
264     // Success outcomes
265     @Override
266     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
267         dependsOnMethods = {"read"})
268     public void readList(String testName) throws Exception {
269
270         // Perform setup.
271         setupReadList(testName);
272
273         // Submit the request to the service and store the response.
274         ClientResponse<IntakesCommonList> res = client.readList();
275         IntakesCommonList list = res.getEntity();
276         int statusCode = res.getStatus();
277
278         // Check the status code of the response: does it match
279         // the expected response(s)?
280         if(logger.isDebugEnabled()){
281             logger.debug(testName + ": status = " + statusCode);
282         }
283         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
286
287         // Optionally output additional data about list members for debugging.
288         boolean iterateThroughList = false;
289         if(iterateThroughList && logger.isDebugEnabled()){
290             List<IntakesCommonList.IntakeListItem> items =
291                     list.getIntakeListItem();
292             int i = 0;
293             for(IntakesCommonList.IntakeListItem item : items){
294                 logger.debug(testName + ": list-item[" + i + "] csid=" +
295                         item.getCsid());
296                 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
297                         item.getEntryNumber());
298                 logger.debug(testName + ": list-item[" + i + "] URI=" +
299                         item.getUri());
300                 i++;
301             }
302         }
303
304     }
305
306     // Failure outcomes
307     // None at present.
308     // ---------------------------------------------------------------
309     // CRUD tests : UPDATE tests
310     // ---------------------------------------------------------------
311     // Success outcomes
312     @Override
313     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
314         dependsOnMethods = {"read"})
315     public void update(String testName) throws Exception {
316
317         // Perform setup.
318         setupUpdate(testName);
319
320         ClientResponse<MultipartInput> res =
321                 client.read(knownResourceId);
322         if(logger.isDebugEnabled()){
323             logger.debug(testName + ": read status = " + res.getStatus());
324         }
325         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
326
327         if(logger.isDebugEnabled()){
328             logger.debug("got object to update with ID: " + knownResourceId);
329         }
330         MultipartInput input = (MultipartInput) res.getEntity();
331         IntakesCommon intake = (IntakesCommon) extractPart(input,
332                 client.getCommonPartName(), IntakesCommon.class);
333         Assert.assertNotNull(intake);
334
335         // Update the content of this resource.
336         // Update the content of this resource.
337         intake.setEntryNumber("updated-" + intake.getEntryNumber());
338         intake.setEntryDate("updated-" + intake.getEntryDate());
339         if(logger.isDebugEnabled()){
340             logger.debug("to be updated object");
341             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
342         }
343         // Submit the request to the service and store the response.
344         MultipartOutput output = new MultipartOutput();
345         OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
346         commonPart.getHeaders().add("label", client.getCommonPartName());
347
348         res = client.update(knownResourceId, output);
349         int statusCode = res.getStatus();
350         // Check the status code of the response: does it match the expected response(s)?
351         if(logger.isDebugEnabled()){
352             logger.debug(testName + ": status = " + statusCode);
353         }
354         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
355                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
356         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
357
358
359         input = (MultipartInput) res.getEntity();
360         IntakesCommon updatedIntake =
361                 (IntakesCommon) extractPart(input,
362                         client.getCommonPartName(), IntakesCommon.class);
363         Assert.assertNotNull(updatedIntake);
364
365         Assert.assertEquals(updatedIntake.getEntryDate(),
366                 intake.getEntryDate(),
367                 "Data in updated object did not match submitted data.");
368
369     }
370
371     // Failure outcomes
372     // Placeholders until the three tests below can be uncommented.
373     // See Issue CSPACE-401.
374     @Override
375     public void updateWithEmptyEntityBody(String testName) throws Exception{
376     }
377     @Override
378     public void updateWithMalformedXml(String testName) throws Exception {
379     }
380     @Override
381     public void updateWithWrongXmlSchema(String testName) throws Exception {
382     }
383
384     /*
385     @Override
386     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
387         dependsOnMethods = {"create", "update", "testSubmitRequest"})
388     public void updateWithEmptyEntityBody(String testName) throws Exception {
389
390     // Perform setup.
391     setupUpdateWithEmptyEntityBody(testName);
392
393     // Submit the request to the service and store the response.
394     String method = REQUEST_TYPE.httpMethodName();
395     String url = getResourceURL(knownResourceId);
396     String mediaType = MediaType.APPLICATION_XML;
397     final String entity = "";
398     int statusCode = submitRequest(method, url, mediaType, entity);
399
400     // Check the status code of the response: does it match
401     // the expected response(s)?
402     if(logger.isDebugEnabled()){
403         logger.debug(testName + ": url=" + url +
404             " status=" + statusCode);
405      }
406     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
409     }
410
411     @Override
412     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
413         dependsOnMethods = {"create", "update", "testSubmitRequest"})
414     public void updateWithMalformedXml(String testName) throws Exception {
415
416     // Perform setup.
417     setupUpdateWithMalformedXml(testName);
418
419     // Submit the request to the service and store the response.
420     String method = REQUEST_TYPE.httpMethodName();
421     String url = getResourceURL(knownResourceId);
422     String mediaType = MediaType.APPLICATION_XML;
423     final String entity = MALFORMED_XML_DATA;
424     int statusCode = submitRequest(method, url, mediaType, entity);
425
426     // Check the status code of the response: does it match
427     // the expected response(s)?
428     if(logger.isDebugEnabled()){
429         logger.debug(testName + ": url=" + url +
430          " status=" + statusCode);
431      }
432     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435     }
436
437     @Override
438     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
439         dependsOnMethods = {"create", "update", "testSubmitRequest"})
440     public void updateWithWrongXmlSchema(String testName) throws Exception {
441
442     // Perform setup.
443     setupUpdateWithWrongXmlSchema(testName);
444
445     // Submit the request to the service and store the response.
446     String method = REQUEST_TYPE.httpMethodName();
447     String url = getResourceURL(knownResourceId);
448     String mediaType = MediaType.APPLICATION_XML;
449     final String entity = WRONG_XML_SCHEMA_DATA;
450     int statusCode = submitRequest(method, url, mediaType, entity);
451
452     // Check the status code of the response: does it match
453     // the expected response(s)?
454     if(logger.isDebugEnabled()){
455         logger.debug(testName + ": url=" + url +
456         " status=" + statusCode);
457      }
458     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
459     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
460     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
461     }
462      */
463
464     @Override
465     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
466         dependsOnMethods = {"update", "testSubmitRequest"})
467     public void updateNonExistent(String testName) throws Exception {
468
469         // Perform setup.
470         setupUpdateNonExistent(testName);
471
472         // Submit the request to the service and store the response.
473         // Note: The ID used in this 'create' call may be arbitrary.
474         // The only relevant ID may be the one used in update(), below.
475
476         // The only relevant ID may be the one used in update(), below.
477         MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
478         ClientResponse<MultipartInput> res =
479                 client.update(NON_EXISTENT_ID, multipart);
480         int statusCode = res.getStatus();
481
482         // Check the status code of the response: does it match
483         // the expected response(s)?
484         if(logger.isDebugEnabled()){
485             logger.debug(testName + ": status = " + statusCode);
486         }
487         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490     }
491
492     // ---------------------------------------------------------------
493     // CRUD tests : DELETE tests
494     // ---------------------------------------------------------------
495     // Success outcomes
496     @Override
497     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
498         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
499     public void delete(String testName) throws Exception {
500
501         // Perform setup.
502         setupDelete(testName);
503
504         // Submit the request to the service and store the response.
505         ClientResponse<Response> res = client.delete(knownResourceId);
506         int statusCode = res.getStatus();
507
508         // Check the status code of the response: does it match
509         // the expected response(s)?
510         if(logger.isDebugEnabled()){
511             logger.debug(testName + ": status = " + statusCode);
512         }
513         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
516     }
517
518     // Failure outcomes
519     @Override
520     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
521         dependsOnMethods = {"delete"})
522     public void deleteNonExistent(String testName) throws Exception {
523
524         // Perform setup.
525         setupDeleteNonExistent(testName);
526
527         // Submit the request to the service and store the response.
528         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
529         int statusCode = res.getStatus();
530
531         // Check the status code of the response: does it match
532         // the expected response(s)?
533         if(logger.isDebugEnabled()){
534             logger.debug(testName + ": status = " + statusCode);
535         }
536         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
539     }
540
541     // ---------------------------------------------------------------
542     // Utility tests : tests of code used in tests above
543     // ---------------------------------------------------------------
544     /**
545      * Tests the code for manually submitting data that is used by several
546      * of the methods above.
547      */
548     @Test(dependsOnMethods = {"create", "read"})
549     public void testSubmitRequest() {
550
551         // Expected status code: 200 OK
552         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
553
554         // Submit the request to the service and store the response.
555         String method = ServiceRequestType.READ.httpMethodName();
556         String url = getResourceURL(knownResourceId);
557         int statusCode = submitRequest(method, url);
558
559         // Check the status code of the response: does it match
560         // the expected response(s)?
561         if(logger.isDebugEnabled()){
562             logger.debug("testSubmitRequest: url=" + url +
563                 " status=" + statusCode);
564         }
565         Assert.assertEquals(statusCode, EXPECTED_STATUS);
566
567     }
568
569     // ---------------------------------------------------------------
570     // Utility methods used by tests above
571     // ---------------------------------------------------------------
572     @Override
573     public String getServicePathComponent() {
574         return SERVICE_PATH_COMPONENT;
575     }
576
577     private MultipartOutput createIntakeInstance(String identifier) {
578         return createIntakeInstance(
579                 "entryNumber-" + identifier,
580                 "entryDate-" + identifier);
581     }
582
583     private MultipartOutput createIntakeInstance(String entryNumber, String entryDate) {
584         IntakesCommon intake = new IntakesCommon();
585         intake.setEntryNumber(entryNumber);
586         intake.setEntryDate(entryDate);
587         MultipartOutput multipart = new MultipartOutput();
588         OutputPart commonPart =
589             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
590         commonPart.getHeaders().add("label", client.getCommonPartName());
591
592         if(logger.isDebugEnabled()){
593             logger.debug("to be created, intake common");
594             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
595         }
596
597         return multipart;
598     }
599 }