using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using Persistence.Client; using Persistence.Client.Clients; using Persistence.Database.Entity; using Persistence.Models; using Persistence.Models.Requests; using System.Net; using Xunit; namespace Persistence.IntegrationTests.Controllers { public class TechMessagesControllerTest : BaseIntegrationTest { private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DrillingSystem).FullName}CacheKey"; private readonly ITechMessagesClient techMessagesClient; private readonly IMemoryCache memoryCache; public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory) { var scope = factory.Services.CreateScope(); var persistenceClientFactory = scope.ServiceProvider .GetRequiredService(); techMessagesClient = persistenceClientFactory.GetClient(); memoryCache = scope.ServiceProvider.GetRequiredService(); } [Fact] public async Task GetPage_returns_success() { //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); var PaginationRequest = new PaginationRequest() { Skip = 1, Take = 2, SortSettings = nameof(TechMessage.CategoryId) }; //act var response = await techMessagesClient.GetPage(PaginationRequest, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content.Items); Assert.Equal(PaginationRequest.Skip, response.Content.Skip); Assert.Equal(PaginationRequest.Take, response.Content.Take); } [Fact] public async Task GetPage_AfterSave_returns_success() { //arrange var dtos = await InsertRange(); var dtosCount = dtos.Count(); var PaginationRequest = new PaginationRequest() { Skip = 0, Take = 2, SortSettings = nameof(TechMessage.CategoryId) }; //act var response = await techMessagesClient.GetPage(PaginationRequest, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Equal(dtosCount, response.Content.Count); } [Fact] public async Task InsertRange_returns_success() { await InsertRange(); } [Fact] public async Task InsertRange_returns_BadRequest() { //arrange var dtos = new List() { new() { EventId = Guid.NewGuid(), CategoryId = -1, // < 0 Timestamp = DateTimeOffset.UtcNow, Depth = -1, // < 0 MessageText = string.Empty, // length < 0 System = string.Concat(Enumerable.Repeat(nameof(TechMessageDto.System), 100)), // length > 256 UserId = Guid.NewGuid() } }; //act var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } [Fact] public async Task GetSystems_returns_success() { //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); //act var response = await techMessagesClient.GetSystems(new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetSystems_AfterSave_returns_success() { //arrange var dtos = await InsertRange(); var systems = dtos .Select(e => e.System) .Distinct() .ToArray(); //act var response = await techMessagesClient.GetSystems(new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); string?[]? content = response.Content?.ToArray(); Assert.Equal(systems, content); } [Fact] public async Task GetStatistics_returns_success() { //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); var importantId = 1; var autoDrillingSystem = nameof(TechMessageDto.System); //act var response = await techMessagesClient.GetStatistics(autoDrillingSystem, importantId, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetStatistics_AfterSave_returns_success() { //arrange var importantId = 0; var autoDrillingSystem = nameof(TechMessageDto.System); var dtos = await InsertRange(); var filteredDtos = dtos.Where(e => e.CategoryId == importantId && e.System == autoDrillingSystem); //act var response = await techMessagesClient.GetStatistics(autoDrillingSystem, importantId, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); var categories = response.Content .FirstOrDefault()?.Categories .FirstOrDefault(e => e.Key == 0).Value; Assert.Equal(filteredDtos.Count(), categories); } [Fact] public async Task GetDatesRange_returns_success() { //act var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); //Assert.Equal(DateTimeOffset.MinValue, response.Content?.From); //Assert.Equal(DateTimeOffset.MaxValue, response.Content?.To); } [Fact] public async Task GetDatesRange_AfterSave_returns_success() { //arrange await InsertRange(); //act var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotNull(response.Content?.From); Assert.NotNull(response.Content?.To); } [Fact] public async Task GetPart_returns_success() { //arrange var dateBegin = DateTimeOffset.UtcNow; var take = 2; //act var response = await techMessagesClient.GetPart(dateBegin, take, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetPart_AfterSave_returns_success() { //arrange var dateBegin = DateTimeOffset.UtcNow; var take = 1; await InsertRange(); //act var response = await techMessagesClient.GetPart(dateBegin, take, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); } private async Task> InsertRange() { //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); var dtos = new List() { new() { EventId = Guid.NewGuid(), CategoryId = 1, Timestamp = DateTimeOffset.UtcNow, Depth = 1.11, MessageText = nameof(TechMessageDto.MessageText), System = nameof(TechMessageDto.System).ToLower(), UserId = Guid.NewGuid() }, new() { EventId = Guid.NewGuid(), CategoryId = 2, Timestamp = DateTimeOffset.UtcNow, Depth = 2.22, MessageText = nameof(TechMessageDto.MessageText), System = nameof(TechMessageDto.System).ToLower(), UserId = Guid.NewGuid() } }; //act var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(dtos.Count, response.Content); return dtos; } } }