3 * Copyright (C) 2009-2023 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.api.batch.sensor.rule.internal;
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;
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;
34 public class DefaultAdHocRuleTest {
38 SensorStorage storage = mock(SensorStorage.class);
39 DefaultAdHocRule rule = new DefaultAdHocRule(storage)
44 .severity(Severity.BLOCKER)
45 .type(RuleType.CODE_SMELL);
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);
55 verify(storage).store(any(DefaultAdHocRule.class));
59 public void description_is_optional() {
60 SensorStorage storage = mock(SensorStorage.class);
61 new DefaultAdHocRule(storage)
65 .severity(Severity.BLOCKER)
66 .type(RuleType.CODE_SMELL)
69 verify(storage).store(any(DefaultAdHocRule.class));
73 public void fail_to_store_if_no_engine_id() {
74 SensorStorage storage = mock(SensorStorage.class);
75 NewAdHocRule rule = new DefaultAdHocRule(storage)
80 .severity(Severity.BLOCKER)
81 .type(RuleType.CODE_SMELL);
83 assertThatThrownBy(() -> rule.save())
84 .isInstanceOf(IllegalStateException.class)
85 .hasMessageContaining("Engine id is mandatory");
89 public void fail_to_store_if_no_rule_id() {
90 SensorStorage storage = mock(SensorStorage.class);
91 NewAdHocRule rule = new DefaultAdHocRule(storage)
96 .severity(Severity.BLOCKER)
97 .type(RuleType.CODE_SMELL);
99 assertThatThrownBy(() -> rule.save())
100 .isInstanceOf(IllegalStateException.class)
101 .hasMessageContaining("Rule id is mandatory");
105 public void fail_to_store_if_no_name() {
106 SensorStorage storage = mock(SensorStorage.class);
107 NewAdHocRule rule = new DefaultAdHocRule(storage)
112 .severity(Severity.BLOCKER)
113 .type(RuleType.CODE_SMELL);
115 assertThatThrownBy(() -> rule.save())
116 .isInstanceOf(IllegalStateException.class)
117 .hasMessageContaining("Name is mandatory");
121 public void fail_to_store_if_no_severity() {
122 SensorStorage storage = mock(SensorStorage.class);
123 NewAdHocRule rule = new DefaultAdHocRule(storage)
128 .type(RuleType.CODE_SMELL);
130 assertThatThrownBy(() -> rule.save())
131 .isInstanceOf(IllegalStateException.class)
132 .hasMessageContaining("Severity is mandatory");
136 public void fail_to_store_if_no_type() {
137 SensorStorage storage = mock(SensorStorage.class);
138 NewAdHocRule rule = new DefaultAdHocRule(storage)
143 .severity(Severity.BLOCKER);
145 assertThatThrownBy(() -> rule.save())
146 .isInstanceOf(IllegalStateException.class)
147 .hasMessageContaining("Type is mandatory");