changeset 2749:ed2aca3cd482

Fix inadvertent test code in jvm-io plugin Unfortunately, some test code got in to my vm-io plugin implementation. This patch fixes that and enhances the test case. Reviewed-by: sgehwolf Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024878.html
author Simon Tooke <stooke@redhat.com>
date Thu, 07 Sep 2017 14:04:15 -0400
parents dd5a4d080f46
children f39efe6bebd7
files common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOImpl.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOImpl.java plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java plugins/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImpl.java plugins/vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImplTest.java
diffstat 7 files changed, 71 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java	Thu Sep 07 14:04:15 2017 -0400
@@ -44,21 +44,20 @@
 import com.redhat.thermostat.agent.http.HttpRequestService;
 import com.redhat.thermostat.agent.http.HttpRequestService.RequestFailedException;
 
-abstract public class PluginDAOBase<Tobj,Tdao> {
+abstract public class PluginDAOBase<Tobj> {
 
     protected abstract String toJsonString(Tobj obj) throws IOException;
     protected abstract HttpRequestService getHttpRequestService();
     protected abstract PluginConfiguration getConfig();
-    protected abstract URI getPostURI(final URI basepath);
+    protected abstract URI getPostURI(final URI basepath, final Tobj obj);
     protected abstract Logger getLogger();
 
     public void put(final Tobj obj) {
         try {
             HttpRequestService httpRequestService = getHttpRequestService();
             String json = toJsonString(obj);
-
             final URI gatewayURI = getConfig().getGatewayURL();
-            final URI postURI = getPostURI(gatewayURI);
+            final URI postURI = getPostURI(gatewayURI, obj);
             httpRequestService.sendHttpRequest(json, postURI, HttpRequestService.Method.POST);
         } catch (IOException | RequestFailedException e) {
             getLogger().log(Level.WARNING, "Failed to send " + obj.getClass().getName() + " to web gateway", e);
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOImpl.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOImpl.java	Thu Sep 07 14:04:15 2017 -0400
@@ -58,7 +58,7 @@
 
 @Component
 @Service(value = CpuStatDAO.class)
-public class CpuStatDAOImpl extends PluginDAOBase<CpuStat, CpuStatDAOImpl> implements CpuStatDAO {
+public class CpuStatDAOImpl extends PluginDAOBase<CpuStat> implements CpuStatDAO {
 
     private static final Logger logger = LoggingUtils.getLogger(CpuStatDAOImpl.class);
 
@@ -115,7 +115,7 @@
     }
 
     @Override
-    protected URI getPostURI(URI basepath) {
+    protected URI getPostURI(URI basepath, CpuStat obj) {
         return basepath.resolve("systems/" + systemID.getSystemID());
     }
 
--- a/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOImpl.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOImpl.java	Thu Sep 07 14:04:15 2017 -0400
@@ -58,7 +58,7 @@
 
 @Component
 @Service(value = MemoryStatDAO.class)
-public class MemoryStatDAOImpl extends PluginDAOBase<MemoryStat, MemoryStatDAOImpl> implements MemoryStatDAO {
+public class MemoryStatDAOImpl extends PluginDAOBase<MemoryStat> implements MemoryStatDAO {
 
     private static final Logger logger = LoggingUtils.getLogger(MemoryStatDAOImpl.class);
 
@@ -115,7 +115,7 @@
     }
 
     @Override
-    protected URI getPostURI(URI basepath) {
+    protected URI getPostURI(URI basepath, MemoryStat obj) {
         return basepath.resolve("systems/" + systemID.getSystemID());
     }
 
--- a/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java	Thu Sep 07 14:04:15 2017 -0400
@@ -58,7 +58,7 @@
 
 @Component
 @Service(value = NetworkInfoListDAO.class)
-public class NetworkInfoListDAOImpl extends PluginDAOBase<NetworkInfoList, NetworkInfoListDAOImpl> implements NetworkInfoListDAO {
+public class NetworkInfoListDAOImpl extends PluginDAOBase<NetworkInfoList> implements NetworkInfoListDAO {
 
     private static final Logger logger = LoggingUtils.getLogger(NetworkInfoListDAOImpl.class);
 
@@ -108,7 +108,7 @@
     }
 
     @Override
-    protected URI getPostURI(URI basepath) {
+    protected URI getPostURI(URI basepath, NetworkInfoList obj) {
         return basepath.resolve("systems/" + systemID.getSystemID());
     }
 
--- a/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java	Thu Sep 07 14:04:15 2017 -0400
@@ -58,7 +58,7 @@
 
 @Component
 @Service(value = HostInfoDAO.class)
-public class HostInfoDAOImpl extends PluginDAOBase<HostInfo, HostInfoDAOImpl> implements HostInfoDAO {
+public class HostInfoDAOImpl extends PluginDAOBase<HostInfo> implements HostInfoDAO {
     
     private static final Logger logger = LoggingUtils.getLogger(HostInfoDAOImpl.class);
     
@@ -93,7 +93,7 @@
         this.config = configCreator.create(configurationInfoSource);
     }
 
-    public URI getPostURI(final URI base) {
+    public URI getPostURI(final URI base, HostInfo obj) {
         return base.resolve("systems/" + systemID.getSystemID());
     }
 
--- a/plugins/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImpl.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImpl.java	Thu Sep 07 14:04:15 2017 -0400
@@ -57,10 +57,9 @@
 
 @Component
 @Service(value = VmIoStatDAO.class)
-public class VmIoStatDAOImpl extends PluginDAOBase<VmIoStat, VmIoStatDAOImpl> implements VmIoStatDAO {
+public class VmIoStatDAOImpl extends PluginDAOBase<VmIoStat> implements VmIoStatDAO {
 
     private static final Logger logger = LoggingUtils.getLogger(VmIoStatDAOImpl.class);
-
     public static final String PLUGIN_ID = "vm-io";
 
     private final JsonHelper jsonHelper;
@@ -114,8 +113,8 @@
     }
 
     @Override
-    protected URI getPostURI(URI basepath) {
-        return basepath.resolve("systems/" + systemID.getSystemID() + "/jvms/" + "ssss");
+    protected URI getPostURI(final URI basepath, final VmIoStat iostat) {
+        return basepath.resolve("systems/" + systemID.getSystemID() + "/jvms/" + iostat.getJvmId());
     }
 
     // DS bind methods
--- a/plugins/vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImplTest.java	Thu Sep 07 17:48:06 2017 +0200
+++ b/plugins/vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoStatDAOImplTest.java	Thu Sep 07 14:04:15 2017 -0400
@@ -36,26 +36,81 @@
 
 package com.redhat.thermostat.vm.io.agent.internal;
 
-import org.junit.Before;
 
 import com.redhat.thermostat.vm.io.model.VmIoStat;
+import com.redhat.thermostat.vm.io.agent.internal.VmIoStatDAOImpl.ConfigurationCreator;
+import com.redhat.thermostat.vm.io.agent.internal.VmIoStatDAOImpl.JsonHelper;
+
+import com.redhat.thermostat.agent.http.HttpRequestService;
+import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
+import com.redhat.thermostat.common.plugin.PluginConfiguration;
+import com.redhat.thermostat.common.plugin.SystemID;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+
+import static org.mockito.Matchers.anyListOf;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class VmIoStatDAOImplTest {
 
     private static final long SOME_TIMESTAMP = 1234;
+    private static final String SOME_SYSTEM_ID = "somesystemid";
     private static final String SOME_VM_ID = "321";
     private static final long SOME_CHARACTERS_READ = 123456;
     private static final long SOME_CHARACTERS_WRITTEN = 67798;
     private static final long SOME_READ_SYSCALLS = 123456;
     private static final long SOME_WRITE_SYSCALLS = 67798;
 
+    private static final String AGENT_ID = "some-agent";
+    private static final String JSON = "{\"this\":\"is\",\"also\":\"JSON\"}";
+
+    private static final URI GATEWAY_URI = URI.create("http://example.com/jvm-io/");
+
     private VmIoStat ioStat;
+    private JsonHelper jsonHelper;
+    private VmIoStatDAOImpl dao;
+    private HttpRequestService httpRequestService;
 
     @Before
-    public void setUp() {
+    public void setUp() throws IOException {
         this.ioStat = new VmIoStat("foo-agent", SOME_VM_ID, SOME_TIMESTAMP,
                 SOME_CHARACTERS_READ, SOME_CHARACTERS_WRITTEN,
                 SOME_READ_SYSCALLS, SOME_WRITE_SYSCALLS);
+
+        jsonHelper = mock(JsonHelper.class);
+        when(jsonHelper.toJson(anyListOf(VmIoStat.class))).thenReturn(JSON);
+
+        ConfigurationInfoSource source = mock(ConfigurationInfoSource.class);
+        PluginConfiguration config = mock(PluginConfiguration.class);
+        when(config.getGatewayURL()).thenReturn(GATEWAY_URI);
+        ConfigurationCreator creator = mock(ConfigurationCreator.class);
+        when(creator.create(source)).thenReturn(config);
+
+        httpRequestService = mock(HttpRequestService.class);
+        dao = new VmIoStatDAOImpl(jsonHelper, creator);
+        dao.bindHttpRequestService(httpRequestService);
+        dao.bindConfigurationInfoSource(source);
     }
 
+    @Test
+    public void verifyPut() throws Exception {
+        SystemID id = mock(SystemID.class);
+        when(id.getSystemID()).thenReturn(SOME_SYSTEM_ID);
+        dao.bindSystemID(id);
+        dao.activate();
+        dao.put(ioStat);
+
+        verify(jsonHelper).toJson(eq(Arrays.asList(ioStat)));
+        verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI.resolve("systems/" + SOME_SYSTEM_ID + "/jvms/" + SOME_VM_ID), HttpRequestService.Method.POST);
+    }
+
+
 }