aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-07-02 15:32:13 +0200
committersonartech <sonartech@sonarsource.com>2018-07-09 10:39:32 +0200
commitabc4af5ab9751bb9babdc50bb3549d8ab53d8ce2 (patch)
tree89320bb089e5ca713da5fb1bfea59fb11aeb6bed /server/sonar-server-common
parent8c7686ec1e25d01f9b5a07077b01a27ecfe67f12 (diff)
downloadsonarqube-abc4af5ab9751bb9babdc50bb3549d8ab53d8ce2.tar.gz
sonarqube-abc4af5ab9751bb9babdc50bb3549d8ab53d8ce2.zip
move metric shared classes to server-common
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/metric/CoreCustomMetrics.java53
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/metric/DefaultMetricFinder.java113
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/metric/package-info.java23
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/metric/CoreCustomMetricsTest.java41
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/metric/DefaultMetricFinderTest.java80
5 files changed, 310 insertions, 0 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/metric/CoreCustomMetrics.java b/server/sonar-server-common/src/main/java/org/sonar/server/metric/CoreCustomMetrics.java
new file mode 100644
index 00000000000..5f32c87e785
--- /dev/null
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/metric/CoreCustomMetrics.java
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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;
+
+import com.google.common.collect.ImmutableList;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
+
+import java.util.List;
+
+public final class CoreCustomMetrics implements Metrics {
+ private static final String DOMAIN = "Management";
+
+ @Override
+ public List<Metric> getMetrics() {
+ return ImmutableList.of(
+ new Metric.Builder("burned_budget", "Burned budget", Metric.ValueType.FLOAT)
+ .setDirection(Metric.DIRECTION_NONE)
+ .setQualitative(false)
+ .setDomain(DOMAIN)
+ .setUserManaged(true)
+ .create(),
+ new Metric.Builder("business_value", "Business value", Metric.ValueType.FLOAT)
+ .setDirection(Metric.DIRECTION_BETTER)
+ .setQualitative(true)
+ .setDomain(DOMAIN)
+ .setUserManaged(true)
+ .create(),
+ new Metric.Builder("team_size", "Team size", Metric.ValueType.INT)
+ .setDirection(Metric.DIRECTION_NONE)
+ .setQualitative(false)
+ .setDomain(DOMAIN)
+ .setUserManaged(true)
+ .create());
+ }
+}
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/metric/DefaultMetricFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/metric/DefaultMetricFinder.java
new file mode 100644
index 00000000000..e3a1914b824
--- /dev/null
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/metric/DefaultMetricFinder.java
@@ -0,0 +1,113 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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;
+
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.metric.MetricDto;
+
+import static com.google.common.collect.FluentIterable.from;
+
+public class DefaultMetricFinder implements MetricFinder {
+
+ private final DbClient dbClient;
+
+ public DefaultMetricFinder(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public Metric findById(int id) {
+ try (DbSession session = dbClient.openSession(false)) {
+ MetricDto dto = dbClient.metricDao().selectById(session, id);
+ if (dto != null && dto.isEnabled()) {
+ return ToMetric.INSTANCE.apply(dto);
+ }
+ return null;
+ }
+ }
+
+ @Override
+ public Metric findByKey(String key) {
+ try (DbSession session = dbClient.openSession(false)) {
+ MetricDto dto = dbClient.metricDao().selectByKey(session, key);
+ if (dto != null && dto.isEnabled()) {
+ return ToMetric.INSTANCE.apply(dto);
+ }
+ return null;
+ }
+ }
+
+ @Override
+ public Collection<Metric> findAll(List<String> metricKeys) {
+ try (DbSession session = dbClient.openSession(false)) {
+ List<MetricDto> dtos = dbClient.metricDao().selectByKeys(session, metricKeys);
+ return from(dtos).filter(IsEnabled.INSTANCE).transform(ToMetric.INSTANCE).toList();
+ }
+ }
+
+ @Override
+ public Collection<Metric> findAll() {
+ try (DbSession session = dbClient.openSession(false)) {
+ List<MetricDto> dtos = dbClient.metricDao().selectEnabled(session);
+ return from(dtos).transform(ToMetric.INSTANCE).toList();
+ }
+ }
+
+ private enum IsEnabled implements Predicate<MetricDto> {
+ INSTANCE;
+ @Override
+ public boolean apply(@Nonnull MetricDto dto) {
+ return dto.isEnabled();
+ }
+ }
+
+ private enum ToMetric implements Function<MetricDto, Metric> {
+ INSTANCE;
+
+ @Override
+ public Metric apply(@Nonnull MetricDto dto) {
+ Metric<Serializable> metric = new Metric<>();
+ metric.setId(dto.getId());
+ metric.setKey(dto.getKey());
+ metric.setDescription(dto.getDescription());
+ metric.setName(dto.getShortName());
+ metric.setBestValue(dto.getBestValue());
+ metric.setDomain(dto.getDomain());
+ metric.setEnabled(dto.isEnabled());
+ metric.setDirection(dto.getDirection());
+ metric.setHidden(dto.isHidden());
+ metric.setQualitative(dto.isQualitative());
+ metric.setType(Metric.ValueType.valueOf(dto.getValueType()));
+ metric.setOptimizedBestValue(dto.isOptimizedBestValue());
+ metric.setUserManaged(dto.isUserManaged());
+ metric.setWorstValue(dto.getWorstValue());
+ return metric;
+ }
+ }
+}
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/metric/package-info.java b/server/sonar-server-common/src/main/java/org/sonar/server/metric/package-info.java
new file mode 100644
index 00000000000..12c259137f9
--- /dev/null
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/metric/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.metric;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/metric/CoreCustomMetricsTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/metric/CoreCustomMetricsTest.java
new file mode 100644
index 00000000000..3e01a7876a6
--- /dev/null
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/metric/CoreCustomMetricsTest.java
@@ -0,0 +1,41 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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;
+
+import org.junit.Test;
+import org.sonar.api.measures.Metric;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CoreCustomMetricsTest {
+
+ @Test
+ public void checkDefinitions() {
+ CoreCustomMetrics coreCustomMetrics = new CoreCustomMetrics();
+ List<Metric> metrics = coreCustomMetrics.getMetrics();
+ assertThat(metrics.size()).isGreaterThan(2);
+ for (Metric metric : metrics) {
+ assertThat(metric.getUserManaged()).isTrue();
+ assertThat(metric.getDomain()).isEqualTo("Management");
+ }
+ }
+}
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/metric/DefaultMetricFinderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/metric/DefaultMetricFinderTest.java
new file mode 100644
index 00000000000..a44564f01b3
--- /dev/null
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/metric/DefaultMetricFinderTest.java
@@ -0,0 +1,80 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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;
+
+import java.util.Arrays;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.metric.MetricDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.metric.MetricTesting.newMetricDto;
+
+
+public class DefaultMetricFinderTest {
+
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+
+ private DefaultMetricFinder underTest = new DefaultMetricFinder(db.getDbClient());
+
+ @Test
+ public void findAll_enabled() {
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setEnabled(false));
+ db.commit();
+
+ assertThat(underTest.findAll()).hasSize(2);
+ }
+
+ @Test
+ public void findAll_by_keys() {
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("ncloc"));
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("foo"));
+ db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("coverage"));
+ db.commit();
+
+ assertThat(underTest.findAll(Arrays.asList("ncloc", "foo"))).extracting(Metric::getKey).containsExactlyInAnyOrder("ncloc", "foo")
+ .doesNotContain("coverage");
+
+ }
+
+ @Test
+ public void findById() {
+ MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ db.commit();
+
+ assertThat(underTest.findById(firstMetric.getId())).extracting(Metric::getKey).containsExactly(firstMetric.getKey());
+ }
+
+ @Test
+ public void findByKey() {
+ MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
+ db.commit();
+
+ assertThat(underTest.findByKey(secondMetric.getKey())).extracting(Metric::getKey).containsExactly(secondMetric.getKey());
+ }
+}