use of org.pentaho.metadata.model.SqlDataSource in project data-access by pentaho.
the class GuiStateModelTest method test.
@Test
public void test() {
GuiStateModel guiStateModel = new GuiStateModel();
Assert.assertEquals(0, guiStateModel.getConnections().size());
Assert.assertEquals(false, guiStateModel.isRelationalValidated());
IDatabaseConnection connection = new DatabaseConnection();
connection.setAccessType(DatabaseAccessType.NATIVE);
// connection.setDriverClass("org.hsqldb.jdbcDriver");
connection.setName("SampleData");
connection.setPassword("password");
// connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
connection.setUsername("pentaho_user");
List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
connectionList.add(connection);
guiStateModel.setConnections(connectionList);
guiStateModel.setPreviewLimit("10");
guiStateModel.validateRelational();
Assert.assertEquals(true, guiStateModel.isRelationalPreviewValidated());
// Assert.assertEquals(false, relationalModel.isValidated());
LogicalColumn logColumn = new LogicalColumn();
logColumn.setDataType(DataType.NUMERIC);
List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
aggTypeList.add(AggregationType.AVERAGE);
logColumn.setAggregationList(aggTypeList);
logColumn.setName(new LocalizedString("En", "Column1"));
BusinessData businessData = new BusinessData();
List<List<String>> dataSample = new ArrayList<List<String>>();
List<String> rowData = new ArrayList<String>();
rowData.add("Data1");
rowData.add("Data2");
rowData.add("Data3");
rowData.add("Data4");
dataSample.add(rowData);
String locale = LocaleHelper.getLocale().toString();
SqlPhysicalModel model = new SqlPhysicalModel();
SqlDataSource dataSource = new SqlDataSource();
dataSource.setDatabaseName("SampleData");
model.setDatasource(dataSource);
SqlPhysicalTable table = new SqlPhysicalTable(model);
model.getPhysicalTables().add(table);
table.setTargetTableType(TargetTableType.INLINE_SQL);
table.setTargetTable("select * from customers");
SqlPhysicalColumn column = new SqlPhysicalColumn(table);
column.setTargetColumn("customername");
column.setName(new LocalizedString(locale, "Customer Name"));
column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
column.setDataType(DataType.STRING);
table.getPhysicalColumns().add(column);
LogicalModel logicalModel = new LogicalModel();
model.setId("MODEL");
model.setName(new LocalizedString(locale, "My Model"));
model.setDescription(new LocalizedString(locale, "A Description of the Model"));
LogicalTable logicalTable = new LogicalTable();
logicalTable.setPhysicalTable(table);
logicalModel.getLogicalTables().add(logicalTable);
LogicalColumn logicalColumn = new LogicalColumn();
logicalColumn.setId("LC_CUSTOMERNAME");
logicalColumn.setPhysicalColumn(column);
logicalTable.addLogicalColumn(logicalColumn);
Category mainCategory = new Category();
mainCategory.setId("CATEGORY");
mainCategory.setName(new LocalizedString(locale, "Category"));
mainCategory.addLogicalColumn(logicalColumn);
logicalModel.getCategories().add(mainCategory);
Domain domain = new Domain();
domain.addPhysicalModel(model);
domain.addLogicalModel(logicalModel);
List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
localeTypeList.add(new LocaleType("Code", "Locale Description"));
domain.setLocales(localeTypeList);
businessData.setData(dataSample);
businessData.setDomain(domain);
guiStateModel.setLogicalModels(domain.getLogicalModels());
guiStateModel.setLocaleCode("en");
Assert.assertEquals(true, guiStateModel.isRelationalValidated());
}
use of org.pentaho.metadata.model.SqlDataSource in project data-access by pentaho.
the class DSWDatasourceServiceImplTest method setUp.
@Before
public void setUp() throws Exception {
SqlDataSource dataSource = new SqlDataSource();
dataSource.setDatabaseName(CONNECTION_NAME);
SqlPhysicalTable sqlTable = new SqlPhysicalTable();
sqlTable.setTargetTable(VALID_QUERY);
SqlPhysicalModel sqlModel = new SqlPhysicalModel();
sqlModel.addPhysicalTable(sqlTable);
sqlModel.setDatasource(dataSource);
analysisModel = new LogicalModel();
analysisModel.setId(LOGICAL_MODEL_ID_ANALYSIS);
analysisModel.setProperty(DSWDatasourceServiceImpl.LM_PROP_VISIBLE, LOGICAL_MODEL_CONTEXTNAME);
reportingModel = new LogicalModel();
reportingModel.setId(LOGICAL_MODEL_ID_REPORTING);
domain2Models = new Domain();
domain2Models.setId(DOMAIN_ID_2MODELS);
domain2Models.addLogicalModel(analysisModel);
domain2Models.addLogicalModel(reportingModel);
domain2Models.setLocales(Arrays.asList(new LocaleType("en_US", "Test locale")));
domain2Models.addPhysicalModel(sqlModel);
Set<String> domains = new TreeSet<String>();
domains.add(DOMAIN_ID_2MODELS);
doReturn(domain2Models).when(domainRepository).getDomain(DOMAIN_ID_2MODELS);
doReturn(domains).when(domainRepository).getDomainIds();
doAnswer(new Answer<Object>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
final String modelId = (String) invocation.getArguments()[1];
final LogicalModel modelToRemove = domain2Models.findLogicalModel(modelId);
domain2Models.getLogicalModels().remove(modelToRemove);
return null;
}
}).when(domainRepository).removeModel(anyString(), anyString());
workspace2Models = mock(ModelerWorkspace.class);
when(workspace2Models.getLogicalModel(ModelerPerspective.ANALYSIS)).thenReturn(analysisModel);
when(workspace2Models.getLogicalModel(ModelerPerspective.REPORTING)).thenReturn(reportingModel);
dswService = spy(new DSWDatasourceServiceImpl(mock(ConnectionServiceImpl.class)));
doNothing().when(dswService).checkSqlQueriesSupported(anyString());
dswService.setMetadataDomainRepository(domainRepository);
Object[][] coumnHeaders = new Object[][] { columns };
SQLMetaData metadata = mock(SQLMetaData.class);
when(metadata.getColumnHeaders()).thenReturn(coumnHeaders);
when(metadata.getJDBCColumnTypes()).thenReturn(columnTypes);
IPentahoResultSet resultSet = mock(IPentahoResultSet.class);
when(resultSet.getMetaData()).thenReturn(metadata);
doReturn(resultSet).when(sqlConnection).executeQuery(matches("(.*" + VALID_QUERY + ".*)"));
when(sqlConnection.executeQuery(matches("(.*" + QUERY_COLUMN_ALREADY_EXIST + ".*)"))).thenThrow(new SQLException("Reason", "S0021", 21));
doReturn(nativeConnection).when(sqlConnection).getNativeConnection();
MondrianCatalog catalog = mock(MondrianCatalog.class);
doReturn(catalog).when(mondrianService).getCatalog(anyString(), any(IPentahoSession.class));
pentahoObjectFactory = mock(IPentahoObjectFactory.class);
when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
return mondrianService;
}
if (invocation.getArguments()[0].equals(IPentahoConnection.class)) {
return sqlConnection;
}
if (invocation.getArguments()[0].equals(IMetadataDomainRepository.class)) {
return domainRepository;
}
return null;
}
});
PentahoSystem.registerObjectFactory(pentahoObjectFactory);
IPentahoSession pentahoSessionMock = mock(IPentahoSession.class);
when(pentahoSessionMock.getName()).thenReturn("sessionName");
PentahoSessionHolder.setSession(pentahoSessionMock);
}
Aggregations