changeset 2646:91412a25d6ec

Send JSON array to jvm-gc microservice Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023059.html
author Elliott Baron <ebaron@redhat.com>
date Thu, 11 May 2017 11:40:48 -0400
parents d58c740a1a0b
children 4e29550f635a
files plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOImpl.java plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapter.java plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOTest.java plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapterTest.java
diffstat 4 files changed, 34 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOImpl.java	Wed May 10 16:02:18 2017 -0400
+++ b/plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOImpl.java	Thu May 11 11:40:48 2017 -0400
@@ -37,6 +37,8 @@
 package com.redhat.thermostat.vm.gc.common.internal;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 import java.util.logging.Level;
@@ -79,7 +81,7 @@
     @Override
     public void putVmGcStat(final VmGcStat stat) {
         try {
-            String json = jsonHelper.toJson(stat);
+            String json = jsonHelper.toJson(Arrays.asList(stat));
             StringContentProvider provider = httpHelper.createContentProvider(json);
 
             String url = buildUrl();
@@ -122,8 +124,8 @@
             this.adapter = adapter;
         }
 
-        public String toJson(VmGcStat stat) throws IOException {
-            return adapter.toJson(stat);
+        public String toJson(List<VmGcStat> list) throws IOException {
+            return adapter.toJson(list);
         }
 
     }
--- a/plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapter.java	Wed May 10 16:02:18 2017 -0400
+++ b/plugins/vm-gc/common/src/main/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapter.java	Thu May 11 11:40:48 2017 -0400
@@ -42,8 +42,9 @@
 import com.redhat.thermostat.vm.gc.common.model.VmGcStat;
 
 import java.io.IOException;
+import java.util.List;
 
-public class VmGcStatTypeAdapter extends TypeAdapter<VmGcStat> {
+public class VmGcStatTypeAdapter extends TypeAdapter<List<VmGcStat>> {
 
     private static final String TYPE_LONG = "$numberLong";
     private static final String AGENT_ID = "agentId";
@@ -54,12 +55,20 @@
     private static final String WALL_TIME_IN_MICROS = "wallTimeInMicros";
 
     @Override
-    public VmGcStat read(JsonReader in) throws IOException {
+    public List<VmGcStat> read(JsonReader in) throws IOException {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public void write(JsonWriter out, VmGcStat stat) throws IOException {
+    public void write(JsonWriter out, List<VmGcStat> stats) throws IOException {
+        out.beginArray();
+        for (VmGcStat stat : stats) {
+            writeGcStat(out, stat);
+        }
+        out.endArray();
+    }
+    
+    private void writeGcStat(JsonWriter out, VmGcStat stat) throws IOException {
         out.beginObject();
         out.name(AGENT_ID);
         out.value(stat.getAgentId());
--- a/plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOTest.java	Wed May 10 16:02:18 2017 -0400
+++ b/plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatDAOTest.java	Thu May 11 11:40:48 2017 -0400
@@ -38,14 +38,14 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-import static com.redhat.thermostat.vm.gc.common.internal.VmGcStatDAOImpl.HttpHelper;
-import static com.redhat.thermostat.vm.gc.common.internal.VmGcStatDAOImpl.JsonHelper;
 
+import java.util.Arrays;
 import java.util.Collection;
 
 import org.eclipse.jetty.client.HttpClient;
@@ -57,9 +57,11 @@
 import org.junit.Before;
 import org.junit.Test;
 
+import com.redhat.thermostat.storage.core.Key;
 import com.redhat.thermostat.vm.gc.common.VmGcStatDAO;
+import com.redhat.thermostat.vm.gc.common.internal.VmGcStatDAOImpl.HttpHelper;
+import com.redhat.thermostat.vm.gc.common.internal.VmGcStatDAOImpl.JsonHelper;
 import com.redhat.thermostat.vm.gc.common.model.VmGcStat;
-import com.redhat.thermostat.storage.core.Key;
 
 public class VmGcStatDAOTest {
 
@@ -91,7 +93,7 @@
         when(request.send()).thenReturn(response);
 
         jsonHelper = mock(JsonHelper.class);
-        when(jsonHelper.toJson(any(VmGcStat.class))).thenReturn(JSON);
+        when(jsonHelper.toJson(anyListOf(VmGcStat.class))).thenReturn(JSON);
         httpHelper = mock(HttpHelper.class);
         contentProvider = mock(StringContentProvider.class);
         when(httpHelper.createContentProvider(anyString())).thenReturn(contentProvider);
@@ -106,7 +108,7 @@
         final String url = VmGcStatDAOImpl.GATEWAY_URL + VmGcStatDAOImpl.GATEWAY_PATH;
         verify(httpClient).newRequest(url);
         verify(request).method(HttpMethod.POST);
-        verify(jsonHelper).toJson(stat);
+        verify(jsonHelper).toJson(eq(Arrays.asList(stat)));
         verify(httpHelper).createContentProvider(JSON);
         verify(request).content(contentProvider, VmGcStatDAOImpl.CONTENT_TYPE);
         verify(request).send();
--- a/plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapterTest.java	Wed May 10 16:02:18 2017 -0400
+++ b/plugins/vm-gc/common/src/test/java/com/redhat/thermostat/vm/gc/common/internal/VmGcStatTypeAdapterTest.java	Thu May 11 11:40:48 2017 -0400
@@ -36,20 +36,19 @@
 
 package com.redhat.thermostat.vm.gc.common.internal;
 
-import com.google.gson.GsonBuilder;
-import com.google.gson.Gson;
-import com.redhat.thermostat.vm.gc.common.model.VmGcStat;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
+import com.redhat.thermostat.vm.gc.common.model.VmGcStat;
 
 public class VmGcStatTypeAdapterTest {
 
     @Test
-    public void testWrite() {
-        GsonBuilder builder = new GsonBuilder();
-        builder.registerTypeAdapter(VmGcStat.class, new VmGcStatTypeAdapter());
-        Gson gson = builder.create();
+    public void testWrite() throws Exception {
+        VmGcStatTypeAdapter typeAdapter = new VmGcStatTypeAdapter();
         VmGcStat stat = new VmGcStat();
         stat.setAgentId("1");
         stat.setVmId("2");
@@ -57,6 +56,7 @@
         stat.setCollectorName("Collector");
         stat.setRunCount(10l);
         stat.setWallTime(200l);
-        assertEquals("{\"agentId\":\"1\",\"vmId\":\"2\",\"timeStamp\":{\"$numberLong\":\"100\"},\"collectorName\":\"Collector\",\"runCount\":10,\"wallTimeInMicros\":200}", gson.toJson(stat));
+        assertEquals("[{\"agentId\":\"1\",\"vmId\":\"2\",\"timeStamp\":{\"$numberLong\":\"100\"},\"collectorName\":\"Collector\",\"runCount\":10,\"wallTimeInMicros\":200}]", 
+                typeAdapter.toJson(Arrays.asList(stat)));
     }
 }