From: Teryk Bellahsene Date: Tue, 26 May 2015 13:03:29 +0000 (+0200) Subject: SONAR-6576 WS api/metrics/domains list all the available metric domains available X-Git-Tag: 5.2-RC1~1831 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=98c512893e4c70f65bfe954219e8752919e63c31;p=sonarqube.git SONAR-6576 WS api/metrics/domains list all the available metric domains available --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java b/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java index ae463280213..e6a9e352be8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java @@ -20,6 +20,8 @@ package org.sonar.server.metric.persistence; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; import com.google.common.collect.Maps; import org.apache.ibatis.session.RowBounds; import org.sonar.api.server.ServerSide; @@ -30,21 +32,23 @@ import org.sonar.core.persistence.DbSession; import org.sonar.server.es.SearchOptions; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import javax.annotation.Nullable; - import java.util.List; import java.util.Map; +import static com.google.common.collect.Lists.newArrayList; + @ServerSide public class MetricDao implements DaoComponent { @CheckForNull public MetricDto selectByKey(DbSession session, String key) { - return session.getMapper(MetricMapper.class).selectByKey(key); + return mapper(session).selectByKey(key); } public List selectEnabled(DbSession session) { - return session.getMapper(MetricMapper.class).selectAllEnabled(); + return mapper(session).selectAllEnabled(); } public List selectEnabled(DbSession session, @Nullable Boolean isCustom, SearchOptions searchOptions) { @@ -53,10 +57,23 @@ public class MetricDao implements DaoComponent { properties.put("isCustom", isCustom); } - return session.getMapper(MetricMapper.class).selectAllEnabled(properties, new RowBounds(searchOptions.getOffset(), searchOptions.getLimit())); + return mapper(session).selectAllEnabled(properties, new RowBounds(searchOptions.getOffset(), searchOptions.getLimit())); } public void insert(DbSession session, MetricDto dto) { - session.getMapper(MetricMapper.class).insert(dto); + mapper(session).insert(dto); + } + + public List selectDomains(DbSession session) { + return newArrayList(Collections2.filter(mapper(session).selectDomains(), new Predicate() { + @Override + public boolean apply(@Nonnull String input) { + return !input.isEmpty(); + } + })); + } + + private MetricMapper mapper(DbSession session) { + return session.getMapper(MetricMapper.class); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DomainsAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DomainsAction.java new file mode 100644 index 00000000000..25cfb3befda --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DomainsAction.java @@ -0,0 +1,71 @@ +/* + * 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.metric.ws; + +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.MyBatis; +import org.sonar.server.db.DbClient; + +import java.util.List; + +public class DomainsAction implements MetricsWsAction { + + private final DbClient dbClient; + + public DomainsAction(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public void define(WebService.NewController context) { + context.createAction("domains") + .setDescription("List all custom metric domains.") + .setSince("5.2") + .setResponseExample(getClass().getResource("example-domains.json")) + .setHandler(this); + } + + @Override + public void handle(Request request, Response response) throws Exception { + DbSession dbSession = dbClient.openSession(false); + try { + List domains = dbClient.metricDao().selectDomains(dbSession); + JsonWriter json = response.newJsonWriter(); + json.beginObject(); + writeDomains(json, domains); + json.endObject(); + json.close(); + } finally { + MyBatis.closeQuietly(dbSession); + } + } + + private void writeDomains(JsonWriter json, List domains) { + json.name("domains"); + json.beginArray(); + json.values(domains); + json.endArray(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 24463a6fd77..f79a14e72ca 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -434,6 +434,7 @@ public class PlatformLevel4 extends PlatformLevel { MetricsWs.class, org.sonar.server.metric.ws.ListAction.class, org.sonar.server.metric.ws.TypesAction.class, + org.sonar.server.metric.ws.DomainsAction.class, // quality gates QualityGateDao.class, diff --git a/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-domains.json b/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-domains.json new file mode 100644 index 00000000000..137bdfd443f --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-domains.json @@ -0,0 +1,9 @@ +{ + "domains": [ + "API Compatibility", + "Issues", + "Rules", + "Tests", + "Documentation" + ] +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DomainsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DomainsActionTest.java new file mode 100644 index 00000000000..b68d4bd0db6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DomainsActionTest.java @@ -0,0 +1,77 @@ +/* + * 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.metric.ws; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.core.metric.db.MetricDto; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.DbTester; +import org.sonar.server.db.DbClient; +import org.sonar.server.metric.persistence.MetricDao; +import org.sonar.server.ws.WsTester; +import org.sonar.test.DbTests; +import org.sonar.test.JsonAssert; + +@Category(DbTests.class) +public class DomainsActionTest { + + @ClassRule + public static DbTester db = new DbTester(); + WsTester ws; + DbClient dbClient; + DbSession dbSession; + + @Before + public void setUp() throws Exception { + dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao()); + dbSession = dbClient.openSession(false); + ws = new WsTester(new MetricsWs(new DomainsAction(dbClient))); + } + + @After + public void tearDown() throws Exception { + dbSession.close(); + } + + @Test + public void json_example_validated() throws Exception { + insertNewMetricDto(MetricTesting.newDto().setDomain("API Compatibility")); + insertNewMetricDto(MetricTesting.newDto().setDomain("Issues")); + insertNewMetricDto(MetricTesting.newDto().setDomain("Rules")); + insertNewMetricDto(MetricTesting.newDto().setDomain("Tests")); + insertNewMetricDto(MetricTesting.newDto().setDomain("Documentation")); + insertNewMetricDto(MetricTesting.newDto().setDomain(null)); + insertNewMetricDto(MetricTesting.newDto().setDomain("")); + + WsTester.Result result = ws.newGetRequest(MetricsWs.ENDPOINT, "domains").execute(); + + JsonAssert.assertJson(result.outputAsString()).isSimilarTo(getClass().getResource("example-domains.json")); + } + + private void insertNewMetricDto(MetricDto metric) { + dbClient.metricDao().insert(dbSession, metric); + dbSession.commit(); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java index e7b4d2cde68..3b1a44375e2 100644 --- a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java @@ -29,8 +29,12 @@ import java.util.Map; public interface MetricMapper { MetricDto selectByKey(@Param("key") String key); + List selectAllEnabled(); + List selectAllEnabled(Map properties, RowBounds rowBounds); + void insert(MetricDto dto); + List selectDomains(); } diff --git a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml index 0f07acb06d0..1ec976a4bea 100644 --- a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml @@ -61,4 +61,10 @@ ) + +