Search in sources :

Example 16 with DatasourceMgmtServiceException

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
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Test(org.junit.Test)

Example 17 with DatasourceMgmtServiceException

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
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Test(org.junit.Test)

Example 18 with DatasourceMgmtServiceException

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;
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)

Example 19 with DatasourceMgmtServiceException

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);
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) HashMap(java.util.HashMap) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Connection(java.sql.Connection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) IPluginResourceLoader(org.pentaho.platform.api.engine.IPluginResourceLoader) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) SimpleDataAccessPermissionHandler(org.pentaho.platform.dataaccess.datasource.wizard.service.impl.SimpleDataAccessPermissionHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Before(org.junit.Before)

Aggregations

DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)19 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)11 IDatasourceMgmtService (org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService)7 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)7 DataSource (javax.sql.DataSource)6 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)6 Before (org.junit.Before)5 DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)5 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)4 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 URL (java.net.URL)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 Matchers.anyString (org.mockito.Matchers.anyString)1 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)1