changeset 147:970dd3a69fe8

Refactor NetworkInterfaceInfo to use DAO reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000460.html
author Jon VanAlten <jon.vanalten@redhat.com>
date Mon, 26 Mar 2012 17:27:54 -0400
parents c82fe34337e0
children 95349a1edfdd 30bab1584397
files client/src/main/java/com/redhat/thermostat/client/HostPanelFacadeImpl.java client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java client/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.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/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverter.java common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAO.java common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAOImpl.java common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverterTest.java common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAOTest.java
diffstat 11 files changed, 163 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/HostPanelFacadeImpl.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/HostPanelFacadeImpl.java	Mon Mar 26 17:27:54 2012 -0400
@@ -36,8 +36,6 @@
 
 package com.redhat.thermostat.client;
 
-import com.mongodb.DB;
-
 import com.redhat.thermostat.client.ui.HostCpuController;
 import com.redhat.thermostat.client.ui.HostMemoryController;
 import com.redhat.thermostat.client.ui.HostOverviewController;
@@ -49,8 +47,8 @@
     private final HostCpuController cpuController;
     private final HostMemoryController memoryController;
 
-    public HostPanelFacadeImpl(HostRef ref, DB db) {
-        overviewController = new HostOverviewController(ref, db);
+    public HostPanelFacadeImpl(HostRef ref) {
+        overviewController = new HostOverviewController(ref);
         cpuController = new HostCpuController(ref);
         memoryController = new HostMemoryController(ref);
     }
--- a/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java	Mon Mar 26 17:27:54 2012 -0400
@@ -64,7 +64,7 @@
 
     @Override
     public HostPanelFacade getHostPanel(HostRef ref) {
-        return new HostPanelFacadeImpl(ref, connection.getDB());
+        return new HostPanelFacadeImpl(ref);
 
     }
 
--- a/client/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java	Mon Mar 26 17:27:54 2012 -0400
@@ -51,16 +51,13 @@
 
 import javax.swing.SwingWorker;
 
-import com.mongodb.BasicDBObject;
-import com.mongodb.DB;
-import com.mongodb.DBCollection;
-import com.mongodb.DBCursor;
 import com.redhat.thermostat.client.AsyncUiFacade;
 import com.redhat.thermostat.client.appctx.ApplicationContext;
 import com.redhat.thermostat.client.locale.LocaleResources;
+import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.HostInfoDAO;
 import com.redhat.thermostat.common.dao.HostRef;
-import com.redhat.thermostat.common.dao.NetworkInterfaceInfoConverter;
+import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO;
 import com.redhat.thermostat.common.model.HostInfo;
 import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
 import com.redhat.thermostat.common.utils.LoggingUtils;
@@ -71,16 +68,17 @@
 
     private final HostRef hostRef;
     private final HostInfoDAO hostInfoDAO;
-    private final DBCollection networkInfoCollection;
+    private final NetworkInterfaceInfoDAO networkInfoDAO;
 
     private final Timer backgroundUpdateTimer;
 
     private final HostOverviewView view;
 
-    public HostOverviewController(HostRef ref, DB db) {
-        hostInfoDAO = ApplicationContext.getInstance().getDAOFactory().getHostInfoDAO(ref);
+    public HostOverviewController(HostRef ref) {
         this.hostRef = ref;
-        networkInfoCollection = db.getCollection("network-info");
+        DAOFactory df = ApplicationContext.getInstance().getDAOFactory();
+        hostInfoDAO = df.getHostInfoDAO(hostRef);
+        networkInfoDAO = df.getNetworkInterfaceInfoDAO(hostRef);
 
         final Vector<String> networkTableColumnVector;
         networkTableColumnVector = new Vector<String>();
@@ -106,18 +104,7 @@
 
         @Override
         protected List<NetworkInterfaceInfo> doInBackground() throws Exception {
-            return getNetworkInfo();
-        }
-
-        private List<NetworkInterfaceInfo> getNetworkInfo() {
-            List<NetworkInterfaceInfo> network = new ArrayList<NetworkInterfaceInfo>();
-            DBCursor cursor = networkInfoCollection.find(new BasicDBObject("agent-id", hostRef.getAgentId()));
-            while (cursor.hasNext()) {
-                NetworkInterfaceInfo info = new NetworkInterfaceInfoConverter().fromDBObject(cursor.next());
-                network.add(info);
-            }
-
-            return network;
+            return networkInfoDAO.getNetworkInterfaces();
         }
 
         @Override
--- a/common/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java	Mon Mar 26 17:27:54 2012 -0400
@@ -56,4 +56,6 @@
     public CpuStatDAO getCpuStatDAO(HostRef ref);
 
     public MemoryStatDAO getMemoryStatDAO(HostRef ref);
+
+    public NetworkInterfaceInfoDAO getNetworkInterfaceInfoDAO(HostRef hostRef);
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java	Mon Mar 26 17:27:54 2012 -0400
@@ -96,4 +96,9 @@
     public MemoryStatDAO getMemoryStatDAO(HostRef ref) {
         return new MemoryStatDAOImpl(storage, ref);
     }
+
+    @Override
+    public NetworkInterfaceInfoDAO getNetworkInterfaceInfoDAO(HostRef ref) {
+        return new NetworkInterfaceInfoDAOImpl(storage, ref);
+    }
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverter.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverter.java	Mon Mar 26 17:27:54 2012 -0400
@@ -36,7 +36,6 @@
 
 package com.redhat.thermostat.common.dao;
 
-import com.mongodb.DBObject;
 import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
 import com.redhat.thermostat.common.storage.Chunk;
 
@@ -56,14 +55,10 @@
         return chunk;
     }
 
-    public NetworkInterfaceInfo fromDBObject(DBObject obj) {
-        NetworkInterfaceInfo info = new NetworkInterfaceInfo((String) obj.get("iface"));
-        if (obj.containsField("ipv4addr")) {
-            info.setIp4Addr((String) obj.get("ipv4addr"));
-        }
-        if (obj.containsField("ipv6addr")) {
-            info.setIp6Addr((String) obj.get("ipv6addr"));
-        }
+    public NetworkInterfaceInfo chunkToNetworkInfo(Chunk chunk) {
+        NetworkInterfaceInfo info = new NetworkInterfaceInfo(chunk.get(NetworkInterfaceInfoDAO.ifaceKey));
+        info.setIp4Addr(chunk.get(NetworkInterfaceInfoDAO.ip4AddrKey));
+        info.setIp6Addr(chunk.get(NetworkInterfaceInfoDAO.ip6AddrKey));
         return info;
     }
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAO.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAO.java	Mon Mar 26 17:27:54 2012 -0400
@@ -36,10 +36,13 @@
 
 package com.redhat.thermostat.common.dao;
 
+import java.util.List;
+
+import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
 import com.redhat.thermostat.common.storage.Category;
 import com.redhat.thermostat.common.storage.Key;
 
-public class NetworkInterfaceInfoDAO {
+public interface NetworkInterfaceInfoDAO {
 
     static Key<String> ifaceKey = new Key<>("iface", true);
     static Key<String> ip4AddrKey = new Key<>("ipv4addr", false);
@@ -47,4 +50,6 @@
 
     public static final Category networkInfoCategory = new Category("network-info",
             Key.TIMESTAMP, ifaceKey, ip4AddrKey, ip6AddrKey);
+
+    public List<NetworkInterfaceInfo> getNetworkInterfaces();
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAOImpl.java	Mon Mar 26 17:27:54 2012 -0400
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
+import com.redhat.thermostat.common.storage.Chunk;
+import com.redhat.thermostat.common.storage.Cursor;
+import com.redhat.thermostat.common.storage.Key;
+import com.redhat.thermostat.common.storage.Storage;
+
+public class NetworkInterfaceInfoDAOImpl implements NetworkInterfaceInfoDAO {
+
+    private Storage storage;
+    private HostRef ref;
+
+    public NetworkInterfaceInfoDAOImpl(Storage storage, HostRef ref) {
+        this.storage = storage;
+        this.ref = ref;
+    }
+
+    @Override
+    public List<NetworkInterfaceInfo> getNetworkInterfaces() {
+        Chunk query = new Chunk(NetworkInterfaceInfoDAO.networkInfoCategory, false);
+        query.put(Key.AGENT_ID, ref.getAgentId());
+
+        Cursor cursor = storage.findAll(query);
+        NetworkInterfaceInfoConverter converter = new NetworkInterfaceInfoConverter();
+        List<NetworkInterfaceInfo> result = new ArrayList<>();
+        while (cursor.hasNext()) {
+            Chunk chunk = cursor.next();
+            NetworkInterfaceInfo stat = converter.chunkToNetworkInfo(chunk);
+            result.add(stat);
+        }
+        return result;
+    }
+
+}
--- a/common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/MongoDAOFactoryTest.java	Mon Mar 26 17:27:54 2012 -0400
@@ -94,4 +94,10 @@
         MemoryStatDAO dao = daoFactory.getMemoryStatDAO(hostRef);
         assertNotNull(dao);
     }
+
+    @Test
+    public void testGetNetworkInterfaceInfoDAO() {
+        NetworkInterfaceInfoDAO dao = daoFactory.getNetworkInterfaceInfoDAO(hostRef);
+        assertNotNull(dao);
+    }
 }
--- a/common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverterTest.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoConverterTest.java	Mon Mar 26 17:27:54 2012 -0400
@@ -41,7 +41,6 @@
 
 import org.junit.Test;
 
-import com.mongodb.BasicDBObject;
 import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
 import com.redhat.thermostat.common.storage.Chunk;
 import com.redhat.thermostat.common.storage.Key;
@@ -64,17 +63,17 @@
     }
 
     @Test
-    public void testDBObjectToNetworkInfo() {
+    public void testChunkToNetworkInfo() {
         final String INTERFACE_NAME = "some interface. maybe eth0";
         final String IPV4_ADDR = "256.256.256.256";
         final String IPV6_ADDR = "100:100:100::::1";
 
-        BasicDBObject dbObj = new BasicDBObject();
-        dbObj.put("iface", INTERFACE_NAME);
-        dbObj.put("ipv4addr", IPV4_ADDR);
-        dbObj.put("ipv6addr", IPV6_ADDR);
+        Chunk chunk = new Chunk(NetworkInterfaceInfoDAO.networkInfoCategory, false);
+        chunk.put(NetworkInterfaceInfoDAO.ifaceKey, INTERFACE_NAME);
+        chunk.put(NetworkInterfaceInfoDAO.ip4AddrKey, IPV4_ADDR);
+        chunk.put(NetworkInterfaceInfoDAO.ip6AddrKey, IPV6_ADDR);
 
-        NetworkInterfaceInfo info = new NetworkInterfaceInfoConverter().fromDBObject(dbObj);
+        NetworkInterfaceInfo info = new NetworkInterfaceInfoConverter().chunkToNetworkInfo(chunk);
         assertNotNull(info);
         assertEquals(INTERFACE_NAME, info.getInterfaceName());
         assertEquals(IPV4_ADDR, info.getIp4Addr());
--- a/common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAOTest.java	Mon Mar 26 15:29:12 2012 -0400
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/NetworkInterfaceInfoDAOTest.java	Mon Mar 26 17:27:54 2012 -0400
@@ -37,13 +37,25 @@
 package com.redhat.thermostat.common.dao;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import java.util.Collection;
+import java.util.List;
 
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 
+import com.redhat.thermostat.common.model.NetworkInterfaceInfo;
+import com.redhat.thermostat.common.storage.Chunk;
+import com.redhat.thermostat.common.storage.Cursor;
 import com.redhat.thermostat.common.storage.Key;
+import com.redhat.thermostat.common.storage.Storage;
 
 public class NetworkInterfaceInfoDAOTest {
 
@@ -59,4 +71,41 @@
         assertTrue(keys.contains(new Key<String>("ipv6addr", false)));
         assertEquals(4, keys.size());
     }
+
+    @Test
+    public void testGetNetworkInterfaces() {
+        final String INTERFACE_NAME = "some interface. maybe eth0";
+        final String IPV4_ADDR = "256.256.256.256";
+        final String IPV6_ADDR = "100:100:100::::1";
+
+        Chunk chunk = new Chunk(NetworkInterfaceInfoDAO.networkInfoCategory, false);
+        chunk.put(NetworkInterfaceInfoDAO.ifaceKey, INTERFACE_NAME);
+        chunk.put(NetworkInterfaceInfoDAO.ip4AddrKey, IPV4_ADDR);
+        chunk.put(NetworkInterfaceInfoDAO.ip6AddrKey, IPV6_ADDR);
+
+        Cursor cursor = mock(Cursor.class);
+        when(cursor.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor.next()).thenReturn(chunk);
+
+        Storage storage = mock(Storage.class);
+        when(storage.findAll(any(Chunk.class))).thenReturn(cursor);
+
+        HostRef hostRef = mock(HostRef.class);
+        when(hostRef.getAgentId()).thenReturn("system");
+
+        NetworkInterfaceInfoDAO dao = new NetworkInterfaceInfoDAOImpl(storage, hostRef);
+        List<NetworkInterfaceInfo> netInfo = dao.getNetworkInterfaces();
+
+        ArgumentCaptor<Chunk> arg = ArgumentCaptor.forClass(Chunk.class);
+        verify(storage).findAll(arg.capture());
+        assertNull(arg.getValue().get(new Key<String>("$where", false)));
+
+        assertEquals(1, netInfo.size());
+
+        NetworkInterfaceInfo info = netInfo.get(0);
+
+        assertEquals(INTERFACE_NAME, info.getInterfaceName());
+        assertEquals(IPV4_ADDR, info.getIp4Addr());
+        assertEquals(IPV6_ADDR, info.getIp6Addr());
+    }
 }