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