@Test(expected = ConnectionServiceException.class)
 public void testIsConnectionExist_Error() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doThrow(mock(DatasourceMgmtServiceException.class))
       .when(connectionServiceImpl.datasourceMgmtSvc)
       .getDatasourceByName(CONN_NAME);
   assertTrue(connectionServiceImpl.isConnectionExist(CONN_NAME));
 }
 @Test
 public void testIsConnectionExist_true() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doReturn(mockDBConnection)
       .when(connectionServiceImpl.datasourceMgmtSvc)
       .getDatasourceByName(CONN_NAME);
   assertTrue(connectionServiceImpl.isConnectionExist(CONN_NAME));
 }
  @Test
  public void testDeleteConnectionConn() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    doNothing().when(connectionServiceImpl.datasourceMgmtSvc).deleteDatasourceByName(CONN_NAME);

    assertTrue(connectionServiceImpl.deleteConnection(mockDBConnection));
    verify(datasourceService).clearDataSource(CONN_NAME);
  }
 @Test
 public void testGetConnections() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   List<IDatabaseConnection> mockConnectionList = Collections.emptyList();
   doReturn(mockConnectionList).when(connectionServiceImpl.datasourceMgmtSvc).getDatasources();
   List<IDatabaseConnection> connectionList = connectionServiceImpl.getConnections();
   verify(connectionServiceImpl).getConnections();
   assertEquals(connectionList, mockConnectionList);
 }
  @Test
  public void testUpdateConnection() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    doReturn("").when(connectionServiceImpl).getConnectionPassword(anyString(), anyString());

    assertTrue(connectionServiceImpl.updateConnection(mockDBConnection));

    verify(connectionServiceImpl).updateConnection(mockDBConnection);
    verify(datasourceService).clearDataSource(CONN_NAME);
  }
 private void testAddConnectionError(Exception ex) throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doThrow(ex).when(connectionServiceImpl.datasourceMgmtSvc).createDatasource(mockDBConnection);
   try {
     connectionServiceImpl.addConnection(mockDBConnection);
     fail(); // This line should never be reached
   } catch (ConnectionServiceException e) {
     // Expected exception
   }
 }
 @Test
 public void testGetConnectionByName() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doReturn(mockDBConnection)
       .when(connectionServiceImpl.datasourceMgmtSvc)
       .getDatasourceByName(CONN_NAME);
   IDatabaseConnection connection = connectionServiceImpl.getConnectionByName(CONN_NAME);
   verify(connectionServiceImpl).getConnectionByName(CONN_NAME);
   assertEquals(mockDBConnection, connection);
 }
 private void testDeleteConnectionErrorString(Exception ex) throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doThrow(ex).when(connectionServiceImpl.datasourceMgmtSvc).deleteDatasourceByName(CONN_NAME);
   try {
     connectionServiceImpl.deleteConnection(CONN_NAME);
     fail(); // This line should never be reached
   } catch (ConnectionServiceException e) {
     // Expected exception
   }
 }
 @Test
 public void testGetConnectionByNameError_NullDataSource() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doReturn(null).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceByName(CONN_NAME);
   try {
     connectionServiceImpl.getConnectionByName(CONN_NAME);
     fail(); // This line should never be reached
   } catch (ConnectionServiceException e) {
     // Expected exception
   }
 }
 @Test
 public void testGetConnectionByIdError() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   DatasourceMgmtServiceException dmsException = mock(DatasourceMgmtServiceException.class);
   doThrow(dmsException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceById(CONN_ID);
   try {
     connectionServiceImpl.getConnectionById(CONN_ID);
     fail(); // This line should never be reached
   } catch (ConnectionServiceException e) {
     // Expected exception
   }
 }
  @Before
  public void setUp()
      throws ConnectionServiceException, ObjectFactoryException, DBDatasourceServiceException {
    doReturn(nativeConnection).when(sqlConnection).getNativeConnection();
    doReturn(SimpleDataAccessPermissionHandler.class.getName())
        .when(loader)
        .getPluginSetting(this.anyClass(), anyString(), anyString());

    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(IPluginResourceLoader.class)) {
                  return loader;
                }
                if (invocation.getArguments()[0].equals(IPentahoConnection.class)) {
                  return sqlConnection;
                }
                if (invocation.getArguments()[0].equals(IDBDatasourceService.class)) {
                  return datasourceService;
                }
                if (invocation.getArguments()[0].equals(PooledDatasourceHelper.class)) {
                  return pdh;
                }
                return null;
              }
            });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);

    doReturn(databaseType).when(mockDBConnection).getDatabaseType();
    doReturn(CONN_NAME).when(mockDBConnection).getName();
    doNothing().when(mockDBConnection).setPassword(anyString());

    connectionServiceImpl = spy(new ConnectionServiceImpl());
    connectionServiceImpl.datasourceMgmtSvc = mock(IDatasourceMgmtService.class);
    connectionServiceImpl.dialectService = mock(DatabaseDialectService.class);
    connectionServiceImpl.datasourceService = datasourceService;
  }
 private void testUpdateConnectionError(Exception ex) throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   doReturn("").when(connectionServiceImpl).getConnectionPassword(anyString(), anyString());
   doThrow(ex)
       .when(connectionServiceImpl.datasourceMgmtSvc)
       .updateDatasourceByName(CONN_NAME, mockDBConnection);
   try {
     connectionServiceImpl.updateConnection(mockDBConnection);
     fail(); // This line should never be reached
   } catch (ConnectionServiceException e) {
     // Expected exception
   }
   verify(connectionServiceImpl).updateConnection(mockDBConnection);
 }
  private void testTestConnection(
      DatabaseAccessType accessType,
      IDatabaseConnection connection,
      String database,
      boolean isPool)
      throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    doReturn("connectionPassword")
        .when(connectionServiceImpl)
        .getConnectionPassword(anyString(), anyString());

    IDatabaseDialect dialect = mock(IDatabaseDialect.class);
    doReturn("NativeDriver").when(dialect).getNativeDriver();
    doReturn(dialect)
        .when(connectionServiceImpl.dialectService)
        .getDialect(any(IDatabaseConnection.class));
    doReturn(database).when(databaseType).getShortName();
    doReturn(accessType).when(mockDBConnection).getAccessType();
    doReturn("DATABASENAME").when(mockDBConnection).getDatabaseName();
    doReturn(isPool).when(mockDBConnection).isUsingConnectionPool();
    assertTrue(connectionServiceImpl.testConnection(connection));
    verify(sqlConnection).close();
  }
 @Test
 public void testIsConnectionExist_false() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   assertFalse(connectionServiceImpl.isConnectionExist(CONN_NAME));
 }
 @Test
 public void testAddConnection() throws Exception {
   doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
   assertTrue(connectionServiceImpl.addConnection(mockDBConnection));
 }