From 266450099096400d6134cc429a462bb52c198b06 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 2 Oct 2017 11:02:12 +0200 Subject: [PATCH] SONAR-9820 update telemetry format - list instead of map --- .../telemetry/TelemetryDataJsonWriter.java | 27 ++++++++- .../server/platform/ws/InfoActionTest.java | 2 +- .../server/telemetry/telemetry-example.json | 57 ++++++++++++++----- .../tests/telemetry/TelemetryUploadTest.java | 18 +++--- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java index dba9dc46afe..569841f7743 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java @@ -35,15 +35,36 @@ public class TelemetryDataJsonWriter { json.prop("id", statistics.getServerId()); json.prop("version", statistics.getVersion()); json.name("plugins"); - json.valueObject(statistics.getPlugins()); + json.beginArray(); + statistics.getPlugins().forEach((plugin, version) -> { + json.beginObject(); + json.prop("name", plugin); + json.prop("version", version); + json.endObject(); + }); + json.endArray(); json.prop("userCount", statistics.getUserCount()); json.prop("projectCount", statistics.getProjectCount()); json.prop(LINES_KEY, statistics.getLines()); json.prop(NCLOC_KEY, statistics.getNcloc()); json.name("projectCountByLanguage"); - json.valueObject(statistics.getProjectCountByLanguage()); + json.beginArray(); + statistics.getProjectCountByLanguage().forEach((language, count) -> { + json.beginObject(); + json.prop("language", language); + json.prop("count", count); + json.endObject(); + }); + json.endArray(); json.name("nclocByLanguage"); - json.valueObject(statistics.getNclocByLanguage()); + json.beginArray(); + statistics.getNclocByLanguage().forEach((language, ncloc) -> { + json.beginObject(); + json.prop("language", language); + json.prop("ncloc", ncloc); + json.endObject(); + }); + json.endArray(); json.endObject(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java index 6efbac1161f..ab6ef319a94 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java @@ -102,7 +102,7 @@ public class InfoActionTest { TestResponse response = ws.newRequest().execute(); // response does not contain empty "Section Three" assertThat(response.getInput()).isEqualTo("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," + - "\"Statistics\":{\"plugins\":{},\"userCount\":0,\"projectCount\":0,\"lines\":0,\"ncloc\":0,\"projectCountByLanguage\":{},\"nclocByLanguage\":{}}}"); + "\"Statistics\":{\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"lines\":0,\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[]}}"); } private void logInAsSystemAdministrator() { diff --git a/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json index b17a4aab7c4..93cf4f1280f 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json @@ -1,23 +1,50 @@ { "id": "AU-TpxcB-iU5OvuD2FL7", "version": "7.5.4", - "plugins": { - "java": "4.12.0.11033", - "scmgit": "1.2", - "other": "undefined" - }, + "plugins": [ + { + "name": "java", + "version": "4.12.0.11033" + }, + { + "name": "scmgit", + "version": "1.2" + }, + { + "name": "other", + "version": "undefined" + } + ], "userCount": 3, "projectCount": 2, "lines": 500, "ncloc": 300, - "projectCountByLanguage": { - "java": 2, - "kotlin": 1, - "js": 1 - }, - "nclocByLanguage": { - "java": 500, - "kotlin": 2500, - "js": 50 - } + "projectCountByLanguage": [ + { + "language": "java", + "count": 2 + }, + { + "language": "kotlin", + "count": 1 + }, + { + "language": "js", + "count": 1 + } + ], + "nclocByLanguage": [ + { + "language": "java", + "ncloc": 500 + }, + { + "language": "kotlin", + "ncloc": 2500 + }, + { + "language": "js", + "ncloc": 50 + } + ] } diff --git a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java index 501c864706a..dc4d3c0a9ff 100644 --- a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java +++ b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java @@ -20,7 +20,9 @@ package org.sonarqube.tests.telemetry; import com.sonar.orchestrator.Orchestrator; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.ws.rs.core.HttpHeaders; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; @@ -32,6 +34,7 @@ import org.sonarqube.ws.client.GetRequest; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static util.ItUtils.jsonToMap; import static util.ItUtils.runProjectAnalysis; import static util.ItUtils.setServerProperty; @@ -80,15 +83,16 @@ public class TelemetryUploadTest { Map json = jsonToMap(request.getBody().readUtf8()); assertThat(json.get("id")).isEqualTo(serverId()); assertThat(getInteger(json.get("userCount"))).isEqualTo(1); - assertThat(((Map) json.get("plugins")).keySet()).contains("xoo"); + List plugins = ((List>) json.get("plugins")).stream().map(p -> p.get("name")).collect(Collectors.toList()); + assertThat(plugins).contains("xoo"); assertThat(getInteger(json.get("ncloc"))).isEqualTo(13 * 2 + 7); assertThat(getInteger(json.get("lines"))).isEqualTo(17 * 3); - Map projectCountByLanguage = (Map) json.get("projectCountByLanguage"); - assertThat(getInteger(projectCountByLanguage.get("xoo"))).isEqualTo(2); - assertThat(getInteger(projectCountByLanguage.get("xoo2"))).isEqualTo(1); - Map nclocByLanguage = (Map) json.get("nclocByLanguage"); - assertThat(getInteger(nclocByLanguage.get("xoo"))).isEqualTo(13 * 2); - assertThat(getInteger(nclocByLanguage.get("xoo2"))).isEqualTo(7); + List> projectCountByLanguage = (List>) json.get("projectCountByLanguage"); + assertThat(projectCountByLanguage).extracting(p -> p.get("language"), p -> getInteger(p.get("count"))) + .contains(tuple("xoo", 2), tuple("xoo2", 1)); + List> nclocByLanguage = (List>) json.get("nclocByLanguage"); + assertThat(nclocByLanguage).extracting(p -> p.get("language"), p -> getInteger(p.get("ncloc"))) + .contains(tuple("xoo", 13 * 2), tuple("xoo2", 7)); } private String serverId() { -- 2.39.5