From 050038cdaf7b09d327cb808e3a395287e2fbd7b6 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 27 Jul 2017 10:06:19 +0200 Subject: [PATCH] SONAR-9551 Index applications when indexing views --- .../org/sonar/db/component/ComponentDao.java | 2 +- .../sonar/db/component/ComponentDaoTest.java | 6 +-- .../server/view/index/ViewIndexerTest.java | 43 ++++++++++++++++--- 3 files changed, 40 insertions(+), 11 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 7caca7706ba..bf9ccb18a7c 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 @@ -216,7 +216,7 @@ public class ComponentDao implements Dao { } public List selectAllViewsAndSubViews(DbSession session) { - return mapper(session).selectUuidsForQualifiers(Qualifiers.VIEW, Qualifiers.SUBVIEW); + return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW); } public List selectProjectsFromView(DbSession session, String viewUuid, String projectViewUuid) { 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 6ab736148a5..21889265e7d 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 @@ -519,11 +519,9 @@ public class ComponentDaoTest { ComponentDto application = db.components().insertApplication(organization); assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting(UuidWithProjectUuidDto::getUuid) - .containsExactlyInAnyOrder("ABCD", "EFGH", "FGHI", "IJKL") - .doesNotContain(application.uuid()); + .containsExactlyInAnyOrder("ABCD", "EFGH", "FGHI", "IJKL", application.uuid()); assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting(UuidWithProjectUuidDto::getProjectUuid) - .containsOnly("ABCD", "EFGH", "IJKL") - .doesNotContain(application.projectUuid()); + .containsOnly("ABCD", "EFGH", "IJKL", application.projectUuid()); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java index e416e031fa0..024605c79cf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java @@ -51,6 +51,7 @@ import org.sonar.server.tester.UserSessionRule; import static com.google.common.collect.Lists.newArrayList; import static java.util.Collections.emptySet; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.component.ComponentTesting.newProjectCopy; public class ViewIndexerTest { @@ -78,7 +79,7 @@ public class ViewIndexerTest { } @Test - public void index() { + public void index_on_startup() { dbTester.prepareDbUnit(getClass(), "index.xml"); underTest.indexOnStartup(emptySet()); @@ -113,14 +114,44 @@ public class ViewIndexerTest { public void index_view_doc() { underTest.index(new ViewDoc().setUuid("EFGH").setProjects(newArrayList("KLMN", "JKLM"))); - List docs = esTester.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); - assertThat(docs).hasSize(1); + List result = esTester.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); - ViewDoc view = docs.get(0); + assertThat(result).hasSize(1); + ViewDoc view = result.get(0); assertThat(view.uuid()).isEqualTo("EFGH"); assertThat(view.projects()).containsOnly("KLMN", "JKLM"); } + @Test + public void index_application() { + ComponentDto application = dbTester.components().insertApplication(dbTester.getDefaultOrganization()); + ComponentDto project = dbTester.components().insertPrivateProject(); + dbTester.components().insertComponent(newProjectCopy("PC1", project, application)); + + underTest.index(application.uuid()); + List result = esTester.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); + + assertThat(result).hasSize(1); + ViewDoc resultApp = result.get(0); + assertThat(resultApp.uuid()).isEqualTo(application.uuid()); + assertThat(resultApp.projects()).containsExactlyInAnyOrder(project.uuid()); + } + + @Test + public void index_application_on_startup() { + ComponentDto application = dbTester.components().insertApplication(dbTester.getDefaultOrganization()); + ComponentDto project = dbTester.components().insertPrivateProject(); + dbTester.components().insertComponent(newProjectCopy("PC1", project, application)); + + underTest.indexOnStartup(emptySet()); + List result = esTester.getDocuments(ViewIndexDefinition.INDEX_TYPE_VIEW, ViewDoc.class); + + assertThat(result).hasSize(1); + ViewDoc resultApp = result.get(0); + assertThat(resultApp.uuid()).isEqualTo(application.uuid()); + assertThat(resultApp.projects()).containsExactlyInAnyOrder(project.uuid()); + } + @Test public void clear_views_lookup_cache_on_index_view_uuid() { IssueIndex issueIndex = new IssueIndex(esTester.client(), System2.INSTANCE, userSessionRule, new AuthorizationTypeSupport(userSessionRule)); @@ -136,7 +167,7 @@ public class ViewIndexerTest { OrganizationDto organizationDto = dbTester.organizations().insert(); ComponentDto view = ComponentTesting.newView(organizationDto, "ABCD"); - ComponentDto techProject1 = ComponentTesting.newProjectCopy("CDEF", project1, view); + ComponentDto techProject1 = newProjectCopy("CDEF", project1, view); dbClient.componentDao().insert(dbSession, view, techProject1); dbSession.commit(); @@ -152,7 +183,7 @@ public class ViewIndexerTest { issueIndexer.indexOnStartup(issueIndexer.getIndexTypes()); permissionIndexer.indexOnStartup(permissionIndexer.getIndexTypes()); - ComponentDto techProject2 = ComponentTesting.newProjectCopy("EFGH", project2, view); + ComponentDto techProject2 = newProjectCopy("EFGH", project2, view); dbClient.componentDao().insert(dbSession, techProject2); dbSession.commit(); underTest.index(viewUuid); -- 2.39.5