]> source.dussan.org Git - sonarqube.git/blob
e6acb186e2ea4e32406e64207b9cbeadb31f4c50
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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
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;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 public class ConditionTest {
33
34   @Rule
35   public ExpectedException expectedException = ExpectedException.none();
36
37   private static final Metric SOME_METRIC = mock(Metric.class);
38   private static final String SOME_OPERATOR = "EQ";
39
40   @Before
41   public void setUp() {
42     when(SOME_METRIC.getKey()).thenReturn("dummy key");
43   }
44
45   @Test(expected = NullPointerException.class)
46   public void constructor_throws_NPE_for_null_metric_argument() {
47     new Condition(null, SOME_OPERATOR, null);
48   }
49
50   @Test(expected = NullPointerException.class)
51   public void constructor_throws_NPE_for_null_operator_argument() {
52     new Condition(SOME_METRIC, null, null);
53   }
54
55   @Test
56   public void constructor_throws_IAE_if_operator_is_not_valid() {
57     expectedException.expect(IllegalArgumentException.class);
58     expectedException.expectMessage("Unsupported operator value: 'troloto'");
59
60     new Condition(SOME_METRIC, "troloto", null);
61   }
62
63   @Test
64   public void verify_getters() {
65     String error = "error threshold";
66
67     Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error);
68
69     assertThat(condition.getMetric()).isSameAs(SOME_METRIC);
70     assertThat(condition.getOperator()).isSameAs(Condition.Operator.EQUALS);
71     assertThat(condition.getErrorThreshold()).isEqualTo(error);
72   }
73
74   @Test
75   public void all_fields_are_displayed_in_toString() {
76     when(SOME_METRIC.toString()).thenReturn("metric1");
77
78     assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l").toString())
79       .isEqualTo("Condition{metric=metric1, operator=EQUALS, errorThreshold=error_l}");
80
81   }
82 }