Skip to content

Commit

Permalink
SLVS-1718 CaYC: refactor, reformat and cleanup code in touched classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource committed Dec 18, 2024
1 parent 31a5752 commit 6c083cc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.UI.ManageBinding;
public class ManageBindingViewModelTests
{
private const string ALocalProjectKey = "local-project-key";
private readonly ServerProject serverProject = new ("a-project", "A Project");
private readonly ConnectionInfo sonarQubeConnectionInfo = new ("http://localhost:9000", ConnectionServerType.SonarQube);
private readonly ConnectionInfo sonarCloudConnectionInfo = new ("organization", ConnectionServerType.SonarCloud);
private readonly BasicAuthCredentials validCredentials = new ("TOKEN", new SecureString());

private readonly ServerProject serverProject = new("a-project", "A Project");
private readonly ConnectionInfo sonarQubeConnectionInfo = new("http://localhost:9000", ConnectionServerType.SonarQube);
private readonly ConnectionInfo sonarCloudConnectionInfo = new("organization", ConnectionServerType.SonarCloud);
private readonly BasicAuthCredentials validCredentials = new("TOKEN", new SecureString());
private readonly SharedBindingConfigModel sonarQubeSharedBindingConfigModel = new() { Uri = new Uri("http://localhost:9000"), ProjectKey = "myProj" };
private readonly SharedBindingConfigModel sonarCloudSharedBindingConfigModel = new() { Organization = "myOrg", ProjectKey = "myProj" };

Expand Down Expand Up @@ -552,7 +552,7 @@ await progressReporterViewModel.Received(1)
x.WarningText == UiResources.LoadingConnectionsFailedText &&
x.AfterProgressUpdated == testSubject.OnProgressUpdated));
}

[TestMethod]
public async Task InitializeDataAsync_DisplaysBindStatusAndReportsProgress()
{
Expand All @@ -566,12 +566,12 @@ await progressReporterViewModel.Received(1)
x.WarningText == UiResources.FetchingBindingStatusFailedText &&
x.AfterProgressUpdated == testSubject.OnProgressUpdated));
}

[TestMethod]
public async Task InitializeDataAsync_WhenStandalone_ChecksForSharedBindingAndReportsProgress()
{
SetupUnboundProject();

await testSubject.InitializeDataAsync();

await progressReporterViewModel.Received(1)
Expand All @@ -582,12 +582,12 @@ await progressReporterViewModel.Received(1)
x.WarningText == UiResources.CheckingForSharedBindingFailedText &&
x.AfterProgressUpdated == testSubject.OnProgressUpdated));
}

[TestMethod]
public async Task InitializeDataAsync_WhenBound_DoesNotChecksForSharedBindingAndReportsProgress()
{
testSubject.BoundProject = serverProject;

await testSubject.InitializeDataAsync();

await progressReporterViewModel.DidNotReceive()
Expand All @@ -603,20 +603,20 @@ await progressReporterViewModel.DidNotReceive()
public async Task DisplayBindStatusAsync_WhenProjectIsNotBound_Succeeds()
{
SetupUnboundProject();

var response = await testSubject.DisplayBindStatusAsync();

response.Should().BeEquivalentTo(new AdapterResponse(true));
}

[TestMethod]
public async Task DisplayBindStatusAsync_WhenProjectIsBoundAndBindingStatusIsFetched_Succeeds()
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupBoundProject(sonarCloudConnection, serverProject);

var response = await testSubject.DisplayBindStatusAsync();

testSubject.BoundProject.Should().NotBeNull();
response.Should().BeEquivalentTo(new AdapterResponse(true));
}
Expand All @@ -626,9 +626,9 @@ public async Task DisplayBindStatusAsync_WhenProjectIsBoundButBindingStatusIsNot
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupBoundProjectThatDoesNotExistOnServer(sonarCloudConnection);

var response = await testSubject.DisplayBindStatusAsync();

testSubject.BoundProject.Should().BeNull();
response.Should().BeEquivalentTo(new AdapterResponse(false));
}
Expand All @@ -638,31 +638,31 @@ public async Task DisplayBindStatusAsync_WhenSolutionIsOpen_FetchesSolutionInfo(
{
solutionInfoProvider.GetSolutionNameAsync().Returns("Local solution name");
solutionInfoProvider.IsFolderWorkspaceAsync().Returns(false);

await testSubject.DisplayBindStatusAsync();

testSubject.SolutionInfo.Should().BeEquivalentTo(new SolutionInfoModel("Local solution name", SolutionType.Solution));
}

[TestMethod]
public async Task DisplayBindStatusAsync_WhenFolderIsOpen_FetchesSolutionInfo()
{
solutionInfoProvider.GetSolutionNameAsync().Returns("Local folder name");
solutionInfoProvider.IsFolderWorkspaceAsync().Returns(true);

await testSubject.DisplayBindStatusAsync();

testSubject.SolutionInfo.Should().BeEquivalentTo(new SolutionInfoModel("Local folder name", SolutionType.Folder));
}

[TestMethod]
public async Task DisplayBindStatusAsync_WhenProjectIsBoundToSonarCloud_SelectsBoundSonarCloudConnection()
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupBoundProject(sonarCloudConnection);

await testSubject.DisplayBindStatusAsync();

testSubject.SelectedConnectionInfo.Should().BeEquivalentTo(new ConnectionInfo("organization", ConnectionServerType.SonarCloud));
}

Expand All @@ -671,17 +671,17 @@ public async Task DisplayBindStatusAsync_WhenProjectIsBoundToSonarQube_SelectsBo
{
var sonarQubeConnection = new ServerConnection.SonarQube(new Uri("http://localhost:9000/"), credentials: validCredentials);
SetupBoundProject(sonarQubeConnection);

await testSubject.DisplayBindStatusAsync();

testSubject.SelectedConnectionInfo.Should().BeEquivalentTo(new ConnectionInfo("http://localhost:9000/", ConnectionServerType.SonarQube));
}

[TestMethod]
public async Task DisplayBindStatusAsync_WhenProjectIsNotBound_SelectedConnectionShouldBeEmpty()
{
SetupUnboundProject();

await testSubject.DisplayBindStatusAsync();

testSubject.SelectedConnectionInfo.Should().BeNull();
Expand All @@ -693,9 +693,9 @@ public async Task DisplayBindStatusAsync_WhenProjectIsBound_SelectsServerProject
var expectedServerProject = new ServerProject("server-project-key", "server-project-name");
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupBoundProject(sonarCloudConnection, expectedServerProject);

await testSubject.DisplayBindStatusAsync();

testSubject.SelectedProject.Should().BeEquivalentTo(expectedServerProject);
testSubject.BoundProject.Should().BeEquivalentTo(testSubject.SelectedProject);
}
Expand All @@ -705,9 +705,9 @@ public async Task DisplayBindStatusAsync_WhenProjectIsBoundButProjectNotFoundOnS
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupBoundProjectThatDoesNotExistOnServer(sonarCloudConnection);

await testSubject.DisplayBindStatusAsync();

testSubject.SelectedProject.Should().BeNull();
testSubject.BoundProject.Should().BeNull();
}
Expand Down Expand Up @@ -787,21 +787,21 @@ public async Task BindAsync_WhenConnectionNotFound_Fails()
callInfo[1] = null;
return false;
});

var response = await testSubject.BindAsync();

response.Success.Should().BeFalse();
}

[TestMethod]
public async Task BindAsync_WhenBindingFailsUnexpectedly_FailsAndLogs()
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupConnectionAndProjectToBind(sonarCloudConnection, serverProject);
bindingController.BindAsync(Arg.Any<BoundServerProject>(), Arg.Any<CancellationToken>()).ThrowsAsync(new Exception("Failed unexpectedly"));

var response = await testSubject.BindAsync();

response.Success.Should().BeFalse();
logger.Received(1).WriteLine(Resources.Binding_Fails, "Failed unexpectedly");
}
Expand All @@ -813,7 +813,7 @@ public async Task BindAsync_WhenBindingCompletesSuccessfully_Succeeds()
SetupConnectionAndProjectToBind(sonarCloudConnection, serverProject);

var response = await testSubject.BindAsync();

response.Success.Should().BeTrue();
}

Expand All @@ -822,9 +822,9 @@ public async Task BindAsync_WhenBindingCompletesSuccessfully_SetsBoundProjectToS
{
var sonarCloudConnection = new ServerConnection.SonarCloud("organization", credentials: validCredentials);
SetupConnectionAndProjectToBind(sonarCloudConnection, serverProject);

await testSubject.BindAsync();

testSubject.BoundProject.Should().BeEquivalentTo(serverProject);
}

Expand All @@ -839,7 +839,7 @@ public async Task CheckForSharedBindingAsync_WhenSharedBindingExists_SetsSharedB
sharedBindingConfigProvider.Received(1).GetSharedBinding();
testSubject.SharedBindingConfigModel.Should().Be(sharedBindingModel);
}

[TestMethod]
public async Task CheckForSharedBindingAsync_WhenSharedBindingDoesNotExist_SetsNullSharedBindingConfigModel()
{
Expand Down Expand Up @@ -879,7 +879,7 @@ public async Task UseSharedBindingAsync_SharedBindingForSonarQubeConnection_Bind
response.Success.Should().BeTrue();
await bindingController.Received(1)
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
}

[TestMethod]
Expand Down Expand Up @@ -918,7 +918,7 @@ public async Task UseSharedBindingAsync_SharedBindingForExistingSonarCloudConnec
{
testSubject.SelectedProject = serverProject; // this is to make sure the SelectedProject is ignored and the shared config is used instead
testSubject.SharedBindingConfigModel = sonarCloudSharedBindingConfigModel;
var expectedServerConnection = new ServerConnection.SonarCloud(testSubject.SharedBindingConfigModel.Organization);
var expectedServerConnection = new ServerConnection.SonarCloud(testSubject.SharedBindingConfigModel.Organization);
SetupBoundProject(expectedServerConnection);

var response = await testSubject.UseSharedBindingAsync();
Expand All @@ -938,7 +938,8 @@ public async Task UseSharedBindingAsync_SharedBindingForNonExistingSonarQubeConn
var response = await testSubject.UseSharedBindingAsync();

response.Success.Should().BeFalse();
messageBox.Received(1).Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
messageBox.Received(1).Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK,
MessageBoxImage.Warning);
logger.WriteLine(Resources.UseSharedBinding_ConnectionNotFound, testSubject.SharedBindingConfigModel.Uri);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
Expand All @@ -955,7 +956,8 @@ public async Task UseSharedBindingAsync_SharedBindingForNonExistingSonarCloudCon

response.Success.Should().BeFalse();
logger.WriteLine(Resources.UseSharedBinding_ConnectionNotFound, testSubject.SharedBindingConfigModel.Organization);
messageBox.Received(1).Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
messageBox.Received(1).Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK,
MessageBoxImage.Warning);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
Expand All @@ -973,13 +975,13 @@ public async Task UseSharedBindingAsync_SharedBindingSonarCloudConnectionWithMis

response.Success.Should().BeFalse();
logger.WriteLine(Resources.UseSharedBinding_CredentiasNotFound, testSubject.SharedBindingConfigModel.Organization);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK,
MessageBoxImage.Warning);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
}


[TestMethod]
public async Task UseSharedBindingAsync_SharedBindingSonarQubeConnectionWithMissingCredentials_ReturnsFalseAndLogsAndInformsUser()
{
Expand All @@ -992,7 +994,8 @@ public async Task UseSharedBindingAsync_SharedBindingSonarQubeConnectionWithMiss

response.Success.Should().BeFalse();
logger.WriteLine(Resources.UseSharedBinding_CredentiasNotFound, testSubject.SharedBindingConfigModel.Uri);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK,
MessageBoxImage.Warning);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
Expand All @@ -1012,7 +1015,6 @@ public async Task UseSharedBindingAsync_BindingFails_ReturnsFalse()
response.Success.Should().BeFalse();
}


private void MockServices()
{
serverConnectionsRepositoryAdapter = Substitute.For<IServerConnectionsRepositoryAdapter>();
Expand Down Expand Up @@ -1043,26 +1045,26 @@ private void MockTryGetAllConnectionsInfo(List<ConnectionInfo> connectionInfos)
return true;
});
}

private void SetupConnectionAndProjectToBind(ServerConnection selectedServerConnection, ServerProject selectedServerProject)
{
SetupBoundProject(selectedServerConnection, selectedServerProject);
testSubject.SelectedConnectionInfo = sonarCloudConnectionInfo;
testSubject.SelectedProject = selectedServerProject;
}

private void SetupBoundProject(ServerConnection serverConnection, ServerProject expectedServerProject = null)
{
expectedServerProject ??= serverProject;

serverConnection.Credentials = validCredentials;
var boundServerProject = new BoundServerProject(ALocalProjectKey, expectedServerProject.Key, serverConnection);
var configurationProvider = Substitute.For<IConfigurationProvider>();
configurationProvider.GetConfiguration().Returns(new BindingConfiguration(boundServerProject, SonarLintMode.Connected, "binding-dir"));
connectedModeServices.ConfigurationProvider.Returns(configurationProvider);
MockTryGetServerConnection(serverConnection);
solutionInfoProvider.GetSolutionNameAsync().Returns(ALocalProjectKey);

MockGetServerProjectByKey(true, expectedServerProject);
}

Expand All @@ -1080,24 +1082,24 @@ private void SetupUnboundProject()
var configurationProvider = Substitute.For<IConfigurationProvider>();
configurationProvider.GetConfiguration().Returns(new BindingConfiguration(null, SonarLintMode.Standalone, null));
connectedModeServices.ConfigurationProvider.Returns(configurationProvider);

MockGetServerProjectByKey(false, null);
}

private void SetupBoundProjectThatDoesNotExistOnServer(ServerConnection serverConnection)
{
var boundServerProject = new BoundServerProject(ALocalProjectKey, "a-server-project", serverConnection);
var configurationProvider = Substitute.For<IConfigurationProvider>();
configurationProvider.GetConfiguration().Returns(new BindingConfiguration(boundServerProject, SonarLintMode.Connected, "binding-dir"));
connectedModeServices.ConfigurationProvider.Returns(configurationProvider);

MockGetServerProjectByKey(false, null);
}

private void MockGetServerProjectByKey(bool success, ServerProject responseData)
{
var slCoreConnectionAdapter = Substitute.For<ISlCoreConnectionAdapter>();
slCoreConnectionAdapter.GetServerProjectByKeyAsync(Arg.Any<ServerConnection>(),Arg.Any<string>())
slCoreConnectionAdapter.GetServerProjectByKeyAsync(Arg.Any<ServerConnection>(), Arg.Any<string>())
.Returns(Task.FromResult(new AdapterResponseWithData<ServerProject>(success, responseData)));
connectedModeServices.SlCoreConnectionAdapter.Returns(slCoreConnectionAdapter);
}
Expand Down
Loading

0 comments on commit 6c083cc

Please sign in to comment.