]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6b69486a56c92685c84edb58db44f37db139e6df
[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.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.Response;
29
30 import org.collectionspace.services.client.AbstractCommonListUtils;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.ReportClient;
35 import org.collectionspace.services.common.invocable.InvocationContext;
36 import org.collectionspace.services.report.ReportsCommon;
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 AbstractPoxServiceTestImpl<AbstractCommonList, ReportsCommon> {
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     private String testDocType = "Acquisition";
64
65     @Test(dataProvider = "testName", dependsOnMethods = {"create"})    
66     public void publishReportInstance(String testName) throws Exception {
67         // Perform setup.
68         setupCreate();
69
70         // Submit the request to the service and store the response.
71         ReportClient client = (ReportClient)this.getClientInstance();
72         
73         createReportInstance()
74         
75         //
76         // Hard coded for now, but need to create this test in the Integration test area where
77         // we'll create an Acquisition instance for this test
78         //
79         InvocationContext invocationContext = new InvocationContext();
80         invocationContext.setDocType("Acquisition");
81         invocationContext.setMode("single");
82         invocationContext.setSingleCSID("34abdc63-944c-421d-a585");
83         
84         String reportCsid = "8680d49c-99b8-4788-aa45";
85         ClientResponse<Response> res = client.publishReport(reportCsid, invocationContext);
86         int statusCode = res.getStatus();
87
88         // Check the status code of the response: does it match
89         // the expected response(s)?
90         if (logger.isDebugEnabled()) {
91             logger.debug(testName + ": status = " + statusCode);
92         }
93         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
94                 invalidStatusCodeMessage(testRequestType, statusCode));
95         Assert.assertEquals(statusCode, testExpectedStatusCode);
96
97         String articleCsid = extractId(res);
98         Assert.assertNotNull(articleCsid);
99     }
100     
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
103      */
104     @Override
105     protected CollectionSpaceClient getClientInstance() {
106         return new ReportClient();
107     }
108     
109     @Test(dataProvider = "testName",
110                 dependsOnMethods = {"CRUDTests"})
111     public void readListFiltered(String testName) throws Exception {
112         // Perform setup.
113         setupReadList();
114
115         // Submit the request to the service and store the response.
116         ReportClient client = new ReportClient();
117         ClientResponse<AbstractCommonList> res = client.readListFiltered(testDocType, "single");
118         AbstractCommonList list = null;
119         try {
120                 assertStatusCode(res, testName);
121                 list = res.getEntity();
122         } finally {
123                 if (res != null) {
124                 res.releaseConnection();
125             }
126         }
127         List<AbstractCommonList.ListItem> items = list.getListItem();
128         // We must find the basic one we created
129         boolean fFoundBaseItem = false;
130                 for (AbstractCommonList.ListItem item : items) {
131                         String itemCsid = AbstractCommonListUtils.ListItemGetCSID(item);
132                         if (knownResourceId.equalsIgnoreCase(itemCsid)) {
133                                 fFoundBaseItem = true;
134                                 break;
135                         }
136                 }
137                 if(!fFoundBaseItem) {
138                         Assert.fail("readListFiltered failed to return base item");
139                 }
140                 
141                 // Now filter for something else, and ensure it is NOT returned
142         res = client.readListFiltered("Intake", "single");
143         try {
144                 assertStatusCode(res, testName);
145                 list = res.getEntity();
146         } finally {
147                 if (res != null) {
148                 res.releaseConnection();
149             }
150         }
151
152         items = list.getListItem();
153         // We must NOT find the basic one we created
154                 for (AbstractCommonList.ListItem item : items) {
155                         Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId, 
156                                 "readListFiltered(\"Intake\", \"single\") incorrectly returned base item");
157                 }
158                 
159                 // Now filter for something else, and ensure it is NOT returned
160         res = client.readListFiltered(testDocType, "group");
161         try {
162                 assertStatusCode(res, testName);
163                 list = res.getEntity();
164         } finally {
165                 if (res != null) {
166                 res.releaseConnection();
167             }
168         }
169
170         items = list.getListItem();
171         // We must NOT find the basic one we created
172                 for (AbstractCommonList.ListItem item : items) {
173                         Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId, 
174                                 "readListFiltered(\""+testDocType+"\", \"group\") incorrectly returned base item");
175                 }
176     }
177
178     /**
179      * This method overrides the delete method in the base class which is marked with the TestNG @Test annotation.
180      * Since we don't want the actually delete test to happen until later in the dependency test chain, we're make this
181      * an empty method.  Later in the test suite, the method localDelete() will get called and it will call super.delete()
182      */
183     @Override
184     public void delete(String testName) throws Exception {
185         //
186         // Do nothing for now.  The test localDelete() will get called later in the dependency chain.
187         //
188     }
189     
190     /**
191      * This test will delete the known resource after the test readListFiltered() is run
192      */
193     @Test(dataProvider = "testName",
194                 dependsOnMethods = {"readListFiltered"})
195     public void localDelete(String testName) throws Exception {
196         super.delete(testName);
197     }
198
199     // ---------------------------------------------------------------
200     // Utility methods used by tests above
201     // ---------------------------------------------------------------
202     @Override
203     protected String getServiceName() {
204         return SERVICE_NAME;
205     }
206
207     /* (non-Javadoc)
208      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
209      */
210     @Override
211     public String getServicePathComponent() {
212         return SERVICE_PATH_COMPONENT;
213     }
214
215     /**
216      * Creates the report instance.
217      *
218      * @param identifier the identifier
219      * @return the multipart output
220      */
221     private PoxPayloadOut createReportInstance(String identifier) {
222         List<String> docTypes = new ArrayList<String>();
223         docTypes.add(testDocType);
224         return createReportInstance(
225                 "Acquisition Summary " + identifier, 
226                 docTypes, true, false, false, true,
227                 "acq_basic.jasper",
228                 "application/pdf");
229     }
230
231     /**
232      * Creates the report instance.
233      *
234      * @param name the report name
235      * @param filename the relative path to the report
236      * @param outputMIME the MIME type we will return for this report
237      * @return the multipart output
238      */
239     private PoxPayloadOut createReportInstance(String name,
240                 List<String> forDocTypeList,
241                 boolean supportsSingle, boolean supportsList, 
242                 boolean supportsGroup, boolean supportsNoContext, 
243             String filename,
244             String outputMIME) {
245         ReportsCommon reportCommon = new ReportsCommon();
246         reportCommon.setName(name);
247         ReportsCommon.ForDocTypes forDocTypes = new ReportsCommon.ForDocTypes(); 
248         List<String> docTypeList = forDocTypes.getForDocType();
249         docTypeList.addAll(forDocTypeList);
250         reportCommon.setForDocTypes(forDocTypes);
251         reportCommon.setSupportsSingleDoc(supportsSingle);
252         reportCommon.setSupportsDocList(supportsList);
253         reportCommon.setSupportsGroup(supportsGroup);
254         reportCommon.setSupportsNoContext(supportsNoContext);
255         reportCommon.setFilename(filename);
256         reportCommon.setOutputMIME(outputMIME);
257         reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
258
259         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
260         PayloadOutputPart commonPart =
261                 multipart.addPart(new ReportClient().getCommonPartName(), reportCommon);
262
263         if (logger.isDebugEnabled()) {
264             logger.debug("to be created, report common");
265             logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
266             logger.debug(multipart.toXML());
267         }
268
269         return multipart;
270     }
271
272         @Override
273         protected PoxPayloadOut createInstance(String commonPartName,
274                         String identifier) {
275         PoxPayloadOut result = createReportInstance(identifier);
276                 return result;
277         }
278
279         @Override
280         protected ReportsCommon updateInstance(ReportsCommon reportsCommon) {
281                 ReportsCommon result = new ReportsCommon();
282                 
283                 result.setSupportsSingleDoc(true);
284                 result.setName("updated-" + reportsCommon.getName());
285                 result.setOutputMIME("updated-" + reportsCommon.getOutputMIME());
286         result.setNotes("updated-" + reportsCommon.getNotes());
287                 
288                 return result;
289         }
290
291         @Override
292         protected void compareUpdatedInstances(ReportsCommon original,
293                         ReportsCommon updated) throws Exception {
294         // Check selected fields in the updated common part.
295         Assert.assertEquals(updated.getName(),
296                         original.getName(),
297                 "Name in updated object did not match submitted data.");
298
299         // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
300         if (logger.isDebugEnabled()) {
301             logger.debug("UTF-8 data sent=" + original.getNotes() + "\n"
302                     + "UTF-8 data received=" + updated.getNotes());
303         }
304         Assert.assertTrue(updated.getNotes().contains(getUTF8DataFragment()),
305                 "UTF-8 data retrieved '" + updated.getNotes()
306                 + "' does not contain expected data '" + getUTF8DataFragment());
307         Assert.assertEquals(updated.getNotes(),
308                         original.getNotes(),
309                 "Notes in updated object did not match submitted data.");
310         }
311         
312         protected void compareReadInstances(ReportsCommon original, ReportsCommon fromRead) throws Exception {
313         Assert.assertEquals(fromRead.getNotes(), getUTF8DataFragment(),
314                 "UTF-8 data retrieved '" + fromRead.getNotes()
315                 + "' does not match expected data '" + getUTF8DataFragment());
316         }       
317         
318     /*
319      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
320      * refer to this method in their @Test annotation declarations.
321      */
322     @Override
323     @Test(dataProvider = "testName",
324                 dependsOnMethods = {
325                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
326         public void CRUDTests(String testName) {
327                 // TODO Auto-generated method stub              
328         }
329 }