Skip to content

Commit

Permalink
SLVS-1376 Binding Dialog: list connections (#5649)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource authored Aug 30, 2024
1 parent ccf0822 commit 30af3c4
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 91 deletions.
125 changes: 125 additions & 0 deletions src/ConnectedMode.UnitTests/ServerConnectionsRepositoryAdapterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.Core;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.TestInfrastructure;
using static SonarLint.VisualStudio.Core.Binding.ServerConnection;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests;

[TestClass]
public class ServerConnectionsRepositoryAdapterTests
{
private IServerConnectionsRepository serverConnectionsRepository;
private ServerConnectionsRepositoryAdapter testSubject;

[TestInitialize]
public void TestInitialize()
{
serverConnectionsRepository = Substitute.For<IServerConnectionsRepository>();
testSubject = new ServerConnectionsRepositoryAdapter(serverConnectionsRepository);
}

[TestMethod]
public void MefCtor_CheckIsExported()
=> MefTestHelpers.CheckTypeCanBeImported<ServerConnectionsRepositoryAdapter, IServerConnectionsRepositoryAdapter>(MefTestHelpers.CreateExport<IServerConnectionsRepository>());

[TestMethod]
public void GetAllConnections_CallServerConnectionsRepository()
{
serverConnectionsRepository.GetAll().Returns([]);

var connections = testSubject.GetAllConnections();

serverConnectionsRepository.Received(1).GetAll();
connections.Should().BeEmpty();
}

[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void GetAllConnections_HasOneSonarCloudConnection_ReturnsOneMappedConnection(bool isSmartNotificationsEnabled)
{
var sonarCloud = CreateSonarCloudServerConnection(isSmartNotificationsEnabled);
serverConnectionsRepository.GetAll().Returns([sonarCloud]);

var connections = testSubject.GetAllConnections();

connections.Should().BeEquivalentTo([new Connection(new ConnectionInfo(sonarCloud.Id, ConnectionServerType.SonarCloud), isSmartNotificationsEnabled)]);
}

[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void GetAllConnections_HasOneSonarQubeConnection_ReturnsOneMappedConnection(bool isSmartNotificationsEnabled)
{
var sonarQube = CreateSonarQubeServerConnection(isSmartNotificationsEnabled);
serverConnectionsRepository.GetAll().Returns([sonarQube]);

var connections = testSubject.GetAllConnections();

connections.Should().BeEquivalentTo([new Connection(new ConnectionInfo(sonarQube.Id, ConnectionServerType.SonarQube), isSmartNotificationsEnabled)]);
}

[TestMethod]
public void GetAllConnectionsInfo_CallServerConnectionsRepository()
{
serverConnectionsRepository.GetAll().Returns([]);

var connections = testSubject.GetAllConnectionsInfo();

serverConnectionsRepository.Received(1).GetAll();
connections.Should().BeEmpty();
}

[TestMethod]
public void GetAllConnectionsInfo_HasOneSonarCloudConnection_ReturnsOneMappedConnection()
{
var sonarCloud = CreateSonarCloudServerConnection();
serverConnectionsRepository.GetAll().Returns([sonarCloud]);

var connections = testSubject.GetAllConnectionsInfo();

connections.Should().BeEquivalentTo([new ConnectionInfo(sonarCloud.Id, ConnectionServerType.SonarCloud)]);
}

[TestMethod]
public void GetAllConnectionsInfo_HasOneSonarQubeConnection_ReturnsOneMappedConnection()
{
var sonarQube = CreateSonarQubeServerConnection();
serverConnectionsRepository.GetAll().Returns([sonarQube]);

var connections = testSubject.GetAllConnectionsInfo();

connections.Should().BeEquivalentTo([new ConnectionInfo(sonarQube.Id, ConnectionServerType.SonarQube)]);
}

private static SonarCloud CreateSonarCloudServerConnection(bool isSmartNotificationsEnabled = true)
{
return new SonarCloud("myOrg", new ServerConnectionSettings(isSmartNotificationsEnabled), Substitute.For<ICredentials>());
}

private static ServerConnection.SonarQube CreateSonarQubeServerConnection(bool isSmartNotificationsEnabled = true)
{
var sonarQube = new ServerConnection.SonarQube(new Uri("http://localhost"), new ServerConnectionSettings(isSmartNotificationsEnabled), Substitute.For<ICredentials>());
return sonarQube;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void MefCtor_CheckIsExported()
MefTestHelpers.CreateExport<ISlCoreConnectionAdapter>(),
MefTestHelpers.CreateExport<IConfigurationProvider>(),
MefTestHelpers.CreateExport<ISharedBindingConfigProvider>(),
MefTestHelpers.CreateExport<IServerConnectionsRepositoryAdapter>(),
MefTestHelpers.CreateExport<ILogger>());
}
}
Loading

0 comments on commit 30af3c4

Please sign in to comment.