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.

ConditionImplTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.ce.task.projectanalysis.api.posttask;
  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 org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.sonar.api.ce.posttask.QualityGate;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. @RunWith(DataProviderRunner.class)
  30. public class ConditionImplTest {
  31. private static final String METRIC_KEY = "metricKey";
  32. private static final String ERROR_THRESHOLD = "error threshold";
  33. private static final String VALUE = "value";
  34. private ConditionImpl.Builder builder = ConditionImpl.newBuilder()
  35. .setStatus(QualityGate.EvaluationStatus.OK)
  36. .setMetricKey(METRIC_KEY)
  37. .setOperator(QualityGate.Operator.GREATER_THAN)
  38. .setErrorThreshold(ERROR_THRESHOLD)
  39. .setValue(VALUE);
  40. @Test
  41. public void build_throws_NPE_if_status_is_null() {
  42. builder.setStatus(null);
  43. assertThatThrownBy(() -> builder.build())
  44. .isInstanceOf(NullPointerException.class)
  45. .hasMessage("status can not be null");
  46. }
  47. @Test
  48. public void build_throws_NPE_if_metricKey_is_null() {
  49. builder.setMetricKey(null);
  50. assertThatThrownBy(() -> builder.build())
  51. .isInstanceOf(NullPointerException.class)
  52. .hasMessage("metricKey can not be null");
  53. }
  54. @Test
  55. public void build_throws_NPE_if_operator_is_null() {
  56. builder.setOperator(null);
  57. assertThatThrownBy(() -> builder.build())
  58. .isInstanceOf(NullPointerException.class)
  59. .hasMessage("operator can not be null");
  60. }
  61. @Test
  62. public void build_throws_NPE_if_error_threshold_is_null() {
  63. builder.setErrorThreshold(null);
  64. assertThatThrownBy(() -> builder.build())
  65. .isInstanceOf(NullPointerException.class)
  66. .hasMessage("errorThreshold can not be null");
  67. }
  68. @Test
  69. public void getValue_throws_ISE_when_condition_type_is_NO_VALUE() {
  70. builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE).setValue(null);
  71. ConditionImpl condition = builder.build();
  72. assertThatThrownBy(condition::getValue)
  73. .isInstanceOf(IllegalStateException.class)
  74. .hasMessage("There is no value when status is NO_VALUE");
  75. }
  76. @DataProvider
  77. public static Object[][] allStatusesButNO_VALUE() {
  78. Object[][] res = new Object[QualityGate.EvaluationStatus.values().length - 1][1];
  79. int i = 0;
  80. for (QualityGate.EvaluationStatus status : QualityGate.EvaluationStatus.values()) {
  81. if (status != QualityGate.EvaluationStatus.NO_VALUE) {
  82. res[i][0] = status;
  83. i++;
  84. }
  85. }
  86. return res;
  87. }
  88. @Test
  89. @UseDataProvider("allStatusesButNO_VALUE")
  90. public void build_throws_IAE_if_value_is_null_but_status_is_not_NO_VALUE(QualityGate.EvaluationStatus status) {
  91. builder.setStatus(status)
  92. .setValue(null);
  93. assertThatThrownBy(() -> builder.build())
  94. .isInstanceOf(IllegalArgumentException.class)
  95. .hasMessage("value can not be null when status is not NO_VALUE");
  96. }
  97. @Test
  98. public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() {
  99. builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE);
  100. assertThatThrownBy(() -> builder.build())
  101. .isInstanceOf(IllegalArgumentException.class)
  102. .hasMessage("value must be null when status is NO_VALUE");
  103. }
  104. @Test
  105. public void toString_ConditionImpl_of_type_different_from_NO_VALUE() {
  106. assertThat(builder.build())
  107. .hasToString(
  108. "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', value='value'}");
  109. }
  110. @Test
  111. public void toString_ConditionImpl_of_type_NO_VALUE() {
  112. builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE)
  113. .setValue(null);
  114. assertThat(builder.build())
  115. .hasToString(
  116. "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', value='null'}");
  117. }
  118. @Test
  119. public void verify_getters() {
  120. ConditionImpl underTest = builder.build();
  121. assertThat(underTest.getStatus()).isEqualTo(QualityGate.EvaluationStatus.OK);
  122. assertThat(underTest.getMetricKey()).isEqualTo(METRIC_KEY);
  123. assertThat(underTest.getOperator()).isEqualTo(QualityGate.Operator.GREATER_THAN);
  124. assertThat(underTest.getErrorThreshold()).isEqualTo(ERROR_THRESHOLD);
  125. assertThat(underTest.getValue()).isEqualTo(VALUE);
  126. }
  127. }