diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-07-19 11:56:44 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-07-19 13:44:39 +0200 |
commit | a2ef7b6b793c38fa75e4bc98abaad5137a752483 (patch) | |
tree | abde6aedea2b52363a77cb654b552704dcdfb733 | |
parent | aa2ad15260a5551cd45339211442cd1f572c1501 (diff) | |
download | sonarqube-a2ef7b6b793c38fa75e4bc98abaad5137a752483.tar.gz sonarqube-a2ef7b6b793c38fa75e4bc98abaad5137a752483.zip |
SONAR-7867 fix create_at of snapshot of projectView
4 files changed, 62 insertions, 11 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ProjectViewAttributes.java b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ProjectViewAttributes.java index 0f704cf499c..d7a8ee232e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ProjectViewAttributes.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ProjectViewAttributes.java @@ -19,24 +19,44 @@ */ package org.sonar.server.computation.component; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @Immutable public class ProjectViewAttributes { private final long projectId; + @CheckForNull + private final Long analysisDate; + /** + * Compatibility with Governance 1.0. + * + * FIXME: remove in SQ 6.0 + */ public ProjectViewAttributes(long projectId) { + this(projectId, null); + } + + public ProjectViewAttributes(long projectId, @Nullable Long analysisDate) { this.projectId = projectId; + this.analysisDate = analysisDate; } public long getProjectId() { return projectId; } + @CheckForNull + public Long getAnalysisDate() { + return analysisDate; + } + @Override public String toString() { return "ProjectViewAttributes{" + "projectId=" + projectId + + ", analysisDate=" + analysisDate + '}'; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistSnapshotsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistSnapshotsStep.java index 98ed3a10794..7e8f21cda6c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistSnapshotsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistSnapshotsStep.java @@ -166,7 +166,7 @@ public class PersistSnapshotsStep implements ComputationStep { .setScope(scope) .setLast(false) .setStatus(SnapshotDto.STATUS_UNPROCESSED) - .setCreatedAt(analysisDate) + .setCreatedAt(computeSnapshotAnalysisDate(component, analysisDate)) .setBuildDate(system2.now()); SnapshotDto parentSnapshot = path.isRoot() ? null : path.parent().getSnapshotDto(); @@ -185,6 +185,16 @@ public class PersistSnapshotsStep implements ComputationStep { return snapshotDto; } + private long computeSnapshotAnalysisDate(Component component, long analysisDate) { + if (component.getType() == Component.Type.PROJECT_VIEW) { + Long projectViewAnalysisDate = component.getProjectViewAttributes().getAnalysisDate(); + if (projectViewAnalysisDate != null) { + return projectViewAnalysisDate; + } + } + return analysisDate; + } + private void addToCache(Component component, SnapshotDto snapshotDto) { dbIdsRepository.setSnapshotId(component, snapshotDto.getId()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistComponentsStepTest.java index ef126bbe1b8..334401e13c5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistComponentsStepTest.java @@ -127,7 +127,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { treeRootHolder.setRoot( createViewBuilder() - .addChildren(createProjectView1Builder(project).build()) + .addChildren(createProjectView1Builder(project, null).build()) .build()); underTest.execute(); @@ -208,7 +208,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { .addChildren( createSubView1Builder() .addChildren( - createProjectView1Builder(project).build()) + createProjectView1Builder(project, null).build()) .build()) .build()); @@ -252,7 +252,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { treeRootHolder.setRoot( createViewBuilder() - .addChildren(createProjectView1Builder(project).build()) + .addChildren(createProjectView1Builder(project, null).build()) .build()); underTest.execute(); @@ -279,7 +279,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { treeRootHolder.setRoot( createViewBuilder() // Project view in the View is linked to the first project2 - .addChildren(createProjectView1Builder(project2).build()) + .addChildren(createProjectView1Builder(project2, null).build()) .build()); underTest.execute(); @@ -297,12 +297,12 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { return builder(SUBVIEW, SUBVIEW_1_KEY).setUuid(SUBVIEW_1_UUID).setName(SUBVIEW_1_NAME).setDescription(SUBVIEW_1_DESCRIPTION); } - private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project) { + private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project, Long analysisDate) { return builder(PROJECT_VIEW, PROJECT_VIEW_1_KEY) .setUuid(PROJECT_VIEW_1_UUID) .setName(PROJECT_VIEW_1_NAME) .setDescription("project view description is not persisted") - .setProjectViewAttributes(new ProjectViewAttributes(project.getId())); + .setProjectViewAttributes(new ProjectViewAttributes(project.getId(), analysisDate)); } private void persistComponents(ComponentDto... componentDtos) { @@ -345,7 +345,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { } /** - * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto)} + * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)} */ private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) { assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java index 0b812822820..2d8355d4339 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java @@ -34,6 +34,7 @@ import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule; import org.sonar.server.computation.batch.TreeRootHolderRule; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.MapBasedDbIdsRepository; +import org.sonar.server.computation.component.ProjectViewAttributes; import org.sonar.server.computation.component.ViewsComponent; import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.PeriodsHolderRule; @@ -111,7 +112,7 @@ public class ViewsPersistSnapshotsStepTest extends BaseStepTest { ComponentDto projectViewDto = save(newProjectCopy("DEFG", projectDto, subViewDto).setKey("3")); dbTester.getSession().commit(); - Component projectView = ViewsComponent.builder(PROJECT_VIEW, 3).setUuid("DEFG").build(); + Component projectView = ViewsComponent.builder(PROJECT_VIEW, 3).setProjectViewAttributes(new ProjectViewAttributes(6845L, null)).setUuid("DEFG").build(); Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("CDEF").addChildren(projectView).build(); Component view = ViewsComponent.builder(VIEW, 1).setUuid("ABCD").addChildren(subView).build(); treeRootHolder.setRoot(view); @@ -175,11 +176,31 @@ public class ViewsPersistSnapshotsStepTest extends BaseStepTest { } @Test + public void use_ProjectViewAttribute_analysisDate_as_createdAt_when_non_null() { + ComponentDto projectDto = save(newProjectDto("proj")); + ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project")); + ComponentDto projectViewDto = save(newProjectCopy("DEFG", projectDto, viewDto).setKey("3")); + + long projectAnalysisDate = 946543544L; + Component projectView = ViewsComponent.builder(PROJECT_VIEW, 3).setProjectViewAttributes(new ProjectViewAttributes(6845L, projectAnalysisDate)).setUuid("DEFG").build(); + Component view = ViewsComponent.builder(VIEW, 1).setUuid("ABCD").addChildren(projectView).build(); + treeRootHolder.setRoot(view); + + dbIdsRepository.setComponentId(view, viewDto.getId()); + dbIdsRepository.setComponentId(projectView, projectViewDto.getId()); + + underTest.execute(); + + SnapshotDto projectViewSnapshot = getUnprocessedSnapshot(projectViewDto.getId()); + assertThat(projectViewSnapshot.getCreatedAt()).isEqualTo(projectAnalysisDate); + } + + @Test public void persist_snapshots_with_periods() { ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project")); ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2")); - SnapshotDto viewSnapshotDto = save(newSnapshotForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); - SnapshotDto subViewSnapshotDto = save(newSnapshotForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); + save(newSnapshotForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); + save(newSnapshotForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); dbTester.getSession().commit(); Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("ABCD").build(); |