Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLCORE-1119 Fix code smells #1207

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -45,9 +46,6 @@
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.test.utils.server.ServerFixture;
import org.sonarsource.sonarlint.core.test.utils.storage.ConfigurationScopeStorageFixture;
import org.sonarsource.sonarlint.core.test.utils.storage.StorageFixture;
import org.jetbrains.annotations.NotNull;
import org.sonarsource.sonarlint.core.SonarCloudActiveEnvironment;
import org.sonarsource.sonarlint.core.rpc.client.ClientJsonRpcLauncher;
Expand Down Expand Up @@ -102,25 +100,32 @@
import org.sonarsource.sonarlint.core.rpc.protocol.common.TokenDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.UsernamePasswordDto;
import org.sonarsource.sonarlint.core.test.utils.plugins.Plugin;
import org.sonarsource.sonarlint.core.test.utils.server.ServerFixture;
import org.sonarsource.sonarlint.core.test.utils.storage.ConfigurationScopeStorageFixture;
import org.sonarsource.sonarlint.core.test.utils.storage.StorageFixture;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.sonarsource.sonarlint.core.test.utils.storage.StorageFixture.newStorage;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.sonarsource.sonarlint.core.telemetry.TelemetrySpringConfig.PROPERTY_TELEMETRY_ENDPOINT;
import static org.sonarsource.sonarlint.core.test.utils.storage.StorageFixture.newStorage;

public class SonarLintBackendFixture {

public static final String USER_AGENT_FOR_TESTS = "SonarLintBackendFixture";

private SonarLintBackendFixture() {
// utility class
}

public static SonarLintBackendBuilder newBackend() {
return newBackend(null);
}

public static SonarLintBackendBuilder newBackend(Consumer<SonarLintTestRpcServer> onBuild) {
public static SonarLintBackendBuilder newBackend(@Nullable Consumer<SonarLintTestRpcServer> onBuild) {
return new SonarLintBackendBuilder(onBuild);
}

Expand All @@ -136,8 +141,8 @@
private final List<ConfigurationScopeStorageFixture.ConfigurationScopeStorageBuilder> configurationScopeStorages = new ArrayList<>();
private final Set<Path> embeddedPluginPaths = new HashSet<>();
private final Map<String, Path> connectedModeEmbeddedPluginPathsByKey = new HashMap<>();
private final Set<Language> enabledLanguages = new HashSet<>();
private final Set<Language> extraEnabledLanguagesInConnectedMode = new HashSet<>();
private final Set<Language> enabledLanguages = EnumSet.noneOf(Language.class);
private final Set<Language> extraEnabledLanguagesInConnectedMode = EnumSet.noneOf(Language.class);
private final Set<String> disabledPluginKeysForAnalysis = new HashSet<>();
private boolean startEmbeddedServer;
private boolean manageSmartNotifications;
Expand All @@ -157,8 +162,6 @@
private boolean isFocusOnNewCode;
private boolean enableDataflowBugDetection;

private Path clientNodeJsPath;
private Path eslintBridgeServerBundlePath;
private String sonarCloudUrl;
private String sonarCloudWebSocketsUrl;
private Duration responseTimeout;
Expand All @@ -169,7 +172,7 @@
private TelemetryMigrationDto telemetryMigration;
private LanguageSpecificRequirements languageSpecificRequirements;

public SonarLintBackendBuilder(Consumer<SonarLintTestRpcServer> onBuild) {
public SonarLintBackendBuilder(@Nullable Consumer<SonarLintTestRpcServer> onBuild) {
this.onBuild = onBuild;
}

Expand Down Expand Up @@ -214,7 +217,7 @@
}

private SonarLintBackendBuilder withSonarQubeConnection(String connectionId, String serverUrl, boolean disableNotifications,
Consumer<StorageFixture.StorageBuilder> storageBuilder) {
@Nullable Consumer<StorageFixture.StorageBuilder> storageBuilder) {
if (storageBuilder != null) {
var storage = newStorage(connectionId);
storageBuilder.accept(storage);
Expand Down Expand Up @@ -243,7 +246,7 @@
}

public SonarLintBackendBuilder withSonarCloudConnection(String connectionId, String organizationKey, boolean disableNotifications,
Consumer<StorageFixture.StorageBuilder> storageBuilder) {
@Nullable Consumer<StorageFixture.StorageBuilder> storageBuilder) {
if (storageBuilder != null) {
var storage = newStorage(connectionId);
storageBuilder.accept(storage);
Expand Down Expand Up @@ -273,7 +276,7 @@
return withUnboundConfigScope(configurationScopeId, name, null);
}

public SonarLintBackendBuilder withUnboundConfigScope(String configurationScopeId, String name, String parentId) {
public SonarLintBackendBuilder withUnboundConfigScope(String configurationScopeId, String name, @Nullable String parentId) {
return withConfigScope(configurationScopeId, name, parentId, new BindingConfigurationDto(null, null, false));
}

Expand All @@ -286,15 +289,15 @@
return withConfigScope(configurationScopeId, configurationScopeId, null, new BindingConfigurationDto(connectionId, projectKey, false), storageBuilder);
}

public SonarLintBackendBuilder withChildConfigScope(String configurationScopeId, String parentScopeId) {
public SonarLintBackendBuilder withChildConfigScope(String configurationScopeId, @Nullable String parentScopeId) {
return withConfigScope(configurationScopeId, configurationScopeId, parentScopeId, new BindingConfigurationDto(null, null, false));
}

public SonarLintBackendBuilder withConfigScope(String configurationScopeId, String name, String parentScopeId, BindingConfigurationDto bindingConfiguration) {
public SonarLintBackendBuilder withConfigScope(String configurationScopeId, String name, @Nullable String parentScopeId, BindingConfigurationDto bindingConfiguration) {
return withConfigScope(configurationScopeId, name, parentScopeId, bindingConfiguration, null);
}

public SonarLintBackendBuilder withConfigScope(String configurationScopeId, String name, String parentScopeId, BindingConfigurationDto bindingConfiguration,
public SonarLintBackendBuilder withConfigScope(String configurationScopeId, String name, @Nullable String parentScopeId, BindingConfigurationDto bindingConfiguration,
@Nullable Consumer<ConfigurationScopeStorageFixture.ConfigurationScopeStorageBuilder> storageBuilder) {
if (storageBuilder != null) {
var builder = ConfigurationScopeStorageFixture.newBuilder(configurationScopeId);
Expand Down Expand Up @@ -410,14 +413,12 @@
return this;
}

public SonarLintBackendBuilder withClientNodeJsPath(Path path) {
clientNodeJsPath = path;
public SonarLintBackendBuilder withClientNodeJsPath(Path clientNodeJsPath) {
languageSpecificRequirements = new LanguageSpecificRequirements(new JsTsRequirementsDto(clientNodeJsPath, null), null);
return this;
}

public SonarLintBackendBuilder withEslintBridgeServerBundlePath(Path path) {
eslintBridgeServerBundlePath = path;
public SonarLintBackendBuilder withEslintBridgeServerBundlePath(Path eslintBridgeServerBundlePath) {
languageSpecificRequirements = new LanguageSpecificRequirements(new JsTsRequirementsDto(null, eslintBridgeServerBundlePath), null);
return this;
}
Expand Down Expand Up @@ -598,7 +599,8 @@
}

public FakeSonarLintRpcClient build() {
return spy(new FakeSonarLintRpcClient(credentialsByConnectionId, printLogsToStdOut, matchedBranchPerScopeId, baseDirsByConfigScope, initialFilesByConfigScope, fileExclusionsByConfigScope));
return spy(new FakeSonarLintRpcClient(credentialsByConnectionId, printLogsToStdOut, matchedBranchPerScopeId, baseDirsByConfigScope, initialFilesByConfigScope,
fileExclusionsByConfigScope));
}

public SonarLintClientBuilder printLogsToStdOut() {
Expand Down Expand Up @@ -643,7 +645,8 @@
Map<String, Boolean> analysisReadinessPerScopeId = new HashMap<>();

public FakeSonarLintRpcClient(Map<String, Either<TokenDto, UsernamePasswordDto>> credentialsByConnectionId, boolean printLogsToStdOut,
Map<String, String> matchedBranchPerScopeId, Map<String, Path> baseDirsByConfigScope, Map<String, List<ClientFileDto>> initialFilesByConfigScope, Map<String, Set<String>> fileExclusionsByConfigScope) {
Map<String, String> matchedBranchPerScopeId, Map<String, Path> baseDirsByConfigScope, Map<String, List<ClientFileDto>> initialFilesByConfigScope,
Map<String, Set<String>> fileExclusionsByConfigScope) {
this.credentialsByConnectionId = credentialsByConnectionId;
this.printLogsToStdOut = printLogsToStdOut;
this.matchedBranchPerScopeId = matchedBranchPerScopeId;
Expand All @@ -669,14 +672,17 @@

@Override
public void showHotspot(String configurationScopeId, HotspotDetailsDto hotspotDetails) {
// no-op
}

@Override
public void showIssue(String configurationScopeId, IssueDetailsDto issueDetails) {
// no-op
}

Check failure on line 681 in test-utils/src/main/java/org/sonarsource/sonarlint/core/test/utils/SonarLintBackendFixture.java

View check run for this annotation

Cirrus CI / test_linux

test-utils/src/main/java/org/sonarsource/sonarlint/core/test/utils/SonarLintBackendFixture.java#L681

mediumtest.issues.OpenIssueInIdeMediumTests.it_should_assist_creating_the_connection_when_server_url_unknown(SonarLintTestHarness)
Raw output
Wanted but not invoked:
fakeSonarLintRpcClient.showIssue(
    "configScopeId",
    <any>
);
-> at org.sonarsource.sonarlint.core.test.utils.SonarLintBackendFixture$FakeSonarLintRpcClient.showIssue(SonarLintBackendFixture.java:681)

However, there were exactly 13 interactions with this mock:
fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.865] [main] INFO org.sonarsource.sonarlint.core.rpc.impl.SonarLintRpcServerImpl - SonarLint backend started, instance=org.sonarsource.sonarlint.core.rpc.impl.SonarLintRpcServerImpl@2765c3fc
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.922] [SonarLint Server RPC sequential executor] INFO sonarlint - Telemetry disabled on server startup
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.927] [SonarLint Server RPC sequential executor] INFO sonarlint - Started embedded server on port 64120
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.93] [SonarLint Server RPC sequential executor] DEBUG sonarlint - Starting local-only issue database from /tmp/slUserHome375515379497417308/xodus-local-only-issue-store17014425921315648697
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.95] [SonarLint Server RPC sequential executor] INFO sonarlint - Monitoring is disabled by feature flag.
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.957] [SonarLint Server RPC sequential executor] ERROR org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because the return value of "org.sonarsource.sonarlint.core.rpc.protocol.client.fs.ListFilesResponse.getFiles()" is null
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1770)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because the return value of "org.sonarsource.sonarlint.core.rpc.protocol.client.fs.ListFilesResponse.getFiles()" is null
	at org.sonarsource.sonarlint.core.fs.ClientFileSystemService.initializeFileSystem(ClientFileSystemService.java:118)
	at org.sonarsource.sonarlint.core.commons.SmartCancelableLoadingCache.lambda$newValueAndScheduleComputation$2(SmartCancelableLoadingCache.java:99)
	at org.sonarsource.sonarlint.core.commons.DebounceComputer.lambda$scheduleComputationAsync$0(DebounceComputer.java:83)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
	... 3 common frames omitted

);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.didChangeAnalysisReadiness(
    [configScopeId],
    true
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$didChangeAnalysisReadiness$34(SonarLintRpcClientImpl.java:372)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.957] [Show Issue or Hotspot Request Handler] DEBUG sonarlint - Assist connection and binding if needed for project projectKey and server http://localhost:35399/
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.assistCreatingConnection(
    org.sonarsource.sonarlint.core.rpc.protocol.client.connection.AssistCreatingConnectionParams@6b4d0be2,
    org.sonarsource.sonarlint.core.rpc.client.SonarLintCancelChecker@3c3f0f2e
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$assistCreatingConnection$16(SonarLintRpcClientImpl.java:236)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.96] [Show Issue or Hotspot Request Handler] DEBUG sonarlint - Waiting for connection creation notification...
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.961] [SonarLint Server RPC sequential executor] DEBUG sonarlint - Binding suggestions computation queued for connection 'connectionId'...
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:06.961] [Binding Suggestion Provider] DEBUG sonarlint - Skipping binding suggestion computation as it is disabled
);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)

fakeSonarLintRpcClient.log(
     [2025-01-13T08:34:07.061] [Show Issue or Hotspot Request Handler] ERROR sonarlint - Unable to show issue
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because the return value of "org.sonarsource.sonarlint.core.rpc.protocol.client.fs.ListFilesResponse.getFiles()" is null
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1770)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because the return value of "org.sonarsource.sonarlint.core.rpc.protocol.client.fs.ListFilesResponse.getFiles()" is null
	at org.sonarsource.sonarlint.core.fs.ClientFileSystemService.initializeFileSystem(ClientFileSystemService.java:118)
	at org.sonarsource.sonarlint.core.commons.SmartCancelableLoadingCache.lambda$newValueAndScheduleComputation$2(SmartCancelableLoadingCache.java:99)
	at org.sonarsource.sonarlint.core.commons.DebounceComputer.lambda$scheduleComputationAsync$0(DebounceComputer.java:83)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
	... 3 more

);
-> at org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientImpl.lambda$log$9(SonarLintRpcClientImpl.java:201)


	at org.sonarsource.sonarlint.core.test.utils.SonarLintBackendFixture$FakeSonarLintRpcClient.showIssue(SonarLintBackendFixture.java:681)
	at mediumtest.issues.OpenIssueInIdeMediumTests.it_should_assist_creating_the_connection_when_server_url_unknown(OpenIssueInIdeMediumTests.java:286)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

@Override
public void showFixSuggestion(String configurationScopeId, String issueKey, FixSuggestionDto fixSuggestion) {
// no-op
}

@Override
Expand Down Expand Up @@ -752,6 +758,7 @@

@Override
public void didReceiveServerHotspotEvent(DidReceiveServerHotspotEvent params) {
// no-op
}

@Override
Expand All @@ -769,11 +776,13 @@

@Override
public void didChangeMatchedSonarProjectBranch(String configScopeId, String newMatchedBranchName) {
// no-op

}

@Override
public void suggestBinding(Map<String, List<BindingSuggestionDto>> suggestionsByConfigScope) {
// no-op

}

Expand All @@ -784,11 +793,13 @@

@Override
public void openUrlInBrowser(URL url) {
// no-op

}

@Override
public void showMessage(MessageType type, String text) {
// no-op

}

Expand Down Expand Up @@ -843,7 +854,8 @@
}

@Override
public void raiseHotspots(String configurationScopeId, Map<URI, List<RaisedHotspotDto>> hotspotsByFileUri, boolean isIntermediatePublication, @org.jetbrains.annotations.Nullable UUID analysisId) {
public void raiseHotspots(String configurationScopeId, Map<URI, List<RaisedHotspotDto>> hotspotsByFileUri, boolean isIntermediatePublication,
@org.jetbrains.annotations.Nullable UUID analysisId) {
raisedHotspotsByScopeId.put(configurationScopeId, hotspotsByFileUri);
}

Expand Down
Loading