diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-15 17:44:56 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-16 09:04:58 +0200 |
commit | cd9f861fb5e397bd0b6da425f3b41e60248efac4 (patch) | |
tree | 5d75868afa85a3a3e58118d5db3450ea9e5ed231 /sonar-db | |
parent | d6ca372cb5ef7410041313c622d1ac7fe94b59d1 (diff) | |
download | sonarqube-cd9f861fb5e397bd0b6da425f3b41e60248efac4.tar.gz sonarqube-cd9f861fb5e397bd0b6da425f3b41e60248efac4.zip |
Move custom measures in sonar-db
Diffstat (limited to 'sonar-db')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/DbClient.java | 9 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/MyBatis.java | 4 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java | 93 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDto.java (renamed from sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureDto.java) | 2 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureMapper.java (renamed from sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureMapper.java) | 2 | ||||
-rw-r--r-- | sonar-db/src/main/resources/org/sonar/db/measure/custom/CustomMeasureMapper.xml (renamed from sonar-db/src/main/resources/org/sonar/db/measure/CustomMeasureMapper.xml) | 2 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java | 159 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureTesting.java | 43 |
8 files changed, 308 insertions, 6 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 5c63a4ab3a8..4ef128b94ec 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -46,6 +46,7 @@ import org.sonar.db.issue.IssueFilterFavouriteDao; 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.notification.NotificationQueueDao; import org.sonar.db.permission.PermissionDao; import org.sonar.db.permission.PermissionTemplateDao; @@ -107,8 +108,9 @@ public class DbClient { private final ProjectQgateAssociationDao projectQgateAssociationDao; private final DuplicationDao duplicationDao; private final NotificationQueueDao notificationQueueDao; + private final CustomMeasureDao customMeasureDao; - public DbClient(Database database, MyBatis myBatis, Dao[] daos) { + public DbClient(Database database, MyBatis myBatis, Dao ... daos) { this.database = database; this.myBatis = myBatis; @@ -156,6 +158,7 @@ public class DbClient { projectQgateAssociationDao = getDao(map, ProjectQgateAssociationDao.class); duplicationDao = getDao(map, DuplicationDao.class); notificationQueueDao = getDao(map, NotificationQueueDao.class); + customMeasureDao = getDao(map, CustomMeasureDao.class); doOnLoad(map); } @@ -336,6 +339,10 @@ public class DbClient { return notificationQueueDao; } + public CustomMeasureDao customMeasureDao() { + return customMeasureDao; + } + 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/MyBatis.java b/sonar-db/src/main/java/org/sonar/db/MyBatis.java index 8bf39f3dedf..e1a6fd9dfd9 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -87,8 +87,8 @@ import org.sonar.db.issue.IssueFilterMapper; import org.sonar.db.issue.IssueMapper; import org.sonar.db.loadedtemplate.LoadedTemplateDto; import org.sonar.db.loadedtemplate.LoadedTemplateMapper; -import org.sonar.db.measure.CustomMeasureDto; -import org.sonar.db.measure.CustomMeasureMapper; +import org.sonar.db.measure.custom.CustomMeasureDto; +import org.sonar.db.measure.custom.CustomMeasureMapper; import org.sonar.db.measure.MeasureDto; import org.sonar.db.measure.MeasureFilterDto; import org.sonar.db.measure.MeasureFilterMapper; diff --git a/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java new file mode 100644 index 00000000000..28af785ebcd --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java @@ -0,0 +1,93 @@ +/* + * 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.measure.custom; + +import com.google.common.base.Function; +import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import org.apache.ibatis.session.RowBounds; +import org.sonar.api.server.ServerSide; +import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; +import org.sonar.db.DbSession; + +@ServerSide +public class CustomMeasureDao implements Dao { + public void insert(DbSession session, CustomMeasureDto customMeasureDto) { + mapper(session).insert(customMeasureDto); + } + + public void update(DbSession session, CustomMeasureDto customMeasure) { + mapper(session).update(customMeasure); + } + + public void delete(DbSession session, long id) { + mapper(session).delete(id); + } + + public void deleteByMetricIds(final DbSession session, final List<Integer> metricIds) { + DatabaseUtils.executeLargeInputsWithoutOutput(metricIds, new Function<List<Integer>, Void>() { + @Override + public Void apply(@Nonnull List<Integer> input) { + mapper(session).deleteByMetricIds(metricIds); + return null; + } + }); + } + + @CheckForNull + public CustomMeasureDto selectNullableById(DbSession session, long id) { + return mapper(session).selectById(id); + } + + public CustomMeasureDto selectById(DbSession session, long id) { + CustomMeasureDto customMeasure = selectNullableById(session, id); + if (customMeasure == null) { + throw new IllegalArgumentException(String.format("Custom measure '%d' not found.", id)); + } + return customMeasure; + } + + public List<CustomMeasureDto> selectByMetricId(DbSession session, int metricId) { + return mapper(session).selectByMetricId(metricId); + } + + public int countByComponentIdAndMetricId(DbSession session, String componentUuid, int metricId) { + return mapper(session).countByComponentIdAndMetricId(componentUuid, metricId); + } + + public List<CustomMeasureDto> selectByComponentUuid(DbSession session, String componentUuid, int offset, int limit) { + return mapper(session).selectByComponentUuid(componentUuid, new RowBounds(offset, limit)); + } + + public List<CustomMeasureDto> selectByComponentUuid(DbSession session, String componentUuid) { + return mapper(session).selectByComponentUuid(componentUuid); + } + + private CustomMeasureMapper mapper(DbSession session) { + return session.getMapper(CustomMeasureMapper.class); + } + + public int countByComponentUuid(DbSession dbSession, String uuid) { + return mapper(dbSession).countByComponentUuid(uuid); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureDto.java b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDto.java index e7387d34cb9..c17e0228fd9 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureDto.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDto.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.db.measure; +package org.sonar.db.measure.custom; public class CustomMeasureDto { private long id; diff --git a/sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureMapper.java index e5f46294633..cb029d6f7c8 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/CustomMeasureMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureMapper.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.db.measure; +package org.sonar.db.measure.custom; import java.util.List; import org.apache.ibatis.annotations.Param; diff --git a/sonar-db/src/main/resources/org/sonar/db/measure/CustomMeasureMapper.xml b/sonar-db/src/main/resources/org/sonar/db/measure/custom/CustomMeasureMapper.xml index 119cec178b4..8c264791389 100644 --- a/sonar-db/src/main/resources/org/sonar/db/measure/CustomMeasureMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/measure/custom/CustomMeasureMapper.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> -<mapper namespace="org.sonar.db.measure.CustomMeasureMapper"> +<mapper namespace="org.sonar.db.measure.custom.CustomMeasureMapper"> <sql id="selectColumns"> m.id, m.metric_id as metricId, diff --git a/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java new file mode 100644 index 00000000000..559b4779032 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java @@ -0,0 +1,159 @@ +/* + * 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.measure.custom; + +import java.util.Arrays; +import java.util.List; +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.test.DbTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.offset; +import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto; + +@Category(DbTests.class) +public class CustomMeasureDaoTest { + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + CustomMeasureDao sut; + DbSession session; + + @Before + public void setUp() { + session = db.getSession(); + sut = new CustomMeasureDao(); + db.truncateTables(); + } + + @Test + public void insert() { + CustomMeasureDto measure = newCustomMeasureDto(); + + sut.insert(session, measure); + + CustomMeasureDto result = sut.selectById(session, measure.getId()); + assertThat(result.getId()).isEqualTo(measure.getId()); + assertThat(result.getMetricId()).isEqualTo(measure.getMetricId()); + assertThat(result.getComponentUuid()).isEqualTo(measure.getComponentUuid()); + assertThat(result.getDescription()).isEqualTo(measure.getDescription()); + assertThat(result.getUserLogin()).isEqualTo(measure.getUserLogin()); + assertThat(result.getTextValue()).isEqualTo(measure.getTextValue()); + assertThat(result.getValue()).isCloseTo(measure.getValue(), offset(0.001d)); + assertThat(result.getCreatedAt()).isEqualTo(measure.getCreatedAt()); + assertThat(result.getUpdatedAt()).isEqualTo(measure.getUpdatedAt()); + } + + @Test + public void delete_by_metric_id() { + CustomMeasureDto measure = newCustomMeasureDto(); + sut.insert(session, measure); + assertThat(sut.selectNullableById(session, measure.getId())).isNotNull(); + + sut.deleteByMetricIds(session, Arrays.asList(measure.getMetricId())); + + assertThat(sut.selectNullableById(session, measure.getId())).isNull(); + } + + @Test + public void update() { + CustomMeasureDto measure = newCustomMeasureDto().setDescription("old-description"); + sut.insert(session, measure); + measure.setDescription("new-description"); + + sut.update(session, measure); + + assertThat(sut.selectNullableById(session, measure.getId()).getDescription()).isEqualTo("new-description"); + } + + @Test + public void delete() { + CustomMeasureDto measure = newCustomMeasureDto(); + sut.insert(session, measure); + + sut.delete(session, measure.getId()); + assertThat(sut.selectNullableById(session, measure.getId())).isNull(); + } + + @Test + public void select_by_component_uuid() { + sut.insert(session, newCustomMeasureDto().setComponentUuid("u1")); + sut.insert(session, newCustomMeasureDto().setComponentUuid("u1")); + sut.insert(session, newCustomMeasureDto().setComponentUuid("u2")); + session.commit(); + + List<CustomMeasureDto> result = sut.selectByComponentUuid(session, "u1"); + + assertThat(result).hasSize(2); + assertThat(result).extracting("componentUuid").containsOnly("u1"); + assertThat(sut.countByComponentUuid(session, "u1")).isEqualTo(2); + } + + @Test + public void select_by_component_uuid_with_options() { + sut.insert(session, newCustomMeasureDto().setComponentUuid("u1")); + sut.insert(session, newCustomMeasureDto().setComponentUuid("u1")); + sut.insert(session, newCustomMeasureDto().setComponentUuid("u2")); + session.commit(); + + List<CustomMeasureDto> result = sut.selectByComponentUuid(session, "u1", 0, 100); + + assertThat(result).hasSize(2); + assertThat(result).extracting("componentUuid").containsOnly("u1"); + } + + @Test + public void select_by_metric_id() { + sut.insert(session, newCustomMeasureDto().setMetricId(123)); + sut.insert(session, newCustomMeasureDto().setMetricId(123)); + + List<CustomMeasureDto> result = sut.selectByMetricId(session, 123); + + assertThat(result).hasSize(2); + } + + @Test + public void count_by_component_uuid_and_metric_id() { + sut.insert(session, newCustomMeasureDto().setMetricId(123).setComponentUuid("123")); + sut.insert(session, newCustomMeasureDto().setMetricId(123).setComponentUuid("123")); + + int count = sut.countByComponentIdAndMetricId(session, "123", 123); + + assertThat(count).isEqualTo(2); + } + + @Test + public void select_by_id_fail_if_no_measure_found() { + expectedException.expect(IllegalArgumentException.class); + + sut.selectById(session, 42L); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureTesting.java b/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureTesting.java new file mode 100644 index 00000000000..6f6dcf5e0d5 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureTesting.java @@ -0,0 +1,43 @@ +/* + * 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.measure.custom; + +import org.apache.commons.lang.RandomStringUtils; +import org.apache.commons.lang.math.RandomUtils; +import org.sonar.api.utils.System2; + +public class CustomMeasureTesting { + private CustomMeasureTesting() { + // static stuff only + } + + public static CustomMeasureDto newCustomMeasureDto() { + return new CustomMeasureDto() + .setDescription(RandomStringUtils.randomAlphanumeric(255)) + .setTextValue(RandomStringUtils.randomAlphanumeric(255)) + .setUserLogin(RandomStringUtils.randomAlphanumeric(255)) + .setValue(RandomUtils.nextDouble()) + .setMetricId(RandomUtils.nextInt()) + .setComponentUuid(RandomStringUtils.randomAlphanumeric(50)) + .setCreatedAt(System2.INSTANCE.now()) + .setUpdatedAt(System2.INSTANCE.now()); + } +} |