import static java.lang.String.format;
import static java.util.Arrays.stream;
+import static java.util.Objects.requireNonNull;
public class ProjectMeasuresQuery {
private List<MetricCriteria> metricCriteria = new ArrayList<>();
}
public enum Operator {
- LTE("<="), GT(">");
+ LTE("<="), GT(">"), EQ("=");
String value;
private final double value;
public MetricCriteria(String metricKey, Operator operator, double value) {
- this.metricKey = metricKey;
- this.operator = operator;
- this.value = value;
+ this.metricKey = requireNonNull(metricKey);
+ this.operator = requireNonNull(operator);
+ this.value = requireNonNull(value);
}
public String getMetricKey() {
class ProjectMeasuresQueryFactory {
- private static final Splitter CRITERIA_SPLITTER = Splitter.on("and");
- private static final Pattern CRITERIA_PATTERN = Pattern.compile("(\\w+)\\s*([<>][=]?)\\s*(\\w+)");
+ private static final Splitter CRITERIA_SPLITTER = Splitter.on(Pattern.compile("and", Pattern.CASE_INSENSITIVE));
+ private static final Pattern CRITERIA_PATTERN = Pattern.compile("(\\w+)\\s*([<>]?[=]?)\\s*(\\w+)");
private ProjectMeasuresQueryFactory() {
// Only static methods
ProjectMeasuresQuery query = new ProjectMeasuresQuery();
- CRITERIA_SPLITTER.split(filter.toLowerCase(ENGLISH))
- .forEach(criteria -> processCriteria(criteria, query));
+ CRITERIA_SPLITTER.split(filter)
+ .forEach(criteria -> processCriterion(criteria, query));
return query;
}
- private static void processCriteria(String criteria, ProjectMeasuresQuery query) {
- Matcher matcher = CRITERIA_PATTERN.matcher(criteria);
- checkArgument(matcher.find() && matcher.groupCount() == 3, "Invalid criterion '%s'", criteria);
- String metric = matcher.group(1);
- Operator operator = Operator.getByValue(matcher.group(2));
- Double value = Double.parseDouble(matcher.group(3));
- query.addMetricCriterion(new MetricCriteria(metric, operator, value));
+ private static void processCriterion(String criterion, ProjectMeasuresQuery query) {
+ try {
+ Matcher matcher = CRITERIA_PATTERN.matcher(criterion);
+ checkArgument(matcher.find() && matcher.groupCount() == 3, "Criterion should have a metric, an operator and a value");
+ String metric = matcher.group(1).toLowerCase(ENGLISH);
+ Operator operator = Operator.getByValue(matcher.group(2));
+ double value = Double.parseDouble(matcher.group(3));
+ query.addMetricCriterion(new MetricCriteria(metric, operator, value));
+ } catch (Exception e) {
+ throw new IllegalArgumentException(String.format("Invalid criterion '%s'", criterion), e);
+ }
}
}
import static org.sonar.server.component.es.ProjectMeasuresQuery.MetricCriteria;
import static org.sonar.server.component.es.ProjectMeasuresQuery.Operator;
import static org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery;
+import static org.sonar.test.ExceptionCauseMatcher.hasType;
public class ProjectMeasuresQueryFactoryTest {
}
@Test
- public void convert_upper_case_to_lower_case() throws Exception {
+ public void create_query_having_equal_operation() throws Exception {
+ ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc = 10");
+
+ assertThat(query.getMetricCriteria())
+ .extracting(MetricCriteria::getMetricKey, MetricCriteria::getOperator, MetricCriteria::getValue)
+ .containsOnly(tuple("ncloc", Operator.EQ, 10d));
+ }
+
+ @Test
+ public void search_is_case_insensitive() throws Exception {
+ assertThat(newProjectMeasuresQuery("ncloc > 10 AnD coverage <= 80 AND debt = 10 AND issues = 20").getMetricCriteria()).hasSize(4);
+ }
+
+ @Test
+ public void convert_metric_to_lower_case() throws Exception {
assertThat(newProjectMeasuresQuery("NCLOC > 10 AND coVERage <= 80").getMetricCriteria())
.extracting(MetricCriteria::getMetricKey, MetricCriteria::getOperator, MetricCriteria::getValue)
.containsOnly(
@Test
public void fail_on_unknown_operator() throws Exception {
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Unknown operator '>='");
+ expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("Unknown operator '>='"));
newProjectMeasuresQuery("ncloc >= 10");
}
newProjectMeasuresQuery("ncloc ? 10");
}
+ @Test
+ public void fail_when_not_double() throws Exception {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Invalid criterion 'ncloc > ten'");
+ newProjectMeasuresQuery("ncloc > ten");
+ }
+
@Test
public void fail_when_no_operator() throws Exception {
expectedException.expect(IllegalArgumentException.class);