From 019fb49ffb784cf652d7d3f7fba3cea30a7cc2b8 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 19 Jul 2017 15:30:48 +0200 Subject: [PATCH] SONAR-9551 Persist applications with APP qualifier --- .../step/PersistComponentsStep.java | 2 +- .../component/ViewsComponent.java | 12 -- .../step/ViewsPersistComponentsStepTest.java | 111 ++++++++++++------ 3 files changed, 79 insertions(+), 46 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java index dfc37c29305..387e1b4816c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java @@ -317,7 +317,7 @@ public class PersistComponentsStep implements ComputationStep { ComponentDto res = createBase(view); res.setScope(Scopes.PROJECT); - res.setQualifier(Qualifiers.VIEW); + res.setQualifier(view.getViewAttributes().getType().getQualifier()); res.setName(view.getName()); res.setDescription(view.getDescription()); res.setLongName(res.name()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java index e129eea1222..9f9824dce02 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java @@ -37,18 +37,12 @@ import static java.util.Objects.requireNonNull; public class ViewsComponent implements Component { private final Type type; private final String key; - @CheckForNull private final String uuid; - @CheckForNull private final String name; - @CheckForNull private final String description; private final List children; - @CheckForNull private final ProjectViewAttributes projectViewAttributes; - @CheckForNull private final SubViewAttributes subViewAttributes; - @CheckForNull private final ViewAttributes viewAttributes; private ViewsComponent(Type type, String key, @Nullable String uuid, @Nullable String name, @Nullable String description, @@ -77,18 +71,12 @@ public class ViewsComponent implements Component { public static final class Builder { private final Type type; private final String key; - @CheckForNull private String uuid; - @CheckForNull private String name; - @CheckForNull private String description; private List children = new ArrayList<>(); - @CheckForNull private ProjectViewAttributes projectViewAttributes; - @CheckForNull private SubViewAttributes subViewAttributes; - @CheckForNull private ViewAttributes viewAttributes; private Builder(Type type, String key) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java index cc0d1a52f1d..e3a912378ac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java @@ -42,6 +42,7 @@ import org.sonar.server.computation.task.projectanalysis.component.MutableDisabl import org.sonar.server.computation.task.projectanalysis.component.ProjectViewAttributes; import org.sonar.server.computation.task.projectanalysis.component.SubViewAttributes; import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule; +import org.sonar.server.computation.task.projectanalysis.component.ViewAttributes; import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent; import org.sonar.server.computation.task.step.ComputationStep; @@ -55,6 +56,8 @@ import static org.sonar.db.component.ComponentTesting.newSubView; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW; +import static org.sonar.server.computation.task.projectanalysis.component.ViewAttributes.Type.APPLICATION; +import static org.sonar.server.computation.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO; import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.builder; public class ViewsPersistComponentsStepTest extends BaseStepTest { @@ -107,7 +110,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { @Test public void persist_empty_view() { - treeRootHolder.setRoot(createViewBuilder().build()); + treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build()); underTest.execute(); @@ -122,7 +125,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { // most of the time view already exists since its supposed to be created when config is uploaded persistComponents(newViewDto(dbTester.organizations().insert())); - treeRootHolder.setRoot(createViewBuilder().build()); + treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build()); underTest.execute(); @@ -137,7 +140,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(project); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren(createProjectView1Builder(project, null).build()) .build()); @@ -152,10 +155,31 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { assertDtoIsProjectView1(pv1Dto, viewDto, viewDto, project); } + @Test + public void persist_application_with_projectView() { + ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()); + persistComponents(project); + + treeRootHolder.setRoot( + createViewBuilder(APPLICATION) + .addChildren(createProjectView1Builder(project, null).build()) + .build()); + + underTest.execute(); + + assertRowsCountInTableProjects(3); + + ComponentDto applicationDto = getComponentFromDb(VIEW_KEY); + assertDtoIsApplication(applicationDto); + + ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY); + assertDtoIsProjectView1(pv1Dto, applicationDto, applicationDto, project); + } + @Test public void persist_empty_subview() { treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null).build()) .build()); @@ -174,7 +198,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { @Test public void persist_empty_subview_having_original_view_uuid() { treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder("ORIGINAL_UUID").build()) .build()); @@ -194,7 +218,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(ComponentTesting.newSubView(viewDto, SUBVIEW_1_UUID, SUBVIEW_1_KEY).setName(SUBVIEW_1_NAME)); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null).build()) .build()); @@ -212,7 +236,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(newViewDto(dbTester.organizations().insert())); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null).build()) .build()); @@ -231,7 +255,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(project); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( @@ -256,7 +280,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { ComponentDto viewDto = newViewDto(dbTester.organizations().insert()).setLongName("another long name").setCreatedAt(now); persistComponents(viewDto); - treeRootHolder.setRoot(createViewBuilder().build()); + treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build()); underTest.execute(); @@ -283,7 +307,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(projectView); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren(createProjectView1Builder(project, null).build()) .build()); @@ -313,7 +337,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(projectView); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) // Project view in the View is linked to the first project2 .addChildren(createProjectView1Builder(project2, null).build()) .build()); @@ -337,7 +361,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { persistComponents(view, subView); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder("NEW_COPY").build()) .build()); @@ -356,7 +380,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { public void persists_new_components_as_public_if_root_does_not_exist_yet_out_of_functional_transaction() { ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert())); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( @@ -378,7 +402,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { ComponentDto view = newViewDto(organization).setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate); dbTester.components().insertComponent(view); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( @@ -405,7 +429,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { dbTester.components().insertComponent(subView); dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setKey("DIR").setPrivate(isRootPrivate)); treeRootHolder.setRoot( - createViewBuilder() + createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( @@ -421,11 +445,12 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { .isEqualTo(isRootPrivate)); } - private static ViewsComponent.Builder createViewBuilder() { + private static ViewsComponent.Builder createViewBuilder(ViewAttributes.Type viewType) { return builder(VIEW, VIEW_KEY) .setUuid(VIEW_UUID) .setName(VIEW_NAME) - .setDescription(VIEW_DESCRIPTION); + .setDescription(VIEW_DESCRIPTION) + .setViewAttributes(new ViewAttributes(viewType)); } private ViewsComponent.Builder createSubView1Builder(@Nullable String originalViewUuid) { @@ -473,23 +498,43 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { } /** - * Assertions to verify the DTO created from {@link #createViewBuilder()} + * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()} + */ + private void assertDtoIsView(ComponentDto dto) { + assertThat(dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); + assertThat(dto.name()).isEqualTo(VIEW_NAME); + assertThat(dto.longName()).isEqualTo(VIEW_NAME); + assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION); + assertThat(dto.path()).isNull(); + assertThat(dto.uuid()).isEqualTo(VIEW_UUID); + assertThat(dto.projectUuid()).isEqualTo(VIEW_UUID); + assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID); + assertThat(dto.moduleUuid()).isNull(); + assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + "."); + assertThat(dto.qualifier()).isEqualTo(Qualifiers.VIEW); + assertThat(dto.scope()).isEqualTo(Scopes.PROJECT); + assertThat(dto.getCopyResourceUuid()).isNull(); + assertThat(dto.getCreatedAt()).isEqualTo(now); + } + + /** + * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()} */ - private void assertDtoIsView(ComponentDto projectDto) { - assertThat(projectDto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); - assertThat(projectDto.name()).isEqualTo(VIEW_NAME); - assertThat(projectDto.longName()).isEqualTo(VIEW_NAME); - assertThat(projectDto.description()).isEqualTo(VIEW_DESCRIPTION); - assertThat(projectDto.path()).isNull(); - assertThat(projectDto.uuid()).isEqualTo(VIEW_UUID); - assertThat(projectDto.projectUuid()).isEqualTo(VIEW_UUID); - assertThat(projectDto.getRootUuid()).isEqualTo(VIEW_UUID); - assertThat(projectDto.moduleUuid()).isNull(); - assertThat(projectDto.moduleUuidPath()).isEqualTo("." + projectDto.uuid() + "."); - assertThat(projectDto.qualifier()).isEqualTo(Qualifiers.VIEW); - assertThat(projectDto.scope()).isEqualTo(Scopes.PROJECT); - assertThat(projectDto.getCopyResourceUuid()).isNull(); - assertThat(projectDto.getCreatedAt()).isEqualTo(now); + private void assertDtoIsApplication(ComponentDto dto) { + assertThat(dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); + assertThat(dto.name()).isEqualTo(VIEW_NAME); + assertThat(dto.longName()).isEqualTo(VIEW_NAME); + assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION); + assertThat(dto.path()).isNull(); + assertThat(dto.uuid()).isEqualTo(VIEW_UUID); + assertThat(dto.projectUuid()).isEqualTo(VIEW_UUID); + assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID); + assertThat(dto.moduleUuid()).isNull(); + assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + "."); + assertThat(dto.qualifier()).isEqualTo(Qualifiers.APP); + assertThat(dto.scope()).isEqualTo(Scopes.PROJECT); + assertThat(dto.getCopyResourceUuid()).isNull(); + assertThat(dto.getCreatedAt()).isEqualTo(now); } /** -- 2.39.5