]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5477 Return global settings in /batch/global WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 22 Jul 2014 16:51:50 +0000 (18:51 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 23 Jul 2014 06:35:05 +0000 (08:35 +0200)
server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java
server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java
server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json [new file with mode: 0644]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java

index 1e8ad12599cafda5716ac764fcde527db96c52ad..f733b8b95661f9ac2a268a0b0401d098a1ccf51c 100644 (file)
 package org.sonar.server.batch;
 
 import org.apache.commons.io.IOUtils;
+import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.batch.protocol.input.GlobalReferentials;
 import org.sonar.core.measure.db.MetricDto;
+import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.plugins.MimeTypes;
+import org.sonar.server.user.UserSession;
+
+import java.util.Map;
 
 public class GlobalReferentialsAction implements RequestHandler {
 
   private final DbClient dbClient;
+  private final Settings settings;
 
-  public GlobalReferentialsAction(DbClient dbClient) {
+  public GlobalReferentialsAction(DbClient dbClient, Settings settings) {
     this.dbClient = dbClient;
+    this.settings = settings;
   }
 
   void define(WebService.NewController controller) {
@@ -50,25 +57,15 @@ public class GlobalReferentialsAction implements RequestHandler {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    // TODO check user permission
+    UserSession userSession = UserSession.get();
+    boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
+    boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);
 
     DbSession session = dbClient.openSession(false);
     try {
       GlobalReferentials ref = new GlobalReferentials();
-      for (MetricDto metric : dbClient.metricDao().findEnabled(session)) {
-        Boolean optimizedBestValue = metric.isOptimizedBestValue();
-        ref.metrics().add(
-          new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
-            metric.getValueType(),
-            metric.getDescription(),
-            metric.getDirection(),
-            metric.getName(),
-            metric.isQualitative(),
-            metric.isUserManaged(),
-            metric.getWorstValue(),
-            metric.getBestValue(),
-            optimizedBestValue != null ? optimizedBestValue : false));
-      }
+      addMetrics(ref, session);
+      addSettings(ref, hasScanPerm, hasDryRunPerm);
 
       response.stream().setMediaType(MimeTypes.JSON);
       IOUtils.write(ref.toJson(), response.stream().output());
@@ -77,4 +74,36 @@ public class GlobalReferentialsAction implements RequestHandler {
     }
   }
 
+  private void addMetrics(GlobalReferentials ref, DbSession session) {
+    for (MetricDto metric : dbClient.metricDao().findEnabled(session)) {
+      Boolean optimizedBestValue = metric.isOptimizedBestValue();
+      ref.addMetric(
+        new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
+          metric.getValueType(),
+          metric.getDescription(),
+          metric.getDirection(),
+          metric.getName(),
+          metric.isQualitative(),
+          metric.isUserManaged(),
+          metric.getWorstValue(),
+          metric.getBestValue(),
+          optimizedBestValue != null ? optimizedBestValue : false));
+    }
+  }
+
+  private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm) {
+    for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) {
+      String key = entry.getKey();
+      String value = entry.getValue();
+
+      if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) {
+        ref.addGlobalSetting(key, value);
+      }
+    }
+  }
+
+  private boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasDryRunPerm){
+    return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasDryRunPerm);
+  }
+
 }
index 689736c314b581fae1a51466a8c100c31e12bc5e..35a48d01451b4db3f3b2ee198f478ede428dadbd 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.config.Settings;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.ws.WsTester;
 
@@ -54,7 +55,7 @@ public class BatchWsTest {
 
   @Before
   public void before() throws IOException {
-    tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class))));
+    tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class), mock(Settings.class))));
   }
 
   @Test
index 107a9f093ff51250e0991dcd2ba04966c20b5d09..42b107eca07372d6ad9956a2d94da0ca18fea928 100644 (file)
@@ -25,10 +25,13 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.config.Settings;
 import org.sonar.core.measure.db.MetricDto;
+import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.measure.persistence.MetricDao;
+import org.sonar.server.user.MockUserSession;
 import org.sonar.server.ws.WsTester;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -44,6 +47,8 @@ public class GlobalReferentialsActionTest {
   @Mock
   MetricDao metricDao;
 
+  Settings settings;
+
   WsTester tester;
 
   @Before
@@ -52,11 +57,13 @@ public class GlobalReferentialsActionTest {
     when(dbClient.openSession(false)).thenReturn(session);
     when(dbClient.metricDao()).thenReturn(metricDao);
 
-    tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient)));
+    settings = new Settings();
+
+    tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, settings)));
   }
 
   @Test
-  public void return_global_referentials() throws Exception {
+  public void return_metrics() throws Exception {
     when(metricDao.findEnabled(session)).thenReturn(newArrayList(
       MetricDto.createFor("coverage").setDescription("Coverage by unit tests").setValueType("PERCENT").setQualitative(true)
         .setWorstValue(0d).setBestValue(100d).setOptimizedBestValue(false).setDirection(1).setEnabled(true)
@@ -65,4 +72,40 @@ public class GlobalReferentialsActionTest {
     WsTester.TestRequest request = tester.newGetRequest("batch", "global");
     request.execute().assertJson(getClass(), "return_global_referentials.json");
   }
+
+  @Test
+  public void return_global_settings() throws Exception {
+    MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
+
+    settings.setProperty("foo", "bar");
+    settings.setProperty("foo.secured", "1234");
+    settings.setProperty("foo.license.secured", "5678");
+
+    WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+    request.execute().assertJson(getClass(), "return_global_settings.json");
+  }
+
+  @Test
+  public void return_only_license_settings_without_scan_but_with_preview_permission() throws Exception {
+    MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.DRY_RUN_EXECUTION);
+
+    settings.setProperty("foo", "bar");
+    settings.setProperty("foo.secured", "1234");
+    settings.setProperty("foo.license.secured", "5678");
+
+    WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+    request.execute().assertJson(getClass(), "return_only_license_settings_without_scan_but_with_preview_permission.json");
+  }
+
+  @Test
+  public void return_no_secured_settings_without_scan_and_preview_permission() throws Exception {
+    MockUserSession.set().setLogin("john").setGlobalPermissions();
+
+    settings.setProperty("foo", "bar");
+    settings.setProperty("foo.secured", "1234");
+    settings.setProperty("foo.license.secured", "5678");
+
+    WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+    request.execute().assertJson(getClass(), "return_no_secured_settings_without_scan_and_preview_permission.json");
+  }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json
new file mode 100644 (file)
index 0000000..d6faa69
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "timestamp": 0,
+  "metrics": [],
+  "globalSettings": {
+    "foo" : "bar",
+    "foo.secured" : "1234",
+    "foo.license.secured" : "5678"
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json
new file mode 100644 (file)
index 0000000..6bc5b2d
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "timestamp": 0,
+  "metrics": [],
+  "globalSettings": {
+    "foo" : "bar"
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json
new file mode 100644 (file)
index 0000000..b0af2f8
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "timestamp": 0,
+  "metrics": [],
+  "globalSettings": {
+    "foo" : "bar",
+    "foo.license.secured" : "5678"
+  }
+}
index a1ad9e31a5a02fa27b2dbbabfeb3b43d1ae1dcf4..4a15c7042d23560335774ee0d50d21bb174d098a 100644 (file)
@@ -41,10 +41,20 @@ public class GlobalReferentials {
     return globalSettings;
   }
 
+  public GlobalReferentials addGlobalSetting(String key, String value){
+    globalSettings.put(key, value);
+    return this;
+  }
+
   public Collection<Metric> metrics() {
     return metrics;
   }
 
+  public GlobalReferentials addMetric(Metric metric){
+    metrics.add(metric);
+    return this;
+  }
+
   public long timestamp() {
     return timestamp;
   }
index 3afd7662fc0a08120c4be5533c9c65ff77072c8d..6ea2a3bf2187f73c00a89f6f0abb1923d5f7afd0 100644 (file)
@@ -31,13 +31,12 @@ import static org.fest.assertions.Assertions.assertThat;
 public class GlobalReferentialsTest {
 
   @Test
-  public void testToJson() throws Exception {
+  public void to_json() throws Exception {
     GlobalReferentials ref = new GlobalReferentials();
-    ref.metrics().add(new Metric(1, "ncloc", "INT", "Description", -1, "NCLOC", true, false, 2.0, 1.0, true));
-    ref.globalSettings().put("prop", "value");
+    ref.addMetric(new Metric(1, "ncloc", "INT", "Description", -1, "NCLOC", true, false, 2.0, 1.0, true));
+    ref.addGlobalSetting("prop", "value");
     ref.setTimestamp(10);
 
-    System.out.println(ref.toJson());
     JSONAssert
       .assertEquals(
         "{timestamp:10,"
@@ -47,7 +46,7 @@ public class GlobalReferentialsTest {
   }
 
   @Test
-  public void testFromJson() throws JSONException {
+  public void from_json() throws JSONException {
     GlobalReferentials ref = GlobalReferentials
       .fromJson(new StringReader(
         "{timestamp:1,"