]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7084 Analysis do not fail when a component do not have a path 661/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 30 Nov 2015 15:51:26 +0000 (16:51 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 2 Dec 2015 09:46:22 +0000 (10:46 +0100)
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderMediumTest.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectRepositories.java
sonar-db/src/main/java/org/sonar/db/component/FilePathWithHashDto.java

index 372118eba96bb128f1c69855e392573c0b8e3eea..98497497ad132fe74dd1410dcd5cba9b06a54a05 100644 (file)
@@ -23,22 +23,28 @@ package org.sonar.server.batch;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
+import org.sonar.batch.protocol.input.FileData;
 import org.sonar.batch.protocol.input.ProjectRepositories;
-import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsBatch.WsProjectResponse;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.test.JsonAssert.assertJson;
 
 public class ProjectActionTest {
 
   ProjectDataLoader projectDataLoader = mock(ProjectDataLoader.class);
 
-  WsTester tester;
+  WsActionTester ws;
 
   @Before
   public void setUp() {
-    tester = new WsTester(new BatchWs(mock(BatchIndex.class), new ProjectAction(projectDataLoader)));
+    ws = new WsActionTester(new ProjectAction(projectDataLoader));
   }
 
   @Test
@@ -51,15 +57,35 @@ public class ProjectActionTest {
     ArgumentCaptor<ProjectDataQuery> queryArgumentCaptor = ArgumentCaptor.forClass(ProjectDataQuery.class);
     when(projectDataLoader.load(queryArgumentCaptor.capture())).thenReturn(projectReferentials);
 
-    WsTester.TestRequest request = tester.newGetRequest("batch", "project")
+    TestResponse response = ws.newRequest()
       .setParam("key", projectKey)
       .setParam("profile", "Default")
-      .setParam("preview", "false");
-    request.execute().assertJson("{\"settingsByModule\": {}}");
+      .setParam("preview", "false")
+      .execute();
+    assertJson(response.getInput()).isSimilarTo("{\"settingsByModule\": {}}");
 
     assertThat(queryArgumentCaptor.getValue().getModuleKey()).isEqualTo(projectKey);
     assertThat(queryArgumentCaptor.getValue().getProfileName()).isEqualTo("Default");
     assertThat(queryArgumentCaptor.getValue().isIssuesMode()).isFalse();
   }
 
+  /**
+   * SONAR-7084
+   */
+  @Test
+  public void do_not_fail_when_a_path_is_null() throws Exception {
+    String projectKey = "org.codehaus.sonar:sonar";
+
+    ProjectRepositories projectRepositories = new ProjectRepositories().addFileData("module-1", null, new FileData(null, null));
+    when(projectDataLoader.load(any(ProjectDataQuery.class))).thenReturn(projectRepositories);
+
+    TestResponse result = ws.newRequest()
+      .setMediaType(MediaTypes.PROTOBUF)
+      .setParam("key", projectKey)
+      .setParam("profile", "Default")
+      .execute();
+
+    WsProjectResponse wsProjectResponse = WsProjectResponse.parseFrom(result.getInputStream());
+    assertThat(wsProjectResponse.getFileDataByModuleAndPath()).isEmpty();
+  }
 }
index babb1c093624fada8d80b4388a9c05a34aae13d0..12dce46265c27bc74bd3204131540511c53fe8b1 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.web.UserRole;
 import org.sonar.batch.protocol.input.FileData;
 import org.sonar.batch.protocol.input.ProjectRepositories;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
@@ -40,7 +41,6 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.source.FileSourceDao;
 import org.sonar.db.source.FileSourceDto;
 import org.sonar.db.source.FileSourceDto.Type;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.tester.ServerTester;
@@ -61,13 +61,13 @@ public class ProjectDataLoaderMediumTest {
 
   DbSession dbSession;
 
-  ProjectDataLoader loader;
+  ProjectDataLoader underTest;
 
   @Before
   public void before() {
     tester.clearDbAndIndexes();
     dbSession = tester.get(DbClient.class).openSession(false);
-    loader = tester.get(ProjectDataLoader.class);
+    underTest = tester.get(ProjectDataLoader.class);
   }
 
   @After
@@ -84,19 +84,17 @@ public class ProjectDataLoaderMediumTest {
 
     // Project properties
     tester.get(DbClient.class).propertiesDao().insertProperty(
-      dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId())
-      );
+      dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
     tester.get(DbClient.class).propertiesDao().insertProperty(
-      dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())
-      );
+      dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId()));
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
+
     Map<String, String> projectSettings = ref.settings(project.key());
     assertThat(projectSettings).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
   }
 
   @Test
@@ -108,18 +106,15 @@ public class ProjectDataLoaderMediumTest {
 
     // Project properties
     tester.get(DbClient.class).propertiesDao().insertProperty(
-      dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId())
-      );
+      dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
     tester.get(DbClient.class).propertiesDao().insertProperty(
-      dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())
-      );
+      dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId()));
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
     Map<String, String> projectSettings = ref.settings(project.key());
     assertThat(projectSettings).isEqualTo(ImmutableMap.of(
-      "sonar.jira.project.key", "SONAR"
-      ));
+      "sonar.jira.project.key", "SONAR"));
   }
 
   @Test
@@ -146,16 +141,14 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
     assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-SERVER",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -178,15 +171,13 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
     assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
   }
 
   @Test
@@ -220,21 +211,18 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
     assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-SERVER",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
     assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-SERVER-DAO",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -267,20 +255,17 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
     assertThat(ref.settings(module1.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-SERVER",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
     assertThat(ref.settings(module2.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-APPLICATION",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
   }
 
   @Test
@@ -297,11 +282,10 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
-      "sonar.jira.login.secured", "john"
-      ));
+      "sonar.jira.login.secured", "john"));
   }
 
   @Test
@@ -328,14 +312,13 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
     assertThat(ref.settings(project.key())).isEmpty();
     assertThat(ref.settings(module.key())).isEmpty();
     assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -363,14 +346,13 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
     assertThat(ref.settings(project.key())).isEmpty();
     assertThat(ref.settings(module.key())).isEmpty();
     assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -396,14 +378,13 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
     assertThat(ref.settings(project.key())).isEmpty();
     assertThat(ref.settings(module.key())).isEmpty();
     assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -430,14 +411,13 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
     assertThat(ref.settings(project.key())).isEmpty();
     assertThat(ref.settings(module.key())).isEmpty();
     assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
       "sonar.jira.project.key", "SONAR-SERVER",
       "sonar.jira.login.secured", "john",
-      "sonar.coverage.exclusions", "**/*.java"
-      ));
+      "sonar.coverage.exclusions", "**/*.java"));
   }
 
   @Test
@@ -449,7 +429,7 @@ public class ProjectDataLoaderMediumTest {
     dbSession.commit();
 
     try {
-      loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+      underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
       fail();
     } catch (Exception e) {
       assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(Messages.NO_PERMISSION);
@@ -465,7 +445,7 @@ public class ProjectDataLoaderMediumTest {
     dbSession.commit();
 
     try {
-      loader.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(false));
+      underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(false));
       fail();
     } catch (Exception e) {
       assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(
@@ -487,7 +467,7 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.fileDataByPath(project.key())).hasSize(1);
     FileData fileData = ref.fileData(project.key(), file.path());
     assertThat(fileData.hash()).isEqualTo("123456");
@@ -515,7 +495,7 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
     assertThat(ref.fileData(project.key(), projectFile.path()).hash()).isEqualTo("123456");
     assertThat(ref.fileData(module.key(), moduleFile.path()).hash()).isEqualTo("789456");
   }
@@ -542,7 +522,7 @@ public class ProjectDataLoaderMediumTest {
 
     dbSession.commit();
 
-    ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(module.key()));
+    ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.key()));
     assertThat(ref.fileData(module.key(), moduleFile.path()).hash()).isEqualTo("789456");
     assertThat(ref.fileData(module.key(), moduleFile.path()).revision()).isEqualTo("123456789");
     assertThat(ref.fileData(project.key(), projectFile.path())).isNull();
index 7d87cb0fd1b56186e945fa083d65e07eb05a0473..2c575bbd8c43857247d1414bd5135a6f70af4b95 100644 (file)
@@ -60,7 +60,7 @@ public class ProjectRepositories {
   public boolean exists() {
     return exists;
   }
-  
+
   public Map<String, Map<String, FileData>> fileDataByModuleAndPath() {
     return fileDataByModuleAndPath;
   }
@@ -69,7 +69,11 @@ public class ProjectRepositories {
     return fileDataByModuleAndPath.containsKey(moduleKey) ? fileDataByModuleAndPath.get(moduleKey) : Collections.<String, FileData>emptyMap();
   }
 
-  public ProjectRepositories addFileData(String moduleKey, String path, FileData fileData) {
+  public ProjectRepositories addFileData(String moduleKey, @Nullable String path, FileData fileData) {
+    if (path == null || (fileData.hash() == null && fileData.revision() == null)) {
+      return this;
+    }
+
     Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey);
     if (existingFileDataByPath == null) {
       existingFileDataByPath = new HashMap<>();
index 08f4394c1c534b064d2d094144445f12f4f8eba3..e07380d93270195aa740055e81649ff65af94586 100644 (file)
@@ -20,6 +20,8 @@
 
 package org.sonar.db.component;
 
+import javax.annotation.CheckForNull;
+
 public class FilePathWithHashDto {
 
   private String uuid;
@@ -44,6 +46,7 @@ public class FilePathWithHashDto {
     this.moduleUuid = moduleUuid;
   }
 
+  @CheckForNull
   public String getPath() {
     return path;
   }