]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6576 WS api/metrics/domains list all the available metric domains available 329/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 26 May 2015 13:03:29 +0000 (15:03 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 26 May 2015 14:34:30 +0000 (16:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/DomainsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-domains.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/metric/ws/DomainsActionTest.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java
sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml

index ae463280213f4de9d0fc49a890d5f1ad26242d13..e6a9e352be87718f9fdfac8e8d0fda1626a95008 100644 (file)
@@ -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<MetricDto> selectEnabled(DbSession session) {
-    return session.getMapper(MetricMapper.class).selectAllEnabled();
+    return mapper(session).selectAllEnabled();
   }
 
   public List<MetricDto> 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<String> selectDomains(DbSession session) {
+    return newArrayList(Collections2.filter(mapper(session).selectDomains(), new Predicate<String>() {
+      @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 (file)
index 0000000..25cfb3b
--- /dev/null
@@ -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<String> 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<String> domains) {
+    json.name("domains");
+    json.beginArray();
+    json.values(domains);
+    json.endArray();
+  }
+}
index 24463a6fd773c741319ce3b98da58d889609fe56..f79a14e72ca62f73de8cd16812f98bf89895bd6a 100644 (file)
@@ -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 (file)
index 0000000..137bdfd
--- /dev/null
@@ -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 (file)
index 0000000..b68d4bd
--- /dev/null
@@ -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();
+  }
+}
index e7b4d2cde68b09a28e9196901b97701a3ec7feae..3b1a44375e239df91173275eb5b0bd414fbfb5f4 100644 (file)
@@ -29,8 +29,12 @@ import java.util.Map;
 public interface MetricMapper {
 
   MetricDto selectByKey(@Param("key") String key);
+
   List<MetricDto> selectAllEnabled();
+
   List<MetricDto> selectAllEnabled(Map<String, Object> properties, RowBounds rowBounds);
+
   void insert(MetricDto dto);
 
+  List<String> selectDomains();
 }
index 0f07acb06d064975b4aec6bff3d3362edb6ddf3b..1ec976a4bea030ba931e2e3966abd5f5b5b43803 100644 (file)
     )
   </insert>
 
+  <select id="selectDomains" resultType="String">
+    select distinct domain
+    from metrics m
+    where m.domain is not null
+  </select>
+
 </mapper>