changeset 171:ca53979873fb

Make MainWindowControllerImpl use HostRefDAO and VmRefDAO. Reviewed-by: mtorre Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000502.html
author Roman Kennke <rkennke@redhat.com>
date Thu, 29 Mar 2012 23:52:18 +0200
parents 08829d9943de
children c69bd1a12299
files client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java common/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java common/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java
diffstat 6 files changed, 132 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Thu Mar 29 23:52:18 2012 +0200
@@ -36,33 +36,22 @@
 
 package com.redhat.thermostat.client;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 import java.util.concurrent.TimeUnit;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.mongodb.BasicDBObject;
-import com.mongodb.DB;
-import com.mongodb.DBCollection;
-import com.mongodb.DBCursor;
-import com.mongodb.DBObject;
 import com.redhat.thermostat.client.appctx.ApplicationContext;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.Timer;
+import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.HostRef;
+import com.redhat.thermostat.common.dao.HostRefDAO;
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.dao.VmRefDAO;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
-public class MainWindowControllerImpl implements MainWindowController, HostsVMsLoader {
-
-    private static final Logger logger = LoggingUtils.getLogger(MainWindowControllerImpl.class);
-
-    private final DBCollection agentConfigCollection;
-    private final DBCollection hostInfoCollection;
-    private final DBCollection vmInfoCollection;
+public class MainWindowControllerImpl implements MainWindowController {
 
     private Timer backgroundUpdater;
 
@@ -70,16 +59,35 @@
 
     private String filter;
 
-    public MainWindowControllerImpl(DB db, MainView view) {
-        this.agentConfigCollection = db.getCollection("agent-config");
-        this.hostInfoCollection = db.getCollection("host-info");
-        this.vmInfoCollection = db.getCollection("vm-info");
+    private HostRefDAO hostRefDAO;
+    private VmRefDAO vmRefDAO;
+
+    public MainWindowControllerImpl(MainView view) {
+
+        ApplicationContext ctx = ApplicationContext.getInstance();
+        DAOFactory daoFactory = ctx.getDAOFactory();
+        hostRefDAO = daoFactory.getHostRefDAO();
+        vmRefDAO = daoFactory.getVmRefDAO();
 
         initView(view);
         initializeTimer();
         start();
     }
 
+    private class HostsVMsLoaderImpl implements HostsVMsLoader {
+
+        @Override
+        public Collection<HostRef> getHosts() {
+            return hostRefDAO.getHosts();
+        }
+
+        @Override
+        public Collection<VmRef> getVMs(HostRef host) {
+            return vmRefDAO.getVMs(host);
+        }
+        
+    }
+
     private void initializeTimer() {
         ApplicationContext ctx = ApplicationContext.getInstance();
         backgroundUpdater = ctx.getTimerFactory().createTimer();
@@ -105,48 +113,14 @@
     }
 
     @Override
-    public Collection<HostRef> getHosts() {
-        List<HostRef> hostRefs = new ArrayList<HostRef>();
-
-        DBCursor cursor = agentConfigCollection.find();
-        while (cursor.hasNext()) {
-            DBObject doc = cursor.next();
-            String id = (String) doc.get("agent-id");
-            if (id != null) {
-                DBObject hostInfo = hostInfoCollection.findOne(new BasicDBObject("agent-id", id));
-                String hostName = (String) hostInfo.get("hostname");
-                HostRef agent = new HostRef(id, hostName);
-                hostRefs.add(agent);
-            }
-        }
-        logger.log(Level.FINER, "found " + hostRefs.size() + " connected agents");
-        return hostRefs;
-    }
-
-    @Override
-    public Collection<VmRef> getVMs(HostRef hostRef) {
-        List<VmRef> vmRefs = new ArrayList<VmRef>();
-        DBCursor cursor = vmInfoCollection.find(new BasicDBObject("agent-id", hostRef.getAgentId()));
-        while (cursor.hasNext()) {
-            DBObject vmObject = cursor.next();
-            Integer id = (Integer) vmObject.get("vm-id");
-            // TODO can we do better than the main class?
-            String mainClass = (String) vmObject.get("main-class");
-            VmRef ref = new VmRef(hostRef, id, mainClass);
-            vmRefs.add(ref);
-        }
-
-        return vmRefs;
-    }
-
-    @Override
     public void setHostVmTreeFilter(String filter) {
         this.filter = filter;
         doUpdateTreeAsync();
     }
 
     public void doUpdateTreeAsync() {
-        view.updateTree(filter, this);
+        HostsVMsLoader loader = new HostsVMsLoaderImpl();
+        view.updateTree(filter, loader);
     }
 
     private void initView(MainView mainView) {
--- a/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java	Thu Mar 29 23:52:18 2012 +0200
@@ -53,7 +53,7 @@
     @Override
     public MainWindowController getMainWindow() {
         MainView mainView = new MainWindow(this);
-        return new MainWindowControllerImpl(connection.getDB(), mainView);
+        return new MainWindowControllerImpl(mainView);
     }
 
     @Override
--- a/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java	Thu Mar 29 23:52:18 2012 +0200
@@ -36,25 +36,35 @@
 
 package com.redhat.thermostat.client;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.mongodb.DB;
 import com.redhat.thermostat.client.appctx.ApplicationContext;
 import com.redhat.thermostat.client.appctx.ApplicationContextUtil;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.Timer;
 import com.redhat.thermostat.common.TimerFactory;
+import com.redhat.thermostat.common.dao.DAOFactory;
+import com.redhat.thermostat.common.dao.HostRef;
+import com.redhat.thermostat.common.dao.HostRefDAO;
+import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.dao.VmRefDAO;
 
 public class MainWindowControllerImplTest {
 
@@ -66,6 +76,9 @@
 
     private Timer mainWindowTimer;
 
+    private HostRefDAO mockHostRefDAO;
+    private VmRefDAO mockVmRefDAO;
+
     @Before
     public void setUp() {
         ApplicationContextUtil.resetApplicationContext();
@@ -74,18 +87,34 @@
         when(timerFactory.createTimer()).thenReturn(mainWindowTimer);
         ApplicationContext.getInstance().setTimerFactory(timerFactory);
 
-        DB db = mock(DB.class);
+        setupDAOs();
+
         view = mock(MainView.class);
         ArgumentCaptor<ActionListener> grabListener = ArgumentCaptor.forClass(ActionListener.class);
         doNothing().when(view).addActionListener(grabListener.capture());
-        controller = new MainWindowControllerImpl(db, view);
+        controller = new MainWindowControllerImpl(view);
         l = grabListener.getValue();
+
+    }
+
+    private void setupDAOs() {
+        mockHostRefDAO = mock(HostRefDAO.class);
+
+        mockVmRefDAO = mock(VmRefDAO.class);
+
+        DAOFactory daoFactory = mock(DAOFactory.class);
+        when(daoFactory.getHostRefDAO()).thenReturn(mockHostRefDAO);
+        when(daoFactory.getVmRefDAO()).thenReturn(mockVmRefDAO);
+        ApplicationContext.getInstance().setDAOFactory(daoFactory);
+
     }
 
     @After
     public void tearDown() {
         view = null;
         controller = null;
+        mockHostRefDAO = null;
+        mockVmRefDAO = null;
         l = null;
         ApplicationContextUtil.resetApplicationContext();
     }
@@ -120,4 +149,48 @@
         controller.showMainMainWindow();
         verify(view).showMainWindow();
     }
+
+    @Test
+    public void verifyUpdateHostsVMsLoadsCorrectHosts() {
+
+        Collection<HostRef> expectedHosts = new ArrayList<>();
+        expectedHosts.add(new HostRef("123", "fluffhost1"));
+        expectedHosts.add(new HostRef("456", "fluffhost2"));
+
+        when(mockHostRefDAO.getHosts()).thenReturn(expectedHosts);
+
+        controller.doUpdateTreeAsync();
+
+        ArgumentCaptor<HostsVMsLoader> arg = ArgumentCaptor.forClass(HostsVMsLoader.class);
+        verify(view).updateTree(anyString(), arg.capture());
+        HostsVMsLoader loader = arg.getValue();
+
+        Collection<HostRef> actualHosts = loader.getHosts();
+        assertEqualCollection(expectedHosts, actualHosts);
+    }
+
+    @Test
+    public void verifyUpdateHostsVMsLoadsCorrectVMs() {
+
+        Collection<VmRef> expectedVMs = new ArrayList<>();
+        HostRef host = new HostRef("123", "fluffhost1");
+        expectedVMs.add(new VmRef(host, 123, "vm1"));
+        expectedVMs.add(new VmRef(host, 456, "vm2"));
+
+        when(mockVmRefDAO.getVMs(any(HostRef.class))).thenReturn(expectedVMs);
+
+        controller.doUpdateTreeAsync();
+
+        ArgumentCaptor<HostsVMsLoader> arg = ArgumentCaptor.forClass(HostsVMsLoader.class);
+        verify(view).updateTree(anyString(), arg.capture());
+        HostsVMsLoader loader = arg.getValue();
+
+        Collection<VmRef> actualVMs = loader.getVMs(host);
+        assertEqualCollection(expectedVMs, actualVMs);
+    }
+
+    private void assertEqualCollection(Collection<?> expected, Collection<?> actual) {
+        assertEquals(expected.size(), actual.size());
+        assertTrue(expected.containsAll(actual));
+    }
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java	Thu Mar 29 23:52:18 2012 +0200
@@ -65,4 +65,8 @@
 
     public NetworkInterfaceInfoDAO getNetworkInterfaceInfoDAO(HostRef ref);
 
+    public HostRefDAO getHostRefDAO();
+
+    public VmRefDAO getVmRefDAO();
+
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java	Thu Mar 29 23:52:18 2012 +0200
@@ -115,4 +115,14 @@
     public VmMemoryStatDAO getVmMemoryStatDAO(VmRef ref) {
         return new VmMemoryStatDAOImpl(storage, ref);
     }
+
+    @Override
+    public HostRefDAO getHostRefDAO() {
+        return new HostRefDAOImpl(storage);
+    }
+
+    @Override
+    public VmRefDAO getVmRefDAO() {
+        return new VmRefDAOImpl(storage);
+    }
 }
--- a/common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java	Thu Mar 29 13:15:28 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java	Thu Mar 29 23:52:18 2012 +0200
@@ -118,4 +118,16 @@
         NetworkInterfaceInfoDAO dao = daoFactory.getNetworkInterfaceInfoDAO(hostRef);
         assertNotNull(dao);
     }
+
+    @Test
+    public void testGetHostRefDAO() {
+        HostRefDAO dao = daoFactory.getHostRefDAO();
+        assertNotNull(dao);
+    }
+
+    @Test
+    public void testGetVmRefDAO() {
+        VmRefDAO dao = daoFactory.getVmRefDAO();
+        assertNotNull(dao);
+    }
 }