Skip to content

Commit

Permalink
Fix pulling logic for new enabled languages
Browse files Browse the repository at this point in the history
  • Loading branch information
eray-felek-sonarsource committed Jul 31, 2023
1 parent 5337b11 commit f8d2a47
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ServerUpdaterUtils {
*/
public static Optional<Instant> computeLastSync(Set<Language> enabledLanguages, Optional<Instant> lastSync,
Set<Language> lastEnabledLanguages) {
if (!lastEnabledLanguages.isEmpty() && (!lastEnabledLanguages.equals(enabledLanguages))) {
if (lastEnabledLanguages.isEmpty() || (!lastEnabledLanguages.equals(enabledLanguages))) {
lastSync = Optional.empty();
}
return lastSync;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Language>();
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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,14 +114,32 @@ void update_project_issues_with_pull_using_last_sync() {
List<ServerIssue> 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);

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<ServerIssue> issues = Collections.singletonList(issue);
var queryTimestamp = Instant.now();
var lastSync = Optional.of(Instant.ofEpochMilli(123456789));
var lastIssueEnabledLanguages = new HashSet<Language>();
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();
Expand Down Expand Up @@ -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<ServerTaintIssue> issues = Collections.singletonList(issue);
var queryTimestamp = Instant.now();
var lastSync = Optional.of(Instant.ofEpochMilli(123456789));
var lastIssueEnabledLanguages = new HashSet<Language>();
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();
Expand Down

0 comments on commit f8d2a47

Please sign in to comment.