From: Teryk Bellahsene Date: Mon, 30 Nov 2015 15:51:26 +0000 (+0100) Subject: SONAR-7084 Analysis do not fail when a component do not have a path X-Git-Tag: 5.3-RC1~110 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f463897daea037addd0ee5d1a45c046b784e1fc5;p=sonarqube.git SONAR-7084 Analysis do not fail when a component do not have a path --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java index 372118eba96..98497497ad1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectActionTest.java @@ -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 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(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderMediumTest.java index babb1c09362..12dce46265c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/ProjectDataLoaderMediumTest.java @@ -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 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 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(); diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectRepositories.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectRepositories.java index 7d87cb0fd1b..2c575bbd8c4 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectRepositories.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectRepositories.java @@ -60,7 +60,7 @@ public class ProjectRepositories { public boolean exists() { return exists; } - + public Map> fileDataByModuleAndPath() { return fileDataByModuleAndPath; } @@ -69,7 +69,11 @@ public class ProjectRepositories { return fileDataByModuleAndPath.containsKey(moduleKey) ? fileDataByModuleAndPath.get(moduleKey) : Collections.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 existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey); if (existingFileDataByPath == null) { existingFileDataByPath = new HashMap<>(); diff --git a/sonar-db/src/main/java/org/sonar/db/component/FilePathWithHashDto.java b/sonar-db/src/main/java/org/sonar/db/component/FilePathWithHashDto.java index 08f4394c1c5..e07380d9327 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/FilePathWithHashDto.java +++ b/sonar-db/src/main/java/org/sonar/db/component/FilePathWithHashDto.java @@ -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; }