KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Date JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import junit.framework.TestCase;
38 import junit.textui.TestRunner;
39
40 import org.springframework.orm.hibernate3.HibernateTemplate;
41 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
42 import org.springframework.context.support.ClassPathXmlApplicationContext;
43
44 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
45 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.persistent.RepoFolder;
46 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
47 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResourceData;
48 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
49 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource;
50 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
51 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
52 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
53 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
54 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
55 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection;
56 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection;
57 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit;
58 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition;
59 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection;
60 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
61 import com.jaspersoft.jasperserver.api.metadata.olap.service.impl.OlapConnectionServiceImpl;
62 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
63 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
64 import com.jaspersoft.jasperserver.api.common.domain.ValidationResult;
65 import com.jaspersoft.jasperserver.api.common.domain.impl.ValidationDetailImpl;
66 import com.jaspersoft.jasperserver.api.common.domain.impl.ValidationResultImpl;
67
68 import mondrian.olap.DriverManager;
69 import com.tonbeller.jpivot.olap.model.OlapModel;
70 import com.tonbeller.jpivot.olap.model.OlapException;
71 import com.tonbeller.jpivot.olap.model.Result;
72 import com.tonbeller.jpivot.mondrian.MondrianResult;
73
74 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor;
75
76 /**
77  * @author sbirney
78  */

79
80 public class OlapConnectionTest extends TestCase {
81
82     private static final Log log = LogFactory.getLog(OlapConnectionTest.class);
83
84     protected final String JavaDoc foodMartSchemaURI = "/queries/FoodMart.xml";
85     protected final String JavaDoc sugarCRMSchemaURI = "/queries/SugarCRMOpps.xml";
86
87     private Properties JavaDoc jdbcProps;
88
89     public static void main(String JavaDoc[] args) {
90         TestRunner.run(OlapConnectionTest.class);
91     }
92
93     protected void setUp() throws Exception JavaDoc {
94
95         ClassPathXmlApplicationContext appContext
96             = new ClassPathXmlApplicationContext(new String JavaDoc[] {"hibernateConfig.xml", "viewService.xml"});
97
98         setRepository( (RepositoryService)appContext.getBean("repoService") );
99         setConnectionService( (OlapConnectionService)appContext.getBean("olapConnectionService") );
100
101         loadJdbcProps();
102     }
103
104     protected Properties JavaDoc loadJdbcProps() throws IOException JavaDoc, FileNotFoundException JavaDoc {
105     jdbcProps = new Properties JavaDoc();
106     String JavaDoc jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties");
107     BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(jdbcPropFile));
108     jdbcProps.load(is);
109     is.close();
110     return jdbcProps;
111     }
112     
113     protected void createFoodmartSchemaResource() {
114         RepositoryService rep = getRepository();
115         FileResource schemaResource = (FileResource) rep.newResource( null, FileResource.class );
116         schemaResource.setName( "FoodmartSchema" );
117         schemaResource.setLabel( "Foodmart Schema" );
118         schemaResource.setDescription( "OLAP Foodmart Schema" );
119         schemaResource.setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
120         InputStream JavaDoc in = getClass().getResourceAsStream( foodMartSchemaURI );
121         schemaResource.readData(in);
122         getConnectionService().saveResource( null, "/olap/schemas/", schemaResource );
123     }
124
125     protected void createSugarCRMSchemaResource() {
126         RepositoryService rep = getRepository();
127         FileResource schemaResource = (FileResource) rep.newResource( null, FileResource.class );
128         schemaResource.setName( "SugarCRMOppsSchema" );
129         schemaResource.setLabel( "SugarCRM Opportunities DataMart" );
130         schemaResource.setDescription( "SugarCRM Opportunities Data Mart Schema" );
131         schemaResource.setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
132         InputStream JavaDoc in = getClass().getResourceAsStream( sugarCRMSchemaURI );
133         schemaResource.readData(in);
134         getConnectionService().saveResource(null, "/olap/schemas/", schemaResource );
135     }
136
137     protected void createFoodmartDataSourceResource() {
138         RepositoryService rep = getRepository();
139         JdbcReportDataSource dataSourceResource
140             = (JdbcReportDataSource) rep.newResource( null, JdbcReportDataSource.class );
141         dataSourceResource.setName( "FoodmartDataSource" );
142         dataSourceResource.setLabel( "Foodmart Data Source" );
143         dataSourceResource.setDescription( "Foodmart Data Source" );
144         dataSourceResource.setDriverClass(jdbcProps.getProperty("foodmart.jdbc.driverClassName"));
145         dataSourceResource.setConnectionUrl(jdbcProps.getProperty("foodmart.jdbc.url"));
146         dataSourceResource.setUsername(jdbcProps.getProperty("foodmart.jdbc.username"));
147         dataSourceResource.setPassword(jdbcProps.getProperty("foodmart.jdbc.password"));
148         getConnectionService().saveResource( null, "/olap/datasources/", dataSourceResource );
149     }
150
151     protected void createSugarCRMDataSourceResource() {
152         RepositoryService rep = getRepository();
153         JdbcReportDataSource dataSourceResource
154             = (JdbcReportDataSource) rep.newResource( null, JdbcReportDataSource.class );
155         dataSourceResource.setName( "SugarCRMDataSource" );
156         dataSourceResource.setLabel( "SugarCRM Data Source" );
157         dataSourceResource.setDescription( "SugarCRM Data Source" );
158         dataSourceResource.setDriverClass(jdbcProps.getProperty("test.jdbc.driverClassName"));
159         dataSourceResource.setConnectionUrl(jdbcProps.getProperty("test.jdbc.url"));
160         dataSourceResource.setUsername(jdbcProps.getProperty("test.jdbc.username"));
161         dataSourceResource.setPassword(jdbcProps.getProperty("test.jdbc.password"));
162         getConnectionService().saveResource( null, "/olap/datasources/", dataSourceResource );
163     }
164
165     protected void createFoodmartMondrianConnectionResource() {
166         RepositoryService rep = getRepository();
167         MondrianConnection connection = (MondrianConnection) rep.newResource( null, MondrianConnection.class );
168         connection.setName("Foodmart");
169         connection.setLabel("Foodmart Mondrian Connection");
170         connection.setDescription("Foodmart Olap Connection");
171         connection.setSchemaReference("/olap/schemas/FoodmartSchema");
172         connection.setDataSourceReference("/olap/datasources/FoodmartDataSource");
173         getConnectionService().saveResource( null, "/olap/connections/", connection );
174     }
175
176     protected void createSugarCRMMondrianConnectionResource() {
177         RepositoryService rep = getRepository();
178         MondrianConnection connection = (MondrianConnection) rep.newResource( null, MondrianConnection.class );
179         connection.setName("SugarCRM");
180         connection.setLabel("SugarCRM OLAP Connection");
181         connection.setDescription("SugarCRM OLAP Connection: only opportunities");
182         connection.setSchemaReference("/olap/schemas/SugarCRMOppsSchema");
183         connection.setDataSourceReference("/datasources/JServerJNDIDS"); // que?
184
getConnectionService().saveResource(null, "/olap/connections/", connection );
185     }
186
187     protected void createSugarCRMTestMondrianConnectionResource() {
188         RepositoryService rep = getRepository();
189         MondrianConnection connection = (MondrianConnection) rep.newResource( null, MondrianConnection.class );
190         connection.setName("SugarCRMTest");
191         connection.setLabel("SugarCRM OLAP Connection");
192         connection.setDescription("SugarCRM OLAP Connection: only opportunities");
193         connection.setSchemaReference("/olap/schemas/SugarCRMOppsSchema");
194         connection.setDataSourceReference("/olap/datasources/SugarCRMDataSource");
195         getConnectionService().saveResource(null, "/olap/connections/", connection );
196     }
197
198     protected void createFoodmartXMLAConnectionResource() {
199         RepositoryService rep = getRepository();
200         XMLAConnection connection = (XMLAConnection) rep.newResource( null, XMLAConnection.class );
201         connection.setName("FoodmartXmlaConnection");
202         connection.setLabel("Foodmart XMLA Connection");
203         connection.setDescription("Foodmart XML/A Connection");
204         connection.setCatalog("Foodmart");
205         connection.setDataSource("Provider=Mondrian;DataSource=Foodmart;");
206         connection.setURI("http://localhost:8080/jasperserver/xmla");
207     // TODO: create a sample USER_XMLA and ROLE_XMLA_USER
208
connection.setUsername("tomcat");
209     connection.setPassword("tomcat");
210         getConnectionService().saveResource( null, "/olap/connections/", connection );
211     }
212
213     protected void createSugarCRMXMLAConnectionResource() {
214         RepositoryService rep = getRepository();
215         XMLAConnection connection = (XMLAConnection) rep.newResource( null, XMLAConnection.class );
216         connection.setName("SugarCRMXmlaConnection");
217         connection.setLabel("SugarCRM XMLA Connection");
218         connection.setDescription("SugarCRM XML/A Connection");
219         connection.setCatalog("SugarCRM");
220         connection.setDataSource("Provider=Mondrian;DataSource=SugarCRM;");
221         connection.setURI("http://localhost:8080/jasperserver/xmla");
222     // TODO: create a sample USER_XMLA and ROLE_XMLA_USER
223
connection.setUsername("tomcat");
224     connection.setPassword("tomcat");
225         getConnectionService().saveResource(null, "/olap/connections/", connection );
226     }
227
228     protected void createFoodmartMondrianXMLADefinitionResource() {
229         RepositoryService rep = getRepository();
230         MondrianXMLADefinition def = (MondrianXMLADefinition) rep.newResource( null, MondrianXMLADefinition.class );
231         def.setName("FoodmartXmlaDefinition");
232         def.setLabel("Foodmart XMLA Connection");
233         def.setDescription("Foodmart XML/A Connection");
234         def.setCatalog("Foodmart");
235         def.setMondrianConnectionReference("/olap/connections/Foodmart");
236
237         getConnectionService().saveResource( null, "/olap/xmla/definitions/", def );
238     }
239
240     protected void createSugarCRMMondrianXMLADefinitionResource() {
241         RepositoryService rep = getRepository();
242         MondrianXMLADefinition def = (MondrianXMLADefinition) rep.newResource( null, MondrianXMLADefinition.class );
243         def.setName("SugarCRMXmlaDefinition");
244         def.setLabel("SugarCRM XMLA Connection");
245         def.setDescription("SugarCRM XML/A Connection");
246         def.setCatalog("SugarCRM");
247         def.setMondrianConnectionReference("/olap/connections/SugarCRMTest");
248
249         getConnectionService().saveResource( null, "/olap/xmla/definitions/", def );
250     }
251
252     private String JavaDoc SAMPLE_FOODMART_MDX_QUERY =
253     "SELECT {[Measures].[Unit Sales]} ON COLUMNS, " +
254     "{[Time].[1997].[Q1]} ON ROWS FROM [Sales]";
255
256
257     protected void createFoodmartOlapUnit() {
258     RepositoryService rep = getRepository();
259     OlapUnit view = (OlapUnit) rep.newResource( null, OlapUnit.class );
260     view.setName("Foodmart_sample_unit_1");
261     view.setLabel("Foodmart Sample Olap Unit 1");
262     view.setDescription("Sample Olap Unit 1: 1997 Q1 Foodmart Unit Sales");
263     view.setOlapClientConnectionReference("/olap/connections/Foodmart");
264     view.setMdxQuery(SAMPLE_FOODMART_MDX_QUERY);
265     getConnectionService().saveResource( null, "/olap/views/", view );
266     OlapUnit cycledView = (OlapUnit)rep.getResource(null,
267                             "/olap/views/Foodmart_sample_unit_1");
268     // calling validate here with view instead of cycledView throws null pointer
269
// XXX jasperserver bug?
270

271     //something tells me foodmart isn't set up right on aphid
272
//assertTrue( "validation of Foodmart Olap Unit failed",
273
// getConnectionService().validate(null, cycledView).
274
// getValidationState() != ValidationResult.STATE_ERROR );
275
}
276
277     protected void createSugarCRMXmlaOlapUnit() {
278     RepositoryService rep = getRepository();
279     OlapUnit view = (OlapUnit) rep.newResource( null, OlapUnit.class );
280     view.setName("SugarCRM_xmla_sample_unit_2");
281     view.setLabel("SugarCRM Sample XMLA Olap Unit 2");
282     view.setDescription("Sample XMLA Olap Unit 2: 1997 Q1 SugarCRM Unit Sales");
283     view.setOlapClientConnectionReference("/olap/connections/SugarCRMXmlaConnection");
284     view.setMdxQuery(SAMPLE_SUGAR_CRM_MDX_QUERY);
285     getConnectionService().saveResource( null, "/olap/views/", view );
286     }
287
288     private String JavaDoc SAMPLE_SUGAR_CRM_MDX_QUERY =
289     "select {[Measures].[Total Sale Amount], [Measures].[Number of Sales], [Measures].[Avg Sale Amount], [Measures].[Avg Time To Close (Days)], [Measures].[Avg Close Probablility]} ON COLUMNS, " +
290     " NON EMPTY {([Account Categorization].[All Accounts], [Close Period].[All Periods])} ON ROWS " +
291     " from [SalesAnalysis] " +
292     " where [Sale State].[All Types].[Closed Won]";
293
294     protected void createSugarCRMOlapUnit() {
295     RepositoryService rep = getRepository();
296     OlapUnit view = (OlapUnit) rep.newResource( null, OlapUnit.class );
297     view.setName("SugarCRM_sample_unit_1");
298     view.setLabel("SugarCRM sample unit 1");
299     view.setDescription("SugarCRM Sample Olap Unit 1: Sales Performance by Industry/Account");
300     view.setOlapClientConnectionReference("/olap/connections/SugarCRM");
301     view.setMdxQuery(SAMPLE_SUGAR_CRM_MDX_QUERY);
302     getConnectionService().saveResource(null, "/olap/views/", view );
303     //OlapUnit cycledView = (OlapUnit)rep.getResource(null,
304
//"/olap/views/SugarCRM_sample_unit_1");
305
// calling validate here with view instead of cycledView throws null pointer
306
// XXX jasperserver bug?
307
//assertTrue( "validation of SugarCRM Olap Unit failed",
308
// getConnectionService().validate(null, cycledView).
309
// getValidationState() != ValidationResult.STATE_ERROR );
310
}
311
312     protected void createSugarCRMTestOlapUnit() {
313     RepositoryService rep = getRepository();
314     OlapUnit view = (OlapUnit) rep.newResource( null, OlapUnit.class );
315     view.setName("SugarCRM_sample_unit_2");
316     view.setLabel("SugarCRM sample unit 2");
317     view.setDescription("SugarCRM Sample Olap Unit 2: Sales Performance by Industry/Account");
318     view.setOlapClientConnectionReference("/olap/connections/SugarCRMTest");
319     view.setMdxQuery(SAMPLE_SUGAR_CRM_MDX_QUERY);
320     getConnectionService().saveResource(null, "/olap/views/", view );
321     OlapUnit cycledView = (OlapUnit)rep.getResource(null,
322                             "/olap/views/SugarCRM_sample_unit_2");
323     // calling validate here with view instead of cycledView throws null pointer
324
// XXX jasperserver bug?
325
assertTrue( "validation of SugarCRM Olap Unit failed",
326             getConnectionService().validate(null, cycledView).
327             getValidationState() != ValidationResult.STATE_ERROR );
328     }
329
330     protected void executeSampleFoodmartQuery() {
331     assertQueryShape( "/olap/connections/Foodmart",
332               SAMPLE_FOODMART_MDX_QUERY,
333               "", 0, 0);
334     }
335
336     protected void executeSampleSugarQuery() {
337     assertQueryShape( "/olap/connections/SugarCRM",
338               SAMPLE_SUGAR_CRM_MDX_QUERY,
339               "Total Sale Amount", 2, 5);
340     }
341
342     protected void executeSampleSugarTestQuery() {
343     assertQueryShape( "/olap/connections/SugarCRMTest",
344               SAMPLE_SUGAR_CRM_MDX_QUERY,
345               "Total Sale Amount", 2, 5);
346     }
347
348     protected void assertQueryShape( String JavaDoc connResourceName,
349                      String JavaDoc mdxQuery,
350                      String JavaDoc expectedCaption,
351                      int expectedNumAxes,
352                      int expectedNumPositions) {
353         mondrian.olap.Connection monConnection
354             = ((OlapConnectionServiceImpl)getConnectionService())
355         .getMondrianConnection( null, connResourceName );
356
357         // perform olap test query with this mondrian Connection
358
mondrian.olap.Query monQuery = monConnection.parseQuery(mdxQuery);
359         mondrian.olap.Result monResult = monConnection.execute(monQuery);
360         mondrian.olap.Axis[] axes = monResult.getAxes();
361         mondrian.olap.Position[] positions = axes[0].positions;
362         mondrian.olap.Member m0 = positions[0].members[0];
363         String JavaDoc caption = m0.getCaption();
364
365     log.debug("caption = " + caption + ", " +
366           "axes.length=" + axes.length + ", " +
367           "positions.length= " + positions.length);
368
369         assertEquals( caption, expectedCaption );
370         assertEquals( axes.length, expectedNumAxes );
371         assertEquals( positions.length, expectedNumPositions );
372     }
373
374     public void testOlapConnection() {
375         // create test metadata in the repository
376
createFoodmartSchemaResource();
377         createSugarCRMSchemaResource();
378         createFoodmartDataSourceResource();
379         createSugarCRMDataSourceResource();
380         createFoodmartMondrianConnectionResource();
381         createFoodmartXMLAConnectionResource();
382         createSugarCRMMondrianConnectionResource();
383         createSugarCRMTestMondrianConnectionResource();
384         createSugarCRMXMLAConnectionResource();
385         createFoodmartOlapUnit();
386         createSugarCRMOlapUnit();
387
388         createSugarCRMTestOlapUnit();
389     createSugarCRMXmlaOlapUnit();
390     createFoodmartMondrianXMLADefinitionResource();
391     createSugarCRMMondrianXMLADefinitionResource();
392
393     // test hitting mondrian directly
394
//executeSampleFoodmartQuery();
395
//executeSampleSugarQuery();
396
executeSampleSugarTestQuery();
397
398     // make some jpivot compatible olap models
399
//OlapModel sampleFoodmartModel1 = getModel( "Foodmart_sample_unit_1" );
400
//OlapModel sampleSugarModel1 = getModel( "SugarCRM_sample_unit_1" );
401
OlapModel sampleSugarModel2 = getModel( "SugarCRM_sample_unit_2" );
402     OlapModel sampleSugarXmlaModel2 = getModel( "SugarCRM_xmla_sample_unit_2" );
403
404     // call getResult on the models and assert they are good
405
assertOlapModelResult( sampleSugarModel2, 5 );
406
407     // server must be running for xmla model to work
408
// should move Xmla to remote-tests
409
//assertOlapModelResult( sampleSugarXmlaModel1, 5 );
410

411     //assertOlapModelResult( sampleSugarXmlaModel2, 5 );
412
}
413
414     private void assertOlapModelResult( OlapModel m, int expectedNumCells ) {
415     try {
416         m.initialize();
417         Result r = m.getResult();
418         log.info( "cell count = " + r.getCells().size() );
419         assertNotNull( r );
420         assertTrue( "sample query returned " + r.getCells().size() +
421             " cells, when expecting " + expectedNumCells,
422             r.getCells().size() == expectedNumCells );
423     } catch (OlapException e) {
424         log.error(e);
425         throw new RuntimeException JavaDoc(e);
426     }
427     }
428
429     public void testOlapUnitFind() {
430     ResourceLookup[] results
431         = getRepository().findResource(null, FilterCriteria.createFilter(OlapUnit.class));
432     assertTrue("got results: " + results.length + " when expecting 4", results.length == 4);
433     }
434
435     private OlapModel getModel( String JavaDoc viewName ) {
436     RepositoryService rep = getRepository();
437     OlapUnit unit = (OlapUnit)rep.getResource( null, "/olap/views/" + viewName );
438     return getConnectionService().createOlapModel( null, unit );
439     }
440
441     /**
442      * property: repository
443      */

444     private RepositoryService mRepository;
445     public RepositoryService getRepository() {
446         return mRepository;
447     }
448     public void setRepository(RepositoryService repository) {
449         mRepository = repository;
450     }
451
452     /**
453      * property: olapConnectionService
454      */

455     private OlapConnectionService mConnectionService;
456     public OlapConnectionService getConnectionService() {
457         return mConnectionService;
458     }
459     public void setConnectionService( OlapConnectionService cs ) {
460         mConnectionService = cs;
461     }
462
463 }
464
Popular Tags