diff --git a/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/ServerUpdaterUtils.java b/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/ServerUpdaterUtils.java index 6637b122dd..b054bdc9a7 100644 --- a/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/ServerUpdaterUtils.java +++ b/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/ServerUpdaterUtils.java @@ -31,7 +31,7 @@ public class ServerUpdaterUtils { */ public static Optional computeLastSync(Set enabledLanguages, Optional lastSync, Set lastEnabledLanguages) { - if (!lastEnabledLanguages.isEmpty() && (!lastEnabledLanguages.equals(enabledLanguages))) { + if (lastEnabledLanguages.isEmpty() || (!lastEnabledLanguages.equals(enabledLanguages))) { lastSync = Optional.empty(); } return lastSync; diff --git a/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerHotspotUpdaterTest.java b/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerHotspotUpdaterTest.java index fd0764901e..17aeb1702e 100644 --- a/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerHotspotUpdaterTest.java +++ b/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerHotspotUpdaterTest.java @@ -20,6 +20,7 @@ package org.sonarsource.sonarlint.core.serverconnection; import java.time.Instant; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -186,4 +187,25 @@ void update_hotspots_with_pull_when_enabled_language_changed() { assertThat(capturedHotspot.getKey()).isEqualTo(hotspotKey); verify(hotspotDownloader).downloadFromPull(eq(hotspotApi), eq(projectBinding.projectKey()), eq("branch"), eq(Optional.empty())); } + + @Test + void update_hotspots_with_pull_when_last_enabled_language_were_not_there() { + var timestamp = Instant.ofEpochMilli(123456789L); + var lastHotspotEnabledLanguages = new HashSet(); + var hotspotKey = "hotspotKey"; + var hotspots = List.of(aServerHotspot(hotspotKey)); + when(hotspotDownloader.downloadFromPull(hotspotApi, PROJECT_KEY, "branch", Optional.empty())) + .thenReturn(new HotspotDownloader.PullResult(timestamp, hotspots, Set.of())); + when(issueStore.getLastHotspotEnabledLanguages("branch")).thenReturn(lastHotspotEnabledLanguages); + when(issueStore.getLastHotspotSyncTimestamp("branch")).thenReturn(Optional.of(timestamp)); + + updater.sync(hotspotApi, PROJECT_KEY, "branch", Set.of(Language.C, Language.GO)); + + var hotspotCaptor = ArgumentCaptor.forClass(List.class); + verify(issueStore).mergeHotspots(eq("branch"), hotspotCaptor.capture(), eq(Set.of()), eq(timestamp), anySet()); + assertThat(hotspotCaptor.getValue()).hasSize(1); + var capturedHotspot = (ServerHotspot) (hotspotCaptor.getValue().get(0)); + assertThat(capturedHotspot.getKey()).isEqualTo(hotspotKey); + verify(hotspotDownloader).downloadFromPull(eq(hotspotApi), eq(projectBinding.projectKey()), eq("branch"), eq(Optional.empty())); + } } diff --git a/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerIssueUpdaterTest.java b/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerIssueUpdaterTest.java index c7963e9611..1875fdc80b 100644 --- a/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerIssueUpdaterTest.java +++ b/server-connection/src/test/java/org/sonarsource/sonarlint/core/serverconnection/ServerIssueUpdaterTest.java @@ -21,9 +21,11 @@ import java.time.Instant; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import kotlin.collections.EmptySet; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.sonarsource.sonarlint.core.commons.Language; @@ -112,7 +114,10 @@ void update_project_issues_with_pull_using_last_sync() { List issues = Collections.singletonList(issue); var queryTimestamp = Instant.now(); var lastSync = Optional.of(Instant.ofEpochMilli(123456789)); + var lastIssueEnabledLanguages = Set.of(Language.C, Language.GO); + when(issueStore.getLastIssueEnabledLanguages("master")).thenReturn(lastIssueEnabledLanguages); when(issueStore.getLastIssueSyncTimestamp("master")).thenReturn(lastSync); + when(downloader.getEnabledLanguages()).thenReturn(Set.of(Language.C, Language.GO)); when(downloader.downloadFromPull(serverApi, projectBinding.projectKey(), "master", lastSync)).thenReturn(new IssueDownloader.PullResult(queryTimestamp, issues, Set.of())); updater.update(serverApi, projectBinding.projectKey(), "master", false, IssueApi.MIN_SQ_VERSION_SUPPORTING_PULL); @@ -120,6 +125,21 @@ void update_project_issues_with_pull_using_last_sync() { verify(issueStore).mergeIssues(eq("master"), anyList(), anySet(), eq(queryTimestamp), anySet()); } + @Test + void update_project_issues_with_pull_when_there_were_no_enabled_languages() { + var issue = aServerIssue(); + List issues = Collections.singletonList(issue); + var queryTimestamp = Instant.now(); + var lastSync = Optional.of(Instant.ofEpochMilli(123456789)); + var lastIssueEnabledLanguages = new HashSet(); + when(issueStore.getLastIssueSyncTimestamp("master")).thenReturn(lastSync); + when(issueStore.getLastIssueEnabledLanguages("master")).thenReturn(lastIssueEnabledLanguages); + when(downloader.getEnabledLanguages()).thenReturn(Set.of(Language.C)); + when(downloader.downloadFromPull(serverApi, projectBinding.projectKey(), "master", Optional.empty())).thenReturn(new IssueDownloader.PullResult(queryTimestamp, issues, Set.of())); + updater.update(serverApi, projectBinding.projectKey(), "master", false, IssueApi.MIN_SQ_VERSION_SUPPORTING_PULL); + verify(downloader).downloadFromPull(eq(serverApi), eq(projectBinding.projectKey()), eq("master"), eq(Optional.empty())); + } + @Test void update_project_issues_with_pull_when_enabled_language_changed() { var issue = aServerIssue(); @@ -150,6 +170,21 @@ void update_project_issues_with_pull_when_enabled_language_not_changed() { verify(downloader).downloadFromPull(eq(serverApi), eq(projectBinding.projectKey()), eq("master"), eq(lastSync)); } + @Test + void update_project_taints_with_pull_when_there_were_no_enabled_languages() { + var issue = aServerTaintIssue(); + List issues = Collections.singletonList(issue); + var queryTimestamp = Instant.now(); + var lastSync = Optional.of(Instant.ofEpochMilli(123456789)); + var lastIssueEnabledLanguages = new HashSet(); + when(issueStore.getLastTaintSyncTimestamp("master")).thenReturn(lastSync); + when(issueStore.getLastTaintEnabledLanguages("master")).thenReturn(lastIssueEnabledLanguages); + when(taintDownloader.downloadTaintFromPull(serverApi, projectBinding.projectKey(), "master", Optional.empty())).thenReturn(new TaintIssueDownloader.PullTaintResult(queryTimestamp, issues, Set.of())); + + updater.syncTaints(serverApi, projectBinding.projectKey(), "master", Set.of(Language.C)); + verify(taintDownloader).downloadTaintFromPull(eq(serverApi), eq(projectBinding.projectKey()), eq("master"), eq(Optional.empty())); + } + @Test void update_project_taints_with_pull_when_enabled_language_changed() { var issue = aServerTaintIssue();