3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.qualitygate;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.ce.task.projectanalysis.metric.Metric;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 public class ConditionTest {
35 public ExpectedException expectedException = ExpectedException.none();
37 private static final Metric SOME_METRIC = mock(Metric.class);
38 private static final String SOME_OPERATOR = "EQ";
42 when(SOME_METRIC.getKey()).thenReturn("dummy key");
45 @Test(expected = NullPointerException.class)
46 public void constructor_throws_NPE_for_null_metric_argument() {
47 new Condition(null, SOME_OPERATOR, null);
50 @Test(expected = NullPointerException.class)
51 public void constructor_throws_NPE_for_null_operator_argument() {
52 new Condition(SOME_METRIC, null, null);
56 public void constructor_throws_IAE_if_operator_is_not_valid() {
57 expectedException.expect(IllegalArgumentException.class);
58 expectedException.expectMessage("Unsupported operator value: 'troloto'");
60 new Condition(SOME_METRIC, "troloto", null);
64 public void verify_getters() {
65 String error = "error threshold";
67 Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error);
69 assertThat(condition.getMetric()).isSameAs(SOME_METRIC);
70 assertThat(condition.getOperator()).isSameAs(Condition.Operator.EQUALS);
71 assertThat(condition.getErrorThreshold()).isEqualTo(error);
75 public void all_fields_are_displayed_in_toString() {
76 when(SOME_METRIC.toString()).thenReturn("metric1");
78 assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l").toString())
79 .isEqualTo("Condition{metric=metric1, operator=EQUALS, errorThreshold=error_l}");