Skip to content

Commit

Permalink
Fix muting of incorrect issues when bound to SonarCloud
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Jan 18, 2024
1 parent 30aa1d7 commit 8c02b5a
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public async Task InvokeAsync_ComponentKeyNotSpecified_ComponentsAreNotIncludedI
}

[TestMethod]
public async Task InvokeAsync_ComponentKeySpecified_ComponentsAreIncludedInQueryString()
public async Task InvokeAsync_ComponentKeySpecified_ComponentsAreNotIncludedInQueryString()
{
var testSubject = CreateTestSubject("any", "any", componentKey: "project1");

Expand All @@ -462,7 +462,7 @@ public async Task InvokeAsync_ComponentKeySpecified_ComponentsAreIncludedInQuery
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

var actualQueryString = GetSingleActualQueryString(handlerMock);
actualQueryString.Contains("components=project1").Should().BeTrue();
actualQueryString.Contains("components=project1").Should().BeFalse();
}

private static GetIssuesRequest CreateTestSubject(string projectKey, string statusesToRequest, string branch = null, string[] issueKeys = null, string ruleId = null, string componentKey = null)
Expand All @@ -475,7 +475,6 @@ private static GetIssuesRequest CreateTestSubject(string projectKey, string stat
Branch = branch,
IssueKeys = issueKeys,
RuleId = ruleId,
ComponentKey = componentKey
};

return testSubject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarQube.Client.Api;
using SonarQube.Client.Api.V7_20;
using SonarQube.Client.Tests.Infra;
using static SonarQube.Client.Tests.Infra.MocksHelper;
Expand All @@ -34,10 +35,15 @@ namespace SonarQube.Client.Tests.Requests.Api.V7_20
[TestClass]
public class GetIssuesRequestWrapperTests
{
[TestMethod]
public async Task InvokeAsync_NoIssueKeys_ExpectedPropertiesArePassedInMultipleRequests()
private const string SonarQubeComponentPropertyName = "components";
private const string SonarCloudComponentPropertyName = "componentKeys";

[DataTestMethod]
[DataRow(SonarQubeComponentPropertyName, DisplayName = "SonarQube")]
[DataRow(SonarCloudComponentPropertyName, DisplayName = "SonarCloud")]
public async Task InvokeAsync_SonarQube_NoIssueKeys_ExpectedPropertiesArePassedInMultipleRequests(string componentPropertyName)
{
var testSubject = CreateTestSubject("aaaProject", "xStatus", "yBranch", null, "rule1", "project1");
var testSubject = CreateTestSubject(componentPropertyName, "aaaProject", "xStatus", "yBranch", null, "rule1", "project1");

var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var httpClient = new HttpClient(handlerMock.Object)
Expand All @@ -51,15 +57,18 @@ public async Task InvokeAsync_NoIssueKeys_ExpectedPropertiesArePassedInMultipleR

// The wrapper is expected to make three calls, for code smells, bugs, then vulnerabilities
handlerMock.Invocations.Count.Should().Be(3);
CheckExpectedQueryStringsParameters(handlerMock, 0, "aaaProject", "xStatus", "yBranch", "CODE_SMELL", "rule1", "project1");
CheckExpectedQueryStringsParameters(handlerMock, 1, "aaaProject", "xStatus", "yBranch", "BUG", "rule1", "project1");
CheckExpectedQueryStringsParameters(handlerMock, 2, "aaaProject", "xStatus", "yBranch", "VULNERABILITY", "rule1", "project1");
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 0, "aaaProject", "xStatus", "yBranch", "rule1", "project1", expectedTypes: "CODE_SMELL");
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 1, "aaaProject", "xStatus", "yBranch", "rule1", "project1", expectedTypes: "BUG");
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 2, "aaaProject", "xStatus", "yBranch", "rule1", "project1", expectedTypes: "VULNERABILITY");
}

[TestMethod]
public async Task InvokeAsync_HasIssueKeys_ExpectedPropertiesArePassedInASingleRequest()
[DataTestMethod]
[DataRow(SonarQubeComponentPropertyName, DisplayName = "SonarQube")]
[DataRow(SonarCloudComponentPropertyName, DisplayName = "SonarCloud")]
public async Task InvokeAsync_HasIssueKeys_ExpectedPropertiesArePassedInASingleRequest(string componentPropertyName)
{
var testSubject = CreateTestSubject("aaaProject", "xStatus", "yBranch", new[] { "issue1", "issue2" }, "rule1", "project1");
var issueKeys = new[] { "issue1", "issue2" };
var testSubject = CreateTestSubject(componentPropertyName,"aaaProject", "xStatus", "yBranch", issueKeys, "rule1", "project1");

var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var httpClient = new HttpClient(handlerMock.Object)
Expand All @@ -74,44 +83,76 @@ public async Task InvokeAsync_HasIssueKeys_ExpectedPropertiesArePassedInASingleR
// The wrapper is expected to make one call with the given issueKeys
handlerMock.Invocations.Count.Should().Be(1);

var actualQueryString = GetActualQueryStringForInvocation(handlerMock, 0);
actualQueryString.Contains("?projects=aaaProject").Should().BeTrue();
actualQueryString.Contains("&statuses=xStatus&").Should().BeTrue();
actualQueryString.Contains("&branch=yBranch&").Should().BeTrue();
actualQueryString.Contains("&issues=issue1%2Cissue2&").Should().BeTrue();
actualQueryString.Contains("&rules=rule1").Should().BeTrue();
actualQueryString.Contains("&components=project1").Should().BeTrue();
actualQueryString.Contains("types").Should().BeFalse();
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 0, "aaaProject", "xStatus", "yBranch", "rule1", "project1", expectedKeys: issueKeys);
}

private static GetIssuesRequestWrapper CreateTestSubject(string projectKey, string statusesToRequest, string branch, string[] issueKeys, string ruleId, string componentKey)
private static IGetIssuesRequest CreateTestSubject(string componentPropertyName, string projectKey, string statusesToRequest, string branch, string[] issueKeys, string ruleId, string componentKey)
{
var testSubject = new GetIssuesRequestWrapper
return componentPropertyName switch
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
Branch = branch,
IssueKeys = issueKeys,
RuleId = ruleId,
ComponentKey = componentKey
SonarQubeComponentPropertyName => new GetIssuesRequestWrapper<GetIssuesWithComponentSonarQubeRequest>
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
Branch = branch,
IssueKeys = issueKeys,
RuleId = ruleId,
ComponentKey = componentKey
},
SonarCloudComponentPropertyName => new GetIssuesRequestWrapper<GetIssuesWithComponentSonarCloudRequest>
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
Branch = branch,
IssueKeys = issueKeys,
RuleId = ruleId,
ComponentKey = componentKey
},
_ => throw new ArgumentOutOfRangeException()
};

return testSubject;
}

private static void CheckExpectedQueryStringsParameters(Mock<HttpMessageHandler> handlerMock, int invocationIndex,
string expectedProject, string expectedStatues, string expectedBranch, string expectedTypes, string expectedRule, string expectedComponent)
private static void CheckExpectedQueryStringsParameters(string componentKeyName,
Mock<HttpMessageHandler> handlerMock,
int invocationIndex,
string expectedProject,
string expectedStatues,
string expectedBranch,
string expectedRule,
string expectedComponent,
string expectedTypes = null,
string[] expectedKeys = null)
{
var actualQueryString = GetActualQueryStringForInvocation(handlerMock, invocationIndex);

Console.WriteLine($"Invocation [{invocationIndex}]: {actualQueryString}");
actualQueryString.Contains($"?projects={expectedProject}").Should().BeTrue();
actualQueryString.Contains($"&statuses={expectedStatues}&").Should().BeTrue();
actualQueryString.Contains($"&branch={expectedBranch}&").Should().BeTrue();
actualQueryString.Contains($"&types={expectedTypes}&").Should().BeTrue();
actualQueryString.Contains($"&rules={expectedRule}&").Should().BeTrue();
actualQueryString.Contains($"&components={expectedComponent}&").Should().BeTrue();
actualQueryString.Contains($"?{componentKeyName}={expectedComponent}").Should().BeTrue();
actualQueryString.Contains($"&projects={expectedProject}").Should().BeTrue();
actualQueryString.Contains($"&statuses={expectedStatues}").Should().BeTrue();
actualQueryString.Contains($"&branch={expectedBranch}").Should().BeTrue();
actualQueryString.Contains($"&rules={expectedRule}").Should().BeTrue();

if (expectedTypes != null)
{
actualQueryString.Contains($"&types={expectedTypes}").Should().BeTrue();
}
else
{
actualQueryString.Contains("types").Should().BeFalse();
}

if (expectedKeys != null)
{
var keys = string.Join("%2C", expectedKeys);
actualQueryString.Contains($"&issues={keys}").Should().BeTrue();
}
else
{
actualQueryString.Contains("issues").Should().BeFalse();
}

}

private static string GetActualQueryStringForInvocation(Mock<HttpMessageHandler> handlerMock, int invocationIndex)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarQube.Client.Api.V7_20;
using SonarQube.Client.Tests.Infra;
using static SonarQube.Client.Tests.Infra.MocksHelper;

namespace SonarQube.Client.Tests.Requests.Api.V7_20;

[TestClass]
public class GetIssuesSonarCloudRequestTests
{
[TestMethod]
public async Task InvokeAsync_ComponentKeyNotSpecified_ComponentsAreNotIncludedInQueryString()
{
var testSubject = CreateTestSubject("any", "any", componentKey: null);

var handlerMock = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri(ValidBaseAddress)
};

SetupHttpRequest(handlerMock, EmptyGetIssuesResponse);
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

var actualQueryString = GetSingleActualQueryString(handlerMock);
actualQueryString.Contains("components").Should().BeFalse();
}

[TestMethod]
public async Task InvokeAsync_ComponentKeySpecified_ComponentsAreIncludedInQueryString()
{
var testSubject = CreateTestSubject("any", "any", componentKey: "project1");

var handlerMock = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri(ValidBaseAddress)
};

SetupHttpRequest(handlerMock, EmptyGetIssuesResponse);
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

var actualQueryString = GetSingleActualQueryString(handlerMock);
actualQueryString.Contains("componentKeys=project1").Should().BeTrue();
}

private static GetIssuesWithComponentSonarCloudRequest CreateTestSubject(string projectKey, string statusesToRequest, string componentKey = null)
{
var testSubject = new GetIssuesWithComponentSonarCloudRequest
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
ComponentKey = componentKey
};

return testSubject;
}

private static string GetSingleActualQueryString(Mock<HttpMessageHandler> handlerMock)
{
handlerMock.Invocations.Count.Should().Be(1);
var requestMessage = (HttpRequestMessage)handlerMock.Invocations[0].Arguments[0];
return requestMessage.RequestUri.Query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarQube.Client.Api.V7_20;
using SonarQube.Client.Tests.Infra;
using static SonarQube.Client.Tests.Infra.MocksHelper;

namespace SonarQube.Client.Tests.Requests.Api.V7_20;

[TestClass]
public class GetIssuesSonarQubeRequestTests
{
[TestMethod]
public async Task InvokeAsync_ComponentKeyNotSpecified_ComponentsAreNotIncludedInQueryString()
{
var testSubject = CreateTestSubject("any", "any", componentKey: null);

var handlerMock = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri(ValidBaseAddress)
};

SetupHttpRequest(handlerMock, EmptyGetIssuesResponse);
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

var actualQueryString = GetSingleActualQueryString(handlerMock);
actualQueryString.Contains("components").Should().BeFalse();
}

[TestMethod]
public async Task InvokeAsync_ComponentKeySpecified_ComponentsAreIncludedInQueryString()
{
var testSubject = CreateTestSubject("any", "any", componentKey: "project1");

var handlerMock = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri(ValidBaseAddress)
};

SetupHttpRequest(handlerMock, EmptyGetIssuesResponse);
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

var actualQueryString = GetSingleActualQueryString(handlerMock);
actualQueryString.Contains("components=project1").Should().BeTrue();
}

private static GetIssuesWithComponentSonarQubeRequest CreateTestSubject(string projectKey, string statusesToRequest, string componentKey = null)
{
var testSubject = new GetIssuesWithComponentSonarQubeRequest
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
ComponentKey = componentKey
};

return testSubject;
}

private static string GetSingleActualQueryString(Mock<HttpMessageHandler> handlerMock)
{
handlerMock.Invocations.Count.Should().Be(1);
var requestMessage = (HttpRequestMessage)handlerMock.Invocations[0].Arguments[0];
return requestMessage.RequestUri.Query;
}
}
Loading

0 comments on commit 8c02b5a

Please sign in to comment.