From 6f621785d7218218432a641e2cbf460763c768f8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 30 Sep 2016 10:35:08 +0200 Subject: [PATCH] SONAR-8178 ignore rows with corrupted data --- .../test/index/TestResultSetIterator.java | 19 +++++++++++------ .../test/index/TestResultSetIteratorTest.java | 21 +++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java index 525a6b0a2f7..86e81e7021d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java @@ -31,6 +31,7 @@ import java.util.Date; import java.util.List; import javax.annotation.Nullable; import org.elasticsearch.action.update.UpdateRequest; +import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -78,14 +79,20 @@ public class TestResultSetIterator extends ResultSetIterator { String projectUuid = rs.getString(1); String fileUuid = rs.getString(2); Date updatedAt = new Date(rs.getLong(3)); - InputStream dataInput = rs.getBinaryStream(4); - List data; + List tests = parseData(fileUuid, rs.getBinaryStream(4)); + return toRow(projectUuid, fileUuid, updatedAt, tests); + } + + private static List parseData(String fileUuid, @Nullable InputStream dataInput) { + List tests = Collections.emptyList(); if (dataInput != null) { - data = FileSourceDto.decodeTestData(dataInput); - } else { - data = Collections.emptyList(); + try { + tests = FileSourceDto.decodeTestData(dataInput); + } catch (Exception e) { + Loggers.get(TestResultSetIterator.class).warn(String.format("Invalid file_sources.binary_data on row with file_uuid='%s', test file will be ignored", fileUuid), e); + } } - return toRow(projectUuid, fileUuid, updatedAt, data); + return tests; } /** diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java index 46d484986b4..ac4b0806ee3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java @@ -28,13 +28,14 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import org.sonar.db.DbTester; import org.sonar.db.protobuf.DbFileSources; import org.sonar.server.source.index.FileSourcesUpdaterHelper; import org.sonar.server.test.db.TestTesting; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; import static org.assertj.core.data.MapEntry.entry; import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILES; import static org.sonar.server.test.index.TestIndexDefinition.FIELD_DURATION_IN_MS; @@ -53,6 +54,9 @@ public class TestResultSetIteratorTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); + @Rule + public LogTester logTester = new LogTester(); + TestResultSetIterator underTest; private static List newFakeTests(int numberOfTests) { @@ -186,19 +190,18 @@ public class TestResultSetIteratorTest { } @Test - public void fail_on_bad_data_format() throws Exception { + public void read_does_not_fail_if_corrupted_data() throws Exception { dbTester.prepareDbUnit(getClass(), "shared.xml"); TestTesting.updateDataColumn(dbTester.getSession(), "F1", "THIS_IS_NOT_PROTOBUF".getBytes()); underTest = TestResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L, null); - try { - assertThat(underTest.hasNext()).isTrue(); - underTest.next(); - fail("it should not be possible to go through not compliant data"); - } catch (IllegalStateException e) { - // ok - } + FileSourcesUpdaterHelper.Row row = underTest.next(); + assertThat(row.getFileUuid()).isEqualTo("F1"); + assertThat(row.getUpdateRequests()).isEmpty(); + assertThat(underTest.hasNext()).isFalse(); + + assertThat(logTester.logs(LoggerLevel.WARN)).contains("Invalid file_sources.binary_data on row with file_uuid='F1', test file will be ignored"); } @Test -- 2.39.5