import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.api.database.DatabaseSession;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
private final MeasuresDao measuresDao;
private final Metrics[] metricsRepositories;
- private final DatabaseSession session;
private final QualityGateConditionDao conditionDao;
- public RegisterMetrics(DatabaseSession session, MeasuresDao measuresDao, QualityGateConditionDao conditionDao, Metrics[] metricsRepositories) {
- this.session = session;
+ public RegisterMetrics(MeasuresDao measuresDao, QualityGateConditionDao conditionDao, Metrics[] metricsRepositories) {
this.measuresDao = measuresDao;
this.metricsRepositories = metricsRepositories;
this.conditionDao = conditionDao;
List<Metric> getMetricsRepositories() {
List<Metric> metricsToRegister = newArrayList();
Map<String, Metrics> metricsByRepository = Maps.newHashMap();
- if (metricsRepositories != null) {
- for (Metrics metrics : metricsRepositories) {
- checkMetrics(metricsByRepository, metrics);
- metricsToRegister.addAll(removeExistingUserManagedMetrics(metrics.getMetrics()));
- }
+
+ for (Metrics metrics : metricsRepositories) {
+ checkMetrics(metricsByRepository, metrics);
+ metricsToRegister.addAll(removeExistingUserManagedMetrics(metrics.getMetrics()));
}
+
return metricsToRegister;
}
}
protected void cleanAlerts() {
- LOG.info("cleaning alert thresholds...");
+ LOG.info("Cleaning quality gate conditions");
conditionDao.deleteConditionsWithInvalidMetrics();
-/*
- Query query = session.createQuery("delete from " + Alert.class.getSimpleName() + " a where NOT EXISTS(FROM Metric m WHERE m=a.metric))");
- query.executeUpdate();
-
- query = session.createQuery("delete from " + Alert.class.getSimpleName() + " a where NOT EXISTS(FROM Metric m WHERE m=a.metric and m.enabled=:enabled))");
- query.setParameter("enabled", Boolean.TRUE);
- query.executeUpdate();
- session.commit();
-*/
}
protected void register(List<Metric> metrics) {
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class RegisterMetricsTest extends AbstractDbUnitTestCase {
setupData("shouldSaveIfNew");
Metric metric1 = new Metric.Builder("new1", "short1", Metric.ValueType.FLOAT)
- .setDescription("desc1")
- .setDirection(1)
- .setQualitative(true)
- .setDomain("domain1")
- .setUserManaged(false)
- .create();
+ .setDescription("desc1")
+ .setDirection(1)
+ .setQualitative(true)
+ .setDomain("domain1")
+ .setUserManaged(false)
+ .create();
Metric metric2 = new Metric.Builder("new2", "short2", Metric.ValueType.FLOAT)
- .setDescription("desc2")
- .setDirection(1)
- .setQualitative(true)
- .setDomain("domain2")
- .setUserManaged(false)
- .create();
-
- RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), null);
+ .setDescription("desc2")
+ .setDirection(1)
+ .setQualitative(true)
+ .setDomain("domain2")
+ .setUserManaged(false)
+ .create();
+
+ RegisterMetrics synchronizer = new RegisterMetrics(new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), new Metrics[0]);
synchronizer.register(Arrays.asList(metric1, metric2));
checkTables("shouldSaveIfNew", "metrics");
}
public void shouldUpdateIfAlreadyExists() {
setupData("shouldUpdateIfAlreadyExists");
- RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), null);
+ RegisterMetrics synchronizer = new RegisterMetrics(new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), new Metrics[0]);
synchronizer.register(newArrayList(new Metric.Builder("key", "new short name", Metric.ValueType.FLOAT)
- .setDescription("new description")
- .setDirection(-1)
- .setQualitative(true)
- .setDomain("new domain")
- .setUserManaged(false)
- .create()));
+ .setDescription("new description")
+ .setDirection(-1)
+ .setQualitative(true)
+ .setDomain("new domain")
+ .setUserManaged(false)
+ .create()));
checkTables("shouldUpdateIfAlreadyExists", "metrics");
}
public void shouldAddUserManagesMetric() {
Metrics metrics = mock(Metrics.class);
when(metrics.getMetrics()).thenReturn(newArrayList(new Metric.Builder("key", "new short name", Metric.ValueType.FLOAT)
- .setDescription("new description")
- .setDirection(-1)
- .setQualitative(true)
- .setDomain("new domain")
- .setUserManaged(true)
- .create()));
+ .setDescription("new description")
+ .setDirection(-1)
+ .setQualitative(true)
+ .setDomain("new domain")
+ .setUserManaged(true)
+ .create()));
MeasuresDao measuresDao = new MeasuresDao(getSession());
- RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
+ RegisterMetrics loader = new RegisterMetrics(measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
List<Metric> result = loader.getMetricsRepositories();
assertThat(result).hasSize(1);
Metrics metrics = mock(Metrics.class);
when(metrics.getMetrics()).thenReturn(newArrayList(new Metric.Builder("key", "new short name", Metric.ValueType.FLOAT)
- .setDescription("new description")
- .setDirection(-1)
- .setQualitative(true)
- .setDomain("new domain")
- .setUserManaged(true)
- .create()));
+ .setDescription("new description")
+ .setDirection(-1)
+ .setQualitative(true)
+ .setDomain("new domain")
+ .setUserManaged(true)
+ .create()));
MeasuresDao measuresDao = new MeasuresDao(getSession());
- RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
+ RegisterMetrics loader = new RegisterMetrics(measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
List<Metric> result = loader.getMetricsRepositories();
assertThat(result).isEmpty();
setupData("shouldEnableOnlyLoadedMetrics");
MeasuresDao measuresDao = new MeasuresDao(getSession());
- RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), null);
+ RegisterMetrics loader = new RegisterMetrics(measuresDao, mock(QualityGateConditionDao.class), new Metrics[0]);
loader.start();
assertThat(measuresDao.getMetric("deprecated").getEnabled()).isFalse();
}
@Test
- public void shouldCleanAlerts() {
+ public void clean_quality_gate_conditions() {
QualityGateConditionDao conditionDao = mock(QualityGateConditionDao.class);
- RegisterMetrics loader = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), conditionDao, null);
+ RegisterMetrics loader = new RegisterMetrics(new MeasuresDao(getSession()), conditionDao, new Metrics[0]);
loader.cleanAlerts();
verify(conditionDao).deleteConditionsWithInvalidMetrics();
}