]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
03cf451777b8d21d3101e1e7ec56ed559bc61217
[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.math.BigDecimal;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.acquisition.AcquisitionSourceList;
33 import org.collectionspace.services.acquisition.AcquisitionsCommon;
34 import org.collectionspace.services.acquisition.StructuredDateGroup;
35 import org.collectionspace.services.client.AbstractCommonListUtils;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.PayloadOutputPart;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.ReportClient;
40 import org.collectionspace.services.common.invocable.InvocationContext;
41 import org.collectionspace.services.report.ReportsCommon;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.client.AcquisitionClient;
44
45 import org.jboss.resteasy.client.ClientResponse;
46 import org.testng.Assert;
47 //import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
55  * ReportServiceTest, carries out tests against a
56  * deployed and running Report Service.
57  *
58  * $LastChangedRevision: 2261 $
59  * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
60  */
61 public class ReportServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ReportsCommon> {
62
63     /** The logger. */
64     private final String CLASS_NAME = ReportServiceTest.class.getName();
65     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66     final String SERVICE_NAME = "reports";
67     final String SERVICE_PATH_COMPONENT = "reports";
68     // Instance variables specific to this test.    
69     private String testDocType = "Acquisition";
70
71     private String createAquisitionResource() {
72         String result = null;
73         
74         AcquisitionClient acquisitionClient = new AcquisitionClient();
75         AcquisitionsCommon acquisitionsCommon = new AcquisitionsCommon();
76         acquisitionsCommon.setAcquisitionReason("It was nice.");
77         acquisitionsCommon.setOriginalObjectPurchasePriceValue(new BigDecimal(500));
78         acquisitionsCommon.setAcquisitionReferenceNumber("2013.003.0004");
79         
80         AcquisitionSourceList asl = new AcquisitionSourceList();
81         List<String> sourceList = asl.getAcquisitionSource();
82         sourceList.add("The Jim Henson Legacy");
83         acquisitionsCommon.setAcquisitionSources(asl);
84         
85         StructuredDateGroup sdg = new StructuredDateGroup();
86         sdg.setDateDisplayDate("12/12/2012");
87         acquisitionsCommon.setAccessionDateGroup(sdg);
88         
89         PoxPayloadOut poxPayloadOut = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
90         PayloadOutputPart commonPart = poxPayloadOut.addPart(acquisitionClient.getCommonPartName(), acquisitionsCommon);
91         Response res = acquisitionClient.create(poxPayloadOut);
92         try {
93             setupCreate();
94                 int statusCode = res.getStatus();
95                 if (logger.isDebugEnabled()) {
96                     logger.debug(this.getClass().getCanonicalName() + ": HTTP status = " + statusCode);
97                 }
98                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
99                         invalidStatusCodeMessage(testRequestType, statusCode));
100                 Assert.assertEquals(statusCode, testExpectedStatusCode);
101         
102                 result = extractId(res);
103                 // Store the IDs from every resource created by tests,
104                 // so they can be deleted after tests have been run.
105                 allResourceIdsCreated.add(result);
106         } finally {
107                 res.close();
108         }
109         
110         return result;
111     }
112     
113     @Test(dataProvider = "testName", dependsOnMethods = {"create"})    
114     public void publishReportInstance(String testName) throws Exception {
115         // Perform setup.
116
117         // Submit the request to the service and store the response.
118         ReportClient client = (ReportClient)this.getClientInstance();
119         String reportCsid = createResource(testName, this.getKnowResourceIdentifier());
120         String acquisitionCsid = createAquisitionResource();
121         
122         //
123         // Hard coded for now, but need to create this test in the Integration test area where
124         // we'll create an Acquisition instance for this test
125         //
126         InvocationContext invocationContext = new InvocationContext();
127         invocationContext.setDocType("Acquisition");
128         invocationContext.setMode("single");
129         invocationContext.setSingleCSID(acquisitionCsid);
130         
131         ClientResponse<Response> res = client.publishReport(reportCsid, invocationContext);
132         int statusCode = res.getStatus();
133         setupCreate();
134
135         // Check the status code of the response: does it match
136         // the expected response(s)?
137         if (logger.isDebugEnabled()) {
138             logger.debug(testName + ": status = " + statusCode);
139         }
140         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
141                 invalidStatusCodeMessage(testRequestType, statusCode));
142         Assert.assertEquals(statusCode, testExpectedStatusCode);
143
144         String publicItemCsid = extractId(res);
145         Assert.assertNotNull(publicItemCsid);
146     }
147     
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
150      */
151     @Override
152     protected CollectionSpaceClient getClientInstance() {
153         return new ReportClient();
154     }
155     
156     @Test(dataProvider = "testName",
157                 dependsOnMethods = {"CRUDTests"})
158     public void readListFiltered(String testName) throws Exception {
159         // Perform setup.
160         setupReadList();
161
162         // Submit the request to the service and store the response.
163         ReportClient client = new ReportClient();
164         ClientResponse<AbstractCommonList> res = client.readListFiltered(testDocType, "single");
165         AbstractCommonList list = null;
166         try {
167                 assertStatusCode(res, testName);
168                 list = res.getEntity();
169         } finally {
170                 if (res != null) {
171                 res.releaseConnection();
172             }
173         }
174         List<AbstractCommonList.ListItem> items = list.getListItem();
175         // We must find the basic one we created
176         boolean fFoundBaseItem = false;
177                 for (AbstractCommonList.ListItem item : items) {
178                         String itemCsid = AbstractCommonListUtils.ListItemGetCSID(item);
179                         if (knownResourceId.equalsIgnoreCase(itemCsid)) {
180                                 fFoundBaseItem = true;
181                                 break;
182                         }
183                 }
184                 if(!fFoundBaseItem) {
185                         Assert.fail("readListFiltered failed to return base item");
186                 }
187                 
188                 // Now filter for something else, and ensure it is NOT returned
189         res = client.readListFiltered("Intake", "single");
190         try {
191                 assertStatusCode(res, testName);
192                 list = res.getEntity();
193         } finally {
194                 if (res != null) {
195                 res.releaseConnection();
196             }
197         }
198
199         items = list.getListItem();
200         // We must NOT find the basic one we created
201                 for (AbstractCommonList.ListItem item : items) {
202                         Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId, 
203                                 "readListFiltered(\"Intake\", \"single\") incorrectly returned base item");
204                 }
205                 
206                 // Now filter for something else, and ensure it is NOT returned
207         res = client.readListFiltered(testDocType, "group");
208         try {
209                 assertStatusCode(res, testName);
210                 list = res.getEntity();
211         } finally {
212                 if (res != null) {
213                 res.releaseConnection();
214             }
215         }
216
217         items = list.getListItem();
218         // We must NOT find the basic one we created
219                 for (AbstractCommonList.ListItem item : items) {
220                         Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId, 
221                                 "readListFiltered(\""+testDocType+"\", \"group\") incorrectly returned base item");
222                 }
223     }
224
225     /**
226      * This method overrides the delete method in the base class which is marked with the TestNG @Test annotation.
227      * Since we don't want the actually delete test to happen until later in the dependency test chain, we're make this
228      * an empty method.  Later in the test suite, the method localDelete() will get called and it will call super.delete()
229      */
230     @Override
231     public void delete(String testName) throws Exception {
232         //
233         // Do nothing for now.  The test localDelete() will get called later in the dependency chain.
234         //
235     }
236     
237     /**
238      * This test will delete the known resource after the test readListFiltered() is run
239      */
240     @Test(dataProvider = "testName",
241                 dependsOnMethods = {"readListFiltered"})
242     public void localDelete(String testName) throws Exception {
243         super.delete(testName);
244     }
245
246     // ---------------------------------------------------------------
247     // Utility methods used by tests above
248     // ---------------------------------------------------------------
249     @Override
250     protected String getServiceName() {
251         return SERVICE_NAME;
252     }
253
254     /* (non-Javadoc)
255      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
256      */
257     @Override
258     public String getServicePathComponent() {
259         return SERVICE_PATH_COMPONENT;
260     }
261
262     /**
263      * Creates the report instance.
264      *
265      * @param identifier the identifier
266      * @return the multipart output
267      */
268     private PoxPayloadOut createReportInstance(String identifier) {
269         List<String> docTypes = new ArrayList<String>();
270         docTypes.add(testDocType);
271         return createReportInstance(
272                 "Acquisition Summary " + identifier, 
273                 docTypes, true, false, false, true,
274                 "acq_basic.jasper",
275                 "application/pdf");
276     }
277
278     /**
279      * Creates the report instance.
280      *
281      * @param name the report name
282      * @param filename the relative path to the report
283      * @param outputMIME the MIME type we will return for this report
284      * @return the multipart output
285      */
286     private PoxPayloadOut createReportInstance(String name,
287                 List<String> forDocTypeList,
288                 boolean supportsSingle, boolean supportsList, 
289                 boolean supportsGroup, boolean supportsNoContext, 
290             String filename,
291             String outputMIME) {
292         ReportsCommon reportCommon = new ReportsCommon();
293         reportCommon.setName(name);
294         ReportsCommon.ForDocTypes forDocTypes = new ReportsCommon.ForDocTypes(); 
295         List<String> docTypeList = forDocTypes.getForDocType();
296         docTypeList.addAll(forDocTypeList);
297         reportCommon.setForDocTypes(forDocTypes);
298         reportCommon.setSupportsSingleDoc(supportsSingle);
299         reportCommon.setSupportsDocList(supportsList);
300         reportCommon.setSupportsGroup(supportsGroup);
301         reportCommon.setSupportsNoContext(supportsNoContext);
302         reportCommon.setFilename(filename);
303         reportCommon.setOutputMIME(outputMIME);
304         reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
305
306         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
307         PayloadOutputPart commonPart =
308                 multipart.addPart(new ReportClient().getCommonPartName(), reportCommon);
309
310         if (logger.isDebugEnabled()) {
311             logger.debug("to be created, report common");
312             logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
313             logger.debug(multipart.toXML());
314         }
315
316         return multipart;
317     }
318
319         @Override
320         protected PoxPayloadOut createInstance(String commonPartName,
321                         String identifier) {
322         PoxPayloadOut result = createReportInstance(identifier);
323                 return result;
324         }
325
326         @Override
327         protected ReportsCommon updateInstance(ReportsCommon reportsCommon) {
328                 ReportsCommon result = new ReportsCommon();
329                 
330                 result.setSupportsSingleDoc(true);
331                 result.setName("updated-" + reportsCommon.getName());
332                 result.setOutputMIME("updated-" + reportsCommon.getOutputMIME());
333         result.setNotes("updated-" + reportsCommon.getNotes());
334                 
335                 return result;
336         }
337
338         @Override
339         protected void compareUpdatedInstances(ReportsCommon original,
340                         ReportsCommon updated) throws Exception {
341         // Check selected fields in the updated common part.
342         Assert.assertEquals(updated.getName(),
343                         original.getName(),
344                 "Name in updated object did not match submitted data.");
345
346         // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
347         if (logger.isDebugEnabled()) {
348             logger.debug("UTF-8 data sent=" + original.getNotes() + "\n"
349                     + "UTF-8 data received=" + updated.getNotes());
350         }
351         Assert.assertTrue(updated.getNotes().contains(getUTF8DataFragment()),
352                 "UTF-8 data retrieved '" + updated.getNotes()
353                 + "' does not contain expected data '" + getUTF8DataFragment());
354         Assert.assertEquals(updated.getNotes(),
355                         original.getNotes(),
356                 "Notes in updated object did not match submitted data.");
357         }
358         
359         protected void compareReadInstances(ReportsCommon original, ReportsCommon fromRead) throws Exception {
360         Assert.assertEquals(fromRead.getNotes(), getUTF8DataFragment(),
361                 "UTF-8 data retrieved '" + fromRead.getNotes()
362                 + "' does not match expected data '" + getUTF8DataFragment());
363         }       
364         
365     /*
366      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
367      * refer to this method in their @Test annotation declarations.
368      */
369     @Override
370     @Test(dataProvider = "testName",
371                 dependsOnMethods = {
372                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
373         public void CRUDTests(String testName) {
374                 // TODO Auto-generated method stub              
375         }
376 }