using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using EasyNetQ; using MalwareMultiScan.Backends.Interfaces; using MalwareMultiScan.Backends.Messages; using MalwareMultiScan.Scanner.Services.Implementations; using MalwareMultiScan.Scanner.Services.Interfaces; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Memory; using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; namespace MalwareMultiScan.Tests.Scanner { public class ScanHostedServiceTests { private Mock _busMock; private Mock _scanBackendMock; private IScanHostedService _scanHostedService; [SetUp] public void SetUp() { var configuration = new ConfigurationRoot(new List { new MemoryConfigurationProvider(new MemoryConfigurationSource()) }) { ["ResultsSubscriptionId"] = "mms.results" }; _busMock = new Mock(); _busMock .Setup(x => x.Receive("dummy", It.IsAny>())) .Callback>((s, func) => { var task = func.Invoke(new ScanRequestMessage { Id = "test", Uri = new Uri("http://test.com") }); task.Wait(); }); _scanBackendMock = new Mock(); _scanBackendMock .Setup(x => x.ScanAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(new[] {"Test"})); _scanBackendMock .SetupGet(x => x.Id) .Returns("dummy"); _scanHostedService = new ScanHostedService( Mock.Of>(), _scanBackendMock.Object, _busMock.Object, configuration); } [Test] public async Task TestBusReceiveScanResultMessage() { await _scanHostedService.StartAsync(default); _busMock.Verify(x => x.SendAsync("mms.results", It.Is( m => m.Succeeded && m.Backend == "dummy" && m.Id == "test" && m.Threats.Contains("Test") ))); } [Test] public async Task TestBusIsDisposedOnStop() { await _scanHostedService.StopAsync(default); _busMock.Verify(x => x.Dispose(), Times.Once); } } }