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.

ConditionStatusTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.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 org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  28. import static org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus.EvaluationStatus.NO_VALUE;
  29. import static org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus.EvaluationStatus.OK;
  30. import static org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus.EvaluationStatus.values;
  31. @RunWith(DataProviderRunner.class)
  32. public class ConditionStatusTest {
  33. private static final String SOME_VALUE = "value";
  34. @Test
  35. public void create_throws_NPE_if_status_argument_is_null() {
  36. assertThatThrownBy(() -> ConditionStatus.create(null, SOME_VALUE))
  37. .isInstanceOf(NullPointerException.class)
  38. .hasMessage("status can not be null");
  39. }
  40. @Test
  41. public void create_throws_IAE_if_status_argument_is_NO_VALUE() {
  42. assertThatThrownBy(() -> ConditionStatus.create(NO_VALUE, SOME_VALUE))
  43. .isInstanceOf(IllegalArgumentException.class)
  44. .hasMessage("EvaluationStatus 'NO_VALUE' can not be used with this method, use constant ConditionStatus.NO_VALUE_STATUS instead.");
  45. }
  46. @Test
  47. @UseDataProvider("allStatusesButNO_VALUE")
  48. public void create_throws_NPE_if_value_is_null_and_status_argument_is_not_NO_VALUE(ConditionStatus.EvaluationStatus status) {
  49. assertThatThrownBy(() -> ConditionStatus.create(status, null))
  50. .isInstanceOf(NullPointerException.class)
  51. .hasMessage("value can not be null");
  52. }
  53. @Test
  54. public void verify_getters() {
  55. ConditionStatus underTest = ConditionStatus.create(OK, SOME_VALUE);
  56. assertThat(underTest.getStatus()).isEqualTo(OK);
  57. assertThat(underTest.getValue()).isEqualTo(SOME_VALUE);
  58. }
  59. @Test
  60. public void verify_toString() {
  61. assertThat(ConditionStatus.create(OK, SOME_VALUE)).hasToString("ConditionStatus{status=OK, value='value'}");
  62. assertThat(ConditionStatus.NO_VALUE_STATUS).hasToString("ConditionStatus{status=NO_VALUE, value='null'}");
  63. }
  64. @Test
  65. public void constant_NO_VALUE_STATUS_has_status_NO_VALUE_and_null_value() {
  66. assertThat(ConditionStatus.NO_VALUE_STATUS.getStatus()).isEqualTo(NO_VALUE);
  67. assertThat(ConditionStatus.NO_VALUE_STATUS.getValue()).isNull();
  68. }
  69. @DataProvider
  70. public static Object[][] allStatusesButNO_VALUE() {
  71. Object[][] res = new Object[values().length - 1][1];
  72. int i = 0;
  73. for (ConditionStatus.EvaluationStatus status : values()) {
  74. if (status != NO_VALUE) {
  75. res[i][0] = status;
  76. i++;
  77. }
  78. }
  79. return res;
  80. }
  81. }