using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.SAUB; using DocumentFormat.OpenXml.Office2010.ExcelAc; using NSubstitute; using Org.BouncyCastle.Asn1.Ocsp; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using Xunit; namespace AsbCloudInfrastructure.Tests.Services; public class MessageServiceTest { private static readonly List telemetries = new List() { new TelemetryBaseDto() { Id = 1, TimeZone = new SimpleTimezoneDto() { Hours = 5 } }, new TelemetryBaseDto() { Id = 2, TimeZone = new SimpleTimezoneDto() { Hours = 5 } } }; private static readonly List events = new List() { new TelemetryEventDto() { Id = 1, IdCategory = 1, MessageTemplate = "Шаблон сообщения 1 категории 1" }, new TelemetryEventDto() { Id = 2, IdCategory = 1, MessageTemplate = "Шаблон сообщения 2 категории 1" }, new TelemetryEventDto() { Id = 3, IdCategory = 1, MessageTemplate = "Шаблон сообщения 3 категории 1" }, new TelemetryEventDto() { Id = 4, IdCategory = 2, MessageTemplate = "Шаблон сообщения 1 категории 2" }, new TelemetryEventDto() { Id = 5, IdCategory = 2, MessageTemplate = "Шаблон сообщения 2 категории 2" }, new TelemetryEventDto() { Id = 6, IdCategory = 3, MessageTemplate = "Шаблон сообщения 1 категории 3" } }; /// /// Структура: /// Телеметрия 1: /// Категория 1: /// Событие 1: /// Сообщение 1 /// Сообщение 2 /// Сообщение 3 /// Событие 2: /// Сообщение 1 /// Сообщение 2 /// Сообщение 3 /// Событие 3: /// Сообщение 1 /// Сообщение 2 /// Сообщение 3 /// Категория 2: /// Событие 2: /// Сообщение 1 /// Сообщение 2 /// Событие 3: /// Сообщение 1 /// Сообщение 2 /// Телеметрия 2 /// Категория 3: /// Событие 3: /// Сообщение 1 /// Сообщение 2 /// Сообщение 3 /// /// private static readonly List messages = new List() { new TelemetryMessageDto() { Id = 1, IdEvent = 1, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 2, IdEvent = 1, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 3, IdEvent = 1, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 4, IdEvent = 2, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 5, IdEvent = 2, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 6, IdEvent = 2, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 7, IdEvent = 3, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 8, IdEvent = 3, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 9, IdEvent = 3, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 10, IdEvent = 4, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 11, IdEvent = 4, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 12, IdEvent = 5, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 13, IdEvent = 5, IdTelemetry = 1 }, new TelemetryMessageDto() { Id = 14, IdEvent = 6, IdTelemetry = 2 }, new TelemetryMessageDto() { Id = 15, IdEvent = 6, IdTelemetry = 2 }, new TelemetryMessageDto() { Id = 16, IdEvent = 6, IdTelemetry = 2 }, }; private readonly IMessageService messageServiceMock = Substitute.For(); private readonly IMessageRepository messageRepositoryMock = Substitute.For(); private readonly IEventRepository eventRepositoryMock = Substitute.For(); private readonly ITelemetryService telemetryServiceMock = Substitute.For(); private readonly IAsbCloudDbContext db = Substitute.For(); public MessageServiceTest() { messageServiceMock = new MessageService(messageRepositoryMock, telemetryServiceMock, eventRepositoryMock); telemetryServiceMock .GetOrDefaultTelemetriesByIdsWells(Arg.Any>()) .Returns(telemetries); eventRepositoryMock .Get(Arg.Any(), Arg.Any()) .Returns(events); messageRepositoryMock .GetMessagesAsync(Arg.Any(), Arg.Any()) .Returns(messages); } [Fact] public async Task GetStatMessages_ShouldReturn_Success() { //act var baseRequest = new MessageRequestBase() { }; var messageRequest = new MessageRequest(baseRequest, [1]); var result = await messageServiceMock.GetStat(messageRequest, CancellationToken.None); Assert.Equal(6, result.Count()); var mCount_Telemetry_1_Category_1_Template_1_Expected = messages .Where(x => x.IdTelemetry == 1) .Where(x => x.IdEvent == 1) .Count(); var mCount_Telemetry_1_Category_1_Template_1_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual); var mCount_Telemetry_1_Category_1_Template_2_Expected = messages .Where(x => x.IdTelemetry == 1) .Where(x => x.IdEvent == 2) .Count(); var mCount_Telemetry_1_Category_1_Template_2_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual); var mCount_Telemetry_1_Category_1_Template_3_Expected = messages .Where(x => x.IdTelemetry == 1) .Where(x => x.IdEvent == 3) .Count(); var mCount_Telemetry_1_Category_1_Template_3_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual); var mCount_Telemetry_1_Category_2_Template_2_Expected = messages .Where(x => x.IdTelemetry == 1) .Where(x => x.IdEvent == 5) .Count(); var mCount_Telemetry_1_Category_2_Template_2_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 2") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual); var mCount_Telemetry_1_Category_2_Template_3_Expected = messages .Where(x => x.IdTelemetry == 1) .Where(x => x.IdEvent == 6) .Count(); var mCount_Telemetry_1_Category_2_Template_3_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 2") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual); var mCount_Telemetry_2_Category_3_Template_1_Expected = messages .Where(x => x.IdTelemetry == 2) .Where(x => x.IdEvent == 6) .Count(); var mCount_Telemetry_2_Category_3_Template_1_Actual = result .Where(x => x.IdTelemetry == 2) .Where(x => x.IdCategory == 3) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 3") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual); } }