Mercurial > hg > release > thermostat-0.13
changeset 1196:dd3c6928c4e6
Add null check for HostInfoDAO.getAliveHosts().
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007686.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Wed, 31 Jul 2013 15:36:00 +0200 |
parents | 1925258e6309 |
children | 74c7131a0bb9 |
files | storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java |
diffstat | 3 files changed, 65 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- 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<HostRef> getHosts(); + + /** + * + * @return A collection of alive hosts which may be empty. + */ Collection<HostRef> getAliveHosts(); }
--- 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<AgentInformation> 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;
--- 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<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setup = setupForNullHostInfo(); + Storage storage = setup.first; + AgentInfoDAO agentInfoDao = setup.second; + PreparedStatement<HostInfo> stmt = setup.third; + + HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfoDao); + Collection<HostRef> hosts = hostsDAO.getAliveHosts(); + + assertEquals(0, hosts.size()); + verify(storage).prepareStatement(anyDescriptor()); + verify(stmt).setString(0, "123"); + verify(stmt).executeQuery(); + } + private Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> 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<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setupForNullHostInfo() + throws DescriptorParsingException, StatementExecutionException { + + // agents + + AgentInformation agentInfo1 = new AgentInformation(); + agentInfo1.setAgentId("123"); + agentInfo1.setAlive(true); + + // cursor + + @SuppressWarnings("unchecked") + Cursor<HostInfo> cursor1 = mock(Cursor.class); + when(cursor1.hasNext()).thenReturn(false); + + // storage + + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) 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 {