123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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.ce.task.projectanalysis.api.posttask;
-
- import com.tngtech.java.junit.dataprovider.DataProvider;
- import com.tngtech.java.junit.dataprovider.DataProviderRunner;
- import com.tngtech.java.junit.dataprovider.UseDataProvider;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.sonar.api.ce.posttask.QualityGate;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
- @RunWith(DataProviderRunner.class)
- public class ConditionImplTest {
- private static final String METRIC_KEY = "metricKey";
- private static final String ERROR_THRESHOLD = "error threshold";
- private static final String VALUE = "value";
-
-
- private ConditionImpl.Builder builder = ConditionImpl.newBuilder()
- .setStatus(QualityGate.EvaluationStatus.OK)
- .setMetricKey(METRIC_KEY)
- .setOperator(QualityGate.Operator.GREATER_THAN)
- .setErrorThreshold(ERROR_THRESHOLD)
- .setValue(VALUE);
-
- @Test
- public void build_throws_NPE_if_status_is_null() {
- builder.setStatus(null);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(NullPointerException.class)
- .hasMessage("status can not be null");
- }
-
- @Test
- public void build_throws_NPE_if_metricKey_is_null() {
- builder.setMetricKey(null);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(NullPointerException.class)
- .hasMessage("metricKey can not be null");
- }
-
- @Test
- public void build_throws_NPE_if_operator_is_null() {
- builder.setOperator(null);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(NullPointerException.class)
- .hasMessage("operator can not be null");
- }
-
- @Test
- public void build_throws_NPE_if_error_threshold_is_null() {
- builder.setErrorThreshold(null);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(NullPointerException.class)
- .hasMessage("errorThreshold can not be null");
- }
-
- @Test
- public void getValue_throws_ISE_when_condition_type_is_NO_VALUE() {
- builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE).setValue(null);
- ConditionImpl condition = builder.build();
-
- assertThatThrownBy(condition::getValue)
- .isInstanceOf(IllegalStateException.class)
- .hasMessage("There is no value when status is NO_VALUE");
- }
-
- @DataProvider
- public static Object[][] allStatusesButNO_VALUE() {
- Object[][] res = new Object[QualityGate.EvaluationStatus.values().length - 1][1];
- int i = 0;
- for (QualityGate.EvaluationStatus status : QualityGate.EvaluationStatus.values()) {
- if (status != QualityGate.EvaluationStatus.NO_VALUE) {
- res[i][0] = status;
- i++;
- }
- }
- return res;
- }
-
- @Test
- @UseDataProvider("allStatusesButNO_VALUE")
- public void build_throws_IAE_if_value_is_null_but_status_is_not_NO_VALUE(QualityGate.EvaluationStatus status) {
- builder.setStatus(status)
- .setValue(null);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("value can not be null when status is not NO_VALUE");
- }
-
- @Test
- public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() {
- builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE);
-
- assertThatThrownBy(() -> builder.build())
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("value must be null when status is NO_VALUE");
- }
-
- @Test
- public void toString_ConditionImpl_of_type_different_from_NO_VALUE() {
- assertThat(builder.build())
- .hasToString(
- "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', value='value'}");
- }
-
- @Test
- public void toString_ConditionImpl_of_type_NO_VALUE() {
- builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE)
- .setValue(null);
-
- assertThat(builder.build())
- .hasToString(
- "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', value='null'}");
- }
-
- @Test
- public void verify_getters() {
- ConditionImpl underTest = builder.build();
-
- assertThat(underTest.getStatus()).isEqualTo(QualityGate.EvaluationStatus.OK);
- assertThat(underTest.getMetricKey()).isEqualTo(METRIC_KEY);
- assertThat(underTest.getOperator()).isEqualTo(QualityGate.Operator.GREATER_THAN);
- assertThat(underTest.getErrorThreshold()).isEqualTo(ERROR_THRESHOLD);
- assertThat(underTest.getValue()).isEqualTo(VALUE);
- }
- }
|