# HG changeset patch # User Christopher Koehler # Date 1507136277 14400 # Node ID c7af874342ed4be058bd6b546132ba8991594854 # Parent 025c66961cf9b8594a49dcccfd1d6631795f1581 Fix /jvms and /jvms/tree response mismatch Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024482.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024808.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025292.html diff -r 025c66961cf9 -r c7af874342ed common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/LongTypeAdapter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/LongTypeAdapter.java Wed Oct 04 12:57:57 2017 -0400 @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2017 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 + * . + * + * 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.gateway.common.mongodb.response; + +import java.io.IOException; + +import com.google.gson.JsonSyntaxException; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public class LongTypeAdapter extends TypeAdapter { + + private static final String NUMBER_LONG_IDENTIFIER = "$numberLong"; + + @Override + public void write(JsonWriter out, Long value) throws IOException { + if (value == null) { + out.nullValue(); + } else { + out.beginObject(); + out.name(NUMBER_LONG_IDENTIFIER); + out.value(value.toString()); + out.endObject(); + } + } + + @Override + public Long read(JsonReader in) throws IOException { + in.beginObject(); + String name = in.nextName(); + if (!name.equals(NUMBER_LONG_IDENTIFIER)) { + throw new JsonSyntaxException("Unexpected name: " + name); + } + long returnValue = in.nextLong(); + in.endObject(); + return returnValue; + } +} \ No newline at end of file diff -r 025c66961cf9 -r c7af874342ed common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoGsonFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoGsonFactory.java Wed Oct 04 12:57:57 2017 -0400 @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2017 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 + * . + * + * 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.gateway.common.mongodb.response; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class MongoGsonFactory { + + public static Gson getGson() { + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(Long.class, new LongTypeAdapter()); + gsonBuilder.registerTypeAdapter(long.class, new LongTypeAdapter()); + return gsonBuilder.create(); + } +} diff -r 025c66961cf9 -r c7af874342ed common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoResponseBuilder.java --- a/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoResponseBuilder.java Mon Oct 02 14:41:38 2017 +0200 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoResponseBuilder.java Wed Oct 04 12:57:57 2017 -0400 @@ -38,13 +38,11 @@ import java.util.ArrayList; -import org.bson.Document; - import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.mongodb.Block; import com.mongodb.client.FindIterable; import com.redhat.thermostat.gateway.common.mongodb.keycloak.KeycloakFields; +import org.bson.Document; /* * Builds the appropriate response after executing the request's MongoDB Query. @@ -66,7 +64,7 @@ private ArrayList queryDocuments; private MongoMetaDataResponseBuilder metaData; - private final Gson gson = new GsonBuilder().create(); + private final Gson gson = MongoGsonFactory.getGson(); public Builder addQueryDocuments(FindIterable documents) { queryDocuments = new ArrayList<>(); diff -r 025c66961cf9 -r c7af874342ed common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoGsonFactoryTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoGsonFactoryTest.java Wed Oct 04 12:57:57 2017 -0400 @@ -0,0 +1,116 @@ +/* + * Copyright 2012-2017 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 + * . + * + * 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.gateway.common.mongodb.response; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.google.gson.JsonSyntaxException; +import org.junit.Test; + +public class MongoGsonFactoryTest { + + private static final String NUMBER_LONG_POJO_STRING = "{\"someInt\":42,\"someLong\":{\"$numberLong\":\"12345\"},\"someString\":\"long\"}"; + private static final String NUMBER_LONG_NONOBJECT_POJO_STRING = "{\"someInt\":42,\"someLong\":\"12345\",\"someString\":\"long\"}"; + + @Test + public void instanceNotNull() { + assertNotNull(MongoGsonFactory.getGson()); + } + + @Test + public void gsonHasNumberLong() { + String actual = MongoGsonFactory.getGson().toJson(new NumberLongPojo(42, 12345L, "long")); + assertEquals(NUMBER_LONG_POJO_STRING, actual); + } + + @Test + public void gsonReadsNumberLong() { + NumberLongPojo numberLongPojo = MongoGsonFactory.getGson().fromJson(NUMBER_LONG_POJO_STRING, NumberLongPojo.class); + assertEquals(42, numberLongPojo.someInt); + assertEquals(new Long(12345L), numberLongPojo.someLong); + assertEquals("long", numberLongPojo.someString); + } + + @Test + public void gsonHasNumberLongPrimitive() { + String actual = MongoGsonFactory.getGson().toJson(new NumberLongPojoWithPrimitive(42, 12345L, "long")); + assertEquals(NUMBER_LONG_POJO_STRING, actual); + } + + @Test + public void gsonReadsNumberLongPrimitive() { + NumberLongPojoWithPrimitive numberLongPojo = MongoGsonFactory.getGson().fromJson(NUMBER_LONG_POJO_STRING, NumberLongPojoWithPrimitive.class); + assertEquals(42, numberLongPojo.someInt); + assertEquals(12345L, numberLongPojo.someLong); + assertEquals("long", numberLongPojo.someString); + } + + @Test(expected = JsonSyntaxException.class) + public void gsonReadFailsWithNoObjectWrapper() { + MongoGsonFactory.getGson().fromJson(NUMBER_LONG_NONOBJECT_POJO_STRING, NumberLongPojo.class); + } + + @Test(expected = JsonSyntaxException.class) + public void gsonReadFailsWithNoObjectWrapperPrimitive() { + MongoGsonFactory.getGson().fromJson(NUMBER_LONG_NONOBJECT_POJO_STRING, NumberLongPojoWithPrimitive.class); + } + + private class NumberLongPojo { + private int someInt; + private Long someLong; + private String someString; + + private NumberLongPojo(int someInt, long someLong, String someString) { + this.someInt = someInt; + this.someLong = someLong; + this.someString = someString; + } + } + + private class NumberLongPojoWithPrimitive { + private int someInt; + private long someLong; + private String someString; + + private NumberLongPojoWithPrimitive(int someInt, long someLong, String someString) { + this.someInt = someInt; + this.someLong = someLong; + this.someString = someString; + } + } +} diff -r 025c66961cf9 -r c7af874342ed services/jvm-gc/src/main/java/com/redhat/thermostat/gateway/service/jvm/gc/mongo/JvmGcMongoStorageHandler.java --- a/services/jvm-gc/src/main/java/com/redhat/thermostat/gateway/service/jvm/gc/mongo/JvmGcMongoStorageHandler.java Mon Oct 02 14:41:38 2017 +0200 +++ b/services/jvm-gc/src/main/java/com/redhat/thermostat/gateway/service/jvm/gc/mongo/JvmGcMongoStorageHandler.java Wed Oct 04 12:57:57 2017 -0400 @@ -96,7 +96,7 @@ long prevWall = pre.get(Fields.WALL_TIME, Long.class); long thisWall = current.get(Fields.WALL_TIME, Long.class); long wallTimeDelta = prevWall - thisWall; - setWallTimeDelta(pre, wallTimeDelta); + pre.put(Fields.WALL_TIME_DELTA, wallTimeDelta); previous.put(collectorName, current); } else { previous.put(collectorName, current); @@ -130,7 +130,7 @@ long lastWall = first.get(Fields.WALL_TIME, Long.class); wallTimeDelta = prevWall - lastWall; } - setWallTimeDelta(item.getValue(), wallTimeDelta); + item.getValue().put(Fields.WALL_TIME_DELTA, wallTimeDelta); } if (metadata) { @@ -155,10 +155,4 @@ response.addMetaData(metaDataResponse.build()); } - - private void setWallTimeDelta(Document toSet, long delta) { - Document d = new Document(); - d.put(LONG_KEY, delta); - toSet.put(Fields.WALL_TIME_DELTA, d); - } } diff -r 025c66961cf9 -r c7af874342ed tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/gc/JvmGcServiceIntegrationTest.java Wed Oct 04 12:57:57 2017 -0400 @@ -297,7 +297,7 @@ assertEquals(200, response.getStatus()); - assertEquals("{\"metaData\":{\"insertCount\":1}}", response.getContentAsString()); + assertEquals("{\"metaData\":{\"insertCount\":{\"$numberLong\":\"1\"}}}", response.getContentAsString()); } @Test @@ -316,7 +316,7 @@ makeHttpMethodRequest(HttpMethod.POST, NO_QUERY, "[{\"f1\":\"test\"}]", "application/json", NO_EXPECTED_RESPONSE, 200); makeHttpMethodRequest(HttpMethod.DELETE, "?" + QUERY_PREFIX + "=f1==test&" + METADATA_PREFIX + "=true", - NO_DATA_TO_SEND, NO_DATA_TYPE, "{\"metaData\":{\"matchCount\":1}}", 200); + NO_DATA_TO_SEND, NO_DATA_TYPE, "{\"metaData\":{\"matchCount\":{\"$numberLong\":\"1\"}}}", 200); } @Test @@ -349,7 +349,7 @@ .send(); assertEquals(200, response.getStatus()); - assertEquals("{\"metaData\":{\"matchCount\":1}}", response.getContentAsString()); + assertEquals("{\"metaData\":{\"matchCount\":{\"$numberLong\":\"1\"}}}", response.getContentAsString()); } @@ -648,9 +648,9 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":600," + - "\"wallTimeInMicros\":212864" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":41977}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"600\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"212864\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"41977\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).send(); verifyResponse(response, expectedResponse, 200); @@ -663,9 +663,9 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":600," + - "\"wallTimeInMicros\":212864" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":0}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"600\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"212864\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"0\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "2").send(); verifyResponse(response, expectedResponse, 200); @@ -682,13 +682,13 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":600," + - "\"wallTimeInMicros\":212864" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":41977}},{\"collectorName\":\"CMS\"," + - "\"timeStamp\":500,\"wallTimeInMicros\":170887" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":156887}},{\"collectorName\":\"CMS\"," + - "\"timeStamp\":400,\"wallTimeInMicros\":14000" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":0}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"600\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"212864\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"41977\"}},{\"collectorName\":\"CMS\"," + + "\"timeStamp\":{\"$numberLong\":\"500\"},\"wallTimeInMicros\":{\"$numberLong\":\"170887\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"156887\"}},{\"collectorName\":\"CMS\"," + + "\"timeStamp\":{\"$numberLong\":\"400\"},\"wallTimeInMicros\":{\"$numberLong\":\"14000\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"0\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10").send(); verifyResponse(response, expectedResponse, 200); @@ -705,17 +705,17 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); String offset = "1"; - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":500," + - "\"wallTimeInMicros\":170887" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":156887}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"500\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"170887\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"156887\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "1").param("o", offset).send(); verifyResponse(response, expectedResponse, 200); offset = "2"; - expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":400," + - "\"wallTimeInMicros\":14000" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":0}}]}"; + expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"400\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"14000\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"0\"}}]}"; response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "1").param("o", offset).send(); verifyResponse(response, expectedResponse, 200); @@ -732,11 +732,11 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":600," + - "\"wallTimeInMicros\":212864" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":41977}},{\"collectorName\":\"CMS\"," + - "\"timeStamp\":500,\"wallTimeInMicros\":170887" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":156887}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"600\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"212864\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"41977\"}},{\"collectorName\":\"CMS\"," + + "\"timeStamp\":{\"$numberLong\":\"500\"},\"wallTimeInMicros\":{\"$numberLong\":\"170887\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"156887\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10").param("a", "400").send(); verifyResponse(response, expectedResponse, 200); @@ -753,9 +753,9 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":500," + - "\"wallTimeInMicros\":170887" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":156887}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"500\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"170887\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"156887\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10") .param("a", "400").param("o", "1").send(); @@ -773,9 +773,9 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":400," + - "\"wallTimeInMicros\":14000" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":0}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"400\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"14000\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"0\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10") .param("b", "500").send(); @@ -793,7 +793,8 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":500,\"wallTimeInMicros\":170887" + SYSTEM_JVM_FRAGMENT + ",\"wallTimeDelta\":{\"$numberLong\":156887}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"500\"},\"wallTimeInMicros\":{\"$numberLong\":\"170887\"}" + + SYSTEM_JVM_FRAGMENT + ",\"wallTimeDelta\":{\"$numberLong\":\"156887\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10") .param("b", "600").param("a", "400").send(); @@ -817,13 +818,13 @@ makeHttpMethodRequest(HttpMethod.POST, "", data, "application/json", "", 200); - String expectedResponse = "{\"response\":[{\"collectorName\":\"ABC\",\"timeStamp\":660," + - "\"wallTimeInMicros\":2000" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":1000}},{\"collectorName\":\"CMS\",\"timeStamp\":600," + - "\"wallTimeInMicros\":212864" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":41977}},{\"collectorName\":\"ABC\",\"timeStamp\":550," + - "\"wallTimeInMicros\":1000" + SYSTEM_JVM_FRAGMENT + "," + - "\"wallTimeDelta\":{\"$numberLong\":500}}]}"; + String expectedResponse = "{\"response\":[{\"collectorName\":\"ABC\",\"timeStamp\":{\"$numberLong\":\"660\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"2000\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"1000\"}},{\"collectorName\":\"CMS\",\"timeStamp\":{\"$numberLong\":\"600\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"212864\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"41977\"}},{\"collectorName\":\"ABC\",\"timeStamp\":{\"$numberLong\":\"550\"}," + + "\"wallTimeInMicros\":{\"$numberLong\":\"1000\"}" + SYSTEM_JVM_FRAGMENT + "," + + "\"wallTimeDelta\":{\"$numberLong\":\"500\"}}]}"; ContentResponse response = client.newRequest(deltaJvmUrl).method(HttpMethod.GET).param("l", "10") .param("a", "500").send(); diff -r 025c66961cf9 -r c7af874342ed tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvm/memory/JvmMemoryServiceIntegrationTest.java Wed Oct 04 12:57:57 2017 -0400 @@ -233,7 +233,7 @@ @Test public void testPostDataWithMetaData() throws InterruptedException, TimeoutException, ExecutionException { HttpTestUtil.addRecords(client, serviceUrl + "?" + METADATA_PREFIX + "=true", - "[{\"fakedata\":\"test\",\"a\":\"b\"},{\"c\":\"d\"}]", "{\"metaData\":{\"insertCount\":2}}"); + "[{\"fakedata\":\"test\",\"a\":\"b\"},{\"c\":\"d\"}]", "{\"metaData\":{\"insertCount\":{\"$numberLong\":\"2\"}}}"); } @Test @@ -274,7 +274,7 @@ public void testDeleteDifferentDataWithMetaData() throws InterruptedException, TimeoutException, ExecutionException { HttpTestUtil.addRecords(client, serviceUrl, "[{\"fakedata\":\"test\",\"a\":\"b\"},{\"c\":\"d\"}]"); HttpTestUtil.testContentlessResponse(client, HttpMethod.DELETE, serviceUrl + "?" + QUERY_PREFIX + "=a==b&" - + METADATA_PREFIX + "=true", 200, "{\"metaData\":{\"matchCount\":1}}"); + + METADATA_PREFIX + "=true", 200, "{\"metaData\":{\"matchCount\":{\"$numberLong\":\"1\"}}}"); } @Test @@ -298,7 +298,7 @@ ",\"c\":\"d\"}]}"; HttpTestUtil.addRecords(client, serviceUrl, "[{\"a\":\"b\"}]"); HttpTestUtil.testContentResponse(client, HttpMethod.PUT, serviceUrl + "?" + QUERY_PREFIX + "=a==b&" + - METADATA_PREFIX + "=true", "{\"set\":{\"c\":\"d\"}}", 200, "{\"metaData\":{\"matchCount\":1}}"); + METADATA_PREFIX + "=true", "{\"set\":{\"c\":\"d\"}}", 200, "{\"metaData\":{\"matchCount\":{\"$numberLong\":\"1\"}}}"); HttpTestUtil.testContentlessResponse(client, HttpMethod.GET, serviceUrl + "?l=5", 200, expectedDataResponse); } @Test diff -r 025c66961cf9 -r c7af874342ed tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java Wed Oct 04 12:57:57 2017 -0400 @@ -197,14 +197,14 @@ Map.of("jvmName", "vm", "agentId", "aid", "javaCommandLine", "j cl", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "javaVersion", "1.8.0_131", "jvmId", "jid2", "isAlive", false, "systemId", "1", "jvmPid", 2, "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", - "stopTime", 1495727607482L, + "stopTime", Map.ofNumberLong(1495727607482L), "vmArguments", "-Djline.log.jul=true", "jvmInfo", "mixed mode", "jvmVersion", "25.131-b12", @@ -241,7 +241,7 @@ "value", "truecolor"), Map.of("key", "_", "value", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre/../bin/java")), - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "uid", 1000)); expectedContentWrapper.matchJsonOrThrow(getResponse.getContentAsString()); } @@ -332,7 +332,7 @@ Map.of("javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "mainClass", "mc", "jvmId", "jid1", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "javaCommandLine", "j cl", "vmArguments", "-Djline.log.jul=true", "isAlive", true, @@ -370,13 +370,13 @@ "value", "truecolor"), Map.of("key", "_", "value", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre/../bin/java")), - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "jvmName", "vm", "jvmInfo", "mixed mode", "agentId", "aid", "systemId", "1", "javaVersion", "1.8.0_131", - "stopTime", -9223372036854775808L, + "stopTime", Map.ofNumberLong(-9223372036854775808L), "jvmPid", 1) ); @@ -396,15 +396,15 @@ ContentWrapper expectedContentWrapper = new ContentWrapper().addToResponse( Map.of("jvmName", "vm", - "lastUpdated", 333, - "stopTime", -9223372036854775808L, + "lastUpdated", Map.ofNumberLong(333), + "stopTime", Map.ofNumberLong(-9223372036854775808L), "mainClass", "mc", "vmArguments", "-Djline.log.jul=true", "jvmPid", 1, "uid", 1000, "jvmVersion", "25.131-b12", "javaVersion", "1.8.0_131", - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "javaCommandLine", "j cl", "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "username", "user", @@ -468,9 +468,9 @@ ContentWrapper expectedContentWrapper = new ContentWrapper().addToResponse( Map.of("username", "user", "vmArguments", "-Djline.log.jul=true", - "stopTime", -9223372036854775808L, + "stopTime", Map.ofNumberLong(-9223372036854775808L), "jvmName", "vm", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "systemId", "1", "isAlive", true, @@ -478,7 +478,7 @@ "javaVersion", "1.8.0_131", "javaCommandLine", "j cl", "jvmInfo", "mixed mode", - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "mainClass", "mc", "jvmVersion", "25.131-b12", "agentId", "aid", @@ -539,7 +539,7 @@ "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "vmArguments", "-Djline.log.jul=true", "jvmVersion", "25.131-b12", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "uid", 1000, "environment", Array.of( Map.of("key", "PATH", @@ -577,8 +577,8 @@ "jvmPid", 1, "jvmName", "vm", "jvmInfo", "mixed mode", - "stopTime", -9223372036854775808L, - "startTime", 1495727607481L), + "stopTime", Map.ofNumberLong(-9223372036854775808L), + "startTime", Map.ofNumberLong(1495727607481L)), Map.of("mainClass", "mc", "username", "user", "systemId", "1", @@ -588,7 +588,7 @@ "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "vmArguments", "-Djline.log.jul=true", "jvmVersion", "25.131-b12", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "uid", 1000, "environment", Array.of( Map.of("key", "PATH", @@ -626,8 +626,8 @@ "jvmPid", 2, "jvmName", "vm", "jvmInfo", "mixed mode", - "stopTime", 1495727607482L, - "startTime", 1495727607481L) + "stopTime", Map.ofNumberLong(1495727607482L), + "startTime", Map.ofNumberLong(1495727607481L)) ); expectedContentWrapper.matchJsonOrThrow(getResponse.getContentAsString()); @@ -681,16 +681,16 @@ "jvmName", "vm", "mainClass", "mc", "jvmPid", 2, - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "jvmInfo", "mixed mode", - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "vmArguments", "-Djline.log.jul=true", "uid", 1000, "jvmId", "jid2", "agentId", "aid", "systemId", "1", - "stopTime", 1495727607482L, + "stopTime", Map.ofNumberLong(1495727607482L), "isAlive", false, "javaVersion", "1.8.0_131", "username", "user", @@ -714,12 +714,12 @@ assertEquals(200, getResponse.getStatus()); ContentWrapper expectedContentWrapper = new ContentWrapper().addToResponse( - Map.of("startTime", 1495727607481L, + Map.of("startTime", Map.ofNumberLong(1495727607481L), "jvmPid", 2, "javaCommandLine", "j cl", "jvmName", "vm", - "lastUpdated", 333, - "stopTime", 1495727607482L, + "lastUpdated", Map.ofNumberLong(333), + "stopTime", Map.ofNumberLong(1495727607482L), "mainClass", "mc", "javaVersion", "1.8.0_131", "jvmVersion", "25.131-b12", @@ -783,7 +783,7 @@ ContentWrapper expectedContentWrapper = new ContentWrapper().addToResponse( Map.of("javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "agentId", "aid", - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "mainClass", "mc", "vmArguments", "-Djline.log.jul=true", "javaVersion", "1.8.0_131", @@ -827,8 +827,8 @@ "jvmVersion", "25.131-b12", "jvmPid", 2, "username", "user", - "stopTime", 1495727607482L, - "lastUpdated", 333, + "stopTime", Map.ofNumberLong(1495727607482L), + "lastUpdated", Map.ofNumberLong(333), "uid", 1000) ); @@ -904,14 +904,14 @@ "systemId", "1", "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", "jvmVersion", "25.131-b12", - "stopTime", -9223372036854775808L, + "stopTime", Map.ofNumberLong(-9223372036854775808L), "jvmId", "jid1", "jvmName", "vm", "jvmInfo", "mixed mode", "vmArguments", "-Djline.log.jul=true", "mainClass", "mc", - "startTime", 1495727607481L, - "lastUpdated", 333, + "startTime", Map.ofNumberLong(1495727607481L), + "lastUpdated", Map.ofNumberLong(333), "username", "user", "javaCommandLine", "j cl") ); @@ -995,11 +995,11 @@ "mainClass", "hello", "uid", 1000, "systemId", "1", - "stopTime", -9223372036854775808L, + "stopTime", Map.ofNumberLong(-9223372036854775808L), "jvmInfo", "mixed mode", "jvmVersion", "25.131-b12", - "lastUpdated", 333, - "startTime", 1495727607481L, + "lastUpdated", Map.ofNumberLong(333), + "startTime", Map.ofNumberLong(1495727607481L), "jvmPid", 1, "vmArguments", "-Djline.log.jul=true", "javaVersion", "1.7.0", @@ -1067,8 +1067,8 @@ "key", "COLORTERM"), Map.of("value", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre/../bin/java", "key", "_")), - "stopTime", -9223372036854775808L, - "lastUpdated", 333, + "stopTime", Map.ofNumberLong(-9223372036854775808L), + "lastUpdated", Map.ofNumberLong(333), "systemId", "1", "uid", 1000, "username", "user", @@ -1082,7 +1082,7 @@ "javaVersion", "1.8.0_131", "jvmInfo", "mixed mode", "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "agentId", "aid") ); @@ -1141,14 +1141,14 @@ "username", "user", "javaCommandLine", "j cl", "isAlive", false, - "startTime", 1495727607481L, + "startTime", Map.ofNumberLong(1495727607481L), "javaHome", "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre", - "stopTime", 1495727607482L, + "stopTime", Map.ofNumberLong(1495727607482L), "mainClass", "mc", "jvmId", "jid2", "agentId", "aid", "javaVersion", "1.8.0_131", - "lastUpdated", 333, + "lastUpdated", Map.ofNumberLong(333), "jvmPid", 2, "jvmName", "vm", "vmArguments", "-Djline.log.jul=true", @@ -1177,7 +1177,7 @@ assertEquals(200, response.getStatus()); ContentWrapper expectedContentWrapper = new ContentWrapper().addToResponse( - Map.of("lastUpdated", 2000, "isAlive", true)); + Map.of("lastUpdated", Map.ofNumberLong(2000), "isAlive", true)); expectedContentWrapper.matchJsonOrThrow(response.getContentAsString()); } diff -r 025c66961cf9 -r c7af874342ed tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/SystemIntegrationTestSuites.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/SystemIntegrationTestSuites.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/SystemIntegrationTestSuites.java Wed Oct 04 12:57:57 2017 -0400 @@ -51,7 +51,6 @@ public abstract class SystemIntegrationTestSuites extends SystemIntegrationTest { - protected static final String TIMESTAMP_TOKEN = "$TIMESTAMP$"; protected static final String AGENT_ID = getRandomSystemId(); diff -r 025c66961cf9 -r c7af874342ed tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/memory/SystemMemoryIntegrationTest.java --- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/memory/SystemMemoryIntegrationTest.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/memory/SystemMemoryIntegrationTest.java Wed Oct 04 12:57:57 2017 -0400 @@ -37,12 +37,14 @@ package com.redhat.thermostat.gateway.service.system.memory; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; +import com.redhat.thermostat.gateway.common.mongodb.response.MongoGsonFactory; import org.eclipse.jetty.client.api.ContentResponse; import org.junit.Test; @@ -65,41 +67,17 @@ private static final String AGENT_ID = getRandomSystemId(); private static final String memInfoJSON = "{\n" + - " \"total\" : 12566220800,\n" + - " \"free\" : 2338582528,\n" + - " \"buffers\" : 0,\n" + - " \"cached\" : 0,\n" + - " \"swapTotal\" : 17666494464,\n" + - " \"swapFree\" : 3524055040,\n" + - " \"commitLimit\" : 0,\n" + - " \"timeStamp\" : " + TIMESTAMP_TOKEN + ",\n" + + " \"total\" : { \"$numberLong\" : \"12566220800\" },\n" + + " \"free\" : { \"$numberLong\" : \"23677331911\" },\n" + + " \"buffers\" : { \"$numberLong\" : \"0\" },\n" + + " \"cached\" : { \"$numberLong\" : \"0\" },\n" + + " \"swapTotal\" : { \"$numberLong\" : \"17666494464\" },\n" + + " \"swapFree\" : { \"$numberLong\" : \"3524055040\" },\n" + + " \"commitLimit\" : { \"$numberLong\" : \"0\" },\n" + + " \"timestamp\" : { \"$numberLong\" : \"" + TIMESTAMP_TOKEN + "\" },\n" + " \"agentId\" : \"" + AGENT_ID + "\",\n" + "}"; - static class TinyMemoryInfo { - private String agentId; - private String systemId; - private long total; - private long free; - private long buffers; - - TinyMemoryInfo(String systemId, String agentId, long total, long free, long buffers) { - this.systemId = systemId; - this.agentId = agentId; - this.total = total; - this.free = free; - this.buffers = buffers; - } - - public String getAgentId() { - return agentId; - } - - public String getSystemId() { - return systemId; - } - } - public SystemMemoryIntegrationTest() { super(serviceURL, collectionName); } @@ -121,16 +99,6 @@ VersionTestUtil.testAllVersions(baseUrl + "/system-memory", versionString, "/systems/" + systemid); } - private static long getLong(JsonObject json, final String id) { - JsonElement el = json.get(id); - if (el.isJsonObject()) { - final JsonObject o = el.getAsJsonObject(); - return o.get("$numberLong").getAsLong(); - } else { - return el.getAsLong(); - } - } - @Override protected List parse(ContentResponse contentResponse, final String expectedSystemId) { List result = new ArrayList<>(); @@ -141,16 +109,24 @@ JsonArray allData = response.getAsJsonArray(); - Gson gson = new Gson(); - for (JsonElement entry : allData) { - TinyMemoryInfo TinyMemoryEntry = gson.fromJson(entry.toString(), TinyMemoryInfo.class); + Gson gson = MongoGsonFactory.getGson(); + for (int entryIndex = 0; entryIndex < allData.size(); entryIndex++) { + TinyMemoryInfo tinyMemoryEntry = gson.fromJson(allData.get(entryIndex).toString(), TinyMemoryInfo.class); - assertEquals(AGENT_ID, TinyMemoryEntry.getAgentId()); + assertEquals(AGENT_ID, tinyMemoryEntry.getAgentId()); if (expectedSystemId != null) { - assertEquals(expectedSystemId, TinyMemoryEntry.getSystemId()); + assertEquals(expectedSystemId, tinyMemoryEntry.getSystemId()); } + assertEquals(12566220800L, tinyMemoryEntry.getTotal()); + assertEquals(23677331911L, tinyMemoryEntry.getFree()); + assertEquals(0L, tinyMemoryEntry.getBuffers()); + assertEquals(0L, tinyMemoryEntry.getCached()); + assertEquals(17666494464L, tinyMemoryEntry.getSwapTotal()); + assertEquals(3524055040L, tinyMemoryEntry.getSwapFree()); + assertEquals(0L, tinyMemoryEntry.getCommitLimit()); + assertNotEquals(0L, tinyMemoryEntry.getTimestamp()); - result.add(TinyMemoryEntry); + result.add(tinyMemoryEntry); } return result; @@ -195,4 +171,57 @@ public void testSystemMemoryDeleteOne() throws InterruptedException, ExecutionException, TimeoutException { super.testDeleteOne(); } + + static class TinyMemoryInfo { + private String agentId; + private String systemId; + private Long timestamp; + private Long total; + private Long free; + private Long buffers; + private Long cached; + private Long swapTotal; + private Long swapFree; + private Long commitLimit; + + public String getAgentId() { + return agentId; + } + + public String getSystemId() { + return systemId; + } + + public long getTimestamp() { + return timestamp; + } + + public long getTotal() { + return total; + } + + public long getFree() { + return free; + } + + public long getBuffers() { + return buffers; + } + + public long getCached() { + return cached; + } + + public long getSwapTotal() { + return swapTotal; + } + + public long getSwapFree() { + return swapFree; + } + + public long getCommitLimit() { + return commitLimit; + } + } } diff -r 025c66961cf9 -r c7af874342ed tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/ContentWrapper.java --- a/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/ContentWrapper.java Mon Oct 02 14:41:38 2017 +0200 +++ b/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/ContentWrapper.java Wed Oct 04 12:57:57 2017 -0400 @@ -171,6 +171,10 @@ } return map; } + + public static HashMap ofNumberLong(long longValue) { + return Map.of("$numberLong", Long.toString(longValue)); + } } // A small class designed to get Gson to write a list as a list of