public async Task Disconnect_WhenConnected_DisposeTheSonarQubeClient() { // Arrange var successResponse = new HttpResponseMessage(HttpStatusCode.OK); var client = GetMockSqClientWithCredentialAndVersion("5.6"); client.As <IDisposable>().Setup(x => x.Dispose()).Verifiable(); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act service.Disconnect(); // Assert client.VerifyAll(); }
public async Task GetProjectDashboardUrl_ReturnsExpectedUrl() { // Arrange var client = GetMockSqClientWithCredentialAndVersion("5.6"); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = service.GetProjectDashboardUrl("myProject"); // Assert client.VerifyAll(); result.Host.Should().Be("mysq.com"); result.LocalPath.Should().Be("/dashboard/index/myProject"); }
private static async Task <SonarQubeIssue[]> GetSonarQubeIssues(string sonarKey) { var service = new SonarQubeService(new HttpClientHandler(), "user-agent-string", Logger); await service.ConnectAsync( new ConnectionInformation(new Uri(AppSettings.SonarServer), AppSettings.SonarUserName, AppSettings.SonarUserPassword.ToSecureString()), CancellationToken.None); var getIssueWrapper = new IssuesRequestWrapper() { ProjectKey = sonarKey, Logger = Logger, HttpMethod = HttpMethod.Get }; var issues = await getIssueWrapper.InvokeAsync(Client, CancellationToken.None); return(issues); }
public void ConnectAsync_WhenCredentialsAreInvalid_ThrowsExpectedException() { // Act var client = new Mock <ISonarQubeClient>(); client .Setup(x => x.ValidateCredentialsAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(Result.Ok(new CredentialResponse { IsValid = false })); var service = new SonarQubeService(WrapInMockFactory(client)); Func <Task> func = async() => await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Assert func.Should().ThrowExactly <Exception>().WithMessage("Invalid credentials."); }
public async Task GetQualityProfileAsync_WhenError404_CallsAgainWithNoProjectKey() { // Arrange var client = GetMockSqClientWithCredentialAndVersion("0.0"); client .SetupSequence(x => x.GetQualityProfilesAsync(It.IsAny <QualityProfileRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(Result.NotFound(new QualityProfileResponse[0])) .ReturnsAsync(Result.Ok(new[] { new QualityProfileResponse { Key = "QP_KEY", Language = "cs" } })); client .Setup(x => x.GetQualityProfileChangeLogAsync(It.IsAny <QualityProfileChangeLogRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(Result.Ok(new QualityProfileChangeLogResponse { Events = new[] { new QualityProfileChangeLogEventResponse { Date = DateTime.MaxValue } } })); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetQualityProfileAsync("PROJECT_KEY", "ORG_KEY", SonarQubeLanguage.CSharp, CancellationToken.None); // Assert client.Verify(x => x.GetQualityProfilesAsync( // Defaults is set to true in the client It.Is <QualityProfileRequest>(r => r.Defaults == null && r.ProjectKey == null && r.OrganizationKey == "ORG_KEY"), It.IsAny <CancellationToken>()), Times.Once); client.Verify(x => x.GetQualityProfilesAsync( It.Is <QualityProfileRequest>(r => r.Defaults == null && r.ProjectKey == "PROJECT_KEY" && r.OrganizationKey == "ORG_KEY"), It.IsAny <CancellationToken>()), Times.Once); client.Verify(x => x.GetQualityProfileChangeLogAsync(It.IsAny <QualityProfileChangeLogRequest>(), It.IsAny <CancellationToken>()), Times.Once); result.Key.Should().Be("QP_KEY"); result.Language.Should().Be("cs"); result.TimeStamp.Should().Be(DateTime.MaxValue); }
public async Task GetQualityProfileAsync_WhenError404_CallsAgainWithNoProjectKey() { // Arrange var client = GetMockSqClientWithCredentialAndVersion("0.0"); client.Setup(x => x.GetQualityProfilesAsync(It.IsAny <QualityProfileRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new Queue <Result <QualityProfileResponse[]> >( new Result <QualityProfileResponse[]>[] { new Result <QualityProfileResponse[]>(new HttpResponseMessage(HttpStatusCode.NotFound), new QualityProfileResponse[0]), new Result <QualityProfileResponse[]>(new HttpResponseMessage(), new[] { new QualityProfileResponse { Key = "QP_KEY", Language = "cs" } }) } ).Dequeue); client.Setup(x => x.GetQualityProfileChangeLogAsync(It.IsAny <QualityProfileChangeLogRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new Result <QualityProfileChangeLogResponse>(new HttpResponseMessage(), new QualityProfileChangeLogResponse { Events = new[] { new QualityProfileChangeLogEventResponse { Date = DateTime.MaxValue } } })); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetQualityProfileAsync("PROJECT_KEY", SonarQubeLanguage.CSharp, CancellationToken.None); // Assert client.Verify(x => x.GetQualityProfilesAsync(It.IsAny <QualityProfileRequest>(), It.IsAny <CancellationToken>()), Times.Exactly(2)); client.Verify(x => x.GetQualityProfileChangeLogAsync(It.IsAny <QualityProfileChangeLogRequest>(), It.IsAny <CancellationToken>()), Times.Once); result.Key.Should().Be("QP_KEY"); result.Language.Should().Be("cs"); result.TimeStamp.Should().Be(DateTime.MaxValue); }
public async Task GetRoslynExportProfileAsync_ReturnsExpectedResult() { // Arrange var roslynExport = new RoslynExportProfileResponse(); var client = GetMockSqClientWithCredentialAndVersion("5.6"); client.Setup(x => x.GetRoslynExportProfileAsync(It.IsAny <RoslynExportProfileRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Result <RoslynExportProfileResponse>(new HttpResponseMessage(), roslynExport)); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetRoslynExportProfileAsync("name", SonarQubeLanguage.CSharp, CancellationToken.None); // Assert client.VerifyAll(); result.Should().Be(roslynExport); }
public async Task SonarQubeService_UsesFactorySelector(string serverUrl, bool isSonarCloud) { var logger = new TestLogger(); var connectionInfo = new ConnectionInformation(new Uri(serverUrl)); var requestFactoryMock = CreateRequestFactory("1.2.3"); var selectorMock = new Mock <IRequestFactorySelector>(); selectorMock.Setup(x => x.Select(isSonarCloud, logger)).Returns(requestFactoryMock.Object); var testSubject = new SonarQubeService(Mock.Of <HttpMessageHandler>(), "user-agent", logger, selectorMock.Object, Mock.Of <ISecondaryIssueHashUpdater>()); await testSubject.ConnectAsync(connectionInfo, CancellationToken.None); selectorMock.Verify(x => x.Select(isSonarCloud, logger), Times.Once); requestFactoryMock.Invocations.Count.Should().Be(2); testSubject.IsConnected.Should().BeTrue(); var expectedServerType = isSonarCloud ? ServerType.SonarCloud : ServerType.SonarQube; testSubject.ServerInfo.ServerType.Should().Be(expectedServerType); testSubject.ServerInfo.Version.Should().Be(new Version(1, 2, 3)); }
public async Task GetAllProjectsAsync_WhenNoOrganizationIsSpecified_ReturnsExpectedResult() { // Arrange var client = GetMockSqClientWithCredentialAndVersion("5.6"); client .Setup(x => x.GetProjectsAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(Result.Ok(new[] { new ProjectResponse { Key = "key", Name = "name" } })); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetAllProjectsAsync(null, CancellationToken.None); // Assert client.VerifyAll(); result.Should().NotBeNull(); result.Should().HaveCount(1); result[0].Key.Should().Be("key"); result[0].Name.Should().Be("name"); }
public async Task GetRoslynExportProfileAsync_WhenServerIsGreaterThanOrEqualTo66_ReturnsExpectedResult() { // Arrange var roslynExport = new RoslynExportProfileResponse(); Expression <Func <RoslynExportProfileRequestV66Plus, bool> > matchRequest = r => r.OrganizationKey == "my-org"; var client = GetMockSqClientWithCredentialAndVersion("6.6"); client .Setup(x => x.GetRoslynExportProfileAsync(It.Is(matchRequest), CancellationToken.None)) .ReturnsAsync(Result.Ok(roslynExport)); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetRoslynExportProfileAsync("name", "my-org", SonarQubeLanguage.CSharp, CancellationToken.None); // Assert client.VerifyAll(); result.Should().Be(roslynExport); }
public async Task GetAllPropertiesAsync_ReturnsExpectedResult() { // Arrange var client = GetMockSqClientWithCredentialAndVersion("5.6"); client.Setup(x => x.GetPropertiesAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Result <PropertyResponse[]>(new HttpResponseMessage(), new[] { new PropertyResponse { Key = "key", Value = "value" } })); var service = new SonarQubeService(WrapInMockFactory(client)); await service.ConnectAsync(new ConnectionInformation(new Uri("http://mysq.com")), CancellationToken.None); // Act var result = await service.GetAllPropertiesAsync(CancellationToken.None); // Assert client.VerifyAll(); result.Should().NotBeNull(); result.Should().HaveCount(1); result[0].Key.Should().Be("key"); result[0].Value.Should().Be("value"); }
public async Task SonarQubeService_FactorySelector_RequestsNewFactoryOnEachConnect() { var logger = new TestLogger(); var sonarQubeConnectionInfo = new ConnectionInformation(new Uri("http://sonarqube")); var sonarCloudConnectionInfo = new ConnectionInformation(new Uri("https://sonarcloud.io")); var qubeFactoryMock = CreateRequestFactory("1.2.3"); var cloudFactoryMock = CreateRequestFactory("9.9"); var selectorMock = new Mock <IRequestFactorySelector>(); selectorMock.Setup(x => x.Select(false /* isSonarCloud */, logger)).Returns(qubeFactoryMock.Object); selectorMock.Setup(x => x.Select(true /* isSonarCloud */, logger)).Returns(cloudFactoryMock.Object); var testSubject = new SonarQubeService(Mock.Of <HttpMessageHandler>(), "user-agent", logger, selectorMock.Object, Mock.Of <ISecondaryIssueHashUpdater>()); // 1. Connect to SonarQube await testSubject.ConnectAsync(sonarQubeConnectionInfo, CancellationToken.None); qubeFactoryMock.Invocations.Count.Should().Be(2); cloudFactoryMock.Invocations.Count.Should().Be(0); testSubject.IsConnected.Should().BeTrue(); testSubject.ServerInfo.ServerType.Should().Be(ServerType.SonarQube); // 2. Disconnect testSubject.Disconnect(); testSubject.IsConnected.Should().BeFalse(); // 3. Connect to SonarCloud await testSubject.ConnectAsync(sonarCloudConnectionInfo, CancellationToken.None); qubeFactoryMock.Invocations.Count.Should().Be(2); cloudFactoryMock.Invocations.Count.Should().Be(2); testSubject.IsConnected.Should().BeTrue(); testSubject.ServerInfo.ServerType.Should().Be(ServerType.SonarCloud); }
protected void ResetService() { messageHandler.Reset(); service = new SonarQubeService(messageHandler.Object, UserAgent, logger, requestFactorySelector, secondaryIssueHashUpdater.Object); }
protected void ResetService() { messageHandler.Reset(); service = new SonarQubeService(messageHandler.Object, requestFactory, UserAgent, logger); }