From: Julien Lancelot Date: Tue, 22 Jul 2014 16:51:50 +0000 (+0200) Subject: SONAR-5477 Return global settings in /batch/global WS X-Git-Tag: 4.5-RC1~418 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bdd27251c8f15e7f59cd8701dc53efd57cb4f9f4;p=sonarqube.git SONAR-5477 Return global settings in /batch/global WS --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java index 1e8ad12599c..f733b8b9566 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java @@ -21,23 +21,30 @@ 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 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); + } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java index 689736c314b..35a48d01451 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java @@ -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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java index 107a9f093ff..42b107eca07 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java @@ -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 index 00000000000..d6faa69622a --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json @@ -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 index 00000000000..6bc5b2da8aa --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json @@ -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 index 00000000000..b0af2f8ca33 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json @@ -0,0 +1,8 @@ +{ + "timestamp": 0, + "metrics": [], + "globalSettings": { + "foo" : "bar", + "foo.license.secured" : "5678" + } +} diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java index a1ad9e31a5a..4a15c7042d2 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java @@ -41,10 +41,20 @@ public class GlobalReferentials { return globalSettings; } + public GlobalReferentials addGlobalSetting(String key, String value){ + globalSettings.put(key, value); + return this; + } + public Collection metrics() { return metrics; } + public GlobalReferentials addMetric(Metric metric){ + metrics.add(metric); + return this; + } + public long timestamp() { return timestamp; } diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java index 3afd7662fc0..6ea2a3bf218 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java @@ -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,"