# HG changeset patch # User Severin Gehwolf # Date 1375277760 -7200 # Node ID dd3c6928c4e61e6f0b25e30894789a9b63ecce1b # Parent 1925258e63090faf5ebe7cce9d0b351b4113fa57 Add null check for HostInfoDAO.getAliveHosts(). Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007686.html diff -r 1925258e6309 -r dd3c6928c4e6 storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java --- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Wed Jul 31 18:58:17 2013 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Wed Jul 31 15:36:00 2013 +0200 @@ -59,11 +59,26 @@ Key.AGENT_ID, hostNameKey, osNameKey, osKernelKey, cpuCountKey, cpuModelKey, hostMemoryTotalKey); + /** + * + * @param ref The host ref for which to get the HostInfo object for. + * @return The corresponding HostInfo object. May return null if the user + * is not permitted to retrieve this HostInfo. + */ HostInfo getHostInfo(HostRef ref); void putHostInfo(HostInfo info); + /** + * + * @return A collection of hosts, which may be empty. + */ Collection getHosts(); + + /** + * + * @return A collection of alive hosts which may be empty. + */ Collection getAliveHosts(); } diff -r 1925258e6309 -r dd3c6928c4e6 storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java --- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Wed Jul 31 18:58:17 2013 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Wed Jul 31 15:36:00 2013 +0200 @@ -145,7 +145,11 @@ List agentInfos = agentInfoDao.getAliveAgents(); for (AgentInformation agentInfo : agentInfos) { HostInfo hostInfo = getHostInfo(agentInfo.getAgentId()); - hosts.add(toHostRef(hostInfo)); + // getHostInfo may return null if user is not allowed to + // see the given host by ACL. + if (hostInfo != null) { + hosts.add(toHostRef(hostInfo)); + } } return hosts; diff -r 1925258e6309 -r dd3c6928c4e6 storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java --- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java Wed Jul 31 18:58:17 2013 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java Wed Jul 31 15:36:00 2013 +0200 @@ -251,14 +251,26 @@ verify(stmt).executeQuery(); } + @Test + public void getAliveHostsEmptyDueToHostInfoBeingNull() throws DescriptorParsingException, StatementExecutionException { + Triple> setup = setupForNullHostInfo(); + Storage storage = setup.first; + AgentInfoDAO agentInfoDao = setup.second; + PreparedStatement stmt = setup.third; + + HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfoDao); + Collection hosts = hostsDAO.getAliveHosts(); + + assertEquals(0, hosts.size()); + verify(storage).prepareStatement(anyDescriptor()); + verify(stmt).setString(0, "123"); + verify(stmt).executeQuery(); + } + private Triple> setupForSingleAliveHost() throws DescriptorParsingException, StatementExecutionException { // agents - - AgentInformation agentConfig1 = new AgentInformation(); - agentConfig1.setAgentId("123"); - agentConfig1.setAlive(true); AgentInformation agentInfo1 = new AgentInformation(); agentInfo1.setAgentId("123"); @@ -294,6 +306,35 @@ return new Triple<>(storage, agentDao, stmt); } + + private Triple> setupForNullHostInfo() + throws DescriptorParsingException, StatementExecutionException { + + // agents + + AgentInformation agentInfo1 = new AgentInformation(); + agentInfo1.setAgentId("123"); + agentInfo1.setAlive(true); + + // cursor + + @SuppressWarnings("unchecked") + Cursor cursor1 = mock(Cursor.class); + when(cursor1.hasNext()).thenReturn(false); + + // storage + + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement stmt = (PreparedStatement) mock(PreparedStatement.class); + when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); + when(stmt.executeQuery()).thenReturn(cursor1); + + AgentInfoDAO agentDao = mock(AgentInfoDAO.class); + when(agentDao.getAliveAgents()).thenReturn(Arrays.asList(agentInfo1)); + + return new Triple<>(storage, agentDao, stmt); + } @Test public void getAliveHost3() throws DescriptorParsingException, StatementExecutionException {