]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11577 Fix direction of certain metrics
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Fri, 4 Jan 2019 13:57:52 +0000 (14:57 +0100)
committerSonarTech <sonartech@sonarsource.com>
Tue, 8 Jan 2019 19:21:07 +0000 (20:21 +0100)
metrics: tests, conditions_to_cover, new_conditions_to_cover

server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java

index 8d7a7896bc4510775a0b04c20614b245fc2a5278..235b8af28003fb338c549a65469c5ff70da177cd 100644 (file)
@@ -29,6 +29,8 @@ public class DbVersion76 implements DbVersion {
     registry
       .add(2500, "Create table USER_PROPERTIES", CreateUserPropertiesTable.class)
       .add(2501, "Add index in table USER_PROPERTIES", AddUniqueIndexInUserPropertiesTable.class)
-      .add(2506, "Migrate quality gate conditions using warning, period and no more supported operations", MigrateNoMoreUsedQualityGateConditions.class);
+      .add(2505, "Fix the direction values of certain metrics (prepare for migration of conditions)", FixDirectionOfMetrics.class)
+      .add(2506, "Migrate quality gate conditions using warning, period and no more supported operations", MigrateNoMoreUsedQualityGateConditions.class)
+    ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java
new file mode 100644 (file)
index 0000000..8bbc699
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.platform.db.migration.version.v76;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.SupportsBlueGreen;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+@SupportsBlueGreen
+public class FixDirectionOfMetrics extends DataChange {
+
+  public FixDirectionOfMetrics(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    String sql = "UPDATE metrics SET direction = ? WHERE name = ? AND direction != ?";
+
+    context.prepareUpsert(sql)
+      .setInt(1, 1)
+      .setString(2, "tests")
+      .setInt(3, 1)
+      .execute()
+      .commit();
+
+    context.prepareUpsert(sql)
+      .setInt(1, -1)
+      .setString(2, "conditions_to_cover")
+      .setInt(3, -1)
+      .execute()
+      .commit();
+
+    context.prepareUpsert(sql)
+      .setInt(1, -1)
+      .setString(2, "new_conditions_to_cover")
+      .setInt(3, -1)
+      .execute()
+      .commit();
+  }
+}
index 88ba43ce5330d7ce640a5b1e2453a5d7d9b49428..bec86f8565a20bf00c53169e0ca722168f59d13d 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion76Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 3);
+    verifyMigrationCount(underTest, 4);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java
new file mode 100644 (file)
index 0000000..5e8d189
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.platform.db.migration.version.v76;
+
+import com.google.common.collect.ImmutableMap;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RunWith(DataProviderRunner.class)
+public class FixDirectionOfMetricsTest {
+
+  private static final String TESTS_METRIC_NAME = "tests";
+  private static final String CONDITIONS_TO_COVER_METRIC_NAME = "conditions_to_cover";
+  private static final String NEW_CONDITIONS_TO_COVER_METRIC_NAME = "new_conditions_to_cover";
+  private static final String UNRELATED_METRIC_NAME = "unrelated";
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(FixDirectionOfMetricsTest.class, "metrics.sql");
+
+  private static long idCounter = 1;
+
+  private FixDirectionOfMetrics underTest = new FixDirectionOfMetrics(db.database());
+
+  @Test
+  @UseDataProvider("interestingCombinationsOfInitialMetricsAndValues")
+  public void fixes_direction_of_relevant_metrics(Map<String, Integer> initialMetricsAndValues) throws SQLException {
+    insertMetric(TESTS_METRIC_NAME, initialMetricsAndValues.get(TESTS_METRIC_NAME));
+    insertMetric(CONDITIONS_TO_COVER_METRIC_NAME, initialMetricsAndValues.get(CONDITIONS_TO_COVER_METRIC_NAME));
+    insertMetric(NEW_CONDITIONS_TO_COVER_METRIC_NAME, initialMetricsAndValues.get(NEW_CONDITIONS_TO_COVER_METRIC_NAME));
+    insertMetric(UNRELATED_METRIC_NAME, initialMetricsAndValues.get(UNRELATED_METRIC_NAME));
+
+    underTest.execute();
+
+    assertThat(selectMetricDirection(TESTS_METRIC_NAME)).isEqualTo(1L);
+    assertThat(selectMetricDirection(CONDITIONS_TO_COVER_METRIC_NAME)).isEqualTo(-1L);
+    assertThat(selectMetricDirection(NEW_CONDITIONS_TO_COVER_METRIC_NAME)).isEqualTo(-1L);
+    assertThat(selectMetricDirection(UNRELATED_METRIC_NAME)).isEqualTo((long) initialMetricsAndValues.get(UNRELATED_METRIC_NAME));
+  }
+
+  @DataProvider
+  public static Object[][] interestingCombinationsOfInitialMetricsAndValues() {
+    List<Map<String, Integer>> mappings = new ArrayList<>();
+    int[] directions = {-1, 0, 1};
+    for (int unrelatedDirection : directions) {
+      // the target metrics with old (wrong) direction, and an unrelated metric with any direction
+      mappings.add(ImmutableMap.of(
+        TESTS_METRIC_NAME, -1,
+        CONDITIONS_TO_COVER_METRIC_NAME, 0,
+        NEW_CONDITIONS_TO_COVER_METRIC_NAME, 0,
+        UNRELATED_METRIC_NAME, unrelatedDirection));
+
+      // the target metrics with correct direction, and an unrelated metric with any direction
+      mappings.add(ImmutableMap.of(
+        TESTS_METRIC_NAME, 1,
+        CONDITIONS_TO_COVER_METRIC_NAME, -1,
+        NEW_CONDITIONS_TO_COVER_METRIC_NAME, -1,
+        UNRELATED_METRIC_NAME, unrelatedDirection));
+    }
+    return mappings.stream().map(m -> new Object[] {m}).toArray(Object[][]::new);
+  }
+
+  private void insertMetric(String name, int direction) {
+    long id = idCounter++;
+    db.executeInsert("METRICS",
+      "ID", id,
+      "NAME", name,
+      "DIRECTION", direction);
+  }
+
+  private long selectMetricDirection(String metricName) {
+    return (long) db.selectFirst("SELECT direction FROM metrics WHERE name = '" + metricName + "'").get("DIRECTION");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql
new file mode 100644 (file)
index 0000000..ced0a62
--- /dev/null
@@ -0,0 +1,19 @@
+CREATE TABLE "METRICS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "NAME" VARCHAR(64) NOT NULL,
+  "DESCRIPTION" VARCHAR(255),
+  "DIRECTION" INTEGER NOT NULL DEFAULT 0,
+  "DOMAIN" VARCHAR(64),
+  "SHORT_NAME" VARCHAR(64),
+  "QUALITATIVE" BOOLEAN NOT NULL DEFAULT FALSE,
+  "VAL_TYPE" VARCHAR(8),
+  "USER_MANAGED" BOOLEAN DEFAULT FALSE,
+  "ENABLED" BOOLEAN DEFAULT TRUE,
+  "WORST_VALUE" DOUBLE,
+  "BEST_VALUE" DOUBLE,
+  "OPTIMIZED_BEST_VALUE" BOOLEAN,
+  "HIDDEN" BOOLEAN,
+  "DELETE_HISTORICAL_DATA" BOOLEAN,
+  "DECIMAL_SCALE" INTEGER
+);
+CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS" ("NAME");
index 1f5e716a71ec4823aba588e4d4cf8675fa78c3b2..ab1968bc9247b154d840d7bee9702145e676751e 100644 (file)
@@ -524,7 +524,7 @@ public final class CoreMetrics {
    */
   public static final Metric<Integer> TESTS = new Metric.Builder(TESTS_KEY, "Unit Tests", Metric.ValueType.INT)
     .setDescription("Number of unit tests")
-    .setDirection(Metric.DIRECTION_WORST)
+    .setDirection(Metric.DIRECTION_BETTER)
     .setQualitative(false)
     .setDomain(DOMAIN_COVERAGE)
     .create();
@@ -704,12 +704,14 @@ public final class CoreMetrics {
   public static final Metric<Integer> CONDITIONS_TO_COVER = new Metric.Builder(CONDITIONS_TO_COVER_KEY, "Conditions to Cover", Metric.ValueType.INT)
     .setDescription("Conditions to cover")
     .setDomain(DOMAIN_COVERAGE)
+    .setDirection(Metric.DIRECTION_WORST)
     .create();
 
   public static final String NEW_CONDITIONS_TO_COVER_KEY = "new_conditions_to_cover";
   public static final Metric<Integer> NEW_CONDITIONS_TO_COVER = new Metric.Builder(NEW_CONDITIONS_TO_COVER_KEY, "Conditions to Cover on New Code", Metric.ValueType.INT)
     .setDescription("Conditions to cover on new code")
     .setDomain(DOMAIN_COVERAGE)
+    .setDirection(Metric.DIRECTION_WORST)
     .setDeleteHistoricalData(true)
     .create();