]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8449 Return some common settings in api/navigation/global
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Nov 2016 15:16:54 +0000 (16:16 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 1 Dec 2016 09:12:48 +0000 (10:12 +0100)
17 files changed:
server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalNavigationAction.java
server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-global.json
server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalNavigationActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/admin.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/anonymous.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/deprecated_logo_settings.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/empty.json
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_admin.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_anonymous.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_user.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/only_logo.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/production_database.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/qualifiers.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/settings.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/user.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/version.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/with_qualifiers.json [deleted file]

index 6f5df35ff4691267c341c03ffa8dbefa12af2fd9..2340b2f1e45c0030b733320f2e931a913afa7381 100644 (file)
  */
 package org.sonar.server.ui.ws;
 
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
 import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
 import org.sonar.api.resources.ResourceType;
 import org.sonar.api.resources.ResourceTypes;
 import org.sonar.api.server.ws.Request;
@@ -28,22 +31,44 @@ 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.db.Database;
+import org.sonar.db.dialect.H2;
 import org.sonar.server.ui.ViewProxy;
 import org.sonar.server.ui.Views;
 
+import static org.sonar.api.CoreProperties.CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY;
+import static org.sonar.api.CoreProperties.HOURS_IN_DAY;
+import static org.sonar.api.CoreProperties.RATING_GRID;
+import static org.sonar.core.config.WebConstants.SONAR_LF_ENABLE_GRAVATAR;
+import static org.sonar.core.config.WebConstants.SONAR_LF_GRAVATAR_SERVER_URL;
 import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_URL;
 import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_WIDTH_PX;
+import static org.sonar.core.config.WebConstants.SONAR_UPDATECENTER_ACTIVATE;
 
 public class GlobalNavigationAction implements NavigationWsAction {
 
+  private static final Set<String> SETTING_KEYS = ImmutableSet.of(
+    SONAR_LF_LOGO_URL,
+    SONAR_LF_LOGO_WIDTH_PX,
+    SONAR_LF_ENABLE_GRAVATAR,
+    SONAR_LF_GRAVATAR_SERVER_URL,
+    SONAR_UPDATECENTER_ACTIVATE,
+    HOURS_IN_DAY,
+    RATING_GRID,
+    CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY);
+
   private final Views views;
   private final Settings settings;
   private final ResourceTypes resourceTypes;
+  private final Server server;
+  private final Database database;
 
-  public GlobalNavigationAction(Views views, Settings settings, ResourceTypes resourceTypes) {
+  public GlobalNavigationAction(Views views, Settings settings, ResourceTypes resourceTypes, Server server, Database database) {
     this.views = views;
     this.settings = settings;
     this.resourceTypes = resourceTypes;
+    this.server = server;
+    this.database = database;
   }
 
   @Override
@@ -60,8 +85,11 @@ public class GlobalNavigationAction implements NavigationWsAction {
   public void handle(Request request, Response response) throws Exception {
     JsonWriter json = response.newJsonWriter().beginObject();
     writePages(json);
-    writeLogoProperties(json);
+    writeSettings(json);
+    writeDeprecatedLogoProperties(json);
     writeQualifiers(json);
+    writeVersion(json);
+    writeDatabaseProduction(json);
     json.endObject().close();
   }
 
@@ -78,7 +106,15 @@ public class GlobalNavigationAction implements NavigationWsAction {
     json.endArray();
   }
 
-  private void writeLogoProperties(JsonWriter json) {
+  private void writeSettings(JsonWriter json) {
+    json.name("settings").beginObject();
+    for (String settingKey : SETTING_KEYS) {
+      json.prop(settingKey, settings.getString(settingKey));
+    }
+    json.endObject();
+  }
+
+  private void writeDeprecatedLogoProperties(JsonWriter json) {
     json.prop("logoUrl", settings.getString(SONAR_LF_LOGO_URL));
     json.prop("logoWidth", settings.getString(SONAR_LF_LOGO_WIDTH_PX));
   }
@@ -90,4 +126,12 @@ public class GlobalNavigationAction implements NavigationWsAction {
     }
     json.endArray();
   }
+
+  private void writeVersion(JsonWriter json) {
+    json.prop("version", server.getVersion());
+  }
+
+  private void writeDatabaseProduction(JsonWriter json) {
+    json.prop("productionDatabase", !database.getDialect().getId().equals(H2.ID));
+  }
 }
index 44dcbd3e24c2a5e7efdcdeab4228d7ffddb78bae..311f46af0320ff5fb014d2ec071e63be58612d79 100644 (file)
@@ -1,17 +1,30 @@
 {
-  "globalDashboards": [
-    {
-      "key": 51,
-      "name": "Helicopter View"
-    }
-  ],
   "globalPages": [
     {
-      "name": "My Plugin",
-      "url": "/plugins/home/my-plugin"
+      "name": "My Plugin Page",
+      "url": "/plugins/home/my_plugin_page"
+    },
+    {
+      "name": "My Rails App",
+      "url": "/my_rails_app"
     }
   ],
+  "settings": {
+    "sonar.lf.logoUrl": "http://example.com/my-custom-logo.png",
+    "sonar.lf.logoWidthPx": "135",
+    "sonar.lf.enableGravatar": "true",
+    "sonar.lf.gravatarServerUrl": "http://some-server.tld/logo.png",
+    "sonar.updatecenter.activate": "false",
+    "sonar.technicalDebt.hoursInDay": "10",
+    "sonar.technicalDebt.ratingGrid": "0.05,0.1,0.2,0.5",
+    "sonar.allowUsersToSignUp": "true"
+  },
   "logoUrl": "http://example.com/my-custom-logo.png",
-  "logoWidth": 135,
-  "qualifiers": ["TRK", "VW"]
+  "logoWidth": "135",
+  "qualifiers": [
+    "PAL",
+    "POL"
+  ],
+  "version": "6.2",
+  "productionDatabase": true
 }
index 759b091eac05c4affaa67f5191ddf615f18a0521..4419611cbfbc8233d887e1c433d35cb4f8c77766 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
 import org.sonar.api.resources.ResourceType;
 import org.sonar.api.resources.ResourceTypeTree;
 import org.sonar.api.resources.ResourceTypes;
@@ -31,11 +32,17 @@ import org.sonar.api.web.Page;
 import org.sonar.api.web.UserRole;
 import org.sonar.api.web.View;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MySql;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ui.Views;
 import org.sonar.server.ws.WsActionTester;
 import org.sonar.test.JsonAssert;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 public class GlobalNavigationActionTest {
 
   @Rule
@@ -43,6 +50,9 @@ public class GlobalNavigationActionTest {
 
   private Settings settings = new MapSettings();
 
+  private Server server = mock(Server.class);
+  private Database database = mock(Database.class);
+
   private WsActionTester ws;
 
   @Test
@@ -53,7 +63,7 @@ public class GlobalNavigationActionTest {
   }
 
   @Test
-  public void with_root_qualifiers() throws Exception {
+  public void return_qualifiers() throws Exception {
     init(new View[] {}, new ResourceTypeTree[] {
       ResourceTypeTree.builder()
         .addType(ResourceType.builder("POL").build())
@@ -67,54 +77,101 @@ public class GlobalNavigationActionTest {
         .build()
     });
 
-    executeAndVerify("with_qualifiers.json");
+    executeAndVerify("qualifiers.json");
+  }
+
+  @Test
+  public void return_settings() throws Exception {
+    init();
+    settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+    settings.setProperty("sonar.lf.logoWidthPx", 135);
+    settings.setProperty("sonar.lf.gravatarServerUrl", "https://secure.gravatar.com/avatar/{EMAIL_MD5}.jpg?s={SIZE}&d=identicon");
+    settings.setProperty("sonar.lf.enableGravatar", true);
+    settings.setProperty("sonar.updatecenter.activate", false);
+    settings.setProperty("sonar.technicalDebt.hoursInDay", "10");
+    settings.setProperty("sonar.technicalDebt.ratingGrid", "0.05,0.1,0.2,0.5");
+    settings.setProperty("sonar.allowUsersToSignUp", true);
+    // This setting should be ignored as it's not needed
+    settings.setProperty("sonar.defaultGroup", "sonar-users");
+
+    executeAndVerify("settings.json");
   }
 
   @Test
-  public void only_logo() throws Exception {
+  public void return_deprecated_logo_settings() throws Exception {
     init();
-    settings.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png");
-    settings.setProperty("sonar.lf.logoWidthPx", "123");
+    settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+    settings.setProperty("sonar.lf.logoWidthPx", 135);
 
-    executeAndVerify("only_logo.json");
+    executeAndVerify("deprecated_logo_settings.json");
   }
 
   @Test
-  public void nominal_call_for_anonymous() throws Exception {
+  public void return_global_pages_for_anonymous() throws Exception {
     init(createViews(), new ResourceTypeTree[] {});
-    addNominalSettings();
 
-    executeAndVerify("anonymous.json");
+    executeAndVerify("global_pages_for_anonymous.json");
   }
 
   @Test
-  public void nominal_call_for_user() throws Exception {
+  public void return_global_pages_for_user() throws Exception {
     init(createViews(), new ResourceTypeTree[] {});
-    addNominalSettings();
-
     userSessionRule.login("obiwan");
 
-    executeAndVerify("user.json");
+    executeAndVerify("global_pages_for_user.json");
   }
 
   @Test
-  public void nominal_call_for_admin() throws Exception {
+  public void return_global_pages_for_admin() throws Exception {
     init(createViews(), new ResourceTypeTree[] {});
-    addNominalSettings();
-
     userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
-    executeAndVerify("admin.json");
+    executeAndVerify("global_pages_for_admin.json");
   }
 
   @Test
-  public void nominal_call_for_user_without_configured_dashboards() throws Exception {
-    init(createViews(), new ResourceTypeTree[] {});
-    addNominalSettings();
+  public void return_sonarqube_version() throws Exception {
+    init();
+    when(server.getVersion()).thenReturn("6.2");
+
+    executeAndVerify("version.json");
+  }
+
+  @Test
+  public void return_if_production_database_or_not() throws Exception {
+    init();
+    when(database.getDialect()).thenReturn(new MySql());
 
-    userSessionRule.login("anakin");
+    executeAndVerify("production_database.json");
+  }
 
-    executeAndVerify("anonymous.json");
+  @Test
+  public void test_example_response() throws Exception {
+    init(createViews(), new ResourceTypeTree[] {
+      ResourceTypeTree.builder()
+        .addType(ResourceType.builder("POL").build())
+        .addType(ResourceType.builder("LOP").build())
+        .addRelations("POL", "LOP")
+        .build(),
+      ResourceTypeTree.builder()
+        .addType(ResourceType.builder("PAL").build())
+        .addType(ResourceType.builder("LAP").build())
+        .addRelations("PAL", "LAP")
+        .build()
+    });
+    settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+    settings.setProperty("sonar.lf.logoWidthPx", 135);
+    settings.setProperty("sonar.lf.gravatarServerUrl", "http://some-server.tld/logo.png");
+    settings.setProperty("sonar.lf.enableGravatar", true);
+    settings.setProperty("sonar.updatecenter.activate", false);
+    settings.setProperty("sonar.technicalDebt.hoursInDay", "10");
+    settings.setProperty("sonar.technicalDebt.ratingGrid", "0.05,0.1,0.2,0.5");
+    settings.setProperty("sonar.allowUsersToSignUp", true);
+    when(server.getVersion()).thenReturn("6.2");
+    when(database.getDialect()).thenReturn(new MySql());
+
+    String result = ws.newRequest().execute().getInput();
+    JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
   }
 
   private void init() {
@@ -122,18 +179,14 @@ public class GlobalNavigationActionTest {
   }
 
   private void init(View[] views, ResourceTypeTree[] resourceTypeTrees) {
-    ws = new WsActionTester(new GlobalNavigationAction(new Views(userSessionRule, views), settings, new ResourceTypes(resourceTypeTrees)));
+    when(database.getDialect()).thenReturn(new H2());
+    ws = new WsActionTester(new GlobalNavigationAction(new Views(userSessionRule, views), settings, new ResourceTypes(resourceTypeTrees), server, database));
   }
 
   private void executeAndVerify(String json) {
     JsonAssert.assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource(GlobalNavigationActionTest.class.getSimpleName() + "/" + json));
   }
 
-  private void addNominalSettings() {
-    settings.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png");
-    settings.setProperty("sonar.lf.logoWidthPx", "123");
-  }
-
   private View[] createViews() {
     Page page = new Page() {
       @Override
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/admin.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/admin.json
deleted file mode 100644 (file)
index 1ad7b80..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-{
-  "globalPages": [
-    {
-      "name": "My Plugin Page",
-      "url": "/plugins/home/my_plugin_page"
-    },
-    {
-      "name": "My Rails App",
-      "url": "/my_rails_app"
-    },
-    {
-      "name": "Admin Page",
-      "url": "/plugins/home/admin_page"
-    }
-  ],
-  "logoUrl": "http://some-server.tld/logo.png",
-  "logoWidth": "123"
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/anonymous.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/anonymous.json
deleted file mode 100644 (file)
index 4bb69ea..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-  "globalPages": [
-    {
-      "name": "My Plugin Page",
-      "url": "/plugins/home/my_plugin_page"
-    },
-    {
-      "name": "My Rails App",
-      "url": "/my_rails_app"
-    }
-  ],
-  "logoUrl": "http://some-server.tld/logo.png",
-  "logoWidth": "123"
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/deprecated_logo_settings.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/deprecated_logo_settings.json
new file mode 100644 (file)
index 0000000..13930da
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "settings": {
+    "sonar.lf.logoUrl": "http://example.com/my-custom-logo.png",
+    "sonar.lf.logoWidthPx": "135"
+  },
+  "logoUrl": "http://example.com/my-custom-logo.png",
+  "logoWidth": "135"
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_admin.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_admin.json
new file mode 100644 (file)
index 0000000..019342b
--- /dev/null
@@ -0,0 +1,16 @@
+{
+  "globalPages": [
+    {
+      "name": "My Plugin Page",
+      "url": "/plugins/home/my_plugin_page"
+    },
+    {
+      "name": "My Rails App",
+      "url": "/my_rails_app"
+    },
+    {
+      "name": "Admin Page",
+      "url": "/plugins/home/admin_page"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_anonymous.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_anonymous.json
new file mode 100644 (file)
index 0000000..b2783b5
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "globalPages": [
+    {
+      "name": "My Plugin Page",
+      "url": "/plugins/home/my_plugin_page"
+    },
+    {
+      "name": "My Rails App",
+      "url": "/my_rails_app"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_user.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/global_pages_for_user.json
new file mode 100644 (file)
index 0000000..b2783b5
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "globalPages": [
+    {
+      "name": "My Plugin Page",
+      "url": "/plugins/home/my_plugin_page"
+    },
+    {
+      "name": "My Rails App",
+      "url": "/my_rails_app"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/only_logo.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/only_logo.json
deleted file mode 100644 (file)
index 08dfe17..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-  "globalPages": [],
-  "logoUrl": "http://some-server.tld/logo.png",
-  "logoWidth": "123"
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/production_database.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/production_database.json
new file mode 100644 (file)
index 0000000..ae8359c
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "productionDatabase": true
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/qualifiers.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/qualifiers.json
new file mode 100644 (file)
index 0000000..ef2107c
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "qualifiers": ["POL", "PAL"]
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/settings.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/settings.json
new file mode 100644 (file)
index 0000000..d6d1fd2
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "settings": {
+    "sonar.lf.logoUrl": "http://example.com/my-custom-logo.png",
+    "sonar.lf.logoWidthPx": "135",
+    "sonar.lf.gravatarServerUrl": "https://secure.gravatar.com/avatar/{EMAIL_MD5}.jpg?s={SIZE}&d=identicon",
+    "sonar.lf.enableGravatar": "true",
+    "sonar.updatecenter.activate": "false",
+    "sonar.technicalDebt.hoursInDay": "10",
+    "sonar.technicalDebt.ratingGrid": "0.05,0.1,0.2,0.5",
+    "sonar.allowUsersToSignUp": "true"
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/user.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/user.json
deleted file mode 100644 (file)
index 4bb69ea..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-  "globalPages": [
-    {
-      "name": "My Plugin Page",
-      "url": "/plugins/home/my_plugin_page"
-    },
-    {
-      "name": "My Rails App",
-      "url": "/my_rails_app"
-    }
-  ],
-  "logoUrl": "http://some-server.tld/logo.png",
-  "logoWidth": "123"
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/version.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/version.json
new file mode 100644 (file)
index 0000000..21cd139
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "version": "6.2"
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/with_qualifiers.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalNavigationActionTest/with_qualifiers.json
deleted file mode 100644 (file)
index 3e56583..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-{
-  "globalPages": [],
-  "qualifiers": ["POL", "PAL"]
-}