Skip to content

Commit

Permalink
SLVS-1448 Prevent deleting connection when in use (#5701)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource committed Sep 27, 2024
1 parent 289c4e6 commit f3b8a61
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 47 deletions.
34 changes: 17 additions & 17 deletions src/ConnectedMode.UnitTests/SlCoreConnectionAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests;
public class SlCoreConnectionAdapterTests
{
private static readonly BasicAuthCredentials ValidToken = new ("I_AM_JUST_A_TOKEN", new SecureString());
private static readonly ServerConnection.SonarQube SonarQubeConnection = new(new Uri("http://localhost:9000/"), new ServerConnectionSettings(true), ValidToken);
private static readonly ServerConnection.SonarCloud SonarCloudConnection = new("myOrg", new ServerConnectionSettings(true), ValidToken);
private readonly ServerConnection.SonarQube sonarQubeConnection = new(new Uri("http://localhost:9000/"), new ServerConnectionSettings(true), ValidToken);
private readonly ServerConnection.SonarCloud sonarCloudConnection = new("myOrg", new ServerConnectionSettings(true), ValidToken);

private SlCoreConnectionAdapter testSubject;
private ISLCoreServiceProvider slCoreServiceProvider;
Expand Down Expand Up @@ -258,7 +258,7 @@ public async Task GetAllProjectsAsync_SwitchesToBackgroundThread()
var threadHandlingMock = Substitute.For<IThreadHandling>();
var slCoreConnectionAdapter = new SlCoreConnectionAdapter(slCoreServiceProvider, threadHandlingMock, logger);

await slCoreConnectionAdapter.GetAllProjectsAsync(SonarQubeConnection);
await slCoreConnectionAdapter.GetAllProjectsAsync(sonarQubeConnection);

await threadHandlingMock.Received(1).RunOnBackgroundThread(Arg.Any<Func<Task<AdapterResponseWithData<List<ServerProject>>>>>());
}
Expand All @@ -268,7 +268,7 @@ public async Task GetAllProjectsAsync_GettingConnectionConfigurationSLCoreServic
{
slCoreServiceProvider.TryGetTransientService(out IConnectionConfigurationSLCoreService _).Returns(false);

var response = await testSubject.GetAllProjectsAsync(SonarQubeConnection);
var response = await testSubject.GetAllProjectsAsync(sonarQubeConnection);

logger.Received(1).LogVerbose($"[{nameof(IConnectionConfigurationSLCoreService)}] {SLCoreStrings.ServiceProviderNotInitialized}");
response.Success.Should().BeFalse();
Expand All @@ -277,7 +277,7 @@ public async Task GetAllProjectsAsync_GettingConnectionConfigurationSLCoreServic
[TestMethod]
public async Task GetAllProjectsAsync_ConnectionToSonarQubeWithToken_CallsGetAllProjectsAsyncWithCorrectParams()
{
await testSubject.GetAllProjectsAsync(SonarQubeConnection);
await testSubject.GetAllProjectsAsync(sonarQubeConnection);

await connectionConfigurationSlCoreService.Received(1)
.GetAllProjectsAsync(Arg.Is<GetAllProjectsParams>(x => IsExpectedSonarQubeConnectionParams(x.transientConnection, ValidToken.UserName)));
Expand All @@ -288,9 +288,9 @@ public async Task GetAllProjectsAsync_ConnectionToSonarQubeWithCredentials_Calls
{
const string username = "username";
const string password = "password";
SonarQubeConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());
sonarQubeConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());

await testSubject.GetAllProjectsAsync(SonarQubeConnection);
await testSubject.GetAllProjectsAsync(sonarQubeConnection);

await connectionConfigurationSlCoreService.Received(1)
.GetAllProjectsAsync(Arg.Is<GetAllProjectsParams>(x => IsExpectedSonarQubeConnectionParams(x.transientConnection, username, password)));
Expand All @@ -299,7 +299,7 @@ await connectionConfigurationSlCoreService.Received(1)
[TestMethod]
public async Task GetAllProjectsAsync_ConnectionToSonarCloudWithToken_CallsGetAllProjectsAsyncWithCorrectParams()
{
await testSubject.GetAllProjectsAsync(SonarCloudConnection);
await testSubject.GetAllProjectsAsync(sonarCloudConnection);

await connectionConfigurationSlCoreService.Received(1)
.GetAllProjectsAsync(Arg.Is<GetAllProjectsParams>(x => IsExpectedSonarCloudConnectionParams(x.transientConnection, ValidToken.UserName)));
Expand All @@ -310,9 +310,9 @@ public async Task GetAllProjectsAsync_ConnectionToSonarCloudWithCredentials_Call
{
const string username = "username";
const string password = "password";
SonarCloudConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());
sonarCloudConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());

await testSubject.GetAllProjectsAsync(SonarCloudConnection);
await testSubject.GetAllProjectsAsync(sonarCloudConnection);

await connectionConfigurationSlCoreService.Received(1)
.GetAllProjectsAsync(Arg.Is<GetAllProjectsParams>(x => IsExpectedSonarCloudConnectionParams(x.transientConnection, username, password)));
Expand All @@ -324,7 +324,7 @@ public async Task GetAllProjectsAsync_ReturnsResponseFromSlCore()
List<SonarProjectDto> expectedServerProjects = [CreateSonarProjectDto("projKey1", "projName1"), CreateSonarProjectDto("projKey2", "projName2")];
connectionConfigurationSlCoreService.GetAllProjectsAsync(Arg.Any<GetAllProjectsParams>()).Returns(new GetAllProjectsResponse(expectedServerProjects));

var response = await testSubject.GetAllProjectsAsync(SonarCloudConnection);
var response = await testSubject.GetAllProjectsAsync(sonarCloudConnection);

response.Success.Should().BeTrue();
response.ResponseData.Count.Should().Be(expectedServerProjects.Count);
Expand All @@ -340,7 +340,7 @@ public async Task GetServerProjectByKeyAsync_SwitchesToBackgroundThread()
var threadHandlingMock = Substitute.For<IThreadHandling>();
var slCoreConnectionAdapter = new SlCoreConnectionAdapter(slCoreServiceProvider, threadHandlingMock, logger);

await slCoreConnectionAdapter.GetServerProjectByKeyAsync(SonarCloudConnection, "server-project-key");
await slCoreConnectionAdapter.GetServerProjectByKeyAsync(sonarCloudConnection, "server-project-key");

await threadHandlingMock.Received(1).RunOnBackgroundThread(Arg.Any<Func<Task<AdapterResponseWithData<ServerProject>>>>());
}
Expand All @@ -350,7 +350,7 @@ public async Task GetServerProjectByKeyAsync_GettingConnectionConfigurationSLCor
{
slCoreServiceProvider.TryGetTransientService(out IConnectionConfigurationSLCoreService _).Returns(false);

var response = await testSubject.GetServerProjectByKeyAsync(SonarCloudConnection, "server-project-key");
var response = await testSubject.GetServerProjectByKeyAsync(sonarCloudConnection, "server-project-key");

logger.Received(1).LogVerbose($"[{nameof(IConnectionConfigurationSLCoreService)}] {SLCoreStrings.ServiceProviderNotInitialized}");
response.Success.Should().BeFalse();
Expand All @@ -364,7 +364,7 @@ public async Task GetServerProjectByKeyAsync_SlCoreThrowsException_ReturnsFailed
connectionConfigurationSlCoreService.When(x => x.GetProjectNamesByKeyAsync(Arg.Any<GetProjectNamesByKeyParams>()))
.Do(_ => throw new Exception(exceptionMessage));

var response = await testSubject.GetServerProjectByKeyAsync(SonarCloudConnection, "server-project-key");
var response = await testSubject.GetServerProjectByKeyAsync(sonarCloudConnection, "server-project-key");

logger.Received(1).LogVerbose($"{Resources.GetServerProjectByKey_Fails}: {exceptionMessage}");
response.Success.Should().BeFalse();
Expand All @@ -378,7 +378,7 @@ public async Task GetServerProjectByKeyAsync_ProjectNotFound_ReturnsFailedRespon
connectionConfigurationSlCoreService.GetProjectNamesByKeyAsync(Arg.Any<GetProjectNamesByKeyParams>())
.Returns(new GetProjectNamesByKeyResponse(slCoreResponse));

var response = await testSubject.GetServerProjectByKeyAsync(SonarCloudConnection, "project-key");
var response = await testSubject.GetServerProjectByKeyAsync(sonarCloudConnection, "project-key");

response.Success.Should().BeFalse();
response.ResponseData.Should().BeNull();
Expand All @@ -393,7 +393,7 @@ public async Task GetServerProjectByKeyAsync_ProjectFound_ReturnsSuccessResponse
};
connectionConfigurationSlCoreService.GetProjectNamesByKeyAsync(Arg.Any<GetProjectNamesByKeyParams>())
.Returns(new GetProjectNamesByKeyResponse(slCoreResponse));
var response = await testSubject.GetServerProjectByKeyAsync(SonarQubeConnection, "project-key");
var response = await testSubject.GetServerProjectByKeyAsync(sonarQubeConnection, "project-key");

response.Success.Should().BeTrue();
response.ResponseData.Should().BeEquivalentTo(new ServerProject("project-key", "project-name"));
Expand All @@ -407,7 +407,7 @@ public async Task GetAllProjectsAsync_SlCoreValidationThrowsException_ReturnsUns
connectionConfigurationSlCoreService.When(x => x.GetAllProjectsAsync(Arg.Any<GetAllProjectsParams>()))
.Do(_ => throw new Exception(exceptionMessage));

var response = await testSubject.GetAllProjectsAsync(SonarCloudConnection);
var response = await testSubject.GetAllProjectsAsync(sonarCloudConnection);

logger.Received(1).LogVerbose($"{Resources.GetAllProjects_Fails}: {exceptionMessage}");
response.Success.Should().BeFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using SonarLint.VisualStudio.ConnectedMode.UI;
using SonarLint.VisualStudio.ConnectedMode.Shared;
using SonarLint.VisualStudio.ConnectedMode.Binding;
using SonarLint.VisualStudio.Core.Binding;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.UI;

Expand All @@ -35,6 +36,7 @@ public void MefCtor_CheckIsExported()
MefTestHelpers.CheckTypeCanBeImported<ConnectedModeBindingServices, IConnectedModeBindingServices>(
MefTestHelpers.CreateExport<IBindingController>(),
MefTestHelpers.CreateExport<ISolutionInfoProvider>(),
MefTestHelpers.CreateExport<ISharedBindingConfigProvider>());
MefTestHelpers.CreateExport<ISharedBindingConfigProvider>(),
MefTestHelpers.CreateExport<ISolutionBindingRepository>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using SonarLint.VisualStudio.ConnectedMode.UI.DeleteConnection;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.UI.DeleteConnection;

[TestClass]
public class PreventDeleteConnectionViewModelTests
{
[TestMethod]
public void Ctor_SetsProperties()
{
var projectsToUnbind = Substitute.For<IReadOnlyList<string>>();
var connectionInfo = new ConnectionInfo(default, default);

var testSubject = new PreventDeleteConnectionViewModel(projectsToUnbind, connectionInfo);

testSubject.ConnectionInfo.Should().BeSameAs(connectionInfo);
testSubject.ProjectsToUnbind.Should().BeSameAs(projectsToUnbind);
}

[DataTestMethod]
public void DisplayProjectList_MultipleProjectsToUnbind_ReturnsTrue()
{
var projects = new[] { "binding1", "binding2"};

var testSubject = new PreventDeleteConnectionViewModel(projects, new ConnectionInfo(default, default));

testSubject.DisplayProjectList.Should().BeTrue();
}

[DataTestMethod]
public void DisplayProjectList_ProjectsIsNull_ReturnsFalse()
{
var testSubject = new PreventDeleteConnectionViewModel(null, new ConnectionInfo(default, default));

testSubject.DisplayProjectList.Should().BeFalse();
}

[DataTestMethod]
public void DisplayProjectList_NoProjectsToUnbind_ReturnsFalse()
{
var testSubject = new PreventDeleteConnectionViewModel([], new ConnectionInfo(default, default));

testSubject.DisplayProjectList.Should().BeFalse();
}
}
Loading

0 comments on commit f3b8a61

Please sign in to comment.