1 21 22 package com.jaspersoft.jasperserver.api.metadata; 23 24 import java.io.BufferedInputStream ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.Properties ; 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 51 52 public class OlapClientTest extends TestCase { 53 54 protected final Log log = LogFactory.getLog(this.getClass()); 55 56 protected final String OLAP_SCHEMAS = "/olap/schemas/"; 57 58 protected final String OLAP_DATASOURCES = "/olap/datasources/"; 59 60 protected final String OLAP_CONNECTIONS = "/olap/connections/"; 61 62 protected final String OLAP_DEFINITIONS = "/olap/xmla/definitions/"; 63 64 protected final String OLAP_VIEWS = "/olap/views/"; 65 66 protected final String MDX_QUERY = 67 "SELECT {[Measures].[Unit Sales]} ON COLUMNS, " + 68 "{[Time].[1997].[Q1]} ON ROWS FROM [Sales]"; 69 70 protected final String XML_SCHEMA = "/olap/schemas/Foodmart.xml"; 71 72 private Properties jdbcProps; 73 74 private RepositoryService repositoryService; 75 76 private OlapConnectionService connectionService; 77 78 protected void setUp() throws Exception 79 { 80 ClassPathXmlApplicationContext appContext = 81 new ClassPathXmlApplicationContext(new String [] {"hibernateConfig.xml", "viewService.xml"}); 82 83 setRepositoryService((RepositoryService) appContext.getBean("repoService")); 84 85 setConnectionService((OlapConnectionService) appContext.getBean("olapConnectionService")); 86 87 loadJdbcProps(); 88 } 89 90 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 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 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 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 strDatasourceSugarCrm = "SugarCRMDataSource"; 143 144 datasourceSugarCrm = 145 (ReportDataSource) repositoryService.getResource(null, OLAP_DATASOURCES + strDatasourceSugarCrm); 146 147 assertTrue(datasourceSugarCrm.getURIString().equals("/olap/datasources/SugarCRMDataSource")); 148 149 } 167 168 static String 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 ((MondrianConnection) connectionOriginal).setSchemaReference(schemaSugarCrm.getURIString()); 196 197 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 ((MondrianConnection) connectionOriginal).setDataSourceReference(datasourceSugarCrm.getURIString()); 221 222 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 strDefinition = "FoodmartXmlaDefinition"; 243 244 MondrianXMLADefinition definition = 245 (MondrianXMLADefinition) repositoryService.getResource(null, "/olap/xmla/definitions/" + strDefinition); 247 248 assertNotNull(definition.getURIString()); } 250 251 public void testUpdateXmlaConnection() 252 { 253 String strConnection = "FoodmartXmlaConnection"; 254 255 OlapClientConnection connection = 256 (OlapClientConnection) repositoryService.getResource(null, "/olap/connections/" + strConnection); 258 259 assertNotNull(connection); 260 } 261 262 public void testUpdateOlapModel() 263 { 264 String olapViewRef = "/olap/connections/Foodmart"; 265 266 String 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 } 281 282 public void testUpdateOlapView() 283 { 284 String 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 297 protected Properties loadJdbcProps() throws IOException , FileNotFoundException 298 { 299 jdbcProps = new Properties (); 300 301 String jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties"); 302 303 BufferedInputStream is = new BufferedInputStream (new FileInputStream (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 |