using System.Net; using Microsoft.AspNetCore.Mvc.TagHelpers.Cache; using Microsoft.Extensions.DependencyInjection; using Persistence.Client; using Persistence.Client.Clients; using Persistence.Database.Model; using Xunit; namespace Persistence.IntegrationTests.Controllers { public class SetpointControllerTest : BaseIntegrationTest { private ISetpointClient setpointClient; private class TestObject { public string? value1 { get; set; } public int? value2 { get; set; } } public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory) { var scope = factory.Services.CreateScope(); var persistenceClientFactory = scope.ServiceProvider .GetRequiredService(); setpointClient = persistenceClientFactory.GetClient(); } [Fact] public async Task GetCurrent_returns_success() { //arrange var setpointKeys = new List() { Guid.NewGuid(), Guid.NewGuid() }; //act var response = await setpointClient.GetCurrent(setpointKeys); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetCurrent_AfterSave_returns_success() { //arrange var setpointKey = await Add(); //act var response = await setpointClient.GetCurrent([setpointKey]); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); Assert.Equal(setpointKey, response.Content.FirstOrDefault()?.Key); } [Fact] public async Task GetHistory_returns_success() { //arrange var setpointKeys = new List() { Guid.NewGuid(), Guid.NewGuid() }; var historyMoment = DateTimeOffset.UtcNow; //act var response = await setpointClient.GetHistory(setpointKeys, historyMoment); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetHistory_AfterSave_returns_success() { //arrange var setpointKey = await Add(); var historyMoment = DateTimeOffset.UtcNow; historyMoment = historyMoment.AddDays(1); //act var response = await setpointClient.GetHistory([setpointKey], historyMoment); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); Assert.Equal(setpointKey, response.Content.FirstOrDefault()?.Key); } [Fact] public async Task GetLog_returns_success() { //arrange var setpointKeys = new List() { Guid.NewGuid(), Guid.NewGuid() }; //act var response = await setpointClient.GetLog(setpointKeys); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.Empty(response.Content); } [Fact] public async Task GetLog_AfterSave_returns_success() { //arrange var setpointKey = await Add(); //act var response = await setpointClient.GetLog([setpointKey]); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); Assert.Equal(setpointKey, response.Content.FirstOrDefault().Key); } [Fact] public async Task GetDatesRange_returns_success() { //arrange dbContext.CleanupDbSet(); //act var response = await setpointClient.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 dbContext.CleanupDbSet(); await Add(); var dateBegin = DateTimeOffset.MinValue; var take = 1; var part = await setpointClient.GetPart(dateBegin, take, new CancellationToken()); //act var response = await setpointClient.GetDatesRangeAsync(new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); var expectedValue = part.Content! .FirstOrDefault()!.Created .ToString("dd.MM.yyyy-HH:mm:ss"); var actualValueFrom = response.Content.From .ToString("dd.MM.yyyy-HH:mm:ss"); Assert.Equal(expectedValue, actualValueFrom); var actualValueTo = response.Content.To .ToString("dd.MM.yyyy-HH:mm:ss"); Assert.Equal(expectedValue, actualValueTo); } [Fact] public async Task GetPart_returns_success() { //arrange var dateBegin = DateTimeOffset.UtcNow; var take = 2; //act var response = await setpointClient.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 Add(); //act var response = await setpointClient.GetPart(dateBegin, take, new CancellationToken()); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); } [Fact] public async Task Save_returns_success() { await Add(); } private async Task Add() { //arrange var setpointKey = Guid.NewGuid(); var setpointValue = new TestObject() { value1 = "1", value2 = 2 }; //act var response = await setpointClient.Add(setpointKey, setpointValue); //assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); return setpointKey; } } }