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.Ignore;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.metric.MetricTesting;
34 public class SearchProjectsQueryBuilderValidatorTest {
37 public ExpectedException expectedException = ExpectedException.none();
40 public DbTester db = DbTester.create(System2.INSTANCE);
42 DbClient dbClient = db.getDbClient();
43 DbSession dbSession = db.getSession();
45 SearchProjectsQueryBuilderValidator validator = new SearchProjectsQueryBuilderValidator(dbClient);
48 public void does_not_fail_when_metric_criteria_contains_an_existing_metric() throws Exception {
49 insertMetric("ncloc");
51 validator.validate(dbSession, SearchProjectsQueryBuilder.build("ncloc > 10"));
55 public void fail_when_metric_does_not_exists() throws Exception {
56 insertMetric("ncloc");
58 expectedException.expect(IllegalArgumentException.class);
59 expectedException.expectMessage("Unknown metric(s) [unknown]");
60 validator.validate(dbSession, SearchProjectsQueryBuilder.build("unknown > 10"));
64 public void return_all_unknown_metrics() throws Exception {
65 insertMetric("ncloc");
67 expectedException.expect(IllegalArgumentException.class);
68 expectedException.expectMessage("Unknown metric(s) [coverage, debt]");
69 validator.validate(dbSession, SearchProjectsQueryBuilder.build("debt > 10 AND ncloc <= 20 AND coverage > 30"));
72 private void insertMetric(String metricKey) {
73 dbClient.metricDao().insert(dbSession, MetricTesting.newMetricDto().setKey(metricKey));