diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-22 15:33:34 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-22 16:17:41 +0200 |
commit | a41640c8e68447aa849b4449332c709ab2149bc3 (patch) | |
tree | 242c4a7417670b773ae68208bf2c76deaf5e244d /sonar-db | |
parent | c06ca182fe2eced095c12dd2de0bc02f0627122d (diff) | |
download | sonarqube-a41640c8e68447aa849b4449332c709ab2149bc3.tar.gz sonarqube-a41640c8e68447aa849b4449332c709ab2149bc3.zip |
Move MetricDao to sonar-db
Diffstat (limited to 'sonar-db')
6 files changed, 377 insertions, 3 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index 4ef128b94ec..fba496c2f90 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -47,6 +47,7 @@ import org.sonar.db.loadedtemplate.LoadedTemplateDao; import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.MeasureFilterDao; import org.sonar.db.measure.custom.CustomMeasureDao; +import org.sonar.db.metric.MetricDao; import org.sonar.db.notification.NotificationQueueDao; import org.sonar.db.permission.PermissionDao; import org.sonar.db.permission.PermissionTemplateDao; @@ -109,8 +110,9 @@ public class DbClient { private final DuplicationDao duplicationDao; private final NotificationQueueDao notificationQueueDao; private final CustomMeasureDao customMeasureDao; + private final MetricDao metricDao; - public DbClient(Database database, MyBatis myBatis, Dao ... daos) { + public DbClient(Database database, MyBatis myBatis, Dao... daos) { this.database = database; this.myBatis = myBatis; @@ -159,6 +161,7 @@ public class DbClient { duplicationDao = getDao(map, DuplicationDao.class); notificationQueueDao = getDao(map, NotificationQueueDao.class); customMeasureDao = getDao(map, CustomMeasureDao.class); + metricDao = getDao(map, MetricDao.class); doOnLoad(map); } @@ -343,6 +346,10 @@ public class DbClient { return customMeasureDao; } + public MetricDao metricDao() { + return metricDao; + } + protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } diff --git a/sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java b/sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java new file mode 100644 index 00000000000..453096efb85 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java @@ -0,0 +1,157 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.metric; + +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import java.util.ArrayList; +import java.util.Collection; +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.DatabaseUtils; +import org.sonar.db.DbSession; + +import static com.google.common.collect.Lists.newArrayList; + +public class MetricDao implements Dao { + + @CheckForNull + public MetricDto selectNullableByKey(DbSession session, String key) { + return mapper(session).selectByKey(key); + } + + public List<MetricDto> selectNullableByKeys(final DbSession session, List<String> keys) { + return DatabaseUtils.executeLargeInputs(keys, new Function<List<String>, List<MetricDto>>() { + @Override + public List<MetricDto> apply(@Nonnull List<String> input) { + return mapper(session).selectByKeys(input); + } + }); + } + + public MetricDto selectByKey(DbSession session, String key) { + MetricDto metric = selectNullableByKey(session, key); + if (metric == null) { + throw new IllegalStateException(String.format("Metric key '%s' not found", key)); + } + return metric; + } + + public List<MetricDto> selectEnabled(DbSession session) { + return mapper(session).selectAllEnabled(); + } + + public List<MetricDto> selectEnabled(DbSession session, @Nullable Boolean isCustom, int offset, int limit) { + Map<String, Object> properties = Maps.newHashMapWithExpectedSize(1); + if (isCustom != null) { + properties.put("isCustom", isCustom); + } + + return mapper(session).selectAllEnabled(properties, new RowBounds(offset, limit)); + } + + public int countEnabled(DbSession session, @Nullable Boolean isCustom) { + return mapper(session).countEnabled(isCustom); + } + + public void insert(DbSession session, MetricDto dto) { + mapper(session).insert(dto); + } + + public void insert(DbSession session, Collection<MetricDto> items) { + for (MetricDto item : items) { + insert(session, item); + } + } + + public void insert(DbSession session, MetricDto item, MetricDto... others) { + insert(session, Lists.asList(item, others)); + } + + public List<String> selectDomains(DbSession session) { + return newArrayList(Collections2.filter(mapper(session).selectDomains(), new NotEmptyPredicate())); + } + + public List<MetricDto> selectAvailableCustomMetricsByComponentUuid(DbSession session, String projectUuid) { + return mapper(session).selectAvailableCustomMetricsByComponentUuid(projectUuid); + } + + public List<MetricDto> selectByIds(final DbSession session, Set<Integer> idsSet) { + List<Integer> ids = new ArrayList<>(idsSet); + return DatabaseUtils.executeLargeInputs(ids, new Function<List<Integer>, List<MetricDto>>() { + @Override + public List<MetricDto> apply(@Nonnull List<Integer> ids) { + return mapper(session).selectByIds(ids); + } + }); + } + + private static class NotEmptyPredicate implements Predicate<String> { + + @Override + public boolean apply(@Nonnull String input) { + return !input.isEmpty(); + } + } + + private MetricMapper mapper(DbSession session) { + return session.getMapper(MetricMapper.class); + } + + public void disableByIds(final DbSession session, List<Integer> ids) { + DatabaseUtils.executeLargeInputsWithoutOutput(ids, new Function<List<Integer>, Void>() { + @Override + public Void apply(@Nonnull List<Integer> input) { + mapper(session).disableByIds(input); + return null; + } + }); + } + + public void disableByKey(final DbSession session, String key) { + mapper(session).disableByKey(key); + } + + public void update(DbSession session, MetricDto metric) { + mapper(session).update(metric); + } + + public MetricDto selectNullableById(DbSession session, long id) { + return mapper(session).selectById(id); + } + + public MetricDto selectById(DbSession session, int id) { + MetricDto metric = mapper(session).selectById(id); + if (metric == null) { + throw new IllegalStateException(String.format("Metric id '%d' not found", id)); + } + return metric; + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/metric/MetricMapper.java b/sonar-db/src/main/java/org/sonar/db/metric/MetricMapper.java index 68ff2ccf0b4..fb5bafdce52 100644 --- a/sonar-db/src/main/java/org/sonar/db/metric/MetricMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/metric/MetricMapper.java @@ -53,6 +53,4 @@ public interface MetricMapper { void update(MetricDto metric); List<MetricDto> selectAvailableCustomMetricsByComponentUuid(String projectUuid); - - List<MetricDto> selectAvailableCustomMetricsByComponentKey(String projectKey); } diff --git a/sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java b/sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java new file mode 100644 index 00000000000..162cb647e50 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java @@ -0,0 +1,190 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.metric; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.test.DbTests; + +import static org.assertj.core.api.Assertions.assertThat; + +@Category(DbTests.class) +public class MetricDaoTest { + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + DbSession session; + + MetricDao dao; + + @Before + public void createDao() { + dbTester.truncateTables(); + session = dbTester.myBatis().openSession(false); + dao = new MetricDao(); + } + + @After + public void tearDown() { + session.close(); + } + + @Test + public void get_by_key() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + MetricDto result = dao.selectNullableByKey(session, "coverage"); + assertThat(result.getId()).isEqualTo(2); + assertThat(result.getKey()).isEqualTo("coverage"); + assertThat(result.getShortName()).isEqualTo("Coverage"); + assertThat(result.getDescription()).isEqualTo("Coverage by unit tests"); + assertThat(result.getDomain()).isEqualTo("Tests"); + assertThat(result.getValueType()).isEqualTo("PERCENT"); + assertThat(result.getDirection()).isEqualTo(1); + assertThat(result.isQualitative()).isTrue(); + assertThat(result.isUserManaged()).isFalse(); + assertThat(result.getWorstValue()).isEqualTo(0d); + assertThat(result.getBestValue()).isEqualTo(100d); + assertThat(result.isOptimizedBestValue()).isFalse(); + assertThat(result.isDeleteHistoricalData()).isFalse(); + assertThat(result.isHidden()).isFalse(); + assertThat(result.isEnabled()).isTrue(); + + // Disabled metrics are returned + result = dao.selectNullableByKey(session, "disabled"); + assertThat(result.getId()).isEqualTo(3); + assertThat(result.isEnabled()).isFalse(); + } + + @Test(expected = IllegalStateException.class) + public void get_nullable_by_key() { + dao.selectByKey(session, "unknown"); + } + + @Test + public void get_manual_metric() { + dbTester.prepareDbUnit(getClass(), "manual_metric.xml"); + + MetricDto result = dao.selectNullableByKey(session, "manual"); + assertThat(result.getId()).isEqualTo(1); + assertThat(result.getKey()).isEqualTo("manual"); + assertThat(result.getShortName()).isEqualTo("Manual metric"); + assertThat(result.getDescription()).isEqualTo("Manual metric"); + assertThat(result.getDomain()).isNullOrEmpty(); + assertThat(result.getValueType()).isEqualTo("INT"); + assertThat(result.getDirection()).isEqualTo(0); + assertThat(result.isQualitative()).isFalse(); + assertThat(result.isUserManaged()).isTrue(); + assertThat(result.getWorstValue()).isNull(); + assertThat(result.getBestValue()).isNull(); + assertThat(result.isOptimizedBestValue()).isFalse(); + assertThat(result.isDeleteHistoricalData()).isFalse(); + assertThat(result.isHidden()).isFalse(); + assertThat(result.isEnabled()).isTrue(); + } + + @Test + public void find_all_enabled() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(dao.selectEnabled(session)).hasSize(2); + } + + @Test + public void insert() { + dao.insert(session, new MetricDto() + .setKey("coverage") + .setShortName("Coverage") + .setDescription("Coverage by unit tests") + .setDomain("Tests") + .setValueType("PERCENT") + .setQualitative(true) + .setUserManaged(true) + .setWorstValue(0d) + .setBestValue(100d) + .setOptimizedBestValue(true) + .setDirection(1) + .setHidden(true) + .setDeleteHistoricalData(true) + .setEnabled(true)); + + MetricDto result = dao.selectNullableByKey(session, "coverage"); + assertThat(result.getId()).isNotNull(); + assertThat(result.getKey()).isEqualTo("coverage"); + assertThat(result.getShortName()).isEqualTo("Coverage"); + assertThat(result.getDescription()).isEqualTo("Coverage by unit tests"); + assertThat(result.getDomain()).isEqualTo("Tests"); + 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(); + assertThat(result.isDeleteHistoricalData()).isTrue(); + assertThat(result.isHidden()).isTrue(); + assertThat(result.isEnabled()).isTrue(); + } + + @Test + public void insert_metrics() { + dao.insert(session, new MetricDto() + .setKey("coverage") + .setShortName("Coverage") + .setDescription("Coverage by unit tests") + .setDomain("Tests") + .setValueType("PERCENT") + .setQualitative(true) + .setUserManaged(true) + .setWorstValue(0d) + .setBestValue(100d) + .setOptimizedBestValue(true) + .setDirection(1) + .setHidden(true) + .setDeleteHistoricalData(true) + .setEnabled(true), + new MetricDto() + .setKey("ncloc") + .setShortName("ncloc") + .setDescription("ncloc") + .setDomain("Tests") + .setValueType("INT") + .setQualitative(true) + .setUserManaged(true) + .setWorstValue(0d) + .setBestValue(100d) + .setOptimizedBestValue(true) + .setDirection(1) + .setHidden(true) + .setDeleteHistoricalData(true) + .setEnabled(true)); + session.commit(); + + assertThat(dbTester.countRowsOfTable("metrics")).isEqualTo(2); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/manual_metric.xml b/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/manual_metric.xml new file mode 100644 index 00000000000..325a5a71661 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/manual_metric.xml @@ -0,0 +1,7 @@ +<dataset> + + <metrics id="1" name="manual" val_type="INT" description="Manual metric" domain="" short_name="Manual metric" + qualitative="[false]" enabled="[true]" worst_value="[null]" optimized_best_value="[false]" best_value="[null]" direction="0" hidden="[false]" + delete_historical_data="[false]" user_managed="[true]"/> + +</dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/shared.xml b/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/shared.xml new file mode 100644 index 00000000000..dda5d3bbb88 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/metric/MetricDaoTest/shared.xml @@ -0,0 +1,15 @@ +<dataset> + + <metrics id="1" name="ncloc" val_type="INT" description="Non Commenting Lines of Code" domain="Size" short_name="Lines of code" + qualitative="[false]" enabled="[true]" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="-1" hidden="[false]" + delete_historical_data="[false]" user_managed="[false]"/> + + <metrics id="2" name="coverage" val_type="PERCENT" description="Coverage by unit tests" domain="Tests" short_name="Coverage" + qualitative="[true]" enabled="[true]" worst_value="0" optimized_best_value="[false]" best_value="100" direction="1" hidden="[false]" + delete_historical_data="[false]" user_managed="[false]"/> + + <metrics id="3" name="disabled" val_type="INT" description="[null]" domain="[null]" short_name="disabled" + qualitative="[false]" enabled="[false]" worst_value="0" optimized_best_value="[true]" best_value="100" direction="1" hidden="[false]" + delete_historical_data="[false]" user_managed="[false]"/> + +</dataset> |