]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5857 Only "better than" operator is available for rating metrics
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 29 Sep 2016 13:47:33 +0000 (15:47 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 3 Oct 2016 16:19:34 +0000 (18:19 +0200)
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java
sonar-db/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDto.java
sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDtoTest.java

index 09a3993fee99731fb792b49b8f53e6fccb2e7229..2b6f9b54cf7a9cb0cbe2b59ba1231e4b40a3af19 100644 (file)
@@ -105,9 +105,9 @@ public class QualityGateConditionsUpdaterTest {
 
   @Test
   public void create_condition_on_rating_metric() {
-    QualityGateConditionDto result = underTest.createCondition(dbSession, qualityGateDto.getId(), "rating_metric", "LT", null, "3", 1);
+    QualityGateConditionDto result = underTest.createCondition(dbSession, qualityGateDto.getId(), "rating_metric", "GT", null, "3", 1);
 
-    verifyCondition(result, ratingMetricDto.getId(), "LT", null, "3", 1);
+    verifyCondition(result, ratingMetricDto.getId(), "GT", null, "3", 1);
   }
 
   @Test
@@ -188,14 +188,14 @@ public class QualityGateConditionsUpdaterTest {
   public void fail_to_create_warning_condition_on_invalid_rating_metric() {
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("'6' is not a valid rating");
-    underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "LT", "6", null, null);
+    underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "GT", "6", null, null);
   }
 
   @Test
   public void fail_to_create_error_condition_on_invalid_rating_metric() {
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("'80' is not a valid rating");
-    underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "LT", null, "80", null);
+    underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "GT", null, "80", null);
   }
 
   @Test
@@ -205,13 +205,6 @@ public class QualityGateConditionsUpdaterTest {
     underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "GT", "5", null, null);
   }
 
-  @Test
-  public void fail_to_create_condition_on_lesser_than_A() {
-    expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("There's no better rating than A (1)");
-    underTest.createCondition(dbSession, qualityGateDto.getId(), ratingMetricDto.getKey(), "LT", null, "1", null);
-  }
-
   @Test
   public void update_condition() {
     QualityGateConditionDto condition = insertCondition(coverageMetricDto.getId(), "LT", null, "80", null);
index b475695b2ee28c4cfd6ecb6ab77fcfcaf9d3eaba..28bde23714397763a2e90b716a8c24b128c1f278 100644 (file)
@@ -47,28 +47,29 @@ public class QualityGateConditionDto {
     OPERATOR_LESS_THAN,
     OPERATOR_GREATER_THAN,
     OPERATOR_EQUALS,
-    OPERATOR_NOT_EQUALS
-    );
+    OPERATOR_NOT_EQUALS);
 
   private static final List<String> NUMERIC_OPERATORS = ImmutableList.of(
     OPERATOR_LESS_THAN,
     OPERATOR_GREATER_THAN,
     OPERATOR_EQUALS,
-    OPERATOR_NOT_EQUALS
-    );
+    OPERATOR_NOT_EQUALS);
+
   private static final List<String> STRING_OPERATORS = ImmutableList.of(
     OPERATOR_EQUALS,
     OPERATOR_NOT_EQUALS,
     OPERATOR_LESS_THAN,
-    OPERATOR_GREATER_THAN
-    );
+    OPERATOR_GREATER_THAN);
+
   private static final List<String> LEVEL_OPERATORS = ImmutableList.of(
     OPERATOR_EQUALS,
-    OPERATOR_NOT_EQUALS
-    );
+    OPERATOR_NOT_EQUALS);
+
   private static final List<String> BOOLEAN_OPERATORS = ImmutableList.of(
-    OPERATOR_EQUALS
-    );
+    OPERATOR_EQUALS);
+
+  private static final List<String> RATING_OPERATORS = ImmutableList.of(
+    OPERATOR_GREATER_THAN);
 
   private static final Map<ValueType, List<String>> OPERATORS_BY_TYPE = ImmutableMap.<ValueType, List<String>>builder()
     .put(ValueType.BOOL, BOOLEAN_OPERATORS)
@@ -78,7 +79,7 @@ public class QualityGateConditionDto {
     .put(ValueType.FLOAT, NUMERIC_OPERATORS)
     .put(ValueType.PERCENT, NUMERIC_OPERATORS)
     .put(ValueType.MILLISEC, NUMERIC_OPERATORS)
-    .put(ValueType.RATING, NUMERIC_OPERATORS)
+    .put(ValueType.RATING, RATING_OPERATORS)
     .put(ValueType.WORK_DUR, NUMERIC_OPERATORS)
     .build();
 
index d21715eded57ec69c1ae76037d2317ab98651970..fdfc7b1202843db56f98d11ca4d9a36cbf022309 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.db.qualitygate;
 
+import java.util.Arrays;
 import org.junit.Test;
 import org.sonar.api.measures.Metric.ValueType;
 
@@ -37,25 +38,42 @@ import static org.sonar.db.qualitygate.QualityGateConditionDto.isOperatorAllowed
 public class QualityGateConditionDtoTest {
 
   @Test
-  public void should_validate_operators_for_metric_type() {
-    assertThat(isOperatorAllowed("WHATEVER", null)).isFalse();
+  public void validate_operators_for_DATA() {
     assertThat(isOperatorAllowed("WHATEVER", DATA)).isFalse();
+  }
 
+  @Test
+  public void validate_operators_for_BOOL() throws Exception {
     assertThat(isOperatorAllowed("EQ", BOOL)).isTrue();
     assertThat(isOperatorAllowed("NE", BOOL)).isFalse();
     assertThat(isOperatorAllowed("LT", BOOL)).isFalse();
     assertThat(isOperatorAllowed("GT", BOOL)).isFalse();
+  }
 
+  @Test
+  public void validate_operators_for_LEVEL() throws Exception {
     assertThat(isOperatorAllowed("EQ", LEVEL)).isTrue();
     assertThat(isOperatorAllowed("NE", LEVEL)).isTrue();
     assertThat(isOperatorAllowed("LT", LEVEL)).isFalse();
     assertThat(isOperatorAllowed("GT", LEVEL)).isFalse();
+  }
 
-    for (ValueType type : new ValueType[] {STRING, INT, FLOAT, PERCENT, MILLISEC, RATING}) {
+  @Test
+  public void validate_operators_for_RATING() throws Exception {
+    assertThat(isOperatorAllowed("EQ", RATING)).isFalse();
+    assertThat(isOperatorAllowed("NE", RATING)).isFalse();
+    assertThat(isOperatorAllowed("LT", RATING)).isFalse();
+    assertThat(isOperatorAllowed("GT", RATING)).isTrue();
+  }
+
+  @Test
+  public void validate_operators_for_other_types() throws Exception {
+    Arrays.stream(new ValueType[] {STRING, INT, FLOAT, PERCENT, MILLISEC}).forEach(type -> {
       assertThat(isOperatorAllowed("EQ", type)).isTrue();
       assertThat(isOperatorAllowed("NE", type)).isTrue();
       assertThat(isOperatorAllowed("LT", type)).isTrue();
       assertThat(isOperatorAllowed("GT", type)).isTrue();
-    }
+    });
   }
+
 }