From ef6696b47f29f9e07f371743e47cb044644e4f51 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Fri, 30 Aug 2024 15:13:53 +0200 Subject: [PATCH] Upd --- .../Binding/BindingProcessImplTests.cs | 44 ------------ .../Persistence/ConnectionInfoConverter.cs | 2 + .../Binding/ServerConnectionTests.cs | 67 +++++++++++++++---- src/Integration/Binding/BindingController.cs | 2 + .../ProjectViewModelToBindingArgsConverter.cs | 2 + 5 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/ConnectedMode.UnitTests/Binding/BindingProcessImplTests.cs b/src/ConnectedMode.UnitTests/Binding/BindingProcessImplTests.cs index 7f1e2d787..50af0a34b 100644 --- a/src/ConnectedMode.UnitTests/Binding/BindingProcessImplTests.cs +++ b/src/ConnectedMode.UnitTests/Binding/BindingProcessImplTests.cs @@ -171,50 +171,6 @@ public async Task DownloadQualityProfile_CreatesBoundProjectAndCallsQPDownloader actualProject.ServerProjectKey.Should().Be("the project key"); } - [TestMethod] - [DataRow("the user name", null)] - [DataRow("the user name", "a password")] - [DataRow(null, null)] - [DataRow(null, "should be ignored")] - public async Task DownloadQualityProfile_HandlesBoundProjectCredentialsCorrectly(string userName, string rawPassword) - { - // todo move test to Unintrusive controller - // var qpDownloader = new Mock(); - // var password = rawPassword == null ? null : rawPassword.ToSecureString(); - // - // var connectionInfo = new Conne ctionInformation(new Uri("http://any"), userName, password); - // var bindingArgs = CreateBindCommandArgs(); - // - // var testSubject = CreateTestSubject(bindingArgs, - // qpDownloader: qpDownloader.Object); - // - // // Act - // var result = await testSubject.DownloadQualityProfileAsync(Mock.Of>(), CancellationToken.None); - // - // result.Should().BeTrue(); - // - // qpDownloader.Verify(x => x.UpdateAsync(It.IsAny(), - // It.IsAny>(), - // It.IsAny()), - // Times.Once); - // - // var actualProject = (BoundSonarQubeProject)qpDownloader.Invocations[0].Arguments[0]; - // - // // Check the credentials were handled correctly - // if (userName == null) - // { - // actualProject.Credentials.Should().BeNull(); - // } - // else - // { - // actualProject.Credentials.Should().BeOfType(); - // var actualCreds = (BasicAuthCredentials)actualProject.Credentials; - // - // actualCreds.UserName.Should().Be(userName); - // CheckIsExpectedPassword(rawPassword, actualCreds.Password); - // } - } - [TestMethod] public async Task DownloadQualityProfile_HandlesInvalidOperationException() { diff --git a/src/ConnectedMode/Persistence/ConnectionInfoConverter.cs b/src/ConnectedMode/Persistence/ConnectionInfoConverter.cs index 2f8defee0..0ec4c60d0 100644 --- a/src/ConnectedMode/Persistence/ConnectionInfoConverter.cs +++ b/src/ConnectedMode/Persistence/ConnectionInfoConverter.cs @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using System.Diagnostics.CodeAnalysis; using SonarLint.VisualStudio.Core.Binding; using SonarQube.Client.Models; @@ -25,6 +26,7 @@ namespace SonarLint.VisualStudio.ConnectedMode.Persistence; public static class ConnectionInfoConverter { + [ExcludeFromCodeCoverage] public static ServerConnection ToServerConnection(this ConnectionInformation connectionInformation) => connectionInformation switch { diff --git a/src/Core.UnitTests/Binding/ServerConnectionTests.cs b/src/Core.UnitTests/Binding/ServerConnectionTests.cs index ae2a6848a..8612fb33f 100644 --- a/src/Core.UnitTests/Binding/ServerConnectionTests.cs +++ b/src/Core.UnitTests/Binding/ServerConnectionTests.cs @@ -19,6 +19,7 @@ */ using SonarLint.VisualStudio.Core.Binding; +using SonarQube.Client.Models; using ICredentials = SonarLint.VisualStudio.Core.Binding.ICredentials; namespace SonarLint.VisualStudio.Core.UnitTests.Binding; @@ -26,9 +27,9 @@ namespace SonarLint.VisualStudio.Core.UnitTests.Binding; [TestClass] public class ServerConnectionTests { - private static readonly Uri localhost = new Uri("http://localhost:5000"); - private static readonly string org = "myOrg"; - + private static readonly Uri Localhost = new Uri("http://localhost:5000"); + private const string Org = "myOrg"; + [TestMethod] public void Ctor_SonarCloud_NullOrganization_Throws() { @@ -40,7 +41,7 @@ public void Ctor_SonarCloud_NullOrganization_Throws() [TestMethod] public void Ctor_SonarCloud_NullSettings_SetDefault() { - var sonarCloud = new ServerConnection.SonarCloud(org, null); + var sonarCloud = new ServerConnection.SonarCloud(Org, null); sonarCloud.Settings.Should().BeSameAs(ServerConnection.DefaultSettings); } @@ -48,7 +49,7 @@ public void Ctor_SonarCloud_NullSettings_SetDefault() [TestMethod] public void Ctor_SonarCloud_NullCredentials_SetsNull() { - var sonarCloud = new ServerConnection.SonarCloud(org, credentials: null); + var sonarCloud = new ServerConnection.SonarCloud(Org, credentials: null); sonarCloud.Credentials.Should().BeNull(); } @@ -58,10 +59,10 @@ public void Ctor_SonarCloud_SetsProperties() { var serverConnectionSettings = new ServerConnectionSettings(false); var credentials = Substitute.For(); - var sonarCloud = new ServerConnection.SonarCloud(org, serverConnectionSettings, credentials); + var sonarCloud = new ServerConnection.SonarCloud(Org, serverConnectionSettings, credentials); - sonarCloud.Id.Should().BeSameAs(org); - sonarCloud.OrganizationKey.Should().BeSameAs(org); + sonarCloud.Id.Should().BeSameAs(Org); + sonarCloud.OrganizationKey.Should().BeSameAs(Org); sonarCloud.ServerUri.Should().Be(new Uri("https://sonarcloud.io")); sonarCloud.Settings.Should().BeSameAs(serverConnectionSettings); sonarCloud.Credentials.Should().BeSameAs(credentials); @@ -78,7 +79,7 @@ public void Ctor_SonarQube_NullUri_Throws() [TestMethod] public void Ctor_SonarQube_NullSettings_SetDefault() { - var sonarQube = new ServerConnection.SonarQube(localhost, null); + var sonarQube = new ServerConnection.SonarQube(Localhost, null); sonarQube.Settings.Should().BeSameAs(ServerConnection.DefaultSettings); } @@ -86,7 +87,7 @@ public void Ctor_SonarQube_NullSettings_SetDefault() [TestMethod] public void Ctor_SonarQube_NullCredentials_SetsNull() { - var sonarQube = new ServerConnection.SonarQube(localhost, credentials: null); + var sonarQube = new ServerConnection.SonarQube(Localhost, credentials: null); sonarQube.Credentials.Should().BeNull(); } @@ -96,11 +97,51 @@ public void Ctor_SonarQube_SetsProperties() { var serverConnectionSettings = new ServerConnectionSettings(false); var credentials = Substitute.For(); - var sonarQube = new ServerConnection.SonarQube(localhost, serverConnectionSettings, credentials); + var sonarQube = new ServerConnection.SonarQube(Localhost, serverConnectionSettings, credentials); - sonarQube.Id.Should().Be(localhost.ToString()); - sonarQube.ServerUri.Should().BeSameAs(localhost); + sonarQube.Id.Should().Be(Localhost.ToString()); + sonarQube.ServerUri.Should().BeSameAs(Localhost); sonarQube.Settings.Should().BeSameAs(serverConnectionSettings); sonarQube.Credentials.Should().BeSameAs(credentials); } + + [TestMethod] + public void FromBoundSonarQubeProject_SonarQubeConnection_ConvertedCorrectly() + { + var credentials = Substitute.For(); + var expectedConnection = new ServerConnection.SonarQube(Localhost, credentials: credentials); + + var connection = ServerConnection.FromBoundSonarQubeProject(new BoundSonarQubeProject(Localhost, "any", "any", credentials)); + + connection.Should().BeEquivalentTo(expectedConnection, options => options.ComparingByMembers()); + } + + [TestMethod] + public void FromBoundSonarQubeProject_SonarCloudConnection_ConvertedCorrectly() + { + var uri = new Uri("https://sonarcloud.io"); + var organization = "org"; + var credentials = Substitute.For(); + var expectedConnection = new ServerConnection.SonarCloud(organization, credentials: credentials); + + var connection = ServerConnection.FromBoundSonarQubeProject(new BoundSonarQubeProject(uri, "any", "any", credentials, new SonarQubeOrganization(organization, null))); + + connection.Should().BeEquivalentTo(expectedConnection, options => options.ComparingByMembers()); + } + + [TestMethod] + public void FromBoundSonarQubeProject_InvalidConnection_ReturnsNull() + { + var connection = ServerConnection.FromBoundSonarQubeProject(new BoundSonarQubeProject(){ ProjectKey = "project"}); + + connection.Should().BeNull(); + } + + [TestMethod] + public void FromBoundSonarQubeProject_NullConnection_ReturnsNull() + { + var connection = ServerConnection.FromBoundSonarQubeProject(null); + + connection.Should().BeNull(); + } } diff --git a/src/Integration/Binding/BindingController.cs b/src/Integration/Binding/BindingController.cs index 19cf6f0a3..ea32ae50d 100644 --- a/src/Integration/Binding/BindingController.cs +++ b/src/Integration/Binding/BindingController.cs @@ -20,6 +20,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.VisualStudio.OLE.Interop; using SonarLint.VisualStudio.ConnectedMode.Binding; @@ -36,6 +37,7 @@ namespace SonarLint.VisualStudio.Integration.Binding /// /// A dedicated controller for the /// + [ExcludeFromCodeCoverage] // todo https://sonarsource.atlassian.net/browse/SLVS-1408 internal class BindingController : HostedCommandControllerBase, IBindingWorkflowExecutor { private readonly System.IServiceProvider serviceProvider; diff --git a/src/Integration/WPF/ProjectViewModelToBindingArgsConverter.cs b/src/Integration/WPF/ProjectViewModelToBindingArgsConverter.cs index 348659b9c..e7c071709 100644 --- a/src/Integration/WPF/ProjectViewModelToBindingArgsConverter.cs +++ b/src/Integration/WPF/ProjectViewModelToBindingArgsConverter.cs @@ -19,6 +19,7 @@ */ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Windows.Data; using SonarLint.VisualStudio.ConnectedMode.Binding; @@ -28,6 +29,7 @@ namespace SonarLint.VisualStudio.Integration.WPF { + [ExcludeFromCodeCoverage] // todo https://sonarsource.atlassian.net/browse/SLVS-1408 [ValueConversion(typeof(ProjectViewModel), typeof(BindCommandArgs))] public class ProjectViewModelToBindingArgsConverter : IValueConverter {