aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-07-23 17:58:02 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-07-24 09:22:47 +0200
commit2659bc296bc643229ff65163aba71a4642362194 (patch)
treed1b24a64eae8b5b17849ad76c4c00daab60e3260 /sonar-db
parent5792a69db7fa49a2f865a6018e25555254a20674 (diff)
downloadsonarqube-2659bc296bc643229ff65163aba71a4642362194.tar.gz
sonarqube-2659bc296bc643229ff65163aba71a4642362194.zip
Improve MetricDao code coverage
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java12
-rw-r--r--sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java169
-rw-r--r--sonar-db/src/test/java/org/sonar/db/metric/MetricTesting.java52
3 files changed, 216 insertions, 17 deletions
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
index 9780c00d382..1037ae9d331 100644
--- a/sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java
@@ -82,8 +82,10 @@ public class MetricDao implements Dao {
return mapper(session).countEnabled(isCustom);
}
- public void insert(DbSession session, MetricDto dto) {
+ public MetricDto insert(DbSession session, MetricDto dto) {
mapper(session).insert(dto);
+
+ return dto;
}
public void insert(DbSession session, Collection<MetricDto> items) {
@@ -96,7 +98,7 @@ public class MetricDao implements Dao {
insert(session, Lists.asList(item, others));
}
- public List<String> selectDomains(DbSession session) {
+ public List<String> selectEnabledDomains(DbSession session) {
return newArrayList(Collections2.filter(mapper(session).selectDomains(), new NotEmptyPredicate()));
}
@@ -126,7 +128,7 @@ public class MetricDao implements Dao {
return session.getMapper(MetricMapper.class);
}
- public void disableByIds(final DbSession session, List<Integer> ids) {
+ public void disableCustomByIds(final DbSession session, List<Integer> ids) {
DatabaseUtils.executeLargeInputsWithoutOutput(ids, new Function<List<Integer>, Void>() {
@Override
public Void apply(@Nonnull List<Integer> input) {
@@ -136,7 +138,7 @@ public class MetricDao implements Dao {
});
}
- public void disableByKey(final DbSession session, String key) {
+ public void disableCustomByKey(final DbSession session, String key) {
mapper(session).disableByKey(key);
}
@@ -149,7 +151,7 @@ public class MetricDao implements Dao {
return mapper(session).selectById(id);
}
- public MetricDto selectOrFailById(DbSession session, int id) {
+ public MetricDto selectOrFailById(DbSession session, long id) {
MetricDto metric = mapper(session).selectById(id);
if (metric == null) {
throw new RowNotFoundException(String.format("Metric id '%d' not found", id));
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
index 0917d79d021..ca6767e6702 100644
--- a/sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java
@@ -20,34 +20,41 @@
package org.sonar.db.metric;
+import java.util.Arrays;
+import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
+import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.test.DbTests;
+import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.metric.MetricTesting.newMetricDto;
@Category(DbTests.class)
public class MetricDaoTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
DbSession session;
- MetricDao dao;
+ MetricDao underTest;
@Before
public void createDao() {
dbTester.truncateTables();
session = dbTester.myBatis().openSession(false);
- dao = new MetricDao();
+ underTest = new MetricDao();
}
@After
@@ -56,10 +63,10 @@ public class MetricDaoTest {
}
@Test
- public void get_by_key() {
+ public void select_by_key() {
dbTester.prepareDbUnit(getClass(), "shared.xml");
- MetricDto result = dao.selectByKey(session, "coverage");
+ MetricDto result = underTest.selectByKey(session, "coverage");
assertThat(result.getId()).isEqualTo(2);
assertThat(result.getKey()).isEqualTo("coverage");
assertThat(result.getShortName()).isEqualTo("Coverage");
@@ -77,21 +84,21 @@ public class MetricDaoTest {
assertThat(result.isEnabled()).isTrue();
// Disabled metrics are returned
- result = dao.selectByKey(session, "disabled");
+ result = underTest.selectByKey(session, "disabled");
assertThat(result.getId()).isEqualTo(3);
assertThat(result.isEnabled()).isFalse();
}
@Test(expected = RowNotFoundException.class)
- public void get_nullable_by_key() {
- dao.selectOrFailByKey(session, "unknown");
+ public void select_or_fail_by_key() {
+ underTest.selectOrFailByKey(session, "unknown");
}
@Test
public void get_manual_metric() {
dbTester.prepareDbUnit(getClass(), "manual_metric.xml");
- MetricDto result = dao.selectByKey(session, "manual");
+ MetricDto result = underTest.selectByKey(session, "manual");
assertThat(result.getId()).isEqualTo(1);
assertThat(result.getKey()).isEqualTo("manual");
assertThat(result.getShortName()).isEqualTo("Manual metric");
@@ -113,12 +120,12 @@ public class MetricDaoTest {
public void find_all_enabled() {
dbTester.prepareDbUnit(getClass(), "shared.xml");
- assertThat(dao.selectEnabled(session)).hasSize(2);
+ assertThat(underTest.selectEnabled(session)).hasSize(2);
}
@Test
public void insert() {
- dao.insert(session, new MetricDto()
+ underTest.insert(session, new MetricDto()
.setKey("coverage")
.setShortName("Coverage")
.setDescription("Coverage by unit tests")
@@ -134,7 +141,7 @@ public class MetricDaoTest {
.setDeleteHistoricalData(true)
.setEnabled(true));
- MetricDto result = dao.selectByKey(session, "coverage");
+ MetricDto result = underTest.selectByKey(session, "coverage");
assertThat(result.getId()).isNotNull();
assertThat(result.getKey()).isEqualTo("coverage");
assertThat(result.getShortName()).isEqualTo("Coverage");
@@ -154,7 +161,7 @@ public class MetricDaoTest {
@Test
public void insert_metrics() {
- dao.insert(session, new MetricDto()
+ underTest.insert(session, new MetricDto()
.setKey("coverage")
.setShortName("Coverage")
.setDescription("Coverage by unit tests")
@@ -188,4 +195,142 @@ public class MetricDaoTest {
assertThat(dbTester.countRowsOfTable("metrics")).isEqualTo(2);
}
+
+ @Test
+ public void selectById() {
+ MetricDto metric = underTest.insert(session, newMetricDto());
+
+ MetricDto result = underTest.selectById(session, metric.getId());
+
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ public void selectOrFailById() {
+ MetricDto metric = underTest.insert(session, newMetricDto());
+
+ MetricDto result = underTest.selectOrFailById(session, metric.getId());
+
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ public void fail_when_no_id_selectOrFailById() {
+ thrown.expect(RowNotFoundException.class);
+
+ underTest.selectOrFailById(session, 42L);
+ }
+
+ @Test
+ public void selectByIds() {
+ MetricDto metric1 = underTest.insert(session, newMetricDto());
+ MetricDto metric2 = underTest.insert(session, newMetricDto());
+
+ List<MetricDto> result = underTest.selectByIds(session, newHashSet(metric1.getId(), metric2.getId()));
+
+ assertThat(result).hasSize(2);
+ }
+
+ @Test
+ public void update() {
+ MetricDto metric = underTest.insert(session, newMetricDto().setKey("first-key"));
+
+ underTest.update(session, metric.setKey("second-key"));
+
+ MetricDto result = underTest.selectByKey(session, "second-key");
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ public void countEnabled() {
+ underTest.insert(session, newMetricDto().setEnabled(true).setUserManaged(true));
+ underTest.insert(session, newMetricDto().setEnabled(true).setUserManaged(true));
+ underTest.insert(session, newMetricDto().setEnabled(false));
+
+ int result = underTest.countEnabled(session, true);
+
+ assertThat(result).isEqualTo(2);
+ }
+
+ @Test
+ public void selectDomains() {
+ underTest.insert(session, newMetricDto().setDomain("first-domain").setEnabled(true));
+ underTest.insert(session, newMetricDto().setDomain("second-domain").setEnabled(true));
+ underTest.insert(session, newMetricDto().setDomain("second-domain").setEnabled(true));
+ underTest.insert(session, newMetricDto().setDomain("third-domain").setEnabled(true));
+
+ List<String> domains = underTest.selectEnabledDomains(session);
+
+ assertThat(domains).hasSize(3).containsOnly("first-domain", "second-domain", "third-domain");
+ }
+
+ @Test
+ public void selectByKeys() {
+ underTest.insert(session, newMetricDto().setKey("first-key"));
+ underTest.insert(session, newMetricDto().setKey("second-key"));
+ underTest.insert(session, newMetricDto().setKey("third-key"));
+
+ List<MetricDto> result = underTest.selectByKeys(session, Arrays.asList("first-key", "second-key", "third-key"));
+
+ assertThat(result).hasSize(3)
+ .extracting("key").containsOnly("first-key", "second-key", "third-key");
+ }
+
+ @Test
+ public void disableByIds() {
+ MetricDto metric1 = underTest.insert(session, newMetricDto().setEnabled(true).setUserManaged(true));
+ MetricDto metric2 = underTest.insert(session, newMetricDto().setEnabled(true).setUserManaged(true));
+
+ underTest.disableCustomByIds(session, Arrays.asList(metric1.getId(), metric2.getId()));
+
+ List<MetricDto> result = underTest.selectByIds(session, newHashSet(metric1.getId(), metric2.getId()));
+ assertThat(result).hasSize(2);
+ assertThat(result).extracting("enabled").containsOnly(false);
+ }
+
+ @Test
+ public void disableByKey() {
+ underTest.insert(session, newMetricDto().setKey("metric-key").setEnabled(true).setUserManaged(true));
+
+ underTest.disableCustomByKey(session, "metric-key");
+
+ MetricDto result = underTest.selectByKey(session, "metric-key");
+ assertThat(result.isEnabled()).isFalse();
+ }
+
+ @Test
+ public void selectOrFailByKey() {
+ underTest.insert(session, newMetricDto().setKey("metric-key"));
+
+ MetricDto result = underTest.selectOrFailByKey(session, "metric-key");
+
+ assertThat(result).isNotNull();
+ assertThat(result.getKey()).isEqualTo("metric-key");
+ }
+
+ @Test
+ public void selectEnabled_with_paging_and_custom() {
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(true));
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(true));
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(true));
+ underTest.insert(session, newMetricDto().setUserManaged(false).setEnabled(true));
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(false));
+
+ List<MetricDto> result = underTest.selectEnabled(session, true, 0, 100);
+
+ assertThat(result).hasSize(3);
+ }
+
+ @Test
+ public void selectAvailableByComponentUuid() {
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(true).setKey("metric-key"));
+ underTest.insert(session, newMetricDto().setUserManaged(false).setEnabled(true).setKey("another-metric-key"));
+ underTest.insert(session, newMetricDto().setUserManaged(true).setEnabled(false).setKey("third-metric-key"));
+
+ List<MetricDto> result = underTest.selectAvailableCustomMetricsByComponentUuid(session, "project-uuid");
+
+ assertThat(result).hasSize(1)
+ .extracting("key").containsOnly("metric-key");
+
+ }
}
diff --git a/sonar-db/src/test/java/org/sonar/db/metric/MetricTesting.java b/sonar-db/src/test/java/org/sonar/db/metric/MetricTesting.java
new file mode 100644
index 00000000000..502f23ea5b7
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/metric/MetricTesting.java
@@ -0,0 +1,52 @@
+/*
+ * 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.apache.commons.lang.RandomStringUtils;
+import org.apache.commons.lang.math.RandomUtils;
+import org.sonar.api.measures.Metric;
+
+public class MetricTesting {
+ private MetricTesting() {
+ // static stuff only
+ }
+
+ public static MetricDto newMetricDto() {
+ Metric.ValueType[] metricTypes = Metric.ValueType.values();
+ return new MetricDto()
+ .setId(RandomUtils.nextInt())
+ .setKey(RandomStringUtils.randomAlphanumeric(64))
+ .setShortName(RandomStringUtils.randomAlphanumeric(64))
+ .setValueType(metricTypes[RandomUtils.nextInt(metricTypes.length - 1)].name())
+ .setDomain(RandomStringUtils.randomAlphanumeric(64))
+ .setDescription(RandomStringUtils.randomAlphanumeric(250))
+ .setBestValue(RandomUtils.nextDouble())
+ .setDeleteHistoricalData(RandomUtils.nextBoolean())
+ .setDirection(RandomUtils.nextInt())
+ .setHidden(RandomUtils.nextBoolean())
+ .setEnabled(RandomUtils.nextBoolean())
+ .setOptimizedBestValue(RandomUtils.nextBoolean())
+ .setQualitative(RandomUtils.nextBoolean())
+ .setUserManaged(RandomUtils.nextBoolean())
+ .setWorstValue(RandomUtils.nextDouble());
+ }
+
+}