@Test public void testCheckForHostsWithoutState() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final DBAccessor mockDBDbAccessor = easyMockSupport.createNiceMock(DBAccessor.class); final Connection mockConnection = easyMockSupport.createNiceMock(Connection.class); final ResultSet mockResultSet = easyMockSupport.createNiceMock(ResultSet.class); final Statement mockStatement = easyMockSupport.createNiceMock(Statement.class); final StackManagerFactory mockStackManagerFactory = easyMockSupport.createNiceMock(StackManagerFactory.class); final EntityManager mockEntityManager = easyMockSupport.createNiceMock(EntityManager.class); final Clusters mockClusters = easyMockSupport.createNiceMock(Clusters.class); final OsFamily mockOSFamily = easyMockSupport.createNiceMock(OsFamily.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(StackManagerFactory.class).toInstance(mockStackManagerFactory); bind(EntityManager.class).toInstance(mockEntityManager); bind(DBAccessor.class).toInstance(mockDBDbAccessor); bind(Clusters.class).toInstance(mockClusters); bind(OsFamily.class).toInstance(mockOSFamily); } }); expect(mockConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)).andReturn(mockStatement); expect(mockStatement.executeQuery("select host_name from hosts where host_id not in (select host_id from hoststate)")).andReturn(mockResultSet); DatabaseConsistencyCheckHelper.setInjector(mockInjector); DatabaseConsistencyCheckHelper.setConnection(mockConnection); easyMockSupport.replayAll(); DatabaseConsistencyCheckHelper.checkForHostsWithoutState(); easyMockSupport.verifyAll(); }
static void checkForHostsWithoutState() { LOG.info("Checking for hosts without state"); String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where host_id not in (select host_id from hoststate)"; Set<String> hostsWithoutStatus = new HashSet<>(); ResultSet rs = null; Statement statement = null; ensureConnection(); try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY); if (rs != null) { while (rs.next()) { hostsWithoutStatus.add(rs.getString("host_name")); } if (!hostsWithoutStatus.isEmpty()) { error("You have host(s) without state (in hoststate table): " + StringUtils.join(hostsWithoutStatus, ",")); } } } catch (SQLException e) { LOG.error("Exception occurred during check for host without state procedure: ", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("Exception occurred during result set closing procedure: ", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { LOG.error("Exception occurred during statement closing procedure: ", e); } } } }
DatabaseConsistencyCheckHelper { static void checkForHostsWithoutState() { LOG.info("Checking for hosts without state"); String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where host_id not in (select host_id from hoststate)"; Set<String> hostsWithoutStatus = new HashSet<>(); ResultSet rs = null; Statement statement = null; ensureConnection(); try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY); if (rs != null) { while (rs.next()) { hostsWithoutStatus.add(rs.getString("host_name")); } if (!hostsWithoutStatus.isEmpty()) { error("You have host(s) without state (in hoststate table): " + StringUtils.join(hostsWithoutStatus, ",")); } } } catch (SQLException e) { LOG.error("Exception occurred during check for host without state procedure: ", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("Exception occurred during result set closing procedure: ", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { LOG.error("Exception occurred during statement closing procedure: ", e); } } } } }
DatabaseConsistencyCheckHelper { static void checkForHostsWithoutState() { LOG.info("Checking for hosts without state"); String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where host_id not in (select host_id from hoststate)"; Set<String> hostsWithoutStatus = new HashSet<>(); ResultSet rs = null; Statement statement = null; ensureConnection(); try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY); if (rs != null) { while (rs.next()) { hostsWithoutStatus.add(rs.getString("host_name")); } if (!hostsWithoutStatus.isEmpty()) { error("You have host(s) without state (in hoststate table): " + StringUtils.join(hostsWithoutStatus, ",")); } } } catch (SQLException e) { LOG.error("Exception occurred during check for host without state procedure: ", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("Exception occurred during result set closing procedure: ", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { LOG.error("Exception occurred during statement closing procedure: ", e); } } } }  }
DatabaseConsistencyCheckHelper { static void checkForHostsWithoutState() { LOG.info("Checking for hosts without state"); String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where host_id not in (select host_id from hoststate)"; Set<String> hostsWithoutStatus = new HashSet<>(); ResultSet rs = null; Statement statement = null; ensureConnection(); try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY); if (rs != null) { while (rs.next()) { hostsWithoutStatus.add(rs.getString("host_name")); } if (!hostsWithoutStatus.isEmpty()) { error("You have host(s) without state (in hoststate table): " + StringUtils.join(hostsWithoutStatus, ",")); } } } catch (SQLException e) { LOG.error("Exception occurred during check for host without state procedure: ", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("Exception occurred during result set closing procedure: ", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { LOG.error("Exception occurred during statement closing procedure: ", e); } } } }  static DatabaseConsistencyCheckResult getLastCheckResult(); static void resetCheckResult(); static void setInjector(Injector injector); static void setConnection(Connection connection); static void closeConnection(); static DatabaseConsistencyCheckResult runAllDBChecks(boolean fixIssues); static void checkDBVersionCompatible(); }
DatabaseConsistencyCheckHelper { static void checkForHostsWithoutState() { LOG.info("Checking for hosts without state"); String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where host_id not in (select host_id from hoststate)"; Set<String> hostsWithoutStatus = new HashSet<>(); ResultSet rs = null; Statement statement = null; ensureConnection(); try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY); if (rs != null) { while (rs.next()) { hostsWithoutStatus.add(rs.getString("host_name")); } if (!hostsWithoutStatus.isEmpty()) { error("You have host(s) without state (in hoststate table): " + StringUtils.join(hostsWithoutStatus, ",")); } } } catch (SQLException e) { LOG.error("Exception occurred during check for host without state procedure: ", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("Exception occurred during result set closing procedure: ", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { LOG.error("Exception occurred during statement closing procedure: ", e); } } } }  static DatabaseConsistencyCheckResult getLastCheckResult(); static void resetCheckResult(); static void setInjector(Injector injector); static void setConnection(Connection connection); static void closeConnection(); static DatabaseConsistencyCheckResult runAllDBChecks(boolean fixIssues); static void checkDBVersionCompatible();  }