Skip to content

Commit

Permalink
SLVS-1474 Refactor ServerConnectionsProvider (#5718)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource authored and vnaskos-sonar committed Oct 1, 2024
1 parent b60af3d commit 1e6caa2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 316 deletions.
36 changes: 10 additions & 26 deletions src/SLCore.Listeners.UnitTests/CredentialsListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Threading.Tasks;
using NSubstitute;
using SonarLint.VisualStudio.ConnectedMode.Binding;
using SonarLint.VisualStudio.SLCore.Common.Helpers;
using SonarLint.VisualStudio.SLCore.Common.Models;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Listener.Credentials;
Expand All @@ -32,15 +28,14 @@ namespace SonarLint.VisualStudio.SLCore.Listeners.UnitTests;
[TestClass]
public class CredentialsListenerTests
{
private const string ConnectionId = "connectionId123";
private static readonly Uri Uri = new("http://myfavouriteuri.nonexistingdomain");
private const string ConnectionId = "http://myfavouriteuri.nonexistingdomain";
private static readonly Uri Uri = new(ConnectionId);

[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<CredentialsListener, ISLCoreListener>(
MefTestHelpers.CreateExport<ICredentialProvider>(),
MefTestHelpers.CreateExport<IConnectionIdHelper>());
MefTestHelpers.CreateExport<ICredentialProvider>());
}

[TestMethod]
Expand All @@ -52,8 +47,7 @@ public void MefCtor_CheckIsSingleton()
[TestMethod]
public async Task GetCredentialsAsync_NullConnectionId_ReturnsNoCredentials()
{
var testSubject = CreateTestSubject(out _, out var connectionIdHelperMock);
connectionIdHelperMock.GetUriFromConnectionId(null).Returns((Uri)null);
var testSubject = CreateTestSubject(out _);

var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(null));

Expand All @@ -63,8 +57,7 @@ public async Task GetCredentialsAsync_NullConnectionId_ReturnsNoCredentials()
[TestMethod]
public async Task GetCredentialsAsync_NullParams_ReturnsNoCredentials()
{
var testSubject = CreateTestSubject(out _, out var connectionIdHelperMock);
connectionIdHelperMock.GetUriFromConnectionId(null).Returns((Uri)null);
var testSubject = CreateTestSubject(out _);

var response = await testSubject.GetCredentialsAsync(null);

Expand All @@ -74,8 +67,7 @@ public async Task GetCredentialsAsync_NullParams_ReturnsNoCredentials()
[TestMethod]
public async Task GetCredentialsAsync_CredentialsNotFound_ReturnsNoCredentials()
{
var testSubject = CreateTestSubject(out var credentialStoreMock, out var connectionIdHelperMock);
SetUpConnectionIdHelper(connectionIdHelperMock);
var testSubject = CreateTestSubject(out var credentialStoreMock);
credentialStoreMock.GetCredentials(Arg.Is<Uri>(x => UriEquals(x, Uri))).Returns((ConnectionCredentials)null);

var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(ConnectionId));
Expand All @@ -90,8 +82,7 @@ public async Task GetCredentialsAsync_SonarQubeUsernameAndPasswordFound_ReturnsU
const string username = "user1";
const string password = "password123";

var testSubject = CreateTestSubject(out var credentialStoreMock, out var connectionIdHelperMock);
SetUpConnectionIdHelper(connectionIdHelperMock);
var testSubject = CreateTestSubject(out var credentialStoreMock);
credentialStoreMock.GetCredentials(Arg.Is<Uri>(x => UriEquals(x, Uri))).Returns(new ConnectionCredentials(username, password));

var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(ConnectionId));
Expand All @@ -104,26 +95,19 @@ public async Task GetCredentialsAsync_SonarQubeTokenFound_ReturnsToken()
{
const string token = "token123";

var testSubject = CreateTestSubject(out var credentialStoreMock, out var connectionIdHelperMock);
SetUpConnectionIdHelper(connectionIdHelperMock);
var testSubject = CreateTestSubject(out var credentialStoreMock);
credentialStoreMock.GetCredentials(Arg.Is<Uri>(x => UriEquals(x, Uri))).Returns(new ConnectionCredentials(token));

var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(ConnectionId));

response.Should().BeEquivalentTo(new GetCredentialsResponse(new TokenDto(token)));
}

private CredentialsListener CreateTestSubject(out ICredentialProvider credentialStoreMock, out IConnectionIdHelper connectionIdHelperMock)
private CredentialsListener CreateTestSubject(out ICredentialProvider credentialStoreMock)
{
credentialStoreMock = Substitute.For<ICredentialProvider>();
connectionIdHelperMock = Substitute.For<IConnectionIdHelper>();

return new CredentialsListener(credentialStoreMock, connectionIdHelperMock);
}

private static void SetUpConnectionIdHelper(IConnectionIdHelper connectionIdHelperMock)
{
connectionIdHelperMock.GetUriFromConnectionId(ConnectionId).Returns(Uri);
return new CredentialsListener(credentialStoreMock);
}

private static bool UriEquals(Uri uri, Uri serverUri)
Expand Down
11 changes: 3 additions & 8 deletions src/SLCore.Listeners/Implementation/CredentialsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
*/

using System.ComponentModel.Composition;
using System.Threading.Tasks;
using SonarLint.VisualStudio.ConnectedMode.Binding;
using SonarLint.VisualStudio.SLCore.Common.Helpers;
using SonarLint.VisualStudio.SLCore.Common.Models;
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.SLCore.Listener.Credentials;
Expand All @@ -36,24 +34,21 @@ namespace SonarLint.VisualStudio.SLCore.Listeners.Implementation
internal class CredentialsListener : ICredentialsListener
{
private readonly ICredentialProvider credentialProvider;
private readonly IConnectionIdHelper connectionIdHelper;

[ImportingConstructor]
public CredentialsListener(ICredentialProvider credentialProvider, IConnectionIdHelper connectionIdHelper)
public CredentialsListener(ICredentialProvider credentialProvider)
{
this.credentialProvider = credentialProvider;
this.connectionIdHelper = connectionIdHelper;
}

public Task<GetCredentialsResponse> GetCredentialsAsync(GetCredentialsParams parameters)
{
var serverUri = connectionIdHelper.GetUriFromConnectionId(parameters?.connectionId);

if (serverUri == null)
if (parameters?.connectionId == null)
{
return Task.FromResult(GetCredentialsResponse.NoCredentials);
}

var serverUri = new Uri(parameters.connectionId);
var credentials = credentialProvider.GetCredentials(serverUri);

if (credentials == null)
Expand Down
111 changes: 0 additions & 111 deletions src/SLCore.UnitTests/Common/Helpers/ConnectionIdHelperTests.cs

This file was deleted.

26 changes: 9 additions & 17 deletions src/SLCore.UnitTests/State/ConfigScopeUpdaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Threading.Tasks;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.SLCore.Common.Helpers;
using SonarLint.VisualStudio.SLCore.State;
using SonarQube.Client.Models;

namespace SonarLint.VisualStudio.SLCore.UnitTests.State;

Expand All @@ -36,7 +33,6 @@ public void MefCtor_CheckIsExported()
MefTestHelpers.CheckTypeCanBeImported<ConfigScopeUpdater, IConfigScopeUpdater>(
MefTestHelpers.CreateExport<IActiveConfigScopeTracker>(),
MefTestHelpers.CreateExport<ISolutionInfoProvider>(),
MefTestHelpers.CreateExport<IConnectionIdHelper>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}

Expand Down Expand Up @@ -86,34 +82,32 @@ public void UpdateConfigScopeForCurrentSolution_UnboundSolutionOpen_SetsCurrentC
[TestMethod]
public void UpdateConfigScopeForCurrentSolution_BoundSolutionOpen_SetsCurrentConfigScope()
{
var binding = new BoundServerProject("solution", "projectKey", new ServerConnection.SonarQube(new Uri("http://localhost")));
var serverConnection = new ServerConnection.SonarQube(new Uri("http://localhost"));
var binding = new BoundServerProject("solution", "projectKey", serverConnection);
var activeConfigScopeTrackerMock = new Mock<IActiveConfigScopeTracker>();
var solutionInfoProviderMock = new Mock<ISolutionInfoProvider>();
solutionInfoProviderMock.Setup(x => x.GetSolutionName()).Returns("sln");
var connectionIdHelperMock = new Mock<IConnectionIdHelper>();
connectionIdHelperMock.Setup(x => x.GetConnectionIdFromServerConnection(binding.ServerConnection)).Returns("conid");
var testSubject = CreateTestSubject(activeConfigScopeTrackerMock.Object, solutionInfoProviderMock.Object, connectionIdHelperMock.Object);
var testSubject = CreateTestSubject(activeConfigScopeTrackerMock.Object, solutionInfoProviderMock.Object);

testSubject.UpdateConfigScopeForCurrentSolution(binding);

activeConfigScopeTrackerMock.Verify(x => x.SetCurrentConfigScope("sln", "conid", binding.ServerProjectKey));
activeConfigScopeTrackerMock.Verify(x => x.SetCurrentConfigScope("sln", serverConnection.Id, binding.ServerProjectKey));
activeConfigScopeTrackerMock.VerifyNoOtherCalls();
}

[TestMethod]
public void UpdateConfigScopeForCurrentSolution_BoundSolutionWithOrganizationOpen_SetsCurrentConfigScope()
{
var binding = new BoundServerProject("solution", "projectKey", new ServerConnection.SonarCloud("org"));
var serverConnection = new ServerConnection.SonarCloud("org");
var binding = new BoundServerProject("solution", "projectKey", serverConnection);
var activeConfigScopeTrackerMock = new Mock<IActiveConfigScopeTracker>();
var solutionInfoProviderMock = new Mock<ISolutionInfoProvider>();
solutionInfoProviderMock.Setup(x => x.GetSolutionName()).Returns("sln");
var connectionIdHelperMock = new Mock<IConnectionIdHelper>();
connectionIdHelperMock.Setup(x => x.GetConnectionIdFromServerConnection(binding.ServerConnection)).Returns("conid");
var testSubject = CreateTestSubject(activeConfigScopeTrackerMock.Object, solutionInfoProviderMock.Object, connectionIdHelperMock.Object);
var testSubject = CreateTestSubject(activeConfigScopeTrackerMock.Object, solutionInfoProviderMock.Object);

testSubject.UpdateConfigScopeForCurrentSolution(binding);

activeConfigScopeTrackerMock.Verify(x => x.SetCurrentConfigScope("sln", "conid", binding.ServerProjectKey));
activeConfigScopeTrackerMock.Verify(x => x.SetCurrentConfigScope("sln", serverConnection.Id, binding.ServerProjectKey));
activeConfigScopeTrackerMock.VerifyNoOtherCalls();
}

Expand All @@ -131,13 +125,11 @@ public void UpdateConfigScopeForCurrentSolution_SolutionClosed_RemovesCurrentCon

private static ConfigScopeUpdater CreateTestSubject(IActiveConfigScopeTracker activeConfigScopeTracker = null,
ISolutionInfoProvider solutionInfoProvider = null,
IConnectionIdHelper connectionIdHelper = null,
IThreadHandling threadHandling = null)
{
activeConfigScopeTracker ??= Mock.Of<IActiveConfigScopeTracker>();
solutionInfoProvider ??= Mock.Of<ISolutionInfoProvider>();
connectionIdHelper ??= new ConnectionIdHelper();
threadHandling ??= new NoOpThreadHandler();
return new ConfigScopeUpdater(activeConfigScopeTracker, solutionInfoProvider, connectionIdHelper, threadHandling);
return new ConfigScopeUpdater(activeConfigScopeTracker, solutionInfoProvider, threadHandling);
}
}
Loading

0 comments on commit 1e6caa2

Please sign in to comment.