Skip to content

Commit

Permalink
Fix muting of incorrect issues when bound to SonarCloud (#5182)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource authored Jan 19, 2024
1 parent 5af2802 commit 1509735
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ public async Task InvokeAsync_ComponentKeyNotSpecified_ComponentsAreNotIncludedI
_ = await testSubject.InvokeAsync(httpClient, CancellationToken.None);

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

[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.Should().NotContain("component");
}

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 ComponentPropertyNameSonarQube = "components";
private const string ComponentPropertyNameSonarCloud = "componentKeys";

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

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, expectedTypes: "CODE_SMELL");
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 1, expectedTypes: "BUG");
CheckExpectedQueryStringsParameters(componentPropertyName, handlerMock, 2, expectedTypes: "VULNERABILITY");
}

[TestMethod]
public async Task InvokeAsync_HasIssueKeys_ExpectedPropertiesArePassedInASingleRequest()
[DataTestMethod]
[DataRow(ComponentPropertyNameSonarQube, DisplayName = "SonarQube")]
[DataRow(ComponentPropertyNameSonarCloud, 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", "component1");

var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var httpClient = new HttpClient(handlerMock.Object)
Expand All @@ -74,44 +83,71 @@ 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, 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
ComponentPropertyNameSonarQube => new GetIssuesRequestWrapper<GetIssuesWithComponentSonarQubeRequest>
{
Logger = new TestLogger(),
ProjectKey = projectKey,
Statuses = statusesToRequest,
Branch = branch,
IssueKeys = issueKeys,
RuleId = ruleId,
ComponentKey = componentKey
},
ComponentPropertyNameSonarCloud => 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 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}=component1").Should().BeTrue();
actualQueryString.Contains("&projects=aaaProject").Should().BeTrue();
actualQueryString.Contains("&statuses=xStatus").Should().BeTrue();
actualQueryString.Contains("&branch=yBranch").Should().BeTrue();
actualQueryString.Contains("&rules=rule1").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 GetIssuesWithComponentSonarCloudRequestTests
{
[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.Should().NotContain("component");
}

[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.Should().Contain("componentKeys=project1");
}

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 GetIssuesWithComponentSonarQubeRequestTests
{
[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.Should().NotContain("component");
}

[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.Should().Contain("components=project1");
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace SonarQube.Client.Tests.Requests
{
[TestClass]
public class DefaultConfiguration_Configure
public class DefaultConfiguration_Configure_Tests
{
[TestMethod]
public void ConfigureSonarQube_Writes_Debug_Messages()
Expand Down Expand Up @@ -62,7 +62,7 @@ public void ConfigureSonarQube_Writes_Debug_Messages()
"Registered SonarQube.Client.Api.V6_60.GetRoslynExportProfileRequest for 6.6",
"Registered SonarQube.Client.Api.V6_60.GetProjectBranchesRequest for 6.6",
"Registered SonarQube.Client.Api.V7_00.GetOrganizationsRequest for 7.0",
"Registered SonarQube.Client.Api.V7_20.GetIssuesRequestWrapper for 7.2",
"Registered SonarQube.Client.Api.V7_20.GetIssuesRequestWrapper`1[SonarQube.Client.Api.V7_20.GetIssuesWithComponentSonarQubeRequest] for 7.2",
"Registered SonarQube.Client.Api.V8_6.GetHotspotRequest for 8.6",
"Registered SonarQube.Client.Api.V8_6.GetTaintVulnerabilitiesRequest for 8.6",
"Registered SonarQube.Client.Api.V7_20.GetExclusionsRequest for 7.2",
Expand Down Expand Up @@ -111,7 +111,7 @@ public void ConfigureSonarCloud_Writes_Debug_Messages()
"Registered SonarQube.Client.Api.V6_60.GetRoslynExportProfileRequest",
"Registered SonarQube.Client.Api.V6_60.GetProjectBranchesRequest",
"Registered SonarQube.Client.Api.V7_00.GetOrganizationsRequest",
"Registered SonarQube.Client.Api.V7_20.GetIssuesRequestWrapper",
"Registered SonarQube.Client.Api.V7_20.GetIssuesRequestWrapper`1[SonarQube.Client.Api.V7_20.GetIssuesWithComponentSonarCloudRequest]",
"Registered SonarQube.Client.Api.V8_6.GetHotspotRequest",
"Registered SonarQube.Client.Api.V10_2.GetTaintVulnerabilitiesWithCCTRequest",
"Registered SonarQube.Client.Api.V7_20.GetExclusionsRequest",
Expand Down
Loading

0 comments on commit 1509735

Please sign in to comment.