From b9251bedd3d41c6e4de2e946fc0f8d14b5bf04c3 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Thu, 23 Jun 2022 14:26:25 +0200 Subject: [PATCH] SLVSCODE-296 Return a result even for "unknown" connections --- .../ls/SonarLintExtendedLanguageClient.java | 1 - .../ls/SonarLintExtendedLanguageServer.java | 13 +------------ .../sonarlint/ls/SonarLintLanguageServer.java | 18 ++++++++++-------- .../ls/connected/ProjectBindingManager.java | 1 - .../mediumtests/LanguageServerMediumTests.java | 18 ++++++++++++------ 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageClient.java b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageClient.java index 1397ce03e..93d6c5d36 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageClient.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageClient.java @@ -24,7 +24,6 @@ import java.util.Collection; import java.util.Objects; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; diff --git a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageServer.java b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageServer.java index 6403439ab..b46357997 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageServer.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintExtendedLanguageServer.java @@ -172,17 +172,6 @@ public void setFolderUri(String folderUri) { @JsonNotification("sonarlint/didLocalBranchNameChange") void didLocalBranchNameChange(DidLocalBranchNameChangeParams params); - class TokenUpdateParams { - private String serverId; - private String token; - - public TokenUpdateParams(String serverId, String token) { - this.serverId = serverId; - this.token = token; - } - - } - @JsonNotification("sonarlint/onTokenUpdate") - void onTokenUpdate(TokenUpdateParams params); + void onTokenUpdate(); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java index 170c33cf9..95a783dfc 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java @@ -63,6 +63,7 @@ import org.eclipse.lsp4j.services.TextDocumentService; import org.eclipse.lsp4j.services.WorkspaceService; import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; +import org.sonarsource.sonarlint.ls.SonarLintExtendedLanguageClient.ConnectionCheckResult; import org.sonarsource.sonarlint.ls.connected.ProjectBindingManager; import org.sonarsource.sonarlint.ls.connected.SecurityHotspotsHandlerServer; import org.sonarsource.sonarlint.ls.connected.TaintVulnerabilitiesCache; @@ -88,6 +89,8 @@ import static java.net.URI.create; import static java.util.Optional.ofNullable; +import static org.sonarsource.sonarlint.ls.SonarLintExtendedLanguageClient.ConnectionCheckResult.failure; +import static org.sonarsource.sonarlint.ls.SonarLintExtendedLanguageClient.ConnectionCheckResult.success; public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, WorkspaceService, TextDocumentService { @@ -430,19 +433,18 @@ public void cancelProgress(WorkDoneProgressCancelParams params) { } @Override - public CompletableFuture checkConnection(ConnectionCheckParams params) { - SonarLintLogger.get().debug("Received refresh request for {}", params.getConnectionId()); - var config = bindingManager.getServerConfigurationFor(params.getConnectionId()); + public CompletableFuture checkConnection(ConnectionCheckParams params) { + String connectionId = params.getConnectionId(); + SonarLintLogger.get().debug("Received refresh request for {}", connectionId); + var config = bindingManager.getServerConfigurationFor(connectionId); if(config != null){ - return config.validateConnection().thenApply(validationResult -> validationResult.success() ? - SonarLintExtendedLanguageClient.ConnectionCheckResult.success(params.getConnectionId()) : - SonarLintExtendedLanguageClient.ConnectionCheckResult.failure(params.getConnectionId(), validationResult.message())); + return config.validateConnection().thenApply(validationResult -> validationResult.success() ? success(connectionId) : failure(connectionId, validationResult.message())); } - return CompletableFuture.completedFuture(null); + return CompletableFuture.completedFuture(failure(connectionId, String.format("Connection '%s' is unknown", connectionId))); } @Override - public void onTokenUpdate(TokenUpdateParams params) { + public void onTokenUpdate() { SonarLintLogger.get().info("Updating configuration on token change."); didChangeConfiguration(new DidChangeConfigurationParams()); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/connected/ProjectBindingManager.java b/src/main/java/org/sonarsource/sonarlint/ls/connected/ProjectBindingManager.java index 54129b1cb..b2815e646 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/connected/ProjectBindingManager.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/connected/ProjectBindingManager.java @@ -234,7 +234,6 @@ public EndpointParamsAndHttpClient getServerConfigurationFor(String connectionId return serverConnectionSettings.getServerConfiguration(); } - private Optional getOrCreateConnectedEngine( String connectionId, EndpointParamsAndHttpClient endpointParamsAndHttpClient, boolean autoUpdate, ProgressFacade progress) { return connectedEngineCacheByConnectionId.computeIfAbsent(connectionId, diff --git a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java index f37df42ca..cc85aee82 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java @@ -99,6 +99,7 @@ void prepare() { @BeforeEach public void mockSonarQube() { mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"9.3\", \"id\": \"xzy\"}"); + mockWebServerExtension.addStringResponse("/api/authentication/validate?format=json", "{\"valid\": true}"); } @Test @@ -706,10 +707,14 @@ void updateBranchNameWithNullBranchShouldLogAnotherMessage() { @Test void testCheckConnectionWithUnknownConnection() throws ExecutionException, InterruptedException { - SonarLintExtendedLanguageServer.ConnectionCheckParams testParams = new SonarLintExtendedLanguageServer.ConnectionCheckParams("unknown"); + String unknownConnectionId = "unknown"; + SonarLintExtendedLanguageServer.ConnectionCheckParams testParams = new SonarLintExtendedLanguageServer.ConnectionCheckParams(unknownConnectionId); CompletableFuture result = lsProxy.checkConnection(testParams); - assertThat(result.get()).isNull(); + SonarLintExtendedLanguageClient.ConnectionCheckResult actual = result.get(); + assertThat(actual).isNotNull(); + assertThat(actual.getConnectionId()).isEqualTo(unknownConnectionId); + assertThat(actual.getReason()).isEqualTo("Connection 'unknown' is unknown"); } @Test @@ -717,8 +722,10 @@ void testCheckConnectionWithKnownConnection() throws ExecutionException, Interru SonarLintExtendedLanguageServer.ConnectionCheckParams testParams = new SonarLintExtendedLanguageServer.ConnectionCheckParams(CONNECTION_ID); CompletableFuture result = lsProxy.checkConnection(testParams); - assertThat(result.get()).isNotNull(); - assertThat(result.get().getConnectionId()).isEqualTo(CONNECTION_ID); + SonarLintExtendedLanguageClient.ConnectionCheckResult actual = result.get(); + assertThat(actual).isNotNull(); + assertThat(actual.getConnectionId()).isEqualTo(CONNECTION_ID); + assertThat(actual.isSuccess()).isTrue(); } @Test @@ -733,8 +740,7 @@ void testSetConnectionIdInCheckConnectionParams() { @Test void shouldUpdateConfigurationOnTokenChange() { - var params = new SonarLintExtendedLanguageServer.TokenUpdateParams("foo", "bar"); - lsProxy.onTokenUpdate(params); + lsProxy.onTokenUpdate(); awaitUntilAsserted(() -> assertThat(client.logs) .extracting(withoutTimestamp())