KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > metadata > OlapClientTest


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.metadata;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.springframework.context.support.ClassPathXmlApplicationContext;
34
35 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource;
36 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
37 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
38 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
39 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection;
40 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition;
41 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection;
42 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit;
43 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
44 import com.jaspersoft.jasperserver.api.metadata.olap.service.impl.OlapConnectionServiceImpl;
45
46 import junit.framework.TestCase;
47
48 /**
49  * @author jshih
50  */

51
52 public class OlapClientTest extends TestCase {
53
54     protected final Log log = LogFactory.getLog(this.getClass());
55     
56     protected final String JavaDoc OLAP_SCHEMAS = "/olap/schemas/";
57
58     protected final String JavaDoc OLAP_DATASOURCES = "/olap/datasources/";
59     
60     protected final String JavaDoc OLAP_CONNECTIONS = "/olap/connections/";
61     
62     protected final String JavaDoc OLAP_DEFINITIONS = "/olap/xmla/definitions/";
63     
64     protected final String JavaDoc OLAP_VIEWS = "/olap/views/";
65     
66     protected final String JavaDoc MDX_QUERY =
67         "SELECT {[Measures].[Unit Sales]} ON COLUMNS, " +
68         "{[Time].[1997].[Q1]} ON ROWS FROM [Sales]";
69
70     protected final String JavaDoc XML_SCHEMA = "/olap/schemas/Foodmart.xml";
71     
72     private Properties JavaDoc jdbcProps;
73
74     private RepositoryService repositoryService;
75     
76     private OlapConnectionService connectionService;
77  
78     protected void setUp() throws Exception JavaDoc
79     {
80         ClassPathXmlApplicationContext appContext =
81             new ClassPathXmlApplicationContext(new String JavaDoc[] {"hibernateConfig.xml", "viewService.xml"});
82
83         setRepositoryService((RepositoryService) appContext.getBean("repoService"));
84         
85         setConnectionService((OlapConnectionService) appContext.getBean("olapConnectionService"));
86
87         loadJdbcProps();
88     }
89
90     // tests
91

92     static FileResource schemaFoodmart;
93     
94     static FileResource schemaSugarCrm;
95     
96     public void testUpdateSchema()
97     {
98         schemaFoodmart =
99             (FileResource) repositoryService.newResource( null, FileResource.class );
100
101         String JavaDoc strSchemaFoodmart = "FoodmartSchema";
102         
103         schemaFoodmart =
104             (FileResource) repositoryService.getResource(null, OLAP_SCHEMAS + strSchemaFoodmart);
105         
106         assertTrue(schemaFoodmart.getParentFolder().equals("/olap/schemas"));
107         
108         assertTrue(schemaFoodmart.getName().equals(strSchemaFoodmart));
109
110         schemaFoodmart.readData(getClass().getResourceAsStream(XML_SCHEMA));
111         
112         assertNotNull(schemaFoodmart.getDataStream());
113         
114         String JavaDoc strSchemaSugarCrm = "SugarCRMOppsSchema";
115         
116         schemaSugarCrm =
117             (FileResource) repositoryService.getResource(null, OLAP_SCHEMAS + strSchemaSugarCrm);
118         
119         assertTrue(schemaSugarCrm.getParentFolder().equals("/olap/schemas"));
120         
121         assertTrue(schemaSugarCrm.getName().equals(strSchemaSugarCrm));
122
123         schemaSugarCrm.readData(getClass().getResourceAsStream(XML_SCHEMA));
124         
125         assertNotNull(schemaSugarCrm.getDataStream());
126         
127     }
128
129     static ReportDataSource datasourceFoodmart;
130     
131     static ReportDataSource datasourceSugarCrm;
132
133     public void testUpdateDatasource()
134     {
135         String JavaDoc strDatasourceFoodmart = "FoodmartDataSource";
136
137         datasourceFoodmart =
138             (ReportDataSource) repositoryService.getResource(null, OLAP_DATASOURCES + strDatasourceFoodmart);
139         
140         assertTrue(datasourceFoodmart.getURIString().equals("/olap/datasources/FoodmartDataSource"));
141         
142         String JavaDoc strDatasourceSugarCrm = "SugarCRMDataSource";
143
144         datasourceSugarCrm =
145             (ReportDataSource) repositoryService.getResource(null, OLAP_DATASOURCES + strDatasourceSugarCrm);
146         
147         assertTrue(datasourceSugarCrm.getURIString().equals("/olap/datasources/SugarCRMDataSource"));
148         
149 // String strDsFoodmart = "FoodmartDS";
150
// JdbcReportDataSource datasource =
151
// (JdbcReportDataSource) repositoryService.newResource( null, JdbcReportDataSource.class );
152
// datasource.setName(strDsFoodmart);
153
// datasource.setLabel(strDsFoodmart);
154
// datasource.setDescription(strDsFoodmart);
155
// datasource.setDriverClass(jdbcProps.getProperty("foodmart.jdbc.driverClassName"));
156
// datasource.setConnectionUrl(jdbcProps.getProperty("foodmart.jdbc.url"));
157
// datasource.setUsername(jdbcProps.getProperty("foodmart.jdbc.username"));
158
// datasource.setPassword(jdbcProps.getProperty("foodmart.jdbc.password"));
159
//// getConnectionService().saveResource( null, "/olap/datasources/", datasource );
160
// repositoryService.saveResource(null, datasource);
161
//
162
// datasourceFoodmart =
163
// (ReportDataSource) repositoryService.getResource(null, OLAP_DATASOURCES + strDsFoodmart);
164
//
165
// assertTrue(datasourceFoodmart.getURIString().equals("/olap/datasources/FoodmartDS"));
166
}
167     
168     static String JavaDoc strConnection;
169
170     static OlapClientConnection connectionOriginal;
171
172     private void setupTestUpdateMondrianConnection() {
173         
174         strConnection = "Foodmart";
175         
176         connectionOriginal =
177             (OlapClientConnection) repositoryService.getResource(null, OLAP_CONNECTIONS + strConnection);
178         
179         assertNotNull(connectionOriginal);
180         
181         assertTrue(connectionOriginal instanceof MondrianConnection);
182         
183         assertTrue(((MondrianConnection) connectionOriginal).getSchema().getReferenceLookup().getName()
184                 .equals("FoodmartSchema"));
185         
186         assertTrue(((MondrianConnection) connectionOriginal).getSchema().getReferenceURI()
187                 .equals("/olap/schemas/FoodmartSchema"));
188     }
189     
190     public void testUpdateMondrianConnectionSchema() {
191
192         setupTestUpdateMondrianConnection();
193         
194         // new schema
195
((MondrianConnection) connectionOriginal).setSchemaReference(schemaSugarCrm.getURIString());
196         
197 // connectionService.saveResource(null, "/olap/connections/", connectionOriginal);
198
repositoryService.saveResource(null, connectionOriginal);
199         
200         OlapClientConnection connectionNew =
201             (OlapClientConnection) repositoryService.getResource(null, OLAP_CONNECTIONS + strConnection);
202         
203         assertNotNull(connectionNew);
204         
205         assertTrue(connectionNew instanceof MondrianConnection);
206         
207         assertTrue(((MondrianConnection) connectionNew).getSchema().getReferenceLookup().getName()
208                 .equals("SugarCRMOppsSchema"));
209         
210         assertTrue(((MondrianConnection) connectionNew).getSchema().getReferenceURI()
211                 .equals("/olap/schemas/SugarCRMOppsSchema"));
212         
213     }
214     
215     public void testUpdateMondrianConnectionDatasource() {
216         
217         setupTestUpdateMondrianConnection();
218         
219         // new datasource
220
((MondrianConnection) connectionOriginal).setDataSourceReference(datasourceSugarCrm.getURIString());
221         
222 // connectionService.saveResource(null, "/olap/connections/", connectionOriginal);
223
repositoryService.saveResource(null, connectionOriginal);
224         
225         OlapClientConnection connectionNew =
226             (OlapClientConnection) repositoryService.getResource(null, OLAP_CONNECTIONS + strConnection);
227         
228         assertNotNull(connectionNew);
229         
230         assertTrue(connectionNew instanceof MondrianConnection);
231         
232         assertTrue(((MondrianConnection) connectionNew).getDataSource().getReferenceLookup().getName()
233                 .equals("SugarCRMDataSource"));
234         
235         assertTrue(((MondrianConnection) connectionNew).getDataSource().getReferenceURI()
236                 .equals("/olap/datasources/SugarCRMDataSource"));
237         
238     }
239
240     public void testUpdateXmlaDefinition()
241     {
242         String JavaDoc strDefinition = "FoodmartXmlaDefinition";
243         
244         MondrianXMLADefinition definition =
245 // (MondrianXMLADefinition) repository.getResource(null, OLAP_DEFINITIONS + strDefinition);
246
(MondrianXMLADefinition) repositoryService.getResource(null, "/olap/xmla/definitions/" + strDefinition);
247         
248         assertNotNull(definition.getURIString()); // null
249
}
250     
251     public void testUpdateXmlaConnection()
252     {
253         String JavaDoc strConnection = "FoodmartXmlaConnection";
254         
255         OlapClientConnection connection =
256 // (OlapClientConnection) repository.getResource(null, OLAP_CONNECTIONS + strConnection);
257
(OlapClientConnection) repositoryService.getResource(null, "/olap/connections/" + strConnection);
258         
259         assertNotNull(connection);
260     }
261     
262     public void testUpdateOlapModel()
263     {
264         String JavaDoc olapViewRef = "/olap/connections/Foodmart";
265         
266         String JavaDoc olapQuery = MDX_QUERY;
267         
268         mondrian.olap.Connection mondrianConnection =
269             ((OlapConnectionServiceImpl) getConnectionService()).getMondrianConnection(null, olapViewRef);
270
271         mondrian.olap.Query mondrianQuery = mondrianConnection.parseQuery(olapQuery);
272         
273         assertNotNull(mondrianQuery);
274         
275         assertTrue(mondrianQuery.getCube().toString().equals("Sales"));
276         
277 // assertTrue(mondrianQuery.axes[0].exp.toString().equals("{[Measures].[Unit Sales]}"));
278
//
279
// assertTrue(mondrianQuery.axes[1].exp.toString().equals("{[Time].[1997].[Q1]}"));
280
}
281     
282     public void testUpdateOlapView()
283     {
284         String JavaDoc strOlapView = "Foodmart_sample_unit_1";
285         
286         OlapUnit olapView =
287             (OlapUnit) repositoryService.getResource(null, OLAP_VIEWS + strOlapView);
288         
289         assertNotNull(olapView.getOlapClientConnection());
290         
291         assertTrue(olapView.getMdxQuery()
292                 .equals("SELECT {[Measures].[Unit Sales]} ON COLUMNS, {[Time].[1997].[Q1]} ON ROWS FROM [Sales]"));
293     }
294
295     // helpers
296

297     protected Properties JavaDoc loadJdbcProps() throws IOException JavaDoc, FileNotFoundException JavaDoc
298     {
299         jdbcProps = new Properties JavaDoc();
300         
301         String JavaDoc jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties");
302         
303         BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(jdbcPropFile));
304         
305         jdbcProps.load(is);
306         
307         is.close();
308         
309         return jdbcProps;
310     }
311     
312     public RepositoryService getRepositoryService()
313     {
314         return repositoryService;
315     }
316     
317     public void setRepositoryService(RepositoryService repository)
318     {
319         this.repositoryService = repository;
320     }
321
322     public OlapConnectionService getConnectionService()
323     {
324         return connectionService;
325     }
326
327     public void setConnectionService(OlapConnectionService connection)
328     {
329         this.connectionService = connection;
330     }
331 }
332
Popular Tags