]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6429 WS to support settings navigation 254/head
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 23 Apr 2015 13:46:21 +0000 (15:46 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 27 Apr 2015 13:06:26 +0000 (15:06 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
server/sonar-server/src/main/java/org/sonar/server/ui/ws/SettingsNavigationAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-settings.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/ui/ws/SettingsNavigationActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/empty.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_update_center.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_views.json [new file with mode: 0644]

index 5cf50dc2bd3ffc211e2de891a567009c99e10bc4..b0e7accb7619c71857ed4c650f94ef82b0f5ab60 100644 (file)
  */
 package org.sonar.server.platform;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Properties;
-
-import javax.annotation.Nullable;
-
+import com.google.common.collect.Lists;
 import org.sonar.api.config.EmailSettings;
 import org.sonar.api.issue.action.Actions;
 import org.sonar.api.platform.ComponentContainer;
@@ -362,6 +356,7 @@ import org.sonar.server.ui.PageDecorations;
 import org.sonar.server.ui.Views;
 import org.sonar.server.ui.ws.GlobalNavigationAction;
 import org.sonar.server.ui.ws.NavigationWs;
+import org.sonar.server.ui.ws.SettingsNavigationAction;
 import org.sonar.server.updatecenter.ws.UpdateCenterWs;
 import org.sonar.server.user.DefaultUserService;
 import org.sonar.server.user.DoPrivileged;
@@ -393,7 +388,12 @@ import org.sonar.server.view.index.ViewIndexer;
 import org.sonar.server.ws.ListingWs;
 import org.sonar.server.ws.WebServiceEngine;
 
-import com.google.common.collect.Lists;
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Properties;
 
 class ServerComponents {
 
@@ -922,6 +922,7 @@ class ServerComponents {
 
     // UI
     pico.addSingleton(GlobalNavigationAction.class);
+    pico.addSingleton(SettingsNavigationAction.class);
     pico.addSingleton(NavigationWs.class);
 
     for (Object components : level4AddedComponents) {
index 3895d6d7a40eae61e2d9b31f9b902e90ec10c83e..0f774f15e039ac3e1006d85466becde66cd104a2 100644 (file)
@@ -21,12 +21,12 @@ package org.sonar.server.plugins;
 
 import com.google.common.base.Charsets;
 import org.apache.commons.io.IOUtils;
-import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
 import org.sonar.api.PropertyType;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.UriReader;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.updatecenter.common.UpdateCenter;
 import org.sonar.updatecenter.common.UpdateCenterDeserializer;
 import org.sonar.updatecenter.common.UpdateCenterDeserializer.Mode;
@@ -43,7 +43,7 @@ import java.util.Date;
  */
 @Properties({
   @Property(
-    key = "sonar.updatecenter.activate",
+    key = UpdateCenterClient.ACTIVATION_PROPERTY,
     defaultValue = "true",
     name = "Enable Update Center",
     project = false,
@@ -63,6 +63,7 @@ import java.util.Date;
 public class UpdateCenterClient {
 
   public static final String URL_PROPERTY = "sonar.updatecenter.url";
+  public static final String ACTIVATION_PROPERTY = "sonar.updatecenter.activate";
   public static final int PERIOD_IN_MILLISECONDS = 60 * 60 * 1000;
 
   private final URI uri;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/SettingsNavigationAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/SettingsNavigationAction.java
new file mode 100644 (file)
index 0000000..5597ac6
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.ui.ws;
+
+import org.sonar.api.config.Settings;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService.NewController;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.api.web.NavigationSection;
+import org.sonar.api.web.Page;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.plugins.UpdateCenterClient;
+import org.sonar.server.ui.ViewProxy;
+import org.sonar.server.ui.Views;
+import org.sonar.server.user.UserSession;
+
+public class SettingsNavigationAction implements NavigationAction {
+
+  private final Settings settings;
+  private final Views views;
+  private final I18n i18n;
+
+  public SettingsNavigationAction(Settings settings, Views views, I18n i18n) {
+    this.views = views;
+    this.settings = settings;
+    this.i18n = i18n;
+  }
+
+  @Override
+  public void define(NewController context) {
+    context.createAction("settings")
+      .setDescription("Get configuration information for the settings page:" +
+        "<ul>" +
+        "  <li>List plugin-contributed pages</li>" +
+        "  <li>Show update center (or not)</li>" +
+        "</ul>")
+      .setHandler(this)
+      .setInternal(true)
+      .setResponseExample(getClass().getResource("example-settings.json"))
+      .setSince("5.2");
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    boolean isAdmin = UserSession.get().hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
+
+    JsonWriter json = response.newJsonWriter().beginObject();
+    json.prop("showUpdateCenter", isAdmin && settings.getBoolean(UpdateCenterClient.ACTIVATION_PROPERTY));
+
+    json.name("plugins").beginArray();
+    if (isAdmin) {
+      for (ViewProxy<Page> page : views.getPages(NavigationSection.CONFIGURATION, null, null, null, null)) {
+        json.beginObject()
+          .prop("name", i18n.message(UserSession.get().locale(), String.format("%s.page", page.getTitle()), page.getTitle()))
+          .prop("url", getPageUrl(page))
+          .endObject();
+      }
+    }
+    json.endArray();
+
+    json.endObject().close();
+  }
+
+  private String getPageUrl(ViewProxy<Page> page) {
+    return page.isController() ? page.getId() : String.format("/plugins/configuration/%s", page.getId());
+  }
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-settings.json b/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-settings.json
new file mode 100644 (file)
index 0000000..e9ebff7
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "showUpdateCenter": true,
+  "plugins": [
+    {
+      "name": "Views",
+      "url": "/plugins/configuration/views"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/SettingsNavigationActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/SettingsNavigationActionTest.java
new file mode 100644 (file)
index 0000000..2c20285
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.ui.ws;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.config.Settings;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.web.NavigationSection;
+import org.sonar.api.web.Page;
+import org.sonar.api.web.View;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.plugins.UpdateCenterClient;
+import org.sonar.server.ui.Views;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import java.util.Locale;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SettingsNavigationActionTest {
+
+  private WsTester wsTester;
+
+  private Settings settings;
+
+  private I18n i18n;
+
+  @Before
+  public void before() throws Exception {
+    settings = new Settings();
+    i18n = mock(I18n.class);
+    when(i18n.message(any(Locale.class), anyString(), anyString())).thenAnswer(new Answer<String>() {
+      @Override
+      public String answer(InvocationOnMock invocation) throws Throwable {
+        return invocation.getArgumentAt(2, String.class);
+      }
+      
+    });
+  }
+
+  @Test
+  public void empty() throws Exception {
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+    wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, new Views(), i18n)));
+
+    wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "empty.json");
+  }
+
+  @Test
+  public void with_views() throws Exception {
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+    wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, createViews(), i18n)));
+
+    wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "with_views.json");
+  }
+
+  @Test
+  public void with_update_center() throws Exception {
+    settings.setProperty(UpdateCenterClient.ACTIVATION_PROPERTY, true);
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+    wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, new Views(), i18n)));
+
+    wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "with_update_center.json");
+  }
+
+  @Test
+  public void with_views_and_update_center_but_not_admin() throws Exception {
+    MockUserSession.set();
+    settings.setProperty(UpdateCenterClient.ACTIVATION_PROPERTY, true);
+    wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, createViews(), i18n)));
+
+    wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "empty.json");
+  }
+
+  private Views createViews() {
+
+
+    @NavigationSection(NavigationSection.CONFIGURATION)
+    class FirstPage implements Page {
+      @Override
+      public String getTitle() {
+        return "First Page";
+      }
+
+      @Override
+      public String getId() {
+        return "first_page";
+      }
+    }
+
+    @NavigationSection(NavigationSection.CONFIGURATION)
+    class SecondPage implements Page {
+      @Override
+      public String getTitle() {
+        return "Second Page";
+      }
+
+      @Override
+      public String getId() {
+        return "/second/page";
+      }
+    }
+
+    Page page = new FirstPage();
+    Page controller = new SecondPage();
+    return new Views(new View[] {page, controller});
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/empty.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/empty.json
new file mode 100644 (file)
index 0000000..b4397d0
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "showUpdateCenter": false,
+  "plugins": []
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_update_center.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_update_center.json
new file mode 100644 (file)
index 0000000..cfed4e3
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "showUpdateCenter": true,
+  "plugins": []
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_views.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/SettingsNavigationActionTest/with_views.json
new file mode 100644 (file)
index 0000000..bcdd264
--- /dev/null
@@ -0,0 +1,13 @@
+{
+  "showUpdateCenter": false,
+  "plugins": [
+    {
+      "name": "First Page",
+      "url": "/plugins/configuration/first_page"
+    },
+    {
+      "name": "Second Page",
+      "url": "/second/page"
+    }
+  ]
+}