You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConditionEvaluatorTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.qualitygate;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Optional;
  25. import javax.annotation.Nullable;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.runner.RunWith;
  30. import org.sonar.api.measures.Metric;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.sonar.api.measures.Metric.ValueType.BOOL;
  33. import static org.sonar.api.measures.Metric.ValueType.DATA;
  34. import static org.sonar.api.measures.Metric.ValueType.DISTRIB;
  35. import static org.sonar.api.measures.Metric.ValueType.STRING;
  36. import static org.sonar.server.qualitygate.FakeMeasure.newMeasureOnLeak;
  37. @RunWith(DataProviderRunner.class)
  38. public class ConditionEvaluatorTest {
  39. @Rule
  40. public ExpectedException expectedException = ExpectedException.none();
  41. @Test
  42. public void GREATER_THAN_double() {
  43. test(new FakeMeasure(10.1d), Condition.Operator.GREATER_THAN, "10.2", EvaluatedCondition.EvaluationStatus.OK, "10.1");
  44. test(new FakeMeasure(10.2d), Condition.Operator.GREATER_THAN, "10.2", EvaluatedCondition.EvaluationStatus.OK, "10.2");
  45. test(new FakeMeasure(10.3d), Condition.Operator.GREATER_THAN, "10.2", EvaluatedCondition.EvaluationStatus.ERROR, "10.3");
  46. }
  47. @Test
  48. public void LESS_THAN_double() {
  49. test(new FakeMeasure(10.1d), Condition.Operator.LESS_THAN, "10.2", EvaluatedCondition.EvaluationStatus.ERROR, "10.1");
  50. test(new FakeMeasure(10.2d), Condition.Operator.LESS_THAN, "10.2", EvaluatedCondition.EvaluationStatus.OK, "10.2");
  51. test(new FakeMeasure(10.3d), Condition.Operator.LESS_THAN, "10.2", EvaluatedCondition.EvaluationStatus.OK, "10.3");
  52. }
  53. @Test
  54. public void GREATER_THAN_int() {
  55. test(new FakeMeasure(10), Condition.Operator.GREATER_THAN, "9", EvaluatedCondition.EvaluationStatus.ERROR, "10");
  56. test(new FakeMeasure(10), Condition.Operator.GREATER_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
  57. test(new FakeMeasure(10), Condition.Operator.GREATER_THAN, "11", EvaluatedCondition.EvaluationStatus.OK, "10");
  58. testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "9", EvaluatedCondition.EvaluationStatus.ERROR, "10");
  59. testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
  60. testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "11", EvaluatedCondition.EvaluationStatus.OK, "10");
  61. }
  62. @Test
  63. public void LESS_THAN_int() {
  64. test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, "10");
  65. test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
  66. test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "11", EvaluatedCondition.EvaluationStatus.ERROR, "10");
  67. testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, "10");
  68. testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
  69. testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "11", EvaluatedCondition.EvaluationStatus.ERROR, "10");
  70. }
  71. @Test
  72. public void evaluate_throws_IAE_if_fail_to_parse_threshold() {
  73. expectedException.expect(IllegalArgumentException.class);
  74. expectedException.expectMessage("Quality Gate: unable to parse threshold '9bar' to compare against foo");
  75. test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "9bar", EvaluatedCondition.EvaluationStatus.ERROR, "10da");
  76. }
  77. @Test
  78. public void no_value_present() {
  79. test(new FakeMeasure((Integer) null), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, null);
  80. test(null, Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, null);
  81. }
  82. @Test
  83. @UseDataProvider("unsupportedMetricTypes")
  84. public void fail_when_condition_is_on_unsupported_metric(Metric.ValueType metricType) {
  85. expectedException.expect(IllegalArgumentException.class);
  86. expectedException.expectMessage(String.format("Condition is not allowed for type %s", metricType));
  87. test(new FakeMeasure(metricType), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, "10");
  88. }
  89. @DataProvider
  90. public static Object[][] unsupportedMetricTypes() {
  91. return new Object[][] {
  92. {BOOL},
  93. {STRING},
  94. {DATA},
  95. {DISTRIB}
  96. };
  97. }
  98. private void test(@Nullable QualityGateEvaluator.Measure measure, Condition.Operator operator, String errorThreshold, EvaluatedCondition.EvaluationStatus expectedStatus, @Nullable String expectedValue) {
  99. Condition condition = new Condition("foo", operator, errorThreshold);
  100. EvaluatedCondition result = ConditionEvaluator.evaluate(condition, new FakeMeasures(measure));
  101. assertThat(result.getStatus()).isEqualTo(expectedStatus);
  102. if (expectedValue == null) {
  103. assertThat(result.getValue()).isNotPresent();
  104. } else {
  105. assertThat(result.getValue()).hasValue(expectedValue);
  106. }
  107. }
  108. private void testOnLeak(QualityGateEvaluator.Measure measure, Condition.Operator operator, String errorThreshold, EvaluatedCondition.EvaluationStatus expectedStatus,
  109. @Nullable String expectedValue) {
  110. Condition condition = new Condition("new_foo", operator, errorThreshold);
  111. EvaluatedCondition result = ConditionEvaluator.evaluate(condition, new FakeMeasures(measure));
  112. assertThat(result.getStatus()).isEqualTo(expectedStatus);
  113. if (expectedValue == null) {
  114. assertThat(result.getValue()).isNotPresent();
  115. } else {
  116. assertThat(result.getValue()).hasValue(expectedValue);
  117. }
  118. }
  119. private static class FakeMeasures implements QualityGateEvaluator.Measures {
  120. private final QualityGateEvaluator.Measure measure;
  121. FakeMeasures(@Nullable QualityGateEvaluator.Measure measure) {
  122. this.measure = measure;
  123. }
  124. @Override
  125. public Optional<QualityGateEvaluator.Measure> get(String metricKey) {
  126. return Optional.ofNullable(measure);
  127. }
  128. }
  129. }