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