]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8391 fix NPE when id is invalid in SNAPSHOTS.PATH
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 17 Nov 2016 10:38:24 +0000 (11:38 +0100)
committerGitHub <noreply@github.com>
Thu, 17 Nov 2016 10:38:24 +0000 (11:38 +0100)
sonar-db/src/main/java/org/sonar/db/version/v60/PopulateUuidPathColumnOnProjects.java
sonar-db/src/test/java/org/sonar/db/version/v60/PopulateUuidPathColumnOnProjectsTest.java

index 4402a1bac62c8666f2345a3fad57dec51266ad87..13efc5f468431cd238ac89bffeaa35ede68ab749 100644 (file)
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import javax.annotation.Nullable;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
@@ -117,8 +118,15 @@ public class PopulateUuidPathColumnOnProjects extends BaseDataChange {
     }
 
     List<String> componentUuidPath = Arrays.stream(snapshot.snapshotPath)
-      .mapToObj(snapshotId -> relations.snapshotsById.get(snapshotId).componentUuid)
-      .collect(toCollection(() -> new ArrayList<>(snapshot.snapshotPath.length)));
+      .mapToObj(snapshotId -> relations.snapshotsById.get(snapshotId))
+      .filter(Objects::nonNull)
+      .map(s -> s.componentUuid)
+      .collect(toCollection(() -> new ArrayList<>()));
+    if (componentUuidPath.size() != snapshot.snapshotPath.length) {
+      LOG.trace("Some component UUIDs not found for snapshots [{}]", snapshot.snapshotPath);
+      return false;
+    }
+
     update.setString(1, PATH_SEPARATOR + PATH_JOINER.join(componentUuidPath) + PATH_SEPARATOR);
     update.setString(2, componentUuid);
     return true;
index 717c9d1f8ea365e53eac4d94d965ec630b3ee7d4..b109bd8c8afe28ff8ab7e4a7895e4b575f59d964 100644 (file)
@@ -117,6 +117,19 @@ public class PopulateUuidPathColumnOnProjectsTest {
     verifyNoNullPath();
   }
 
+  @Test
+  public void ignore_snapshots_with_invalid_snapshots_in_path() throws SQLException {
+    insert(QUALIFIER_PROJECT, A_PROJECT_UUID, A_PROJECT_UUID, new Snapshot(1L, "", true));
+    // the ID 999999 is unknown in the path
+    insert(QUALIFIER_DIR, A_DIR_UUID, A_PROJECT_UUID, new Snapshot(2L, "1.999999.", true));
+
+    underTest.execute();
+
+    verifyPath(A_PROJECT_UUID, ".");
+    // path of orphans is the path to project only
+    verifyPath(A_DIR_UUID, format(".%s.", A_PROJECT_UUID));
+  }
+
   private void insert(String qualifier, String uuid, String rootUuid, Snapshot... snapshots) {
     db.executeInsert(
       TABLE_PROJECTS,
@@ -144,11 +157,6 @@ public class PopulateUuidPathColumnOnProjectsTest {
     assertThat(row.get("UUID_PATH")).isEqualTo(expectedUuidPath);
   }
 
-  private void verifyProjectUuid(String componentUuid, String expectedProjectUuid) {
-    Map<String, Object> row = db.selectFirst("select project_uuid from projects where uuid='" + componentUuid + "'");
-    assertThat(row.get("PROJECT_UUID")).isEqualTo(expectedProjectUuid);
-  }
-
   private void verifyNoNullPath() {
     assertThat(db.select("select * from projects where uuid_path is null or uuid_path = ''")).isEmpty();
   }