]> source.dussan.org Git - sonarqube.git/blob
354a729c0bb0de81ef9d342aa60c018ef0fbce20
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.api.batch.sensor.rule.internal;
21
22 import org.junit.Test;
23 import org.sonar.api.batch.rule.Severity;
24 import org.sonar.api.batch.sensor.internal.SensorStorage;
25 import org.sonar.api.batch.sensor.rule.NewAdHocRule;
26 import org.sonar.api.rules.RuleType;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33
34 public class DefaultAdHocRuleTest {
35
36   @Test
37   public void store() {
38     SensorStorage storage = mock(SensorStorage.class);
39     DefaultAdHocRule rule = new DefaultAdHocRule(storage)
40       .engineId("engine")
41       .ruleId("ruleId")
42       .name("name")
43       .description("desc")
44       .severity(Severity.BLOCKER)
45       .type(RuleType.CODE_SMELL);
46     rule.save();
47
48     assertThat(rule.engineId()).isEqualTo("engine");
49     assertThat(rule.ruleId()).isEqualTo("ruleId");
50     assertThat(rule.name()).isEqualTo("name");
51     assertThat(rule.description()).isEqualTo("desc");
52     assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
53     assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
54
55     verify(storage).store(any(DefaultAdHocRule.class));
56   }
57
58   @Test
59   public void description_is_optional() {
60     SensorStorage storage = mock(SensorStorage.class);
61     new DefaultAdHocRule(storage)
62       .engineId("engine")
63       .ruleId("ruleId")
64       .name("name")
65       .severity(Severity.BLOCKER)
66       .type(RuleType.CODE_SMELL)
67       .save();
68
69     verify(storage).store(any(DefaultAdHocRule.class));
70   }
71
72   @Test
73   public void fail_to_store_if_no_engine_id() {
74     SensorStorage storage = mock(SensorStorage.class);
75     NewAdHocRule rule = new DefaultAdHocRule(storage)
76       .engineId(" ")
77       .ruleId("ruleId")
78       .name("name")
79       .description("desc")
80       .severity(Severity.BLOCKER)
81       .type(RuleType.CODE_SMELL);
82
83     assertThatThrownBy(() -> rule.save())
84       .isInstanceOf(IllegalStateException.class)
85       .hasMessageContaining("Engine id is mandatory");
86   }
87
88   @Test
89   public void fail_to_store_if_no_rule_id() {
90     SensorStorage storage = mock(SensorStorage.class);
91     NewAdHocRule rule = new DefaultAdHocRule(storage)
92       .engineId("engine")
93       .ruleId("  ")
94       .name("name")
95       .description("desc")
96       .severity(Severity.BLOCKER)
97       .type(RuleType.CODE_SMELL);
98
99     assertThatThrownBy(() -> rule.save())
100       .isInstanceOf(IllegalStateException.class)
101       .hasMessageContaining("Rule id is mandatory");
102   }
103
104   @Test
105   public void fail_to_store_if_no_name() {
106     SensorStorage storage = mock(SensorStorage.class);
107     NewAdHocRule rule = new DefaultAdHocRule(storage)
108       .engineId("engine")
109       .ruleId("ruleId")
110       .name("  ")
111       .description("desc")
112       .severity(Severity.BLOCKER)
113       .type(RuleType.CODE_SMELL);
114
115     assertThatThrownBy(() -> rule.save())
116       .isInstanceOf(IllegalStateException.class)
117       .hasMessageContaining("Name is mandatory");
118   }
119
120   @Test
121   public void fail_to_store_if_no_severity() {
122     SensorStorage storage = mock(SensorStorage.class);
123     NewAdHocRule rule = new DefaultAdHocRule(storage)
124       .engineId("engine")
125       .ruleId("ruleId")
126       .name("name")
127       .description("desc")
128       .type(RuleType.CODE_SMELL);
129
130     assertThatThrownBy(() -> rule.save())
131       .isInstanceOf(IllegalStateException.class)
132       .hasMessageContaining("Severity is mandatory");
133   }
134
135   @Test
136   public void fail_to_store_if_no_type() {
137     SensorStorage storage = mock(SensorStorage.class);
138     NewAdHocRule rule = new DefaultAdHocRule(storage)
139       .engineId("engine")
140       .ruleId("ruleId")
141       .name("name")
142       .description("desc")
143       .severity(Severity.BLOCKER);
144
145     assertThatThrownBy(() -> rule.save())
146       .isInstanceOf(IllegalStateException.class)
147       .hasMessageContaining("Type is mandatory");
148   }
149 }