diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-30 14:04:29 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-30 14:04:29 +0100 |
commit | 8a2d479837ab5080f9dfdf2a19433f1016169bfd (patch) | |
tree | ba9e4df0f9997344fd78dc0ed1c53c3b41eed18c | |
parent | 63801eb380f32bbf59659a8d241b51cc5f8ff412 (diff) | |
download | sonarqube-8a2d479837ab5080f9dfdf2a19433f1016169bfd.tar.gz sonarqube-8a2d479837ab5080f9dfdf2a19433f1016169bfd.zip |
SONAR-5755 Filter on project when searching for last synchronisation for issues and issues authorization
19 files changed, 154 insertions, 41 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/IndexProjectIssuesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/IndexProjectIssuesStep.java index 83014c6b9bb..51bb0f364f3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/IndexProjectIssuesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/IndexProjectIssuesStep.java @@ -25,6 +25,7 @@ import org.sonar.core.computation.db.AnalysisReportDto; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.DbClient; import org.sonar.server.issue.index.IssueIndex; +import org.sonar.server.issue.index.IssueNormalizer; import org.sonar.server.search.IndexClient; public class IndexProjectIssuesStep implements ComputationStep { @@ -39,7 +40,7 @@ public class IndexProjectIssuesStep implements ComputationStep { @Override public void execute(DbSession session, AnalysisReportDto report) { - indexProjectIssues(session, report.getProjectKey()); + indexProjectIssues(session, report); } @Override @@ -47,10 +48,10 @@ public class IndexProjectIssuesStep implements ComputationStep { return "Update issues index"; } - private void indexProjectIssues(DbSession session, String projectKey) { + private void indexProjectIssues(DbSession session, AnalysisReportDto report) { dbClient.issueDao().synchronizeAfter(session, - index.get(IssueIndex.class).getLastSynchronization(), - ImmutableMap.of("project", projectKey)); + index.get(IssueIndex.class).getLastSynchronization(ImmutableMap.of(IssueNormalizer.IssueField.PROJECT.field(), report.getProject().uuid())), + ImmutableMap.of(IssueNormalizer.IssueField.PROJECT.field(), report.getProject().uuid())); session.commit(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/SynchronizeProjectPermissionsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/SynchronizeProjectPermissionsStep.java index e7f1cd91522..a529ccf59f0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/SynchronizeProjectPermissionsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/SynchronizeProjectPermissionsStep.java @@ -24,10 +24,12 @@ import com.google.common.collect.ImmutableMap; import org.sonar.core.computation.db.AnalysisReportDto; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.DbClient; -import org.sonar.server.issue.db.IssueAuthorizationDao; import org.sonar.server.issue.index.IssueAuthorizationIndex; +import org.sonar.server.issue.index.IssueAuthorizationNormalizer; import org.sonar.server.search.IndexClient; +import java.util.Map; + public class SynchronizeProjectPermissionsStep implements ComputationStep { private final DbClient dbClient; @@ -51,8 +53,8 @@ public class SynchronizeProjectPermissionsStep implements ComputationStep { private void synchronizeProjectPermissionsIfNotFound(DbSession session, AnalysisReportDto report) { String projectUuid = report.getProject().uuid(); if (index.get(IssueAuthorizationIndex.class).getNullableByKey(projectUuid) == null) { - dbClient.issueAuthorizationDao().synchronizeAfter(session, null, - ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, projectUuid)); + Map<String, String> params = ImmutableMap.of(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field(), projectUuid); + dbClient.issueAuthorizationDao().synchronizeAfter(session, null, params); session.commit(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java index c375f650eb6..7810f90be0d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java @@ -30,6 +30,7 @@ import org.sonar.core.issue.db.IssueAuthorizationMapper; import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; +import org.sonar.server.issue.index.IssueAuthorizationNormalizer; import org.sonar.server.search.DbSynchronizationHandler; import org.sonar.server.search.IndexDefinition; import org.sonar.server.search.action.DeleteKey; @@ -41,8 +42,6 @@ import java.util.Map; public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, IssueAuthorizationDto, String> implements DaoComponent { - public static final String PROJECT_UUID = "project"; - public IssueAuthorizationDao() { this(System2.INSTANCE); } @@ -87,7 +86,7 @@ public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, Iss @Override public void enqueueCollected() { - String projectUuid = getParams().get("project"); + String projectUuid = getParams().get(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field()); if (authorizationDtoMap.isEmpty() && projectUuid != null) { getSession().enqueue(new DeleteKey<String>(getIndexType(), projectUuid)); } else { @@ -104,7 +103,7 @@ public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, Iss Map<String, Object> finalParams = super.getSynchronizationParams(date, params); finalParams.put("permission", UserRole.USER); finalParams.put("anyone", DefaultGroups.ANYONE); - finalParams.put(PROJECT_UUID, params.get(PROJECT_UUID)); + finalParams.put(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field(), params.get(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field())); return finalParams; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java index c9f4fbfd0e0..9ac3f2022ca 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueDao.java @@ -27,6 +27,7 @@ import org.sonar.core.issue.db.IssueMapper; import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; +import org.sonar.server.issue.index.IssueNormalizer; import org.sonar.server.search.IndexDefinition; import javax.annotation.Nullable; @@ -38,8 +39,6 @@ import java.util.Map; public class IssueDao extends BaseDao<IssueMapper, IssueDto, String> implements DaoComponent { - public static final String PROJECT_KEY = "project"; - public IssueDao() { this(System2.INSTANCE); } @@ -85,8 +84,7 @@ public class IssueDao extends BaseDao<IssueMapper, IssueDto, String> implements @Override protected Map<String, Object> getSynchronizationParams(@Nullable Date date, Map<String, String> params) { Map<String, Object> finalParams = super.getSynchronizationParams(date, params); - // TODO replace usage of project key by project uuid - finalParams.put(PROJECT_KEY, params.get(PROJECT_KEY)); + finalParams.put(IssueNormalizer.IssueField.PROJECT.field(), params.get(IssueNormalizer.IssueField.PROJECT.field())); return finalParams; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationIndex.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationIndex.java index 589f437fb38..8590ae013c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationIndex.java @@ -21,6 +21,8 @@ package org.sonar.server.issue.index; import com.google.common.base.Preconditions; +import org.elasticsearch.index.query.FilterBuilder; +import org.elasticsearch.index.query.FilterBuilders; import org.sonar.core.issue.db.IssueAuthorizationDto; import org.sonar.server.search.BaseIndex; import org.sonar.server.search.IndexDefinition; @@ -62,4 +64,13 @@ public class IssueAuthorizationIndex extends BaseIndex<IssueAuthorizationDoc, Is Preconditions.checkNotNull(fields, "Cannot construct IssueAuthorization with null response"); return new IssueAuthorizationDoc(fields); } + + @Override + protected FilterBuilder getLastSynchronizationBuilder(Map<String, String> params) { + String projectUuid = params.get(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field()); + if (projectUuid != null) { + return FilterBuilders.boolFilter().must(FilterBuilders.termsFilter(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field(), projectUuid)); + } + return super.getLastSynchronizationBuilder(params); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java index 424b7616e00..d75ff641735 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java @@ -140,6 +140,15 @@ public class IssueIndex extends BaseIndex<Issue, IssueDto, String> { return null; } + @Override + protected FilterBuilder getLastSynchronizationBuilder(Map<String, String> params) { + String projectUuid = params.get(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field()); + if (projectUuid != null) { + return FilterBuilders.boolFilter().must(FilterBuilders.termsFilter(IssueNormalizer.IssueField.PROJECT.field(), projectUuid)); + } + return super.getLastSynchronizationBuilder(params); + } + public List<FacetValue> listAssignees(IssueQuery query) { QueryContext queryContext = new QueryContext().setPage(1, 0); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java index 7cbddf2ae3e..e567f42a7af 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java @@ -38,8 +38,8 @@ import org.sonar.core.user.UserDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.issue.db.IssueAuthorizationDao; import org.sonar.server.issue.index.IssueAuthorizationIndex; +import org.sonar.server.issue.index.IssueAuthorizationNormalizer; import org.sonar.server.search.IndexClient; import org.sonar.server.user.UserSession; @@ -280,8 +280,7 @@ public class InternalPermissionService implements ServerComponent { } public void synchronizeProjectPermissions(DbSession session, String projectUuid) { - dbClient.issueAuthorizationDao().synchronizeAfter(session, - index.get(IssueAuthorizationIndex.class).getLastSynchronization(), - ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, projectUuid)); + Map<String, String> params = ImmutableMap.of(IssueAuthorizationNormalizer.IssueAuthorizationField.PROJECT.field(), projectUuid); + dbClient.issueAuthorizationDao().synchronizeAfter(session, index.get(IssueAuthorizationIndex.class).getLastSynchronization(params), params); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 3353bdffdb0..9f8d748d22d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -194,12 +194,20 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial /* Synchronization methods */ @Override + @CheckForNull public Date getLastSynchronization() { + return getLastSynchronization(Collections.<String, String>emptyMap()); + } - Date date; + @Override + @CheckForNull + public Date getLastSynchronization(Map<String, String> params) { SearchRequestBuilder request = client.prepareSearch(this.getIndexName()) .setTypes(this.getIndexType()) - .setQuery(QueryBuilders.matchAllQuery()) + .setQuery(QueryBuilders.filteredQuery( + QueryBuilders.matchAllQuery(), + getLastSynchronizationBuilder(params) + )) .setSize(0) .addAggregation(AggregationBuilders.max("latest") .field(BaseNormalizer.UPDATED_AT_FIELD)); @@ -207,15 +215,18 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial SearchResponse response = client.execute(request); Max max = response.getAggregations().get("latest"); - if (max.getValue() > 0) { - date = new DateTime(Double.valueOf(max.getValue()).longValue()).toDate(); + Date date = new DateTime(Double.valueOf(max.getValue()).longValue()).toDate(); + LOG.debug("Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date); + return date; } else { - date = new Date(0L); + LOG.debug("Index {}:{} has no last update date", this.getIndexName(), this.getIndexType()); + return null; } + } - LOG.debug("Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date); - return date; + protected FilterBuilder getLastSynchronizationBuilder(Map<String, String> params) { + return FilterBuilders.matchAllFilter(); } /* Index management methods */ diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/Index.java b/server/sonar-server/src/main/java/org/sonar/server/search/Index.java index f839a257a90..8ae1b962217 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/Index.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/Index.java @@ -28,6 +28,7 @@ import javax.annotation.CheckForNull; import java.io.Serializable; import java.util.Date; import java.util.Iterator; +import java.util.Map; public interface Index<DOMAIN, DTO extends Dto<KEY>, KEY extends Serializable> extends Startable, ServerComponent { @@ -38,8 +39,12 @@ public interface Index<DOMAIN, DTO extends Dto<KEY>, KEY extends Serializable> e String getIndexName(); + @CheckForNull Date getLastSynchronization(); + @CheckForNull + Date getLastSynchronization(Map<String, String> params); + IndexStat getIndexStat(); Iterator<DOMAIN> scroll(String scrollId); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/IndexProjectIssuesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/IndexProjectIssuesStepTest.java index 83f2c24b617..88b3f432762 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/IndexProjectIssuesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/IndexProjectIssuesStepTest.java @@ -100,7 +100,6 @@ public class IndexProjectIssuesStepTest { @Test public void add_issues_in_index() { ComponentDto project = insertPermissionsForProject(DEFAULT_PROJECT_KEY); - db.issueAuthorizationDao().synchronizeAfter(session, new Date(0)); ComponentDto file = ComponentTesting.newFileDto(project); @@ -119,6 +118,7 @@ public class IndexProjectIssuesStepTest { queue.add(DEFAULT_PROJECT_KEY, 123L); AnalysisReportDto report = queue.bookNextAvailable(); + report.setProject(project); sut.execute(session, report); @@ -152,9 +152,11 @@ public class IndexProjectIssuesStepTest { queue.add(DEFAULT_PROJECT_KEY, 123L); List<AnalysisReportDto> reports = queue.findByProjectKey(DEFAULT_PROJECT_KEY); + AnalysisReportDto reportDto = reports.get(0); + reportDto.setProject(project); // ACT - sut.execute(session, reports.get(0)); + sut.execute(session, reportDto); session.commit(); session.clearCache(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/SynchronizeProjectPermissionsStepMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/SynchronizeProjectPermissionsStepMediumTest.java index 4a669ef5c3a..cf354cf0e69 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/SynchronizeProjectPermissionsStepMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/SynchronizeProjectPermissionsStepMediumTest.java @@ -36,7 +36,6 @@ import org.sonar.core.persistence.MyBatis; import org.sonar.core.user.UserDto; import org.sonar.server.component.ComponentTesting; import org.sonar.server.db.DbClient; -import org.sonar.server.issue.db.IssueAuthorizationDao; import org.sonar.server.issue.index.IssueAuthorizationDoc; import org.sonar.server.issue.index.IssueAuthorizationIndex; import org.sonar.server.tester.ServerTester; @@ -105,7 +104,7 @@ public class SynchronizeProjectPermissionsStepMediumTest { public void not_add_project_issue_permission_if_already_existing() throws Exception { ComponentDto project = insertPermissionsForProject(DEFAULT_PROJECT_KEY); // Synchronisation on project is already done - db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, project.uuid())); + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", project.uuid())); // To check that permission will not be synchronized again, add a new permission on the project in db, this permission should not be in the index tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.USERS, UserRole.USER, session); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java index 0d96d7d4ee8..32b4e62e246 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java @@ -80,7 +80,7 @@ public class IssueAuthorizationDaoTest extends AbstractDaoTestCase { assertThat(session.getActionCount()).isEqualTo(0); - dao.synchronizeAfter(session, DateUtils.parseDate("2014-01-01"), ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, "ABCD")); + dao.synchronizeAfter(session, DateUtils.parseDate("2014-01-01"), ImmutableMap.of("project", "ABCD")); // SynchronizeAfter adds an implicit action (refresh) after execution of synchronization assertThat(session.getActionCount()).isEqualTo(2); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java index ce9173349e0..89dee12057f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueDaoTest.java @@ -180,7 +180,7 @@ public class IssueDaoTest extends AbstractDaoTestCase { public void find_after_dates_with_project() throws Exception { setupData("shared", "find_after_dates_with_project"); - assertThat(dao.findAfterDate(session, DateUtils.parseDate("2014-01-01"), ImmutableMap.of("project", "struts"))).hasSize(1); + assertThat(dao.findAfterDate(session, DateUtils.parseDate("2014-01-01"), ImmutableMap.of("project", "ABCD"))).hasSize(1); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexMediumTest.java index 10b3167fdf3..fb8e51c52d0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexMediumTest.java @@ -34,7 +34,6 @@ import org.sonar.core.user.GroupDto; import org.sonar.core.user.UserDto; import org.sonar.server.component.ComponentTesting; import org.sonar.server.db.DbClient; -import org.sonar.server.issue.db.IssueAuthorizationDao; import org.sonar.server.platform.Platform; import org.sonar.server.tester.ServerTester; @@ -141,7 +140,7 @@ public class IssueAuthorizationIndexMediumTest { session.commit(); assertThat(index.getNullableByKey(project.uuid())).isNull(); - db.issueAuthorizationDao().synchronizeAfter(session, new Date(0), ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, project.uuid())); + db.issueAuthorizationDao().synchronizeAfter(session, new Date(0), ImmutableMap.of("project", project.uuid())); session.commit(); IssueAuthorizationDoc issueAuthorizationDoc = index.getByKey(project.uuid()); @@ -154,6 +153,50 @@ public class IssueAuthorizationIndexMediumTest { } @Test + public void get_last_synchronization_with_project() throws Exception { + project = ComponentTesting.newProjectDto() + .setAuthorizationUpdatedAt(DateUtils.parseDate("2014-09-11")); + db.componentDao().insert(session, project); + + GroupDto sonarUsers = new GroupDto().setName("devs"); + db.groupDao().insert(session, sonarUsers); + tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), "devs", UserRole.USER, session); + session.commit(); + + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project.uuid()))).isNull(); + + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", project.uuid())); + session.commit(); + + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project.uuid()))).isNotNull(); + assertThat(index.getLastSynchronization(ImmutableMap.of("project", "Another project"))).isNull(); + } + + @Test + public void synchronizing_another_project_should_not_update_last_synchronization_date() throws Exception { + project = ComponentTesting.newProjectDto() + .setAuthorizationUpdatedAt(DateUtils.parseDate("2014-09-11")); + db.componentDao().insert(session, project); + + GroupDto sonarUsers = new GroupDto().setName("devs"); + db.groupDao().insert(session, sonarUsers); + tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), "devs", UserRole.USER, session); + session.commit(); + + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", project.uuid())); + session.commit(); + Date date = index.getLastSynchronization(ImmutableMap.of("project", project.uuid())); + assertThat(date).isNotNull(); + + // Another project is synchronized + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", "Another project")); + session.commit(); + + // Last sync date of current project is not updated + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project.uuid()))).isEqualTo(date); + } + + @Test public void remove_data_when_synchronizing_project_with_empty_permission() throws Exception { project = ComponentTesting.newProjectDto() .setKey("Sample") @@ -170,13 +213,13 @@ public class IssueAuthorizationIndexMediumTest { // Insert one permission tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), "devs", UserRole.USER, session); - db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, project.uuid())); + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", project.uuid())); session.commit(); assertThat(index.getByKey(project.uuid())).isNotNull(); // Delete the permission tester.get(PermissionFacade.class).deleteGroupPermission(project.getId(), "devs", UserRole.USER, session); - db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of(IssueAuthorizationDao.PROJECT_UUID, project.uuid())); + db.issueAuthorizationDao().synchronizeAfter(session, null, ImmutableMap.of("project", project.uuid())); session.commit(); assertThat(index.getNullableByKey(project.uuid())).isNull(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java index 0139d113aa0..9e1e78551d2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java @@ -865,6 +865,37 @@ public class IssueIndexMediumTest { } @Test + public void get_last_synchronization_with_project() throws Exception { + ComponentDto project1 = ComponentTesting.newProjectDto().setKey("project1"); + ComponentDto project2 = ComponentTesting.newProjectDto().setKey("project2"); + tester.get(ComponentDao.class).insert(session, project1, project2); + + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project1.uuid()))).isNull(); + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project2.uuid()))).isNull(); + + db.issueDao().insert(session, IssueTesting.newDto(rule, file, project1)); + session.commit(); + session.clearCache(); + + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project1.uuid()))).isNotNull(); + assertThat(index.getLastSynchronization(ImmutableMap.of("project", project2.uuid()))).isNull(); + } + + @Test + public void get_last_synchronization() throws Exception { + ComponentDto project = ComponentTesting.newProjectDto().setKey("project1"); + tester.get(ComponentDao.class).insert(session, project); + + assertThat(index.getLastSynchronization()).isNull(); + + db.issueDao().insert(session, IssueTesting.newDto(rule, file, project)); + session.commit(); + session.clearCache(); + + assertThat(index.getLastSynchronization()).isNotNull(); + } + + @Test public void delete_closed_issues_from_one_project_older_than_specific_date() { // ARRANGE Date today = new Date(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index 81907de87f2..f2cfdf6343e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -78,10 +78,10 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest { // 3. Assert startup picks it up tester.clearIndexes(); - Date before = index.get(ActiveRuleIndex.class).getLastSynchronization(); + assertThat(index.get(ActiveRuleIndex.class).getLastSynchronization()).isNull(); tester.get(Platform.class).executeStartupTasks(); assertThat(index.get(ActiveRuleIndex.class).getNullableByKey(activeRule.getKey())).isNotNull(); - assertThat(before.before(index.get(ActiveRuleIndex.class).getLastSynchronization())).isTrue(); + assertThat(index.get(ActiveRuleIndex.class).getLastSynchronization()).isNotNull(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/SearchHealthMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/SearchHealthMediumTest.java index 2edefc9b664..e6cc0d6cb71 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/SearchHealthMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/SearchHealthMediumTest.java @@ -82,7 +82,10 @@ public class SearchHealthMediumTest { assertThat(indexHealth).isNotEmpty(); for (IndexHealth index : indexHealth.values()) { assertThat(index.getDocumentCount()).isGreaterThanOrEqualTo(0L); - assertThat(index.getLastSynchronization().before(now)).isTrue(); + Date lastSync = index.getLastSynchronization(); + if (lastSync != null) { + assertThat(lastSync.before(now)).isTrue(); + } assertThat(index.isOptimized()).isIn(true, false); } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java index c769c30825e..5e51b8c3a56 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java @@ -47,5 +47,5 @@ public interface IssueMapper { int updateIfBeforeSelectedDate(IssueDto issue); - List<IssueDto> selectAfterDate(@Nullable @Param("date") Timestamp timestamp, @Nullable @Param("project") String project); + List<IssueDto> selectAfterDate(@Nullable @Param("date") Timestamp timestamp, @Nullable @Param("project") String projectUuid); } diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 662acb333c1..57fb48e104c 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -153,7 +153,7 @@ AND (i.updated_at IS NULL or i.updated_at >= #{date}) </if> <if test="project != null"> - AND root.kee = #{project} + AND root.uuid = #{project} </if> </where> </select> |