Skip to content

Commit

Permalink
SLVSCODE-296 Return a result even for "unknown" connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Jun 23, 2022
1 parent 2a58ee8 commit b9251be
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -430,19 +433,18 @@ public void cancelProgress(WorkDoneProgressCancelParams params) {
}

@Override
public CompletableFuture<SonarLintExtendedLanguageClient.ConnectionCheckResult> checkConnection(ConnectionCheckParams params) {
SonarLintLogger.get().debug("Received refresh request for {}", params.getConnectionId());
var config = bindingManager.getServerConfigurationFor(params.getConnectionId());
public CompletableFuture<ConnectionCheckResult> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ public EndpointParamsAndHttpClient getServerConfigurationFor(String connectionId
return serverConnectionSettings.getServerConfiguration();
}


private Optional<ConnectedSonarLintEngine> getOrCreateConnectedEngine(
String connectionId, EndpointParamsAndHttpClient endpointParamsAndHttpClient, boolean autoUpdate, ProgressFacade progress) {
return connectedEngineCacheByConnectionId.computeIfAbsent(connectionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -706,19 +707,25 @@ 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<SonarLintExtendedLanguageClient.ConnectionCheckResult> 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
void testCheckConnectionWithKnownConnection() throws ExecutionException, InterruptedException {
SonarLintExtendedLanguageServer.ConnectionCheckParams testParams = new SonarLintExtendedLanguageServer.ConnectionCheckParams(CONNECTION_ID);
CompletableFuture<SonarLintExtendedLanguageClient.ConnectionCheckResult> 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
Expand All @@ -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())
Expand Down

0 comments on commit b9251be

Please sign in to comment.