Skip to content

Commit

Permalink
SONAR-6620 Condition should expose operator as an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Jun 15, 2015
1 parent 609ad2c commit cc9c0c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
Expand Up @@ -31,24 +31,44 @@
@Immutable
public class Condition {

enum Operator {
EQUALS("EQ"), NOT_EQUALS("NE"), GREATER_THAN("GT"), LESS_THAN("LT");

private final String dbValue;

Operator(String dbValue) {
this.dbValue = dbValue;
}
}

private final Metric metric;
@CheckForNull
private final Integer period;
private final String operator;
private final Operator operator;
@CheckForNull
private final String warningThreshold;
@CheckForNull
private final String errorThreshold;
@CheckForNull
private final Integer period;

public Condition(Metric metric, @Nullable Integer period,
String operator, @Nullable String errorThreshold, @Nullable String warningThreshold) {
public Condition(Metric metric, String operator,
@Nullable String errorThreshold, @Nullable String warningThreshold,
@Nullable Integer period) {
this.metric = requireNonNull(metric);
this.operator = requireNonNull(operator);
this.operator = parseFromDbValue(requireNonNull(operator));
this.period = period;
this.errorThreshold = errorThreshold;
this.warningThreshold = warningThreshold;
}

private static Operator parseFromDbValue(String str) {
for (Operator operator : Operator.values()) {
if (operator.dbValue.equals(str)) {
return operator;
}
}
throw new IllegalArgumentException(String.format("Unsupported operator value: '%s'", str));
}

public Metric getMetric() {
return metric;
}
Expand All @@ -58,7 +78,7 @@ public Integer getPeriod() {
return period;
}

public String getOperator() {
public Operator getOperator() {
return operator;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public QualityGateServiceImpl(QualityGateDao qualityGateDao, QualityGateConditio
@Nonnull
public Condition apply(@Nonnull QualityGateConditionDto input) {
Metric metric = metricRepository.getById(input.getMetricId());
return new Condition(metric, input.getPeriod(), input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold());
return new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold(), input.getPeriod());
}
};
}
Expand Down
Expand Up @@ -19,37 +19,51 @@
*/
package org.sonar.server.computation.qualitygate;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.server.computation.metric.Metric;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ConditionTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

private static final Metric SOME_METRIC = mock(Metric.class);
private static final String SOME_OPERATOR = "ope";
private static final String SOME_OPERATOR = "EQ";

@Test(expected = NullPointerException.class)
public void constructor_throws_NPE_for_null_metric_argument() {
new Condition(null, null, SOME_OPERATOR, null, null);
new Condition(null, SOME_OPERATOR, null, null, null);
}

@Test(expected = NullPointerException.class)
public void constructor_throws_NPE_for_null_operator_argument() {
new Condition(SOME_METRIC, null, null, null, null);
}

@Test
public void constructor_throws_IAE_if_operator_is_not_valid() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Unsupported operator value: 'troloto'");

new Condition(SOME_METRIC, "troloto", null, null, null);
}

@Test
public void verify_getters() {
Integer period = 1;
String error = "error threshold";
String warning = "warning threshold";

Condition condition = new Condition(SOME_METRIC, period, SOME_OPERATOR, error, warning);
Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error, warning, period);

assertThat(condition.getMetric()).isSameAs(SOME_METRIC);
assertThat(condition.getOperator()).isSameAs(SOME_OPERATOR);
assertThat(condition.getOperator()).isSameAs(Condition.Operator.EQUALS);
assertThat(condition.getPeriod()).isEqualTo(period);
assertThat(condition.getErrorThreshold()).isEqualTo(error);
assertThat(condition.getWarningThreshold()).isEqualTo(warning);
Expand All @@ -59,8 +73,8 @@ public void verify_getters() {
public void all_fields_are_displayed_in_toString() {
when(SOME_METRIC.toString()).thenReturn("metric1");

assertThat(new Condition(SOME_METRIC, 1, SOME_OPERATOR, "error_l", "warn").toString())
.isEqualTo("Condition{metric=metric1, period=1, operator=ope, warningThreshold=warn, errorThreshold=error_l}");
assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l", "warn", 1).toString())
.isEqualTo("Condition{metric=metric1, period=1, operator=EQUALS, warningThreshold=warn, errorThreshold=error_l}");

}

Expand Down
Expand Up @@ -43,8 +43,8 @@ public class QualityGateServiceImplTest {
private static final long METRIC_ID_2 = 753;
private static final Metric METRIC_1 = mock(Metric.class);
private static final Metric METRIC_2 = mock(Metric.class);
private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("=").setPeriod(1).setWarningThreshold("warnin_th").setErrorThreshold("error_th");
private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("=");
private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("EQ").setPeriod(1).setWarningThreshold("warnin_th").setErrorThreshold("error_th");
private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("NE");

private QualityGateDao qualityGateDao = mock(QualityGateDao.class);
private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class);
Expand Down Expand Up @@ -81,8 +81,8 @@ public void findById_returns_conditions_when_there_is_some_in_DB() {
assertThat(res).isPresent();
assertThat(res.get().getName()).isEqualTo(SOME_NAME);
assertThat(res.get().getConditions()).containsOnly(
new Condition(METRIC_1, CONDITION_1.getPeriod(), CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold(), CONDITION_1.getWarningThreshold()),
new Condition(METRIC_2, CONDITION_2.getPeriod(), CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold())
new Condition(METRIC_1, CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold(), CONDITION_1.getWarningThreshold(), CONDITION_1.getPeriod()),
new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold(), CONDITION_2.getPeriod())
);
}
}

0 comments on commit cc9c0c5

Please sign in to comment.