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

SLVS-1455 Make Id of type URL #5698

Merged
merged 12 commits into from
Sep 25, 2024
7 changes: 4 additions & 3 deletions src/ConnectedMode.UnitTests/ConnectionInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

using SonarLint.VisualStudio.ConnectedMode.UI.Resources;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Binding;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests;
Expand All @@ -29,11 +30,11 @@ public class ConnectionInfoTests
[TestMethod]
public void FromServerConnection_ShouldReturnConnectionInfoWithSameId()
{
var sonarCloudServerConnection = new ServerConnection.SonarCloud("id");
var sonarCloudServerConnection = new ServerConnection.SonarCloud("organization");

var connectionInfo = ConnectionInfo.From(sonarCloudServerConnection);

connectionInfo.Id.Should().Be("id");
connectionInfo.Id.Should().Be("organization");
}

[TestMethod]
Expand Down Expand Up @@ -61,7 +62,7 @@ public void GetIdForTransientConnection_SonarCloudWithNullId_ReturnsSonarCloudUr
{
var connectionInfo = new ConnectionInfo(null, ConnectionServerType.SonarCloud);

connectionInfo.GetIdForTransientConnection().Should().Be(UiResources.SonarCloudUrl);
connectionInfo.GetIdForTransientConnection().Should().Be(CoreStrings.SonarCloudUrl);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void Dto_FromSonarCloudBinding_SerializedAsExpected()
serializeObject.Should().BeEquivalentTo(
"""
{
"ServerConnectionId": "org_key_123",
"ServerConnectionId": "https://sonarcloud.io/organizations/org_key_123",
"ServerUri": "https://sonarcloud.io",
"Organization": {
"Key": "org_key_123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void TryGet_FileExistsAndConnectionDoesNotExist_ReturnsFalse()
[TestMethod]
public void TryGet_FileExistsAndConnectionIsSonarCloud_ReturnsSonarCloudConnection()
{
var sonarCloudModel = GetSonarCloudJsonModel("myOrg");
var expectedConnection = new SonarCloud(sonarCloudModel.Id);
var sonarCloudModel = GetSonarCloudJsonModel();
var expectedConnection = new SonarCloud(sonarCloudModel.OrganizationKey);
MockReadingFile(new ServerConnectionsListJsonModel { ServerConnections = [sonarCloudModel] });
serverConnectionModelMapper.GetServerConnection(sonarCloudModel).Returns(expectedConnection);

Expand Down Expand Up @@ -200,7 +200,7 @@ public void TryGetAll_FileExistsAndIsEmpty_ReturnsEmptyList()
[TestMethod]
public void TryGetAll_FileExistsAndHasConnection_MapsModel()
{
var cloudModel = GetSonarCloudJsonModel("myOrg");
var cloudModel = GetSonarCloudJsonModel();
MockReadingFile(new ServerConnectionsListJsonModel { ServerConnections = [cloudModel] });

testSubject.TryGetAll(out _);
Expand Down Expand Up @@ -467,7 +467,7 @@ public void TryUpdateSettingsById_FileExistsAndConnectionExists_UpdatesSettings(
jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(true);
MockFileWithOneSonarCloudConnection(oldSmartNotifications);

var succeeded = testSubject.TryUpdateSettingsById("myOrg", new ServerConnectionSettings(newSmartNotifications));
var succeeded = testSubject.TryUpdateSettingsById("https://sonarcloud.io/organizations/myOrg", new ServerConnectionSettings(newSmartNotifications));

succeeded.Should().BeTrue();
Received.InOrder(() =>
Expand Down Expand Up @@ -552,8 +552,8 @@ public void TryUpdateCredentialsById_SavingCredentialsThrows_ReturnsFalseAndLogs

private SonarCloud MockFileWithOneSonarCloudConnection(bool isSmartNotificationsEnabled = true)
{
var sonarCloudModel = GetSonarCloudJsonModel("myOrg", isSmartNotificationsEnabled);
var sonarCloud = new SonarCloud(sonarCloudModel.Id, sonarCloudModel.Settings, Substitute.For<ICredentials>());
var sonarCloudModel = GetSonarCloudJsonModel(isSmartNotificationsEnabled);
var sonarCloud = new SonarCloud(sonarCloudModel.OrganizationKey, sonarCloudModel.Settings, Substitute.For<ICredentials>());
MockReadingFile(new ServerConnectionsListJsonModel { ServerConnections = [sonarCloudModel] });
serverConnectionModelMapper.GetServerConnection(sonarCloudModel).Returns(sonarCloud);

Expand All @@ -575,12 +575,12 @@ private void MockReadingFile(ServerConnectionsListJsonModel modelToReturn)
jsonFileHandler.ReadFile<ServerConnectionsListJsonModel>(Arg.Any<string>()).Returns(modelToReturn);
}

private static ServerConnectionJsonModel GetSonarCloudJsonModel(string id, bool isSmartNotificationsEnabled = false)
private static ServerConnectionJsonModel GetSonarCloudJsonModel(bool isSmartNotificationsEnabled = false)
{
return new ServerConnectionJsonModel
{
Id = id,
OrganizationKey = id,
Id = "https://sonarcloud.io/organizations/myOrg",
OrganizationKey = "myOrg",
Settings = new ServerConnectionSettings(isSmartNotificationsEnabled)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public void MefCtor_CheckIsExported()
[TestMethod]
public void TryGetServerConnectionById_CallServerConnectionsRepository()
{
var expectedServerConnection = new SonarCloud("connection-id");
serverConnectionsRepository.TryGet("connection-id", out _).Returns(callInfo =>
var expectedServerConnection = new SonarCloud("myOrg");
serverConnectionsRepository.TryGet("https://sonarcloud.io/organizations/myOrg", out _).Returns(callInfo =>
{
callInfo[1] = expectedServerConnection;
return true;
});

testSubject.TryGetServerConnectionById("connection-id", out var serverConnection);
testSubject.TryGet(new ConnectionInfo("myOrg", ConnectionServerType.SonarCloud), out var serverConnection);

serverConnection.Should().Be(expectedServerConnection);
}
Expand All @@ -80,7 +80,7 @@ public void TryGetAllConnections_HasOneSonarCloudConnection_ReturnsOneMappedConn

testSubject.TryGetAllConnections(out var connections);

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

[TestMethod]
Expand Down Expand Up @@ -128,7 +128,7 @@ public void TryGetAllConnectionsInfo_HasOneSonarCloudConnection_ReturnsOneMapped

testSubject.TryGetAllConnectionsInfo(out var connections);

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

[TestMethod]
Expand Down Expand Up @@ -179,7 +179,7 @@ public void TryAddConnection_AddsSonarCloudConnection_CallsSlCoreWithMappedConne

serverConnectionsRepository.Received(1)
.TryAdd(Arg.Is<SonarCloud>(sc =>
sc.Id == sonarCloud.Info.Id &&
sc.Id == $"https://sonarcloud.io/organizations/{sonarCloud.Info.Id}" &&
sc.OrganizationKey == sonarCloud.Info.Id &&
sc.Settings.IsSmartNotificationsEnabled == sonarCloud.EnableSmartNotifications));
}
Expand Down Expand Up @@ -240,10 +240,11 @@ public void TryAddConnection_NullCredentials_TriesAddingAConnectionWithNoCredent
[DataRow(false)]
public void TryDeleteConnection_ReturnsStatusFromSlCore(bool expectedStatus)
{
var connectionInfoId = "http://localhost:9000";
const string connectionInfoId = "http://localhost:9000/";
var connectionInfo = new ConnectionInfo(connectionInfoId, ConnectionServerType.SonarQube);
serverConnectionsRepository.TryDelete(connectionInfoId).Returns(expectedStatus);

var succeeded = testSubject.TryRemoveConnection(connectionInfoId);
var succeeded = testSubject.TryRemoveConnection(connectionInfo);

succeeded.Should().Be(expectedStatus);
}
Expand All @@ -253,11 +254,12 @@ public void TryDeleteConnection_ReturnsStatusFromSlCore(bool expectedStatus)
[DataRow(false)]
public void TryGet_ReturnsStatusFromSlCore(bool expectedStatus)
{
var connectionId = "myOrg";
const string connectionInfoId = "myOrg";
var connectionInfo = new ConnectionInfo(connectionInfoId, ConnectionServerType.SonarCloud);
var expectedServerConnection = new SonarCloud("myOrg");
MockTryGet(connectionId, expectedStatus, expectedServerConnection);
MockTryGet("https://sonarcloud.io/organizations/myOrg", expectedStatus, expectedServerConnection);

var succeeded = testSubject.TryGet(connectionId, out var receivedServerConnection);
var succeeded = testSubject.TryGet(connectionInfo, out var receivedServerConnection);

succeeded.Should().Be(expectedStatus);
receivedServerConnection.Should().Be(expectedServerConnection);
Expand Down
Loading