]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8178 ignore rows with corrupted data
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 30 Sep 2016 08:35:08 +0000 (10:35 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 30 Sep 2016 09:37:05 +0000 (11:37 +0200)
server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java
server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java

index 53adca4d368718ebdf38d1c512190cf0900ab4dd..a93dc8c0f4639810680b0704248520223b13f1c2 100644 (file)
@@ -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;
@@ -79,14 +80,20 @@ public class TestResultSetIterator extends ResultSetIterator<Row> {
     String projectUuid = rs.getString(1);
     String fileUuid = rs.getString(2);
     Date updatedAt = new Date(rs.getLong(3));
-    InputStream dataInput = rs.getBinaryStream(4);
-    List<DbFileSources.Test> data;
+    List<DbFileSources.Test> tests = parseData(fileUuid, rs.getBinaryStream(4));
+    return toRow(projectUuid, fileUuid, updatedAt, tests);
+  }
+
+  private static List<DbFileSources.Test> parseData(String fileUuid, @Nullable InputStream dataInput) {
+    List<DbFileSources.Test> 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;
   }
 
   /**
index 5b942c5565c7c576497664cc8a45ad4cb634a21c..7a5f8ba4a4cc43d754521b08eceef21a94438a3b 100644 (file)
@@ -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;
@@ -52,6 +53,9 @@ public class TestResultSetIteratorTest {
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
+  @Rule
+  public LogTester logTester = new LogTester();
+
   TestResultSetIterator underTest;
 
   private static List<DbFileSources.Test> newFakeTests(int numberOfTests) {
@@ -183,19 +187,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