Skip to content

Commit

Permalink
SLVS-1621 Clean up custom SSE implementation for taints (#5826)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource authored Nov 14, 2024
1 parent fb03a90 commit 7dc403b
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 1,023 deletions.
103 changes: 46 additions & 57 deletions src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,70 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


using System;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarQube.Client;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.Taint;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.Issue;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.QualityProfile;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.ServerSentEvents
namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.ServerSentEvents;

[TestClass]
public class SSESessionFactoryTests
{
[TestClass]
public class SSESessionFactoryTests
[TestMethod]
public void MefCtor_CheckIsExported()
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SSESessionFactory, ISSESessionFactory>(
MefTestHelpers.CreateExport<ISonarQubeService>(),
MefTestHelpers.CreateExport<ITaintServerEventSourcePublisher>(),
MefTestHelpers.CreateExport<IIssueServerEventSourcePublisher>(),
MefTestHelpers.CreateExport<IQualityProfileServerEventSourcePublisher>(),
MefTestHelpers.CreateExport<ILogger>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}
MefTestHelpers.CheckTypeCanBeImported<SSESessionFactory, ISSESessionFactory>(
MefTestHelpers.CreateExport<ISonarQubeService>(),
MefTestHelpers.CreateExport<IIssueServerEventSourcePublisher>(),
MefTestHelpers.CreateExport<IQualityProfileServerEventSourcePublisher>(),
MefTestHelpers.CreateExport<ILogger>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}

[TestMethod]
public void Create_ReturnsCorrectType()
{
var testSubject = CreateTestSubject();
[TestMethod]
public void Create_ReturnsCorrectType()
{
var testSubject = CreateTestSubject();

var sseSession = testSubject.Create("MyProjectName", null);
var sseSession = testSubject.Create("MyProjectName", null);

sseSession.Should().NotBeNull().And.BeOfType<SSESessionFactory.SSESession>();
}
sseSession.Should().NotBeNull().And.BeOfType<SSESessionFactory.SSESession>();
}

[TestMethod]
public void Create_AfterDispose_Throws()
{
var testSubject = CreateTestSubject();
[TestMethod]
public void Create_AfterDispose_Throws()
{
var testSubject = CreateTestSubject();

testSubject.Dispose();
Action act = () => testSubject.Create("MyProjectName", null);
testSubject.Dispose();
Action act = () => testSubject.Create("MyProjectName", null);

act.Should().Throw<ObjectDisposedException>();
}
act.Should().Throw<ObjectDisposedException>();
}

[TestMethod]
public void Dispose_IdempotentAndDisposesPublishers()
{
var taintPublisherMock = new Mock<ITaintServerEventSourcePublisher>();
var issuesPublisherMock = new Mock<IIssueServerEventSourcePublisher>();
var qualityProfilePublisherMock = new Mock<IQualityProfileServerEventSourcePublisher>();
var testSubject = CreateTestSubject(taintPublisherMock, issuesPublisherMock, qualityProfilePublisherMock);
[TestMethod]
public void Dispose_IdempotentAndDisposesPublishers()
{
var issuesPublisherMock = new Mock<IIssueServerEventSourcePublisher>();
var qualityProfilePublisherMock = new Mock<IQualityProfileServerEventSourcePublisher>();
var testSubject = CreateTestSubject(issuesPublisherMock, qualityProfilePublisherMock);

testSubject.Dispose();
testSubject.Dispose();
testSubject.Dispose();
testSubject.Dispose();
testSubject.Dispose();
testSubject.Dispose();

taintPublisherMock.Verify(p => p.Dispose(), Times.Once);
issuesPublisherMock.Verify(p => p.Dispose(), Times.Once);
qualityProfilePublisherMock.Verify(p => p.Dispose(), Times.Once);
}
issuesPublisherMock.Verify(p => p.Dispose(), Times.Once);
qualityProfilePublisherMock.Verify(p => p.Dispose(), Times.Once);
}

private SSESessionFactory CreateTestSubject(Mock<ITaintServerEventSourcePublisher> taintPublisher = null,
Mock<IIssueServerEventSourcePublisher> issuePublisher = null,
Mock<IQualityProfileServerEventSourcePublisher> qualityProfileServerEventSourcePublisher = null)
{
return new SSESessionFactory(Mock.Of<ISonarQubeService>(),
taintPublisher?.Object ?? Mock.Of<ITaintServerEventSourcePublisher>(),
issuePublisher?.Object ?? Mock.Of<IIssueServerEventSourcePublisher>(),
qualityProfileServerEventSourcePublisher?.Object ?? Mock.Of<IQualityProfileServerEventSourcePublisher>(),
private SSESessionFactory CreateTestSubject(Mock<IIssueServerEventSourcePublisher> issuePublisher = null,
Mock<IQualityProfileServerEventSourcePublisher> qualityProfileServerEventSourcePublisher = null) =>
new(Mock.Of<ISonarQubeService>(),
issuePublisher?.Object ?? Mock.Of<IIssueServerEventSourcePublisher>(),
qualityProfileServerEventSourcePublisher?.Object ?? Mock.Of<IQualityProfileServerEventSourcePublisher>(),
Mock.Of<IThreadHandling>(),
Mock.Of<ILogger>());
}
}
Mock.Of<ILogger>());
}
23 changes: 1 addition & 22 deletions src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarQube.Client;
using SonarQube.Client.Models.ServerSentEvents;
using SonarQube.Client.Models.ServerSentEvents.ClientContract;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.Taint;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.Issue;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents.QualityProfile;

Expand Down Expand Up @@ -62,22 +55,17 @@ public async Task PumpAllAsync_SelectsPublisherCorrectlyAndPreservesOrderWithinT
var inputSequence = new IServerEvent[]
{
Mock.Of<IIssueChangedServerEvent>(),
Mock.Of<ITaintVulnerabilityRaisedServerEvent>(),
Mock.Of<IQualityProfileEvent>(),
Mock.Of<IIssueChangedServerEvent>(),
Mock.Of<ITaintVulnerabilityClosedServerEvent>(),
Mock.Of<ITaintVulnerabilityClosedServerEvent>(),
Mock.Of<IQualityProfileEvent>(),
Mock.Of<IIssueChangedServerEvent>(),
Mock.Of<ITaintVulnerabilityRaisedServerEvent>()
};
testScope.SetUpSwitchToBackgroundThread();
var sseStreamMock = testScope.SetUpSQServiceToSuccessfullyReturnSSEStreamReader();
testScope.SetUpSSEStreamReaderToReturnEventsSequenceAndExit(sseStreamMock, inputSequence);

await testScope.TestSubject.PumpAllAsync();

CheckEventsSequence<ITaintServerEvent>(testScope.TaintPublisherMock.Invocations);
CheckEventsSequence<IIssueChangedServerEvent>(testScope.IssuePublisherMock.Invocations);
CheckEventsSequence<IQualityProfileEvent>(testScope.QualityProfilePublisherMock.Invocations);

Expand All @@ -100,18 +88,15 @@ public async Task PumpAllAsync_WhenNullEvent_Ignores()
testScope.SetUpSSEStreamReaderToReturnEventsSequenceAndExit(sseStreamMock,
new IServerEvent[]
{
Mock.Of<ITaintVulnerabilityRaisedServerEvent>(),
Mock.Of<IQualityProfileEvent>(),
null,
Mock.Of<ITaintVulnerabilityRaisedServerEvent>(),
Mock.Of<IIssueChangedServerEvent>(),
Mock.Of<IQualityProfileEvent>(),
Mock.Of<IIssueChangedServerEvent>()
});

await testScope.TestSubject.PumpAllAsync();

testScope.TaintPublisherMock.Verify(publisher => publisher.Publish(It.IsAny<ITaintServerEvent>()), Times.Exactly(2));
testScope.IssuePublisherMock.Verify(publisher => publisher.Publish(It.IsAny<IIssueChangedServerEvent>()), Times.Exactly(2));
testScope.QualityProfilePublisherMock.Verify(publisher => publisher.Publish(It.IsAny<IQualityProfileEvent>()), Times.Exactly(2));
}
Expand All @@ -126,16 +111,13 @@ public async Task PumpAllAsync_WhenUnsupportedEvent_Ignores()
testScope.SetUpSSEStreamReaderToReturnEventsSequenceAndExit(sseStreamMock,
new IServerEvent[]
{
Mock.Of<ITaintVulnerabilityRaisedServerEvent>(),
Mock.Of<IQualityProfileEvent>(),
Mock.Of<IDummyServerEvent>(),
Mock.Of<ITaintVulnerabilityRaisedServerEvent>(),
Mock.Of<IIssueChangedServerEvent>()
});

await testScope.TestSubject.PumpAllAsync();

testScope.TaintPublisherMock.Verify(publisher => publisher.Publish(It.IsAny<ITaintServerEvent>()), Times.Exactly(2));
testScope.IssuePublisherMock.Verify(publisher => publisher.Publish(It.IsAny<IIssueChangedServerEvent>()), Times.Exactly(1));
testScope.QualityProfilePublisherMock.Verify(publisher => publisher.Publish(It.IsAny<IQualityProfileEvent>()), Times.Exactly(1));
}
Expand Down Expand Up @@ -232,7 +214,6 @@ public TestScope()
{
mockRepository = new MockRepository(MockBehavior.Strict);
SonarQubeServiceMock = mockRepository.Create<ISonarQubeService>();
TaintPublisherMock = mockRepository.Create<ITaintServerEventSourcePublisher>(MockBehavior.Loose);
IssuePublisherMock = mockRepository.Create<IIssueServerEventSourcePublisher>(MockBehavior.Loose);
QualityProfilePublisherMock = mockRepository.Create<IQualityProfileServerEventSourcePublisher>(MockBehavior.Loose);
ThreadHandlingMock = mockRepository.Create<IThreadHandling>();
Expand All @@ -241,18 +222,16 @@ public TestScope()

var factory = new SSESessionFactory(
SonarQubeServiceMock.Object,
TaintPublisherMock.Object,
IssuePublisherMock.Object,
QualityProfilePublisherMock.Object,
ThreadHandlingMock.Object,
LoggerMock.Object);

TestSubject = factory.Create("blalala", OnSessionFailedAsyncMock.Object);
}

private Mock<IThreadHandling> ThreadHandlingMock { get; }
public Mock<ISonarQubeService> SonarQubeServiceMock { get; }
public Mock<ITaintServerEventSourcePublisher> TaintPublisherMock { get; }
public Mock<IIssueServerEventSourcePublisher> IssuePublisherMock { get; }
public Mock<IQualityProfileServerEventSourcePublisher> QualityProfilePublisherMock { get; }
public Mock<ILogger> LoggerMock { get; }
Expand Down
Loading

0 comments on commit 7dc403b

Please sign in to comment.