diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-03-02 14:02:03 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-03-23 20:20:52 +0100 |
commit | 97fa907f891466b4e622e500d6ef94cf4c7eea8f (patch) | |
tree | bfe6a8b4bd146a314b2b75d4e7162cf6a363597b /server | |
parent | eb87d2d89414c2d621515c8b44a91e94ee653660 (diff) | |
download | sonarqube-97fa907f891466b4e622e500d6ef94cf4c7eea8f.tar.gz sonarqube-97fa907f891466b4e622e500d6ef94cf4c7eea8f.zip |
GOV-325 trigger refresh of view with project and not with it anymore
Diffstat (limited to 'server')
4 files changed, 206 insertions, 1 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 535d585ad73..eeea7944264 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -265,6 +265,13 @@ public class ComponentDao implements Dao { return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW); } + /** + * Used by Governance + */ + public Set<String> selectViewKeysWithEnabledCopyOfProject(DbSession session, String projectUuid) { + return mapper(session).selectViewKeysWithEnabledCopyOfProject(projectUuid); + } + public List<String> selectProjectsFromView(DbSession session, String viewUuid, String projectViewUuid) { return mapper(session).selectProjectsFromView("%." + viewUuid + ".%", projectViewUuid); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 4560af88e6c..95025cd3290 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -21,6 +21,7 @@ package org.sonar.db.component; import java.util.Collection; import java.util.List; +import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; @@ -120,6 +121,8 @@ public interface ComponentMapper { */ List<KeyWithUuidDto> selectUuidsByKeyFromProjectKey(@Param("projectKey") String projectKey); + Set<String> selectViewKeysWithEnabledCopyOfProject(@Param("projectUuid") String projectUuid); + /** * Return technical projects from a view or a sub-view */ diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 0ba7f68f4e8..0473a825eb2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -396,6 +396,22 @@ </foreach> </select> + <select id="selectViewKeysWithEnabledCopyOfProject" resultType="String"> + select + distinct p.kee + from projects p + inner join projects leaf on + leaf.qualifier = 'TRK' + and leaf.scope = 'FIL' + and leaf.enabled = ${_true} + and leaf.copy_component_uuid = #{projectUuid,jdbcType=VARCHAR} + where + p.enabled = ${_true} + and p.uuid = leaf.project_uuid + and p.scope = 'PRJ' + and p.qualifier in ('VW', 'APP') + </select> + <select id="selectProjectsFromView" resultType="String"> select p.copy_component_uuid from projects p diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java index 9dda85ffaff..53d327009d4 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -20,11 +20,17 @@ package org.sonar.db.component; import com.google.common.base.Optional; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -34,6 +40,7 @@ import org.assertj.core.api.ListAssert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; @@ -49,6 +56,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; @@ -64,6 +72,7 @@ import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN; import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES; import static org.sonar.db.component.SnapshotTesting.newAnalysis; +@RunWith(DataProviderRunner.class) public class ComponentDaoTest { private static final String PROJECT_UUID = "project-uuid"; @@ -79,6 +88,7 @@ public class ComponentDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); + private Random random = new Random(); private DbSession dbSession = db.getSession(); private ComponentDao underTest = new ComponentDao(); @@ -648,6 +658,175 @@ public class ComponentDaoTest { } @Test + public void selectViewKeysWithEnabledCopyOfProject_returns_empty_when_there_is_no_view() { + String projectUuid = randomAlphabetic(5); + + assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, projectUuid)).isEmpty(); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_returns_root_view_with_direct_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view = insertView(organization, rootViewQualifier); + insertProjectCopy(view, project); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_direct_copy_of_other_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project1 = insertProject(organization); + ComponentDto project2 = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier); + insertProjectCopy(view1, project1); + ComponentDto view2 = insertView(organization, rootViewQualifier); + insertProjectCopy(view2, project2); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project2.uuid()); + + assertThat(keys).containsOnly(view2.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_disabled_direct_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier); + insertProjectCopy(view1, project); + ComponentDto view2 = insertView(organization, rootViewQualifier); + insertProjectCopy(view2, project, t -> t.setEnabled(false)); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view1.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_disabled_root_view_with_direct_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier, t -> t.setEnabled(false)); + insertProjectCopy(view1, project); + ComponentDto view2 = insertView(organization, rootViewQualifier); + insertProjectCopy(view2, project); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view2.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_returns_root_view_with_indirect_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview = insertSubviews(view); + insertProjectCopy(lowestSubview, project); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_indirect_copy_of_other_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project1 = insertProject(organization); + ComponentDto project2 = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview1 = insertSubviews(view1); + insertProjectCopy(lowestSubview1, project1); + ComponentDto view2 = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview2 = insertSubviews(view2); + insertProjectCopy(lowestSubview2, project2); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project2.uuid()); + + assertThat(keys).containsOnly(view2.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_disabled_indirect_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview1 = insertSubviews(view1); + insertProjectCopy(lowestSubview1, project); + ComponentDto view2 = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview2 = insertSubviews(view2); + insertProjectCopy(lowestSubview2, project, t -> t.setEnabled(false)); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view1.getDbKey()); + } + + @Test + @UseDataProvider("portfolioOrApplicationRootViewQualifier") + public void selectViewKeysWithEnabledCopyOfProject_does_not_return_disabled_root_view_with_indirect_copy_of_project(String rootViewQualifier) { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = insertProject(organization); + ComponentDto view1 = insertView(organization, rootViewQualifier, t -> t.setEnabled(false)); + ComponentDto lowestSubview1 = insertSubviews(view1); + insertProjectCopy(lowestSubview1, project); + ComponentDto view2 = insertView(organization, rootViewQualifier); + ComponentDto lowestSubview2 = insertSubviews(view2); + insertProjectCopy(lowestSubview2, project); + + Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, project.uuid()); + + assertThat(keys).containsOnly(view2.getDbKey()); + } + + @DataProvider + public static Object[][] portfolioOrApplicationRootViewQualifier() { + return new Object[][] { + {Qualifiers.VIEW}, + {Qualifiers.APP}, + }; + } + + private ComponentDto insertSubviews(ComponentDto view) { + ComponentDto lowestView = view; + int subviewsCount1 = 1 + random.nextInt(5); + for (int i = 0; i < subviewsCount1; i++) { + lowestView = db.components().insertSubView(lowestView); + } + return lowestView; + } + + @SafeVarargs + private final ComponentDto insertView(OrganizationDto organization, String rootViewQualifier, Consumer<ComponentDto>... dtoPopulators) { + ComponentDbTester tester = db.components(); + if (rootViewQualifier.equals(Qualifiers.VIEW)) { + return random.nextBoolean() ? tester.insertPublicPortfolio(organization, dtoPopulators) : tester.insertPrivatePortfolio(organization, dtoPopulators); + } + return random.nextBoolean() ? tester.insertPublicApplication(organization, dtoPopulators) : tester.insertPrivatePortfolio(organization, dtoPopulators); + } + + private ComponentDto insertProject(OrganizationDto organization) { + return random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); + } + + @SafeVarargs + private final ComponentDto insertProjectCopy(ComponentDto view, ComponentDto project, Consumer<ComponentDto>... decorators) { + ComponentDto component = ComponentTesting.newProjectCopy(project, view); + Arrays.stream(decorators).forEach(decorator -> decorator.accept(component)); + return db.components().insertComponent(component); + } + + @Test public void select_projects_from_view() { ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(); @@ -934,7 +1113,7 @@ public class ComponentDaoTest { List<ComponentDto> components = new ArrayList<>(); underTest.scrollForIndexing(dbSession, projectUuid, context -> components.add(context.getResultObject())); - return (ListAssert<String>)assertThat(components).extracting(ComponentDto::uuid); + return (ListAssert<String>) assertThat(components).extracting(ComponentDto::uuid); } @Test |