2024-11-28 13:13:07 +05:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2024-11-26 12:27:52 +05:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Persistence.Client;
|
2024-12-09 14:45:35 +05:00
|
|
|
using Persistence.Client.Clients.Interfaces;
|
2024-11-26 12:27:52 +05:00
|
|
|
using Persistence.Database.Entity;
|
|
|
|
using Persistence.Models;
|
2024-12-05 11:30:07 +05:00
|
|
|
using Persistence.Models.Requests;
|
2024-11-26 12:27:52 +05:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Persistence.IntegrationTests.Controllers
|
|
|
|
{
|
|
|
|
public class TechMessagesControllerTest : BaseIntegrationTest
|
|
|
|
{
|
2024-12-02 15:14:00 +05:00
|
|
|
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DrillingSystem).FullName}CacheKey";
|
2024-11-26 12:27:52 +05:00
|
|
|
private readonly ITechMessagesClient techMessagesClient;
|
2024-11-28 13:13:07 +05:00
|
|
|
private readonly IMemoryCache memoryCache;
|
2024-11-26 12:27:52 +05:00
|
|
|
public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory)
|
|
|
|
{
|
|
|
|
var scope = factory.Services.CreateScope();
|
|
|
|
var persistenceClientFactory = scope.ServiceProvider
|
|
|
|
.GetRequiredService<PersistenceClientFactory>();
|
|
|
|
|
2024-12-09 14:45:35 +05:00
|
|
|
techMessagesClient = persistenceClientFactory.GetTechMessagesClient();
|
2024-11-28 13:13:07 +05:00
|
|
|
memoryCache = scope.ServiceProvider.GetRequiredService<IMemoryCache>();
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetPage_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
2024-12-02 15:14:00 +05:00
|
|
|
memoryCache.Remove(SystemCacheKey);
|
2024-11-26 12:27:52 +05:00
|
|
|
dbContext.CleanupDbSet<TechMessage>();
|
2024-12-02 15:14:00 +05:00
|
|
|
dbContext.CleanupDbSet<Database.Entity.DrillingSystem>();
|
|
|
|
|
2024-12-10 13:55:01 +05:00
|
|
|
var requestDto = new PaginationRequest()
|
2024-11-26 12:27:52 +05:00
|
|
|
{
|
|
|
|
Skip = 1,
|
|
|
|
Take = 2,
|
2024-11-28 13:13:07 +05:00
|
|
|
SortSettings = nameof(TechMessage.CategoryId)
|
2024-11-26 12:27:52 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
//act
|
2024-12-10 13:55:01 +05:00
|
|
|
var response = await techMessagesClient.GetPage(requestDto, new CancellationToken());
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Empty(response.Items);
|
|
|
|
Assert.Equal(requestDto.Skip, response.Skip);
|
|
|
|
Assert.Equal(requestDto.Take, response.Take);
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetPage_AfterSave_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
var dtos = await InsertRange();
|
|
|
|
var dtosCount = dtos.Count();
|
2024-12-05 11:30:07 +05:00
|
|
|
var PaginationRequest = new PaginationRequest()
|
2024-11-26 12:27:52 +05:00
|
|
|
{
|
|
|
|
Skip = 0,
|
|
|
|
Take = 2,
|
2024-11-28 13:13:07 +05:00
|
|
|
SortSettings = nameof(TechMessage.CategoryId)
|
2024-11-26 12:27:52 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
//act
|
2024-12-05 11:30:07 +05:00
|
|
|
var response = await techMessagesClient.GetPage(PaginationRequest, new CancellationToken());
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Equal(dtosCount, response.Count);
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task InsertRange_returns_success()
|
|
|
|
{
|
|
|
|
await InsertRange();
|
|
|
|
}
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
[Fact]
|
|
|
|
public async Task InsertRange_returns_BadRequest()
|
|
|
|
{
|
|
|
|
//arrange
|
2024-12-09 14:45:35 +05:00
|
|
|
var exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса";
|
2024-11-28 13:13:07 +05:00
|
|
|
var dtos = new List<TechMessageDto>()
|
|
|
|
{
|
|
|
|
new TechMessageDto()
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-12-09 14:45:35 +05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
//act
|
|
|
|
var response = await techMessagesClient.AddRange(dtos, new CancellationToken());
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
//assert
|
|
|
|
Assert.Equal(exceptionMessage, ex.Message);
|
|
|
|
}
|
2024-11-28 13:13:07 +05:00
|
|
|
}
|
|
|
|
|
2024-11-26 12:27:52 +05:00
|
|
|
[Fact]
|
|
|
|
public async Task GetSystems_returns_success()
|
|
|
|
{
|
2024-11-28 13:13:07 +05:00
|
|
|
//arrange
|
|
|
|
memoryCache.Remove(SystemCacheKey);
|
2024-12-02 15:14:00 +05:00
|
|
|
dbContext.CleanupDbSet<TechMessage>();
|
|
|
|
dbContext.CleanupDbSet<Database.Entity.DrillingSystem>();
|
2024-11-28 13:13:07 +05:00
|
|
|
|
2024-11-26 12:27:52 +05:00
|
|
|
//act
|
|
|
|
var response = await techMessagesClient.GetSystems(new CancellationToken());
|
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Empty(response);
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetSystems_AfterSave_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
var dtos = await InsertRange();
|
|
|
|
var systems = dtos
|
2024-11-28 08:55:50 +05:00
|
|
|
.Select(e => e.System)
|
2024-11-26 12:27:52 +05:00
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
//act
|
|
|
|
var response = await techMessagesClient.GetSystems(new CancellationToken());
|
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
string?[]? content = response?.ToArray();
|
2024-11-26 12:27:52 +05:00
|
|
|
Assert.Equal(systems, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetStatistics_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
2024-12-02 15:14:00 +05:00
|
|
|
memoryCache.Remove(SystemCacheKey);
|
2024-11-26 12:27:52 +05:00
|
|
|
dbContext.CleanupDbSet<TechMessage>();
|
2024-12-02 15:14:00 +05:00
|
|
|
dbContext.CleanupDbSet<Database.Entity.DrillingSystem>();
|
|
|
|
|
2024-11-26 12:27:52 +05:00
|
|
|
var imortantId = 1;
|
2024-11-28 08:55:50 +05:00
|
|
|
var autoDrillingSystem = nameof(TechMessageDto.System);
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//act
|
2024-11-28 13:13:07 +05:00
|
|
|
var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, new CancellationToken());
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Empty(response);
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetStatistics_AfterSave_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
2024-11-28 13:13:07 +05:00
|
|
|
var imortantId = 0;
|
2024-11-28 08:55:50 +05:00
|
|
|
var autoDrillingSystem = nameof(TechMessageDto.System);
|
2024-11-26 12:27:52 +05:00
|
|
|
var dtos = await InsertRange();
|
2024-12-02 15:14:00 +05:00
|
|
|
var filteredDtos = dtos.Where(e => e.CategoryId == imortantId && e.System == autoDrillingSystem);
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//act
|
2024-11-28 13:13:07 +05:00
|
|
|
var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, new CancellationToken());
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
var categories = response
|
2024-12-02 15:14:00 +05:00
|
|
|
.FirstOrDefault()?.Categories
|
|
|
|
.FirstOrDefault(e => e.Key == 0).Value;
|
|
|
|
Assert.Equal(filteredDtos.Count(), categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetDatesRange_returns_success()
|
|
|
|
{
|
2024-12-09 14:45:35 +05:00
|
|
|
//arrange
|
|
|
|
memoryCache.Remove(SystemCacheKey);
|
|
|
|
dbContext.CleanupDbSet<TechMessage>();
|
|
|
|
dbContext.CleanupDbSet<DrillingSystem>();
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
//act
|
|
|
|
var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken());
|
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Equal(DateTimeOffset.MinValue, response?.From);
|
|
|
|
Assert.Equal(DateTimeOffset.MaxValue, response?.To);
|
2024-12-02 15:14:00 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetDatesRange_AfterSave_returns_success()
|
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
await InsertRange();
|
|
|
|
|
|
|
|
//act
|
|
|
|
var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken());
|
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.NotNull(response?.From);
|
|
|
|
Assert.NotNull(response?.To);
|
2024-12-02 15:14:00 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.Empty(response);
|
2024-12-02 15:14:00 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.NotNull(response);
|
|
|
|
Assert.NotEmpty(response);
|
2024-11-26 12:27:52 +05:00
|
|
|
}
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
private async Task<IEnumerable<TechMessageDto>> InsertRange()
|
2024-11-26 12:27:52 +05:00
|
|
|
{
|
|
|
|
//arrange
|
2024-12-02 15:14:00 +05:00
|
|
|
memoryCache.Remove(SystemCacheKey);
|
|
|
|
dbContext.CleanupDbSet<TechMessage>();
|
|
|
|
dbContext.CleanupDbSet<DrillingSystem>();
|
|
|
|
|
2024-11-26 12:27:52 +05:00
|
|
|
var dtos = new List<TechMessageDto>()
|
|
|
|
{
|
|
|
|
new TechMessageDto()
|
|
|
|
{
|
|
|
|
EventId = Guid.NewGuid(),
|
2024-11-28 08:55:50 +05:00
|
|
|
CategoryId = 1,
|
|
|
|
Timestamp = DateTimeOffset.UtcNow,
|
2024-11-26 12:27:52 +05:00
|
|
|
Depth = 1.11,
|
|
|
|
MessageText = nameof(TechMessageDto.MessageText),
|
2024-12-02 15:14:00 +05:00
|
|
|
System = nameof(TechMessageDto.System).ToLower(),
|
2024-11-26 12:27:52 +05:00
|
|
|
UserId = Guid.NewGuid()
|
|
|
|
},
|
|
|
|
new TechMessageDto()
|
|
|
|
{
|
|
|
|
EventId = Guid.NewGuid(),
|
2024-11-28 08:55:50 +05:00
|
|
|
CategoryId = 2,
|
|
|
|
Timestamp = DateTimeOffset.UtcNow,
|
2024-11-26 12:27:52 +05:00
|
|
|
Depth = 2.22,
|
|
|
|
MessageText = nameof(TechMessageDto.MessageText),
|
2024-12-02 15:14:00 +05:00
|
|
|
System = nameof(TechMessageDto.System).ToLower(),
|
2024-11-26 12:27:52 +05:00
|
|
|
UserId = Guid.NewGuid()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//act
|
2024-12-04 14:44:33 +05:00
|
|
|
var response = await techMessagesClient.AddRange(dtos, new CancellationToken());
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
//assert
|
2024-12-09 14:45:35 +05:00
|
|
|
Assert.Equal(dtos.Count, response);
|
2024-11-26 12:27:52 +05:00
|
|
|
|
|
|
|
return dtos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|