diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-07-06 11:32:34 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-07-06 15:45:19 +0200 |
commit | 6c7fae043cb0dc7fe586a77e3096660364e92804 (patch) | |
tree | 3db59f0e9951b37aaa6ff4613e2d3666b05fc33b /sonar-db/src | |
parent | 969a0c08299cb3e4fbd419ec57b571fd83bd1bbd (diff) | |
download | sonarqube-6c7fae043cb0dc7fe586a77e3096660364e92804.tar.gz sonarqube-6c7fae043cb0dc7fe586a77e3096660364e92804.zip |
SONAR-7705 refactoring
including some renaming of "snapshot" to "analysis"
Diffstat (limited to 'sonar-db/src')
6 files changed, 82 insertions, 55 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java index 30ba82be9c1..f9240f3a0a1 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -62,34 +62,37 @@ public class SnapshotDao implements Dao { return value; } - public List<SnapshotDto> selectByIds(DbSession dbSession, List<Long> snapshotIds) { + public List<SnapshotDto> selectByIds(DbSession dbSession, Collection<Long> snapshotIds) { return executeLargeInputs(snapshotIds, mapper(dbSession)::selectByIds); } - public Optional<SnapshotDto> selectLastSnapshotByComponentUuid(DbSession session, String componentUuid) { + public Optional<SnapshotDto> selectLastAnalysisByComponentUuid(DbSession session, String componentUuid) { return Optional.ofNullable(mapper(session).selectLastSnapshotByComponentUuid(componentUuid)); } - public Optional<SnapshotDto> selectLastSnapshotByRootComponentUuid(DbSession session, String componentUuid) { + public Optional<SnapshotDto> selectLastAnalysisByRootComponentUuid(DbSession session, String componentUuid) { return Optional.ofNullable(mapper(session).selectLastSnapshotByRootComponentUuid(componentUuid)); } - public List<SnapshotDto> selectLastSnapshotsByRootComponentUuids(DbSession dbSession, List<String> componentUuids) { - return componentUuids.isEmpty() ? emptyList() : mapper(dbSession).selectLastSnapshotsByRootComponentUuids(componentUuids); + public List<SnapshotDto> selectLastAnalysesByRootComponentUuids(DbSession dbSession, Collection<String> componentUuids) { + if (componentUuids.isEmpty()) { + return emptyList(); + } + return executeLargeInputs(componentUuids, mapper(dbSession)::selectLastSnapshotsByRootComponentUuids); } - public List<SnapshotDto> selectSnapshotsByQuery(DbSession session, SnapshotQuery query) { + public List<SnapshotDto> selectAnalysesByQuery(DbSession session, SnapshotQuery query) { return mapper(session).selectSnapshotsByQuery(query); } @CheckForNull - public SnapshotDto selectSnapshotByQuery(DbSession session, SnapshotQuery query) { - List<SnapshotDto> snapshotDtos = mapper(session).selectSnapshotsByQuery(query); - if (snapshotDtos.isEmpty()) { + public SnapshotDto selectAnalysisByQuery(DbSession session, SnapshotQuery query) { + List<SnapshotDto> dtos = mapper(session).selectSnapshotsByQuery(query); + if (dtos.isEmpty()) { return null; } - checkState(snapshotDtos.size() == 1, "Expected one snapshot to be returned, got %s", snapshotDtos.size()); - return snapshotDtos.get(0); + checkState(dtos.size() == 1, "Expected one analysis to be returned, got %s", dtos.size()); + return dtos.get(0); } /** @@ -109,11 +112,9 @@ public class SnapshotDao implements Dao { return Optional.ofNullable(mapper(dbSession).selectByUuid(analysisUuid)); } - public void unsetIsLastFlagForComponentUuid(DbSession dbSession, String componentUuid) { - mapper(dbSession).unsetIsLastFlagForComponentUuid(componentUuid); - } - - public void setIsLastFlagForAnalysisUuid(DbSession dbSession, String analysisUuid) { + public void switchIsLastFlagAndSetProcessedStatus(DbSession dbSession, String componentUuid, String analysisUuid) { + SnapshotMapper mapper = mapper(dbSession); + mapper.unsetIsLastFlagForComponentUuid(componentUuid); mapper(dbSession).setIsLastFlagForAnalysisUuid(analysisUuid); } diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java index 5dee4a8d297..b235f4f8dc0 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -19,6 +19,7 @@ */ package org.sonar.db.component; +import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; @@ -39,7 +40,7 @@ public interface SnapshotMapper { @CheckForNull SnapshotDto selectLastSnapshotByRootComponentUuid(@Param("componentUuid") String componentUuid); - List<SnapshotDto> selectLastSnapshotsByRootComponentUuids(@Param("componentUuids") List<String> componentIds); + List<SnapshotDto> selectLastSnapshotsByRootComponentUuids(@Param("componentUuids") Collection<String> componentIds); List<SnapshotDto> selectSnapshotsByQuery(@Param("query") SnapshotQuery query); diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java index 9cbdeead4d2..ebee0a46a45 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java @@ -26,7 +26,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static java.util.Arrays.asList; -import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; +import static org.sonar.db.component.SnapshotTesting.newAnalysis; public class ComponentDbTester { private final DbTester db; @@ -41,7 +41,7 @@ public class ComponentDbTester { public SnapshotDto insertProjectAndSnapshot(ComponentDto component) { dbClient.componentDao().insert(dbSession, component); - SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(component)); + SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(component)); db.commit(); return snapshot; @@ -49,7 +49,7 @@ public class ComponentDbTester { public SnapshotDto insertViewAndSnapshot(ComponentDto component) { dbClient.componentDao().insert(dbSession, component); - SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(component)); + SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(component)); db.commit(); return snapshot; @@ -57,7 +57,7 @@ public class ComponentDbTester { public SnapshotDto insertDeveloperAndSnapshot(ComponentDto component) { dbClient.componentDao().insert(dbSession, component); - SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(component)); + SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(component)); db.commit(); return snapshot; diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index 017b5da7350..be33663ff03 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -38,7 +38,7 @@ import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC; -import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; +import static org.sonar.db.component.SnapshotTesting.newAnalysis; public class SnapshotDaoTest { @@ -113,7 +113,7 @@ public class SnapshotDaoTest { @Test public void selectLastSnapshotByRootComponentUuid_returns_absent_when_no_last_snapshot() { - Optional<SnapshotDto> snapshot = underTest.selectLastSnapshotByRootComponentUuid(db.getSession(), "uuid_123"); + Optional<SnapshotDto> snapshot = underTest.selectLastAnalysisByRootComponentUuid(db.getSession(), "uuid_123"); assertThat(snapshot).isNotPresent(); } @@ -122,7 +122,7 @@ public class SnapshotDaoTest { public void selectLastSnapshotByRootComponentUuid_returns_snapshot_flagged_as_last() { db.prepareDbUnit(getClass(), "snapshots.xml"); - Optional<SnapshotDto> snapshot = underTest.selectLastSnapshotByRootComponentUuid(db.getSession(), "uuid_2"); + Optional<SnapshotDto> snapshot = underTest.selectLastAnalysisByRootComponentUuid(db.getSession(), "uuid_2"); assertThat(snapshot.get().getId()).isEqualTo(4L); } @@ -131,14 +131,14 @@ public class SnapshotDaoTest { public void selectLastSnapshotByRootComponentUuid_returns_absent_if_only_unprocessed_snapshots() { db.prepareDbUnit(getClass(), "snapshots.xml"); - Optional<SnapshotDto> snapshot = underTest.selectLastSnapshotByRootComponentUuid(db.getSession(), "uuid_5"); + Optional<SnapshotDto> snapshot = underTest.selectLastAnalysisByRootComponentUuid(db.getSession(), "uuid_5"); assertThat(snapshot).isNotPresent(); } @Test public void selectLastSnapshotsByRootComponentUuids_returns_empty_list_if_empty_input() { - List<SnapshotDto> result = underTest.selectLastSnapshotsByRootComponentUuids(dbSession, emptyList()); + List<SnapshotDto> result = underTest.selectLastAnalysesByRootComponentUuids(dbSession, emptyList()); assertThat(result).isEmpty(); } @@ -146,13 +146,13 @@ public class SnapshotDaoTest { @Test public void selectLastSnapshotsByRootComponentUuids_returns_snapshots_flagged_as_last() { ComponentDto firstProject = componentDb.insertComponent(newProjectDto("PROJECT_UUID_1")); - dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(firstProject).setLast(false)); - SnapshotDto lastSnapshotOfFirstProject = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(firstProject).setLast(true)); + dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(false)); + SnapshotDto lastSnapshotOfFirstProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(true)); ComponentDto secondProject = componentDb.insertComponent(newProjectDto("PROJECT_UUID_2")); - SnapshotDto lastSnapshotOfSecondProject = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(secondProject).setLast(true)); + SnapshotDto lastSnapshotOfSecondProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(secondProject).setLast(true)); componentDb.insertProjectAndSnapshot(newProjectDto()); - List<SnapshotDto> result = underTest.selectLastSnapshotsByRootComponentUuids(dbSession, newArrayList(firstProject.uuid(), secondProject.uuid())); + List<SnapshotDto> result = underTest.selectLastAnalysesByRootComponentUuids(dbSession, newArrayList(firstProject.uuid(), secondProject.uuid())); assertThat(result).extracting(SnapshotDto::getId).containsOnly(lastSnapshotOfFirstProject.getId(), lastSnapshotOfSecondProject.getId()); } @@ -161,22 +161,22 @@ public class SnapshotDaoTest { public void select_snapshots_by_query() { db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml"); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery())).hasSize(6); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery())).hasSize(6); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setSort(BY_DATE, ASC)).get(0).getId()).isEqualTo(1L); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setSort(BY_DATE, DESC)).get(0).getId()).isEqualTo(3L); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD"))).hasSize(3); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isEmpty(); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isEmpty(); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setSort(BY_DATE, ASC)).get(0).getId()).isEqualTo(1L); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setSort(BY_DATE, DESC)).get(0).getId()).isEqualTo(3L); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD"))).hasSize(3); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isEmpty(); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isEmpty(); } @Test public void select_snapshot_by_query() { db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml"); - assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setIsLast(true))).isNotNull(); - assertThat(underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isNull(); - assertThat(underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isNull(); + assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setIsLast(true))).isNotNull(); + assertThat(underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isNull(); + assertThat(underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isNull(); } @Test @@ -184,9 +184,9 @@ public class SnapshotDaoTest { db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml"); expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Expected one snapshot to be returned, got 6"); + expectedException.expectMessage("Expected one analysis to be returned, got 6"); - underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery()); + underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery()); } @Test @@ -209,9 +209,9 @@ public class SnapshotDaoTest { db.getDbClient().componentDao().insert(dbSession, project); db.getDbClient().snapshotDao().insert(dbSession, - newSnapshotForProject(project).setCreatedAt(5L), - newSnapshotForProject(project).setCreatedAt(2L), - newSnapshotForProject(project).setCreatedAt(1L)); + newAnalysis(project).setCreatedAt(5L), + newAnalysis(project).setCreatedAt(2L), + newAnalysis(project).setCreatedAt(1L)); dbSession.commit(); SnapshotDto dto = underTest.selectOldestSnapshot(dbSession, project.uuid()); @@ -281,6 +281,35 @@ public class SnapshotDaoTest { assertThat(isLast).isFalse(); } + @Test + public void switchIsLastFlagAndSetProcessedStatus() { + insertAnalysis("P1", "A1", SnapshotDto.STATUS_PROCESSED, true); + insertAnalysis("P1", "A2", SnapshotDto.STATUS_UNPROCESSED, false); + insertAnalysis("P2", "A3", SnapshotDto.STATUS_PROCESSED, true); + db.commit(); + + underTest.switchIsLastFlagAndSetProcessedStatus(db.getSession(), "P1", "A2"); + + verifyStatusAndIsLastFlag("A1", SnapshotDto.STATUS_PROCESSED, false); + verifyStatusAndIsLastFlag("A2", SnapshotDto.STATUS_PROCESSED, true); + // other project is untouched + verifyStatusAndIsLastFlag("A3", SnapshotDto.STATUS_PROCESSED, true); + } + + private void insertAnalysis(String projectUuid, String uuid, String status, boolean isLastFlag) { + SnapshotDto snapshot = SnapshotTesting.newAnalysis(ComponentTesting.newProjectDto(projectUuid)) + .setLast(isLastFlag) + .setStatus(status) + .setUuid(uuid); + underTest.insert(db.getSession(), snapshot); + } + + private void verifyStatusAndIsLastFlag(String uuid, String expectedStatus, boolean expectedLastFlag) { + Optional<SnapshotDto> analysis = underTest.selectByUuid(db.getSession(), uuid); + assertThat(analysis.get().getStatus()).isEqualTo(expectedStatus); + assertThat(analysis.get().getLast()).isEqualTo(expectedLastFlag); + } + private static SnapshotDto defaultSnapshot() { return new SnapshotDto() .setUuid("u1") diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java index a7f70b209cb..b640b707a06 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java +++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java @@ -26,16 +26,12 @@ import static org.apache.commons.lang.RandomStringUtils.randomAscii; public class SnapshotTesting { - public static SnapshotDto newSnapshotForProject(ComponentDto project) { - return createBasicSnapshot(project); - } - - private static SnapshotDto createBasicSnapshot(ComponentDto component) { - checkNotNull(component.uuid(), "Project UUID must be set"); - checkArgument(component.uuid().equals(component.projectUuid()), "Component is not a tree root"); + public static SnapshotDto newAnalysis(ComponentDto rootComponent) { + checkNotNull(rootComponent.uuid(), "Project UUID must be set"); + checkArgument(rootComponent.uuid().equals(rootComponent.projectUuid()), "Component is not a tree root"); return new SnapshotDto() .setUuid(randomAlphanumeric(40)) - .setComponentUuid(component.uuid()) + .setComponentUuid(rootComponent.uuid()) .setStatus(SnapshotDto.STATUS_PROCESSED) .setCreatedAt(System.currentTimeMillis()) .setBuildDate(System.currentTimeMillis()) diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureTesting.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureTesting.java index 90a5388d80f..a59577bbb72 100644 --- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureTesting.java +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureTesting.java @@ -34,15 +34,15 @@ public class MeasureTesting { // static methods only } - public static MeasureDto newMeasureDto(MetricDto metricDto, ComponentDto component, SnapshotDto snapshot) { + public static MeasureDto newMeasureDto(MetricDto metricDto, ComponentDto component, SnapshotDto analysis) { checkNotNull(metricDto.getId()); checkNotNull(metricDto.getKey()); checkNotNull(component.uuid()); - checkNotNull(snapshot.getUuid()); + checkNotNull(analysis.getUuid()); return new MeasureDto() .setMetricId(metricDto.getId()) .setComponentUuid(component.uuid()) - .setAnalysisUuid(snapshot.getUuid()); + .setAnalysisUuid(analysis.getUuid()); } public static MeasureDto newMeasure() { |