use of org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException in project data-access by pentaho.
the class ConnectionServiceImplTest method testGetConnectionByNameError.
@Test
public void testGetConnectionByNameError() throws Exception {
doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
DatasourceMgmtServiceException dmsException = mock(DatasourceMgmtServiceException.class);
doThrow(dmsException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceByName(CONN_NAME);
try {
connectionServiceImpl.getConnectionByName(CONN_NAME);
// This line should never be reached
fail();
} catch (ConnectionServiceException e) {
// Expected exception
}
}
use of org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException in project data-access by pentaho.
the class ConnectionServiceImplTest method testGetConnectionsError.
@Test
public void testGetConnectionsError() throws Exception {
doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
DatasourceMgmtServiceException mockDatasourceMgmtServiceException = mock(DatasourceMgmtServiceException.class);
doThrow(mockDatasourceMgmtServiceException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasources();
try {
connectionServiceImpl.getConnections();
// This line should never be reached
fail();
} catch (ConnectionServiceException e) {
// Expected exception
}
}
use of org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException in project data-access by pentaho.
the class ConnectionServiceImpl method getConnections.
public List<IDatabaseConnection> getConnections(boolean hidePassword) throws ConnectionServiceException {
ensureDataAccessPermission();
List<IDatabaseConnection> connectionList = null;
try {
connectionList = datasourceMgmtSvc.getDatasources();
for (IDatabaseConnection conn : connectionList) {
sanitizer.unsanitizeConnectionParameters(conn);
if (hidePassword) {
conn.setPassword(null);
}
}
} catch (DatasourceMgmtServiceException dme) {
String message = Messages.getErrorString(// $NON-NLS-1$
"ConnectionServiceImpl.ERROR_0002_UNABLE_TO_GET_CONNECTION_LIST", dme.getLocalizedMessage());
logger.error(message);
throw new ConnectionServiceException(message, dme);
}
return connectionList;
}
use of org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException in project data-access by pentaho.
the class DatasourceInMemoryServiceHelperTest method setUp.
@Before
public void setUp() throws Exception {
URL resource = getClass().getClassLoader().getResource(getClass().getPackage().getName().replace(".", "/") + "/DatasourceInMemoryServiceHelperTest.csv");
csvFile = resource.getPath();
sqlConnection = mock(Connection.class);
when(resultSet.getMetaData()).thenReturn(rsMetaData);
when(statment.executeQuery(eq(SAMPLE_VALID_QUERY))).thenReturn(resultSet);
when(sqlConnection.createStatement(anyInt(), anyInt())).thenReturn(statment);
Map<String, String> attributesPrivateDriver = new HashMap<>();
attributesPrivateDriver.put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, PrivateSQLDriver.class.getName());
when(genericConnPrivateDriver.getAttributes()).thenReturn(attributesPrivateDriver);
when(genericConnPrivateDriver.getDatabaseType()).thenReturn(genericDBType);
Map<String, String> attributesWithNoExistClass = new HashMap<>();
attributesWithNoExistClass.put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.test.NoExistDriver");
when(genericConnWithNoExistClass.getAttributes()).thenReturn(attributesWithNoExistClass);
when(genericConnWithNoExistClass.getDatabaseType()).thenReturn(genericDBType);
Map<String, String> attributesNonDriverClass = new HashMap<>();
attributesNonDriverClass.put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, String.class.getName());
when(genericConnWithNoDriverClass.getAttributes()).thenReturn(attributesNonDriverClass);
when(genericConnWithNoDriverClass.getDatabaseType()).thenReturn(genericDBType);
Map<String, String> attributesWithoutClass = new HashMap<>();
attributesWithoutClass.put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "");
when(genericConnWithoutClass.getAttributes()).thenReturn(attributesWithoutClass);
when(genericConnWithoutClass.getDatabaseType()).thenReturn(genericDBType);
Map<String, String> attributesWithClass = new HashMap<>();
attributesWithClass.put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, TestSQLDriver.class.getName());
when(genericConnWithClass.getAttributes()).thenReturn(attributesWithClass);
when(genericConnWithClass.getDatabaseType()).thenReturn(genericDBType);
when(service.getDatasourceByName(anyString())).thenAnswer(new Answer<IDatabaseConnection>() {
@Override
public IDatabaseConnection answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].equals(GENERIC_CONN_PRIVATE_DRIVER)) {
return genericConnPrivateDriver;
}
if (invocation.getArguments()[0].equals(GENERIC_CONN_NOT_DRIVER_CLASS)) {
return genericConnWithNoDriverClass;
}
if (invocation.getArguments()[0].equals(GENERIC_CONN_NOT_FOUND_CLASS)) {
return genericConnWithNoExistClass;
}
if (invocation.getArguments()[0].equals(GENERIC_CONN_WITHOUT_DRIVER_CLASS)) {
return genericConnWithoutClass;
}
if (invocation.getArguments()[0].equals(GENERIC_CONN_DRIVER_CLASS)) {
return genericConnWithClass;
}
if (invocation.getArguments()[0].equals(CONN_CAN_NOT_PREPARED)) {
return null;
}
// throw exception for check if we get exception from getting connection
throw new DatasourceMgmtServiceException();
}
});
when(resLoader.getPluginSetting(this.anyClass(), anyString(), anyString())).thenReturn(SimpleDataAccessPermissionHandler.class.getName());
when(policy.isAllowed(anyString())).thenReturn(true);
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(IDatasourceMgmtService.class)) {
return service;
}
if (invocation.getArguments()[0].equals(IAuthorizationPolicy.class)) {
return policy;
}
if (invocation.getArguments()[0].equals(IPluginResourceLoader.class)) {
return resLoader;
}
return null;
}
});
PentahoSystem.registerObjectFactory(pentahoObjectFactory);
}
Aggregations