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.ce.task.projectanalysis.issue.commonrule;
22 import com.google.common.collect.ImmutableMap;
23 import org.junit.Test;
24 import org.sonar.api.rule.Severity;
25 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
26 import org.sonar.db.rule.RuleTesting;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
31 public class CommonRuleTest {
33 private static final String PLUGIN_KEY = "java";
34 private static final String QP_KEY = "qp1";
38 public void test_getMinDensityParam() {
39 ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "30.5"), 1_000L, PLUGIN_KEY, QP_KEY);
40 double minDensity = CommonRule.getMinDensityParam(activeRule, "minDensity");
42 assertThat(minDensity).isEqualTo(30.5);
46 public void getMinDensityParam_fails_if_param_value_is_absent() {
47 assertThatThrownBy(() -> {
48 ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, PLUGIN_KEY, QP_KEY);
49 CommonRule.getMinDensityParam(activeRule, "minDensity");
51 .isInstanceOf(IllegalStateException.class)
52 .hasMessage("Required parameter [minDensity] is missing on rule [xoo:x1]");
56 public void getMinDensityParam_fails_if_param_value_is_negative() {
57 assertThatThrownBy(() -> {
58 ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, PLUGIN_KEY, QP_KEY);
59 CommonRule.getMinDensityParam(activeRule, "minDensity");
61 .isInstanceOf(IllegalStateException.class)
62 .hasMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100.");
66 public void getMinDensityParam_fails_if_param_value_is_greater_than_100() {
67 assertThatThrownBy(() -> {
68 ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, PLUGIN_KEY, QP_KEY);
69 CommonRule.getMinDensityParam(activeRule, "minDensity");
71 .isInstanceOf(IllegalStateException.class)
72 .hasMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100.");