1 21 package com.jaspersoft.jasperserver.util.test; 22 23 27 28 import java.io.BufferedInputStream ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.util.Properties ; 34 35 import junit.framework.TestCase; 36 import junit.textui.TestRunner; 37 38 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext; 39 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl; 40 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService; 41 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType; 42 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 43 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder; 44 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl; 45 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query; 46 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource; 47 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference; 48 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.DataTypeImpl; 49 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.FolderImpl; 50 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 51 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryService; 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.ReportUnit; 55 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.client.ReportUnitImpl; 56 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.impl.RepoReportUnit; 57 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection; 58 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition; 59 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit; 60 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection; 61 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService; 62 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService; 63 import com.jaspersoft.jasperserver.util.DataSourceBean; 64 import com.jaspersoft.jasperserver.util.ExportResource; 65 import com.jaspersoft.jasperserver.util.FileResourceBean; 66 import com.jaspersoft.jasperserver.util.ImportResource; 67 import com.jaspersoft.jasperserver.util.ReportUnitBean; 68 69 import org.hibernate.Session; 70 import org.hibernate.SessionFactory; 71 import org.springframework.context.ApplicationContext; 72 import org.springframework.context.support.ClassPathXmlApplicationContext; 73 import org.springframework.orm.hibernate3.HibernateTemplate; 74 import org.springframework.orm.hibernate3.SessionFactoryUtils; 75 import org.springframework.orm.hibernate3.SessionHolder; 76 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 77 import org.springframework.transaction.support.TransactionSynchronizationManager; 78 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 79 80 public class ExportOlapTest extends TestCase { 81 82 static final String PATH_SEP = "/"; 83 static final String LABEL = "_label"; 84 static final String DESC = "_description"; 85 static final String DESC_TEXT = " for export-import testing"; 86 87 protected final String foodMartSchemaURI = "/queries/Foodmart.xml"; 88 protected final String sugarCRMSchemaURI = "/queries/SugarCRMOpps.xml"; 89 90 final String OLAP_FOLDER = "test_olap_05"; 91 final String OLAP_REPORT_NAME = "OlapSugarMondrianView"; 92 93 private RepositoryService repo; 94 private OlapConnectionService olapConn; 95 96 UserAuthorityService userAuthService = null; private Properties jdbcProps; 98 private ExecutionContext context; 99 100 private String SAMPLE_SUGAR_CRM_MDX_QUERY = 101 "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, " + 102 " NON EMPTY {([Account Categorization].[All Accounts], [Close Period].[All Periods])} ON ROWS " + 103 " from [SalesAnalysis] " + 104 " where [Sale State].[All Types].[Closed Won]"; 105 106 public ExportOlapTest(String name) { 107 super(name); 108 } 109 110 public static void main(String [] args) { 111 TestRunner.run(ExportOlapTest.class); 112 } 113 114 public void setUp() throws Exception { 115 System.out.println("ExportOlapTest: setUp"); 116 117 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( 118 new String [] {"hibernateConfig.xml", "viewService.xml"}); 119 120 repo = (RepositoryService) appContext.getBean("repoService"); 121 122 olapConn = (OlapConnectionService) appContext.getBean("olapConnectionService"); 123 124 loadJdbcProps(); 125 } 126 127 public void tearDown() { 128 System.out.println("ExportOlapTest: tearDown"); 129 } 130 131 132 137 public void testOlapView() { 138 139 createOlapConnectionSimpleResources(); 140 141 ExportResource exporter = new ExportResource(repo, 142 userAuthService, 143 null, 144 context, 145 PATH_SEP + OLAP_FOLDER + PATH_SEP + OLAP_REPORT_NAME, 146 false, 147 null, 148 null, 149 false, 150 null, 151 ExportResource.CATALOG_DIR_NAME, 152 ExportResource.CATALOG_FILE_NAME); 153 154 exporter.process(); 155 156 157 deleteOlapConnectionSimpleResources(); 158 159 OlapUnit ou_null = (OlapUnit) repo.getResource(null, 161 PATH_SEP + OLAP_FOLDER + PATH_SEP + OLAP_REPORT_NAME); 162 assertNull(ou_null); 163 164 165 ImportResource importer = new ImportResource(repo, 166 null, 167 null, 168 context, 169 ExportResource.CATALOG_DIR_NAME, 170 ExportResource.CATALOG_FILE_NAME, 171 ""); 172 173 importer.process(); 174 175 OlapUnit unit = (OlapUnit) repo.getResource(null, 177 PATH_SEP + OLAP_FOLDER + PATH_SEP + OLAP_REPORT_NAME); 178 assertNotNull(unit); 179 180 deleteOlapConnectionSimpleResources(); 181 } 182 183 protected void createOlapConnectionSimpleResources() { 184 185 Folder folder = new FolderImpl(); 186 folder.setName(OLAP_FOLDER); 187 folder.setLabel(OLAP_FOLDER + LABEL); 188 folder.setDescription(OLAP_FOLDER + DESC); 189 folder.setParentFolder("/"); 190 repo.saveFolder(null, folder); 191 192 FileResource schema = (FileResource) repo.newResource( null, FileResource.class ); 193 schema.setName("SugarCRMSchema"); 194 schema.setLabel("SugarCRM Opportunities DataMart"); 195 schema.setDescription("SugarCRM Opportunities Data Mart Schema"); 196 schema.setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA); 197 InputStream in = getClass().getResourceAsStream(sugarCRMSchemaURI); 198 schema.readData(in); 199 olapConn.saveResource( null, PATH_SEP + OLAP_FOLDER + PATH_SEP, schema ); 200 201 JdbcReportDataSource ds = 202 (JdbcReportDataSource) repo.newResource( null, JdbcReportDataSource.class ); 203 ds.setName("SugarCRMDataSource"); 204 ds.setLabel("SugarCRM Data Source"); 205 ds.setDescription("SugarCRM Data Source"); 206 ds.setDriverClass(jdbcProps.getProperty("test.jdbc.driverClassName")); 207 ds.setConnectionUrl(jdbcProps.getProperty("test.jdbc.url")); 208 ds.setUsername(jdbcProps.getProperty("test.jdbc.username")); 209 ds.setPassword(jdbcProps.getProperty("test.jdbc.password")); 210 olapConn.saveResource( null, PATH_SEP + OLAP_FOLDER + PATH_SEP, ds ); 211 212 MondrianConnection mondConn = 213 (MondrianConnection) repo.newResource( null, MondrianConnection.class ); 214 mondConn.setName("SugarCRM"); 215 mondConn.setLabel("SugarCRM OLAP Connection"); 216 mondConn.setDescription("SugarCRM OLAP Connection: only opportunities"); 217 mondConn.setSchemaReference(PATH_SEP + OLAP_FOLDER + PATH_SEP + "SugarCRMSchema"); 218 mondConn.setDataSourceReference(PATH_SEP + OLAP_FOLDER + PATH_SEP + "SugarCRMDataSource"); 219 olapConn.saveResource(null, PATH_SEP + OLAP_FOLDER + PATH_SEP, mondConn ); 220 221 XMLAConnection xmlaConn = (XMLAConnection) repo.newResource( null, XMLAConnection.class ); 222 xmlaConn.setName("SugarCRMXmlaConnection"); 223 xmlaConn.setLabel("SugarCRM XML/A Connection"); 224 xmlaConn.setDescription("SugarCRM XML/A Connection"); 225 xmlaConn.setCatalog("SugarCRM"); 226 xmlaConn.setDataSource("Provider=Mondrian;DataSource=SugarCRM;"); 227 xmlaConn.setURI("http://localhost:8080/jasperserver/xmla"); 228 xmlaConn.setUsername("tomcat"); 229 xmlaConn.setPassword("tomcat"); 230 olapConn.saveResource(null, PATH_SEP + OLAP_FOLDER + PATH_SEP, xmlaConn ); 231 232 MondrianXMLADefinition def = 233 (MondrianXMLADefinition) repo.newResource( null, MondrianXMLADefinition.class ); 234 def.setName("SugarCRMXmlaDefinition"); 235 def.setLabel("SugarCRM XMLA Connection"); 236 def.setDescription("SugarCRM XMLA Connection"); 237 def.setCatalog("SugarCRM"); 238 def.setMondrianConnectionReference(PATH_SEP + OLAP_FOLDER + PATH_SEP + "SugarCRM"); 239 240 olapConn.saveResource( null, PATH_SEP + OLAP_FOLDER + PATH_SEP, def ); 241 242 OlapUnit view = (OlapUnit) repo.newResource( null, OlapUnit.class ); 244 view.setName(OLAP_REPORT_NAME); 245 view.setLabel("SugarCRM sample unit 1"); 246 view.setDescription("SugarCRM Sample Olap Unit 1: Sales Performance by Industry/Account"); 247 view.setOlapClientConnectionReference(PATH_SEP + OLAP_FOLDER + PATH_SEP + "SugarCRM"); 248 view.setMdxQuery(SAMPLE_SUGAR_CRM_MDX_QUERY); 249 olapConn.saveResource(null, PATH_SEP + OLAP_FOLDER + PATH_SEP, view ); 250 } 251 252 protected void deleteOlapConnectionSimpleResources() { 253 254 repo.deleteFolder(null, PATH_SEP + OLAP_FOLDER); 255 } 256 257 258 public void setConnectionService(OlapConnectionService cs) { 259 260 } 261 262 protected Properties loadJdbcProps() throws IOException , FileNotFoundException { 263 jdbcProps = new Properties (); 264 String jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties"); 265 BufferedInputStream is = new BufferedInputStream (new FileInputStream (jdbcPropFile)); 266 jdbcProps.load(is); 267 is.close(); 268 return jdbcProps; 269 } 270 271 } 272 | Popular Tags |