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