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.server.component.ws;
22 import java.util.Arrays;
23 import org.junit.Test;
24 import org.sonar.server.measure.index.ProjectMeasuresQuery;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion.create;
29 public class ProjectMeasuresQueryValidatorTest {
33 public void query_with_empty_metrics_is_valid() {
34 ProjectMeasuresQueryValidator.validate(new ProjectMeasuresQuery());
38 public void filter_by_ncloc_is_valid() {
39 assertValidFilterKey("ncloc");
43 public void filter_by_duplicated_lines_density_is_valid() {
44 assertValidFilterKey("duplicated_lines_density");
48 public void filter_by_coverage_is_valid() {
49 assertValidFilterKey("coverage");
53 public void filter_by_sqale_rating_is_valid() {
54 assertValidFilterKey("sqale_rating");
58 public void filter_by_reliability_rating_is_valid() {
59 assertValidFilterKey("reliability_rating");
63 public void filter_by_security_rating_is_valid() {
64 assertValidFilterKey("security_rating");
68 public void filter_by_alert_status_is_valid() {
69 assertValidFilterKey("alert_status");
73 public void filter_by_ncloc_language_distribution_is_valid() {
74 assertValidFilterKey("ncloc_language_distribution");
78 public void filter_by_new_security_rating_is_valid() {
79 assertValidFilterKey("new_security_rating");
83 public void filter_by_new_maintainability_rating_is_valid() {
84 assertValidFilterKey("new_maintainability_rating");
88 public void filter_by_new_coverage_is_valid() {
89 assertValidFilterKey("new_coverage");
93 public void filter_by_new_duplicated_lines_density_is_valid() {
94 assertValidFilterKey("new_duplicated_lines_density");
98 public void filter_by_new_lines_is_valid() {
99 assertValidFilterKey("new_lines");
103 public void filter_by_new_reliability_rating_is_valid() {
104 assertValidFilterKey("new_reliability_rating");
108 public void filter_by_bla_is_invalid() {
109 assertInvalidFilterKey("bla");
113 public void filter_by_bla_and_new_lines_is_invalid() {
114 assertInvalidFilterKeys("Following metrics are not supported: 'bla'", "bla", "new_lines");
118 public void filter_by_new_lines_and_bla_is_invalid() {
119 assertInvalidFilterKeys("Following metrics are not supported: 'bla'", "new_lines", "bla");
123 public void filter_by_NeW_LiNeS_is_invalid() {
124 assertInvalidFilterKey("NeW_LiNeS");
128 public void filter_by_empty_string_is_invalid() {
129 assertInvalidFilterKey("");
133 public void sort_by_ncloc_is_valid() {
134 assertValidSortKey("ncloc");
138 public void sort_by_duplicated_lines_density_is_valid() {
139 assertValidSortKey("duplicated_lines_density");
143 public void sort_by_coverage_is_valid() {
144 assertValidSortKey("coverage");
148 public void sort_by_sqale_rating_is_valid() {
149 assertValidSortKey("sqale_rating");
153 public void sort_by_reliability_rating_is_valid() {
154 assertValidSortKey("reliability_rating");
158 public void sort_by_security_rating_is_valid() {
159 assertValidSortKey("security_rating");
163 public void sort_by_alert_status_is_valid() {
164 assertValidSortKey("alert_status");
168 public void sort_by_ncloc_language_distribution_is_valid() {
169 assertValidSortKey("ncloc_language_distribution");
173 public void sort_by_new_security_rating_is_valid() {
174 assertValidSortKey("new_security_rating");
178 public void sort_by_new_maintainability_rating_is_valid() {
179 assertValidSortKey("new_maintainability_rating");
183 public void sort_by_new_coverage_is_valid() {
184 assertValidSortKey("new_coverage");
188 public void sort_by_new_duplicated_lines_density_is_valid() {
189 assertValidSortKey("new_duplicated_lines_density");
193 public void sort_by_new_lines_is_valid() {
194 assertValidSortKey("new_lines");
198 public void sort_by_new_reliability_rating_is_valid() {
199 assertValidSortKey("new_reliability_rating");
203 public void sort_by_bla_is_invalid() {
204 assertInvalidSortKey("bla");
208 public void sort_by_NeW_lInEs_is_invalid() {
209 assertInvalidSortKey("NeW_lInEs");
213 public void sort_by_empty_string_is_invalid() {
214 assertInvalidSortKey("");
217 private void assertValidSortKey(String metricKey) {
218 // do not expect an exception
219 ProjectMeasuresQueryValidator.validate(new ProjectMeasuresQuery().setSort(metricKey));
222 private void assertInvalidSortKey(String metricKey) {
223 assertThatThrownBy(() -> ProjectMeasuresQueryValidator.validate(new ProjectMeasuresQuery().setSort(metricKey)))
224 .isInstanceOf(IllegalArgumentException.class)
225 .hasMessage("Following metrics are not supported: '" + metricKey + "'");
228 private static void assertValidFilterKey(String... metricKeys) {
229 // do not expect an exception
230 validateFilterKeys(metricKeys);
233 private void assertInvalidFilterKey(String metricKey) {
234 assertInvalidFilterKeys("Following metrics are not supported: '" + metricKey + "'", metricKey);
237 private void assertInvalidFilterKeys(String message, String... metricKeys) {
238 assertThatThrownBy(() -> validateFilterKeys(metricKeys))
239 .isInstanceOf(IllegalArgumentException.class)
240 .hasMessage(message);
243 private static void validateFilterKeys(String... metricKeys) {
244 ProjectMeasuresQuery query = new ProjectMeasuresQuery();
245 Arrays.stream(metricKeys).forEachOrdered(metricKey -> query.addMetricCriterion(create(metricKey, ProjectMeasuresQuery.Operator.LT, 80d)));
246 ProjectMeasuresQueryValidator.validate(query);