3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.
21 package org.sonar.server.component.ws;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.server.component.es.ProjectMeasuresQuery;
27 import org.sonar.test.TestUtils;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.tuple;
31 import static org.sonar.server.component.es.ProjectMeasuresQuery.MetricCriterion;
32 import static org.sonar.server.component.es.ProjectMeasuresQuery.Operator;
33 import static org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery;
34 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK;
35 import static org.sonar.test.ExceptionCauseMatcher.hasType;
37 public class ProjectMeasuresQueryFactoryTest {
40 public ExpectedException expectedException = ExpectedException.none();
43 public void create_query() throws Exception {
44 ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc > 10 and coverage <= 80");
46 assertThat(query.getMetricCriteria())
47 .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue)
49 tuple("ncloc", Operator.GT, 10d),
50 tuple("coverage", Operator.LTE, 80d));
54 public void create_query_having_equal_operation() throws Exception {
55 ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc = 10");
57 assertThat(query.getMetricCriteria())
58 .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue)
59 .containsOnly(tuple("ncloc", Operator.EQ, 10d));
63 public void create_query_on_quality_gate() throws Exception {
64 ProjectMeasuresQuery query = newProjectMeasuresQuery("alert_status = OK");
66 assertThat(query.getQualityGateStatus().name()).isEqualTo(OK.name());
70 public void fail_to_create_query_on_quality_gate_when_operator_is_not_equal() throws Exception {
71 expectedException.expect(IllegalArgumentException.class);
72 newProjectMeasuresQuery("alert_status > OK");
76 public void search_is_case_insensitive() throws Exception {
77 assertThat(newProjectMeasuresQuery("ncloc > 10 AnD coverage <= 80 AND debt = 10 AND issues = 20").getMetricCriteria()).hasSize(4);
81 public void convert_metric_to_lower_case() throws Exception {
82 assertThat(newProjectMeasuresQuery("NCLOC > 10 AND coVERage <= 80").getMetricCriteria())
83 .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue)
85 tuple("ncloc", Operator.GT, 10d),
86 tuple("coverage", Operator.LTE, 80d));
90 public void ignore_white_spaces() throws Exception {
91 assertThat(newProjectMeasuresQuery(" ncloc > 10 ").getMetricCriteria())
92 .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue)
93 .containsOnly(tuple("ncloc", Operator.GT, 10d));
97 public void accept_empty_query() throws Exception {
98 ProjectMeasuresQuery result = newProjectMeasuresQuery("");
100 assertThat(result.getMetricCriteria()).isEmpty();
104 public void fail_on_unknown_operator() throws Exception {
105 expectedException.expect(IllegalArgumentException.class);
106 expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("Unknown operator '>='"));
107 newProjectMeasuresQuery("ncloc >= 10");
111 public void fail_on_invalid_criteria() throws Exception {
112 expectedException.expect(IllegalArgumentException.class);
113 expectedException.expectMessage("Invalid criterion 'ncloc ? 10'");
114 newProjectMeasuresQuery("ncloc ? 10");
118 public void fail_when_not_double() throws Exception {
119 expectedException.expect(IllegalArgumentException.class);
120 expectedException.expectMessage("Invalid criterion 'ncloc > ten'");
121 newProjectMeasuresQuery("ncloc > ten");
125 public void fail_when_no_operator() throws Exception {
126 expectedException.expect(IllegalArgumentException.class);
127 expectedException.expectMessage("Invalid criterion 'ncloc 10'");
128 newProjectMeasuresQuery("ncloc 10");
132 public void fail_when_no_key() throws Exception {
133 expectedException.expect(IllegalArgumentException.class);
134 expectedException.expectMessage("Invalid criterion '>= 10'");
135 newProjectMeasuresQuery(">= 10");
139 public void fail_when_no_value() throws Exception {
140 expectedException.expect(IllegalArgumentException.class);
141 expectedException.expectMessage("Invalid criterion 'ncloc >='");
142 newProjectMeasuresQuery("ncloc >=");
146 public void private_constructor() {
147 assertThat(TestUtils.hasOnlyPrivateConstructors(ProjectMeasuresQueryFactory.class)).isTrue();