*/
package org.sonar.db.metric;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import org.apache.ibatis.session.RowBounds;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
-import static com.google.common.collect.Lists.newArrayList;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
-import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
public class MetricDao implements Dao {
return mapper(session).selectAllEnabled();
}
- public List<MetricDto> selectEnabled(DbSession session, @Nullable Boolean isCustom, int offset, int limit) {
- Map<String, Object> properties;
- if (isCustom != null) {
- properties = Collections.singletonMap("isCustom", isCustom);
- } else {
- properties = Collections.emptyMap();
- }
-
- return mapper(session).selectAllEnabled(properties, new RowBounds(offset, limit));
+ public List<MetricDto> selectEnabled(DbSession session, int offset, int limit) {
+ return mapper(session).selectAllEnabled(new RowBounds(offset, limit));
}
- public int countEnabled(DbSession session, @Nullable Boolean isCustom) {
- return mapper(session).countEnabled(isCustom);
+ public int countEnabled(DbSession session) {
+ return mapper(session).countEnabled();
}
public MetricDto insert(DbSession session, MetricDto dto) {
insert(session, Lists.asList(item, others));
}
- public List<String> selectEnabledDomains(DbSession session) {
- return newArrayList(Collections2.filter(mapper(session).selectDomains(), new NotEmptyPredicate()));
- }
-
public List<MetricDto> selectByUuids(DbSession session, Set<String> uuidsSet) {
return executeLargeInputs(new ArrayList<>(uuidsSet), mapper(session)::selectByUuids);
}
- private static class NotEmptyPredicate implements Predicate<String> {
-
- @Override
- public boolean apply(@Nonnull String input) {
- return !input.isEmpty();
- }
- }
-
private static MetricMapper mapper(DbSession session) {
return session.getMapper(MetricMapper.class);
}
- public void disableCustomByUuids(final DbSession session, List<String> uuids) {
- executeLargeInputsWithoutOutput(uuids, input -> mapper(session).disableByUuids(input));
- }
-
/**
* Disable a metric and return {@code false} if the metric does not exist
* or is already disabled.
*/
- public boolean disableCustomByKey(DbSession session, String key) {
+ public boolean disableByKey(DbSession session, String key) {
return mapper(session).disableByKey(key) == 1;
}
private boolean qualitative;
- private boolean userManaged;
-
private Double worstValue;
private Double bestValue;
return this;
}
- public boolean isUserManaged() {
- return userManaged;
- }
-
- public MetricDto setUserManaged(boolean userManaged) {
- this.userManaged = userManaged;
- return this;
- }
-
@CheckForNull
public Double getWorstValue() {
return worstValue;
package org.sonar.db.metric;
import java.util.List;
-import java.util.Map;
-import javax.annotation.Nullable;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
List<MetricDto> selectAllEnabled();
- List<MetricDto> selectAllEnabled(Map<String, Object> properties, RowBounds rowBounds);
+ List<MetricDto> selectAllEnabled(RowBounds rowBounds);
void insert(MetricDto dto);
- List<String> selectDomains();
-
- void disableByUuids(@Param("uuids") List<String> uuids);
-
int disableByKey(@Param("key") String key);
- int countEnabled(@Param("isCustom") @Nullable Boolean isCustom);
+ int countEnabled();
void update(MetricDto metric);
}
m.short_name as shortName,
m.qualitative,
m.val_type as valueType,
- m.user_managed as userManaged,
m.enabled,
m.worst_value as worstValue,
m.best_value as bestValue,
FROM metrics m
<where>
m.enabled=${_true}
- <if test="isCustom!=null">
- <if test="isCustom.equals(true)">
- AND m.user_managed=${_true}
- </if>
- <if test="isCustom.equals(false)">
- AND m.user_managed=${_false}
- </if>
- </if>
</where>
ORDER BY UPPER(m.short_name), m.short_name
</select>
FROM metrics m
<where>
m.enabled=${_true}
- <if test="isCustom!=null">
- <if test="isCustom.equals(true)">
- AND m.user_managed=${_true}
- </if>
- <if test="isCustom.equals(false)">
- AND m.user_managed=${_false}
- </if>
- </if>
</where>
</select>
<insert id="insert" parameterType="org.sonar.db.metric.MetricDto" useGeneratedKeys="false">
INSERT INTO metrics (
- uuid, name, description, direction, domain, short_name, qualitative, val_type, user_managed, enabled, worst_value,
+ uuid, name, description, direction, domain, short_name, qualitative, val_type, enabled, worst_value,
best_value, optimized_best_value, hidden, delete_historical_data, decimal_scale)
VALUES (
#{uuid, jdbcType=VARCHAR}, #{kee, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{direction, jdbcType=INTEGER},
#{domain, jdbcType=VARCHAR}, #{shortName, jdbcType=VARCHAR}, #{qualitative, jdbcType=BOOLEAN},
- #{valueType, jdbcType=VARCHAR}, #{userManaged, jdbcType=BOOLEAN}, #{enabled, jdbcType=BOOLEAN},
+ #{valueType, jdbcType=VARCHAR}, #{enabled, jdbcType=BOOLEAN},
#{worstValue, jdbcType=DOUBLE}, #{bestValue, jdbcType=DOUBLE},
#{optimizedBestValue, jdbcType=BOOLEAN}, #{hidden, jdbcType=BOOLEAN}, #{deleteHistoricalData, jdbcType=BOOLEAN},
#{decimalScale, jdbcType=INTEGER}
where uuid=#{uuid}
</update>
- <select id="selectDomains" resultType="String">
- select distinct domain
- from metrics m
- where m.domain is not null and m.enabled=${_true}
- </select>
-
- <update id="disableByUuids">
- update metrics
- set enabled=${_false}
- <where>
- AND user_managed=${_true}
- AND uuid in
- <foreach item="uuid" collection="uuids" open="(" separator="," close=")">
- #{uuid}
- </foreach>
- </where>
- </update>
-
<update id="disableByKey" parameterType="string">
update metrics
set enabled=${_false}
@Test
public void select_by_key_enabled_metric() {
- MetricDto expected = db.measures().insertMetric(t -> t.setEnabled(true).setUserManaged(false));
+ MetricDto expected = db.measures().insertMetric(t -> t.setEnabled(true));
assertEquals(expected, underTest.selectByKey(dbSession, expected.getKey()));
}
@Test
public void select_by_key_disabled_metric() {
- MetricDto expected = db.measures().insertMetric(t -> t.setEnabled(false).setUserManaged(false));
-
- assertEquals(expected, underTest.selectByKey(dbSession, expected.getKey()));
- }
-
- @Test
- public void select_by_key_manual_metric() {
- MetricDto expected = db.measures().insertMetric(t -> t.setUserManaged(true));
+ MetricDto expected = db.measures().insertMetric(t -> t.setEnabled(false));
assertEquals(expected, underTest.selectByKey(dbSession, expected.getKey()));
}
.setDomain("Tests")
.setValueType("PERCENT")
.setQualitative(true)
- .setUserManaged(true)
+
.setWorstValue(0d)
.setBestValue(100d)
.setOptimizedBestValue(true)
assertThat(result.getValueType()).isEqualTo("PERCENT");
assertThat(result.getDirection()).isEqualTo(1);
assertThat(result.isQualitative()).isTrue();
- assertThat(result.isUserManaged()).isTrue();
assertThat(result.getWorstValue()).isEqualTo(0d);
assertThat(result.getBestValue()).isEqualTo(100d);
assertThat(result.isOptimizedBestValue()).isTrue();
.setDomain("Tests")
.setValueType("PERCENT")
.setQualitative(true)
- .setUserManaged(true)
+
.setWorstValue(0d)
.setBestValue(100d)
.setOptimizedBestValue(true)
.setDomain("Tests")
.setValueType("INT")
.setQualitative(true)
- .setUserManaged(true)
+
.setWorstValue(0d)
.setBestValue(100d)
.setOptimizedBestValue(true)
@Test
public void countEnabled() {
- underTest.insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(true));
- underTest.insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(true));
+ underTest.insert(dbSession, newMetricDto().setEnabled(true));
+ underTest.insert(dbSession, newMetricDto().setEnabled(true));
underTest.insert(dbSession, newMetricDto().setEnabled(false));
- int result = underTest.countEnabled(dbSession, true);
+ int result = underTest.countEnabled(dbSession);
assertThat(result).isEqualTo(2);
}
- @Test
- public void selectDomains() {
- underTest.insert(dbSession, newMetricDto().setDomain("first-domain").setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setDomain("second-domain").setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setDomain("second-domain").setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setDomain("third-domain").setEnabled(true));
-
- List<String> domains = underTest.selectEnabledDomains(dbSession);
-
- assertThat(domains).hasSize(3).containsOnly("first-domain", "second-domain", "third-domain");
- }
-
@Test
public void selectByKeys() {
underTest.insert(dbSession, newMetricDto().setKey("first-key"));
.extracting("key").containsOnly("first-key", "second-key", "third-key");
}
- @Test
- public void disableByUuids() {
- MetricDto metric1 = underTest.insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(true));
- MetricDto metric2 = underTest.insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(true));
-
- underTest.disableCustomByUuids(dbSession, Arrays.asList(metric1.getUuid(), metric2.getUuid()));
-
- List<MetricDto> result = underTest.selectByUuids(dbSession, newHashSet(metric1.getUuid(), metric2.getUuid()));
- assertThat(result).hasSize(2);
- assertThat(result).extracting("enabled").containsOnly(false);
- }
-
@Test
public void disableByKey() {
- underTest.insert(dbSession, newMetricDto().setKey("metric-key").setEnabled(true).setUserManaged(true));
+ underTest.insert(dbSession, newMetricDto().setKey("metric-key").setEnabled(true));
- boolean updated = underTest.disableCustomByKey(dbSession, "metric-key");
+ boolean updated = underTest.disableByKey(dbSession, "metric-key");
assertThat(updated).isTrue();
MetricDto result = underTest.selectByKey(dbSession, "metric-key");
assertThat(result.isEnabled()).isFalse();
// disable again -> zero rows are touched
- updated = underTest.disableCustomByKey(dbSession, "metric-key");
+ updated = underTest.disableByKey(dbSession, "metric-key");
assertThat(updated).isFalse();
}
}
@Test
- public void selectEnabled_with_paging_and_custom() {
- underTest.insert(dbSession, newMetricDto().setUserManaged(true).setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setUserManaged(true).setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setUserManaged(true).setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setUserManaged(false).setEnabled(true));
- underTest.insert(dbSession, newMetricDto().setUserManaged(true).setEnabled(false));
+ public void selectEnabled_with_paging() {
+ underTest.insert(dbSession, newMetricDto().setEnabled(true));
+ underTest.insert(dbSession, newMetricDto().setEnabled(true));
+ underTest.insert(dbSession, newMetricDto().setEnabled(true));
+ underTest.insert(dbSession, newMetricDto().setEnabled(false));
- List<MetricDto> result = underTest.selectEnabled(dbSession, true, 0, 100);
+ List<MetricDto> result = underTest.selectEnabled(dbSession, 0, 100);
assertThat(result).hasSize(3);
}
assertThat(result.getValueType()).isEqualTo(expected.getValueType());
assertThat(result.getDirection()).isEqualTo(expected.getDirection());
assertThat(result.isQualitative()).isEqualTo(expected.isQualitative());
- assertThat(result.isUserManaged()).isEqualTo(expected.isUserManaged());
assertThat(result.getWorstValue()).isCloseTo(expected.getWorstValue(), within(0.001d));
assertThat(result.getBestValue()).isCloseTo(expected.getBestValue(), within(0.001d));
assertThat(result.isOptimizedBestValue()).isEqualTo(expected.isOptimizedBestValue());
assertThat(result.isHidden()).isEqualTo(expected.isHidden());
assertThat(result.isEnabled()).isEqualTo(expected.isEnabled());
assertThat(result.getDecimalScale()).isEqualTo(expected.getDecimalScale());
- assertThat(result.isUserManaged()).isEqualTo(expected.isUserManaged());
}
}
.setDomain("Tests")
.setValueType("PERCENT")
.setQualitative(true)
- .setUserManaged(false)
.setWorstValue(0d)
.setBestValue(100d)
.setOptimizedBestValue(true)
assertThat(metricDto.getDomain()).isEqualTo("Tests");
assertThat(metricDto.getValueType()).isEqualTo("PERCENT");
assertThat(metricDto.isQualitative()).isTrue();
- assertThat(metricDto.isUserManaged()).isFalse();
assertThat(metricDto.getWorstValue()).isEqualTo(0d);
assertThat(metricDto.getBestValue()).isEqualTo(100d);
assertThat(metricDto.isOptimizedBestValue()).isTrue();
.setEnabled(true)
.setOptimizedBestValue(RandomUtils.nextBoolean())
.setQualitative(RandomUtils.nextBoolean())
- .setUserManaged(RandomUtils.nextBoolean())
.setWorstValue(RandomUtils.nextDouble());
}
}
metric.setQualitative(dto.isQualitative());
metric.setType(Metric.ValueType.valueOf(dto.getValueType()));
metric.setOptimizedBestValue(dto.isOptimizedBestValue());
- metric.setUserManaged(dto.isUserManaged());
metric.setWorstValue(dto.getWorstValue());
return metric;
}
dto.setQualitative(metric.getQualitative());
dto.setValueType(metric.getType().name());
dto.setOptimizedBestValue(Optional.ofNullable(metric.isOptimizedBestValue()).orElse(false));
- dto.setUserManaged(metric.getUserManaged());
dto.setWorstValue(metric.getWorstValue());
dto.setDeleteHistoricalData(metric.getDeleteHistoricalData());
dto.setDecimalScale(metric.getDecimalScale());
*/
package org.sonar.server.startup;
+import static com.google.common.collect.FluentIterable.concat;
+import static com.google.common.collect.Lists.newArrayList;
+
import com.google.common.annotations.VisibleForTesting;
import java.util.HashMap;
import java.util.List;
import org.sonar.db.metric.MetricDto;
import org.sonar.server.metric.MetricToDto;
-import static com.google.common.collect.FluentIterable.from;
-import static com.google.common.collect.Iterables.concat;
-import static com.google.common.collect.Lists.newArrayList;
-
public class RegisterMetrics implements Startable {
private static final Logger LOG = Loggers.get(RegisterMetrics.class);
private void save(DbSession session, Iterable<Metric> metrics) {
Map<String, MetricDto> basesByKey = new HashMap<>();
- for (MetricDto base : from(dbClient.metricDao().selectAll(session)).toList()) {
+ var allMetrics = dbClient.metricDao().selectAll(session);
+ for (MetricDto base : allMetrics) {
basesByKey.put(base.getKey(), base);
}
// new metric, never installed
dto.setUuid(uuidFactory.create());
dbClient.metricDao().insert(session, dto);
- } else if (!base.isUserManaged()) {
- // existing metric, update changes. Existing custom metrics are kept without applying changes.
+ } else {
dto.setUuid(base.getUuid());
dbClient.metricDao().update(session, dto);
}
}
for (MetricDto nonUpdatedBase : basesByKey.values()) {
- if (!nonUpdatedBase.isUserManaged() && dbClient.metricDao().disableCustomByKey(session, nonUpdatedBase.getKey())) {
+ if (dbClient.metricDao().disableByKey(session, nonUpdatedBase.getKey())) {
LOG.info("Disable metric {} [{}]", nonUpdatedBase.getShortName(), nonUpdatedBase.getKey());
}
}
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
public class RegisterMetricsTest {
}
/**
- * Update existing metrics, except if custom metric
+ * Update existing metrics
*/
@Test
- public void update_non_custom_metrics() {
+ public void update_metrics() {
dbTester.measures().insertMetric(t -> t.setKey("m1")
.setShortName("name")
.setValueType(Metric.ValueType.INT.name())
.setDomain("old domain")
.setShortName("old short name")
.setQualitative(false)
- .setUserManaged(false)
.setEnabled(true)
.setOptimizedBestValue(false)
.setDirection(1)
.setHidden(false));
- MetricDto customMetric = dbTester.measures().insertMetric(t -> t.setKey("custom")
- .setValueType(Metric.ValueType.FLOAT.name())
- .setDescription("old desc")
- .setShortName("Custom")
- .setQualitative(false)
- .setUserManaged(true)
- .setEnabled(true)
- .setOptimizedBestValue(false)
- .setDirection(0)
- .setHidden(false)
- .setDecimalScale(1));
RegisterMetrics register = new RegisterMetrics(dbClient, uuidFactory);
Metric m1 = new Metric.Builder("m1", "New name", Metric.ValueType.FLOAT)
.setDecimalScale(3)
.setHidden(true)
.create();
- Metric custom = new Metric.Builder("custom", "New custom", Metric.ValueType.FLOAT)
- .setDescription("New description of custom metric")
- .setUserManaged(true)
- .create();
- register.register(asList(m1, custom));
+ register.register(asList(m1));
Map<String, MetricDto> metricsByKey = selectAllMetrics();
- assertThat(metricsByKey).hasSize(2);
+ assertThat(metricsByKey).hasSize(1);
assertEquals(m1, metricsByKey.get("m1"));
- MetricDto actual = metricsByKey.get("custom");
- assertThat(actual.getKey()).isEqualTo(custom.getKey());
- assertThat(actual.getShortName()).isEqualTo(customMetric.getShortName());
- assertThat(actual.getValueType()).isEqualTo(customMetric.getValueType());
- assertThat(actual.getDescription()).isEqualTo(customMetric.getDescription());
- assertThat(actual.getDirection()).isEqualTo(customMetric.getDirection());
- assertThat(actual.isQualitative()).isEqualTo(customMetric.isQualitative());
- assertThat(actual.isUserManaged()).isEqualTo(customMetric.isUserManaged());
}
@Test
Random random = new Random();
int count = 1 + random.nextInt(10);
IntStream.range(0, count)
- .forEach(t -> dbTester.measures().insertMetric(m -> m.setEnabled(random.nextBoolean()).setUserManaged(false)));
+ .forEach(t -> dbTester.measures().insertMetric(m -> m.setEnabled(random.nextBoolean())));
RegisterMetrics register = new RegisterMetrics(dbClient, uuidFactory);
register.register(Collections.emptyList());
@Test
public void enable_disabled_metrics() {
- MetricDto enabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(true).setUserManaged(false));
- MetricDto disabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(false).setUserManaged(false));
+ MetricDto enabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(true));
+ MetricDto disabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(false));
RegisterMetrics register = new RegisterMetrics(dbClient, uuidFactory);
register.register(asList(builderOf(enabledMetric).create(), builderOf(disabledMetric).create()));
.containsOnly(true, true);
}
- @Test
- public void does_not_enable_disabled_custom_metrics() {
- MetricDto enabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(true).setUserManaged(true));
- MetricDto disabledMetric = dbTester.measures().insertMetric(t -> t.setEnabled(false).setUserManaged(true));
-
- RegisterMetrics register = new RegisterMetrics(dbClient, uuidFactory);
- register.register(asList(builderOf(enabledMetric).create(), builderOf(disabledMetric).create()));
-
- assertThat(selectAllMetrics().values())
- .extracting(MetricDto::getKey, MetricDto::isEnabled)
- .containsOnly(
- tuple(enabledMetric.getKey(), true),
- tuple(disabledMetric.getKey(), false));
- }
-
@Test
public void insert_core_metrics() {
RegisterMetrics register = new RegisterMetrics(dbClient, uuidFactory);
assertThat(actual.getDescription()).isEqualTo(expected.getDescription());
assertThat(actual.getDirection()).isEqualTo(expected.getDirection());
assertThat(actual.isQualitative()).isEqualTo(expected.getQualitative());
- assertThat(actual.isUserManaged()).isEqualTo(expected.getUserManaged());
}
private static Metric.Builder builderOf(MetricDto enabledMetric) {
.setQualitative(enabledMetric.isQualitative())
.setQualitative(enabledMetric.isQualitative())
.setDomain(enabledMetric.getDomain())
- .setUserManaged(enabledMetric.isUserManaged())
.setHidden(enabledMetric.isHidden());
}
}
}
metric.setQualitative(metricDto.isQualitative());
metric.setHidden(metricDto.isHidden());
- metric.setCustom(metricDto.isUserManaged());
ofNullable(metricDto.getDecimalScale()).ifPresent(metric::setDecimalScale);
ofNullable(metricDto.getBestValue()).ifPresent(bv -> metric.setBestValue(formatNumericalValue(bv, metricDto)));
ofNullable(metricDto.getWorstValue()).ifPresent(wv -> metric.setWorstValue(formatNumericalValue(wv, metricDto)));
.setPage(request.mandatoryParamAsInt(Param.PAGE),
request.mandatoryParamAsInt(Param.PAGE_SIZE));
try (DbSession dbSession = dbClient.openSession(false)) {
- List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, false, searchOptions.getOffset(), searchOptions.getLimit());
- int nbMetrics = dbClient.metricDao().countEnabled(dbSession, false);
+ List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, searchOptions.getOffset(), searchOptions.getLimit());
+ int nbMetrics = dbClient.metricDao().countEnabled(dbSession);
try (JsonWriter json = response.newJsonWriter()) {
json.beginObject();
- writeMetrics(json, metrics);
+ MetricJsonWriter.write(json, metrics);
searchOptions.writeJson(json, nbMetrics);
json.endObject();
}
}
}
-
- public static void writeMetrics(JsonWriter json, List<MetricDto> metrics) {
- MetricJsonWriter.write(json, metrics);
- }
}
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": false,
- "hidden": false,
- "custom": false
+ "hidden": false
},
{
"key": "ncloc",
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": false,
- "hidden": false,
- "custom": false
+ "hidden": false
},
{
"key": "new_violations",
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": true,
- "hidden": false,
- "custom": false
+ "hidden": false
}
],
"period": {
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": false,
- "hidden": false,
- "custom": false
+ "hidden": false
},
{
"key": "ncloc",
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": false,
- "hidden": false,
- "custom": false
+ "hidden": false
},
{
"key": "new_violations",
"type": "INT",
"higherValuesAreBetter": false,
"qualitative": true,
- "hidden": false,
- "custom": false,
+ "hidden": false
"bestValue": "0"
}
],
.setValueType("INT")
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.measures().insertLiveMeasure(file, complexity,
m -> m.setValue(12.0d)
.setVariation(2.0d)
.setValueType("INT")
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.measures().insertLiveMeasure(file, ncloc,
m -> m.setValue(114.0d)
.setVariation(3.0d)
.setValueType("INT")
.setDirection(-1)
.setQualitative(true)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.measures().insertLiveMeasure(file, newViolations,
m -> m.setVariation(25.0d)
.setValue(null)
return MetricTesting.newMetricDto()
.setWorstValue(null)
.setBestValue(null)
- .setOptimizedBestValue(false)
- .setUserManaged(false);
+ .setOptimizedBestValue(false);
}
private MetricDto insertNewViolationsMetric() {
.setDirection(-1)
.setQualitative(true)
.setHidden(false)
- .setUserManaged(false)
.setOptimizedBestValue(true)
.setBestValue(0.0d));
db.commit();
.setValueType(INT.name())
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
.setValueType(INT.name())
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
.setValueType(FLOAT.name())
.setDirection(1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
return newMetricDto()
.setWorstValue(null)
.setOptimizedBestValue(false)
- .setBestValue(null)
- .setUserManaged(false);
+ .setBestValue(null);
}
private MetricDto insertNclocMetric() {
.setValueType("INT")
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
.setValueType("INT")
.setDirection(-1)
.setQualitative(false)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
.setValueType("INT")
.setDirection(-1)
.setQualitative(true)
- .setHidden(false)
- .setUserManaged(false));
+ .setHidden(false));
db.commit();
return metric;
}
.setDirection(0)
.setQualitative(true)
.setHidden(false)
- .setEnabled(true)
- .setUserManaged(false));
+ .setEnabled(true));
db.measures().insertMetric(metricDto -> metricDto
.setKey("custom-key-2")
.setShortName("custom-name-2")
.setDirection(0)
.setQualitative(true)
.setHidden(false)
- .setEnabled(true)
- .setUserManaged(false));
+ .setEnabled(true));
db.measures().insertMetric(metricDto -> metricDto
.setKey("custom-key-3")
.setShortName("custom-name-3")
.setDirection(0)
.setQualitative(true)
.setHidden(false)
- .setEnabled(true)
- .setUserManaged(false));
+ .setEnabled(true));
TestResponse result = ws.newRequest().execute();
private void insertMetrics(String... ids) {
for (String id : ids) {
- db.measures().insertMetric(metricDto -> metricDto.setUuid(id).setShortName("name-" + id).setEnabled(true).setUserManaged(false));
+ db.measures().insertMetric(metricDto -> metricDto.setUuid(id).setShortName("name-" + id).setEnabled(true));
}
dbSession.commit();
}
db.qualityGates().setDefaultQualityGate(qualityGate);
MetricDto metric = db.measures().insertMetric();
db.qualityGates().addCondition(qualityGate, metric);
- db.getDbClient().metricDao().disableCustomByKey(db.getSession(), metric.getKey());
+ db.getDbClient().metricDao().disableByKey(db.getSession(), metric.getKey());
db.commit();
expectedException.expect(IllegalStateException.class);
/**
* @return whether the metric is a managed by the users ("manual metric")
*/
+ @Deprecated
public Boolean getUserManaged() {
return userManaged;
}
* @return the builder
* @since 2.10
*/
+ @Deprecated
public Builder setUserManaged(boolean b) {
this.userManaged = b;
return this;
public class DefaultMetricsRepositoryLoader implements MetricsRepositoryLoader {
- private static final String METRICS_SEARCH_URL = "/api/metrics/search?f=name,description,direction,qualitative,custom&ps=500&p=";
+ private static final String METRICS_SEARCH_URL = "/api/metrics/search?ps=500&p=";
private DefaultScannerWsClient wsClient;
public DefaultMetricsRepositoryLoader(DefaultScannerWsClient wsClient) {
.create()
.setDirection(metric.getDirection())
.setQualitative(metric.isQualitative())
- .setUserManaged(metric.isCustom())
+ .setUserManaged(false)
.setDescription(metric.getDescription())
.setUuid(metric.getUuid()));
}
private String description;
private int direction;
private boolean qualitative;
- private boolean custom;
public String getUuid() {
return uuid;
return qualitative;
}
- public boolean isCustom() {
- return custom;
- }
}
private static class WsMetricsResponse {
import java.io.StringReader;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.scanner.WsTestUtil;
import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
public class DefaultMetricsRepositoryLoaderTest {
- private static final String WS_URL = "/api/metrics/search?f=name,description,direction,qualitative,custom&ps=500&p=";
+ private static final String WS_URL = "/api/metrics/search?ps=500&p=";
private DefaultScannerWsClient wsClient;
private DefaultMetricsRepositoryLoader metricsRepositoryLoader;
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
@Before
public void setUp() throws IOException {
wsClient = mock(DefaultScannerWsClient.class);
Reader reader = mock(Reader.class);
when(reader.read(any(char[].class), anyInt(), anyInt())).thenThrow(new IOException());
WsTestUtil.mockReader(wsClient, reader);
- exception.expect(IllegalStateException.class);
- metricsRepositoryLoader.load();
+ assertThatThrownBy(() -> metricsRepositoryLoader.load())
+ .isInstanceOf(IllegalStateException.class);
}
@Test
when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1);
doThrow(new IOException()).when(reader).close();
WsTestUtil.mockReader(wsClient, reader);
- exception.expect(IllegalStateException.class);
- metricsRepositoryLoader.load();
+ assertThatThrownBy(() -> metricsRepositoryLoader.load())
+ .isInstanceOf(IllegalStateException.class);
}
}