]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6376 JSON response must have a root object
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 15 Apr 2015 15:07:03 +0000 (17:07 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 17 Apr 2015 14:46:14 +0000 (16:46 +0200)
this leave the door open to extend the response without breaking the API

server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledPluginsWsAction.java
server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-installed_plugins.json
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledPluginsWsActionTest.java

index dc5345f01918761f92dfa2ce204fdc8c6616d0c9..7eb6051e7cdfcf1b5b6c8cb25a2a71954c071010 100644 (file)
@@ -45,6 +45,7 @@ public class InstalledPluginsWsAction implements PluginsWsAction {
   private static final String PROPERTY_KEY = "key";
   private static final String PROPERTY_NAME = "name";
   private static final String PROPERTY_VERSION = "version";
+  private static final String ARRAY_PLUGINS = "plugins";
 
   private final PluginRepository pluginRepository;
 
@@ -68,7 +69,14 @@ public class InstalledPluginsWsAction implements PluginsWsAction {
   @Override
   public void handle(Request request, Response response) throws Exception {
     Collection<PluginMetadata> pluginMetadatas = retrieveAndSortPluginMetadata();
-    writeMetadataList(response, pluginMetadatas);
+
+    JsonWriter jsonWriter = response.newJsonWriter();
+    jsonWriter.beginObject();
+
+    writeMetadataList(jsonWriter, pluginMetadatas);
+
+    jsonWriter.endObject();
+    jsonWriter.close();
   }
 
   private SortedSet<PluginMetadata> retrieveAndSortPluginMetadata() {
@@ -78,14 +86,13 @@ public class InstalledPluginsWsAction implements PluginsWsAction {
       );
   }
 
-  private void writeMetadataList(Response response, Collection<PluginMetadata> pluginMetadatas) {
-    JsonWriter jsonWriter = response.newJsonWriter();
-    jsonWriter.beginArray();
-    for (PluginMetadata pluginMetadata : pluginMetadatas) {
-      writeMetadata(jsonWriter, pluginMetadata);
-    }
-    jsonWriter.endArray();
-    jsonWriter.close();
+  private void writeMetadataList(JsonWriter jsonWriter, Collection<PluginMetadata> pluginMetadatas) {
+      jsonWriter.name(ARRAY_PLUGINS);
+      jsonWriter.beginArray();
+      for (PluginMetadata pluginMetadata : pluginMetadatas) {
+        writeMetadata(jsonWriter, pluginMetadata);
+      }
+      jsonWriter.endArray();
   }
 
   private void writeMetadata(JsonWriter jsonWriter, PluginMetadata pluginMetadata) {
index ca65c9072da730380255730067b90fa0f8c3000b..546ff975a24a19f3c2a969d18264c470be3f8456 100644 (file)
@@ -1,17 +1,20 @@
-[
-  {
-    "key": "findbugs",
-    "name": "Findbugs",
-    "version": "2.1"
-  },
-  {
-    "key": "l10nfr",
-    "name": "French Pack",
-    "version": "1.10"
-  },
-  {
-    "key": "jira",
-    "name": "JIRA",
-    "version": "1.2"
-  }
-]
+{
+  "plugins":
+  [
+    {
+      "key": "findbugs",
+      "name": "Findbugs",
+      "version": "2.1"
+    },
+    {
+      "key": "l10nfr",
+      "name": "French Pack",
+      "version": "1.10"
+    },
+    {
+      "key": "jira",
+      "name": "JIRA",
+      "version": "1.2"
+    }
+  ]
+}
index 55f641c443bf159610745c5054d8cb8e580bb044..b497fa0acc5e92cc011fda91ee173bd19f542521 100644 (file)
@@ -36,7 +36,10 @@ import static org.sonar.test.JsonAssert.assertJson;
 
 public class InstalledPluginsWsActionTest {
   private static final String DUMMY_CONTROLLER_KEY = "dummy";
-  private static final String JSON_EMPTY_ARRAY = "[]";
+  private static final String JSON_EMPTY_PLUGIN_LIST =
+    "{" +
+      "  \"plugins\":" + "[]" +
+      "}";
 
   private PluginRepository pluginRepository = mock(PluginRepository.class);
   private InstalledPluginsWsAction underTest = new InstalledPluginsWsAction(pluginRepository);
@@ -78,7 +81,7 @@ public class InstalledPluginsWsActionTest {
   public void empty_array_is_returned_when_there_is_not_plugin_installed() throws Exception {
     underTest.handle(request, response);
 
-    assertThat(response.outputAsString()).isEqualTo("[]");
+    assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
   }
 
   @Test
@@ -87,7 +90,7 @@ public class InstalledPluginsWsActionTest {
 
     underTest.handle(request, response);
 
-    assertThat(response.outputAsString()).isEqualTo(JSON_EMPTY_ARRAY);
+    assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
   }
 
   @Test
@@ -97,74 +100,85 @@ public class InstalledPluginsWsActionTest {
     underTest.handle(request, response);
 
     assertJson(response.outputAsString()).isSimilarTo(
-        "[" +
-            "{" +
-            "  \"key\": \"plugKey\"," +
-            "  \"name\": \"plugName\"," +
-            "  \"version\": \"10\"" +
-            "}" +
-            "]"
-    );
+      "{" +
+        "  \"plugins\":" +
+        "  [" +
+        "    {" +
+        "      \"key\": \"plugKey\"," +
+        "      \"name\": \"plugName\"," +
+        "      \"version\": \"10\"" +
+        "    }" +
+        "  ]" +
+        "}"
+      );
   }
 
   @Test
   public void plugins_are_sorted_by_name_then_key_and_only_one_plugin_can_have_a_specific_name() throws Exception {
     when(pluginRepository.getMetadata()).thenReturn(
-        of(
-            plugin("A", "name2"),
-            plugin("B", "name1"),
-            plugin("C", "name0"),
-            plugin("D", "name0")
-        )
-    );
+      of(
+        plugin("A", "name2"),
+        plugin("B", "name1"),
+        plugin("C", "name0"),
+        plugin("D", "name0")
+      )
+      );
 
     underTest.handle(request, response);
 
     assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
-        "[" +
-            "{\"key\": \"C\"}" + "," +
-            "{\"key\": \"D\"}" + "," +
-            "{\"key\": \"B\"}" + "," +
-            "{\"key\": \"A\"}" +
-            "]"
-    );
+      "{" +
+        "  \"plugins\":" +
+        "  [" +
+        "    {\"key\": \"C\"}" + "," +
+        "    {\"key\": \"D\"}" + "," +
+        "    {\"key\": \"B\"}" + "," +
+        "    {\"key\": \"A\"}" +
+        "  ]" +
+        "}"
+      );
   }
 
   @Test
   public void only_one_plugin_can_have_a_specific_name_and_key() throws Exception {
     when(pluginRepository.getMetadata()).thenReturn(
-        of(
-            plugin("A", "name2"),
-            plugin("A", "name2")
-        )
-    );
+      of(
+        plugin("A", "name2"),
+        plugin("A", "name2")
+      )
+      );
 
     underTest.handle(request, response);
 
     assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
-        "[" +
-            "{\"key\": \"A\"}" +
-            "]"
-    );
+      "{" +
+        "  \"plugins\":" +
+        "  [" +
+        "    {\"key\": \"A\"}" +
+        "  ]" +
+        "}"
+      );
     assertThat(response.outputAsString()).containsOnlyOnce("name2");
   }
 
   @Test
   public void dash_is_returned_when_version_is_null() throws Exception {
     when(pluginRepository.getMetadata()).thenReturn(
-        of(
-            (PluginMetadata) DefaultPluginMetadata.create("key").setCore(false).setVersion(null)
-        )
-    );
+      of(
+      (PluginMetadata) DefaultPluginMetadata.create("key").setCore(false).setVersion(null)
+      )
+      );
 
     underTest.handle(request, response);
 
-    assertJson(response.outputAsString())
-        .isSimilarTo(
-            "[" +
-                "{\"version\": \"-\"}" +
-            "]"
-        );
+    assertJson(response.outputAsString()).isSimilarTo(
+      "{" +
+        "  \"plugins\":" +
+        "  [" +
+        "    {\"version\": \"-\"}" +
+        "  ]" +
+        "}"
+      );
 
   }
-}
\ No newline at end of file
+}