diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-17 16:43:27 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-18 09:22:45 +0100 |
commit | a17301de1b5050815a9219003f138330ffc707bb (patch) | |
tree | a79d66df94b38c8af35b23a0c12b7fd01b6f3f7f /server | |
parent | e67fec2e7356cd8ae99ccc318e7d65f25839ba61 (diff) | |
download | sonarqube-a17301de1b5050815a9219003f138330ffc707bb.tar.gz sonarqube-a17301de1b5050815a9219003f138330ffc707bb.zip |
Batch should set updated date to 0 on updated sources and Compute should update this sources for current project
Diffstat (limited to 'server')
4 files changed, 98 insertions, 4 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java index 7746f78c72a..46e8836c1af 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java @@ -20,14 +20,22 @@ package org.sonar.server.computation.step; import org.sonar.api.resources.Qualifiers; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.MyBatis; import org.sonar.server.computation.ComputationContext; +import org.sonar.server.db.DbClient; import org.sonar.server.source.index.SourceLineIndexer; public class IndexSourceLinesStep implements ComputationStep { + private final DbClient dbClient; + private final System2 system2; private final SourceLineIndexer indexer; - public IndexSourceLinesStep(SourceLineIndexer indexer) { + public IndexSourceLinesStep(DbClient dbClient, System2 system2, SourceLineIndexer indexer) { + this.dbClient = dbClient; + this.system2 = system2; this.indexer = indexer; } @@ -38,9 +46,22 @@ public class IndexSourceLinesStep implements ComputationStep { @Override public void execute(ComputationContext context) { + updateSourceUpdateDate(context.getProject().uuid()); indexer.index(); } + // Temporary solution to only index in E/S updated sources from current project + // Should be removed when source wil be persisted in compute engine + private void updateSourceUpdateDate(String projectUuid){ + DbSession session = dbClient.openSession(true); + try { + dbClient.fileSourceDao().updateDateWhenUpdatedDateIsZero(session, projectUuid, system2.now()); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } + @Override public String getDescription() { return "Index source lines"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java index fb0042ba9ea..8b5d1f9aad5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java @@ -19,22 +19,81 @@ */ package org.sonar.server.computation.step; +import org.elasticsearch.search.SearchHit; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.utils.System2; +import org.sonar.batch.protocol.output.BatchReportReader; +import org.sonar.core.persistence.DbTester; +import org.sonar.core.source.db.FileSourceDao; +import org.sonar.server.component.ComponentTesting; +import org.sonar.server.computation.ComputationContext; +import org.sonar.server.db.DbClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.source.db.FileSourceTesting; +import org.sonar.server.source.index.SourceLineDoc; +import org.sonar.server.source.index.SourceLineIndexDefinition; import org.sonar.server.source.index.SourceLineIndexer; import java.io.IOException; +import java.sql.Connection; +import java.util.Date; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class IndexSourceLinesStepTest extends BaseStepTest { - @Test - public void supportedProjectQualifiers() throws Exception { + @ClassRule + public static DbTester dbTester = new DbTester(); + + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings())); + + System2 system2; + + DbClient dbClient; + @Before + public void setUp() throws Exception { + dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new FileSourceDao(null)); + system2 = mock(System2.class); } @Override protected ComputationStep step() throws IOException { - return new IndexSourceLinesStep(mock(SourceLineIndexer.class)); + SourceLineIndexer sourceLineIndexer = new SourceLineIndexer(dbClient, esTester.client()); + sourceLineIndexer.setEnabled(true); + return new IndexSourceLinesStep(dbClient, system2, sourceLineIndexer); + } + + @Test + public void supported_project_qualifiers() throws Exception { + assertThat(step().supportedProjectQualifiers()).containsOnly(Qualifiers.PROJECT); + } + + @Test + public void update_source_date_on_sources_with_update_at_to_zero() throws Exception { + when(system2.now()).thenReturn(150000000002L); + dbTester.prepareDbUnit(getClass(), "update_source_date_on_sources_with_update_at_to_zero.xml"); + Connection connection = dbTester.openConnection(); + FileSourceTesting.updateDataColumn(connection, "FILE1_UUID", FileSourceTesting.newRandomData(1).build()); + connection.close(); + + step().execute(new ComputationContext(mock(BatchReportReader.class), ComponentTesting.newProjectDto("ABCD"))); + + dbTester.assertDbUnit(getClass(), "update_source_date_on_sources_with_update_at_to_zero-result.xml"); + + List<SearchHit> docs = esTester.getDocuments(SourceLineIndexDefinition.INDEX, SourceLineIndexDefinition.TYPE); + assertThat(docs).hasSize(1); + SourceLineDoc doc = new SourceLineDoc(docs.get(0).sourceAsMap()); + assertThat(doc.projectUuid()).isEqualTo("ABCD"); + assertThat(doc.fileUuid()).isEqualTo("FILE1_UUID"); + assertThat(doc.updateDate()).isEqualTo(new Date(system2.now())); } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml new file mode 100644 index 00000000000..2a8f8a09034 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml @@ -0,0 +1,7 @@ +<dataset> + + <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="1500000000002"/> + +</dataset> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml new file mode 100644 index 00000000000..31135717348 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml @@ -0,0 +1,7 @@ +<dataset> + + <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="0"/> + +</dataset> |