diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-23 09:59:59 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-23 10:00:11 +0200 |
commit | fa5e05ed1f61182122c2a72d292c5f45061dba2c (patch) | |
tree | 31e730741ac323e6ca9500793d501c8f36084fd4 /server | |
parent | 543bf9e522cb581bae64b3520af36927cbffd42d (diff) | |
download | sonarqube-fa5e05ed1f61182122c2a72d292c5f45061dba2c.tar.gz sonarqube-fa5e05ed1f61182122c2a72d292c5f45061dba2c.zip |
SONAR-5477 Return global settings from database in /batch/global WS
Diffstat (limited to 'server')
3 files changed, 33 insertions, 28 deletions
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 f733b8b9566..286f37021be 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,7 +21,6 @@ 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; @@ -31,25 +30,25 @@ 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.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; 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; + private final PropertiesDao propertiesDao; - public GlobalReferentialsAction(DbClient dbClient, Settings settings) { + public GlobalReferentialsAction(DbClient dbClient, PropertiesDao propertiesDao) { this.dbClient = dbClient; - this.settings = settings; + this.propertiesDao = propertiesDao; } void define(WebService.NewController controller) { controller.createAction("global") - .setDescription("Return global referentials") + .setDescription("Return metrics and global properties") .setSince("4.5") .setInternal(true) .setHandler(this); @@ -65,7 +64,7 @@ public class GlobalReferentialsAction implements RequestHandler { try { GlobalReferentials ref = new GlobalReferentials(); addMetrics(ref, session); - addSettings(ref, hasScanPerm, hasDryRunPerm); + addSettings(ref, hasScanPerm, hasDryRunPerm, session); response.stream().setMediaType(MimeTypes.JSON); IOUtils.write(ref.toJson(), response.stream().output()); @@ -91,10 +90,10 @@ public class GlobalReferentialsAction implements RequestHandler { } } - 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(); + private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm, DbSession session) { + for (PropertyDto propertyDto : propertiesDao.selectGlobalProperties(session)) { + String key = propertyDto.getKey(); + String value = propertyDto.getValue(); if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) { ref.addGlobalSetting(key, value); 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 35a48d01451..bef1af04f54 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,7 +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.core.properties.PropertiesDao; import org.sonar.server.db.DbClient; import org.sonar.server.ws.WsTester; @@ -55,7 +55,7 @@ public class BatchWsTest { @Before public void before() throws IOException { - tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class), mock(Settings.class)))); + tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class), mock(PropertiesDao.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 42b107eca07..d3f9fb9906a 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,11 @@ 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.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; import org.sonar.server.db.DbClient; import org.sonar.server.measure.persistence.MetricDao; import org.sonar.server.user.MockUserSession; @@ -47,7 +48,8 @@ public class GlobalReferentialsActionTest { @Mock MetricDao metricDao; - Settings settings; + @Mock + PropertiesDao propertiesDao; WsTester tester; @@ -57,9 +59,7 @@ public class GlobalReferentialsActionTest { when(dbClient.openSession(false)).thenReturn(session); when(dbClient.metricDao()).thenReturn(metricDao); - settings = new Settings(); - - tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, settings))); + tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao))); } @Test @@ -77,9 +77,11 @@ public class GlobalReferentialsActionTest { 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"); + when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList( + new PropertyDto().setKey("foo").setValue("bar"), + new PropertyDto().setKey("foo.secured").setValue("1234"), + new PropertyDto().setKey("foo.license.secured").setValue("5678") + )); WsTester.TestRequest request = tester.newGetRequest("batch", "global"); request.execute().assertJson(getClass(), "return_global_settings.json"); @@ -89,9 +91,11 @@ public class GlobalReferentialsActionTest { 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"); + when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList( + new PropertyDto().setKey("foo").setValue("bar"), + new PropertyDto().setKey("foo.secured").setValue("1234"), + new PropertyDto().setKey("foo.license.secured").setValue("5678") + )); WsTester.TestRequest request = tester.newGetRequest("batch", "global"); request.execute().assertJson(getClass(), "return_only_license_settings_without_scan_but_with_preview_permission.json"); @@ -101,9 +105,11 @@ public class GlobalReferentialsActionTest { 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"); + when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList( + new PropertyDto().setKey("foo").setValue("bar"), + new PropertyDto().setKey("foo.secured").setValue("1234"), + new PropertyDto().setKey("foo.license.secured").setValue("5678") + )); WsTester.TestRequest request = tester.newGetRequest("batch", "global"); request.execute().assertJson(getClass(), "return_no_secured_settings_without_scan_and_preview_permission.json"); |