persistence/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs

160 lines
3.7 KiB
C#
Raw Normal View History

using System.Net;
2024-11-21 14:50:36 +05:00
using Microsoft.Extensions.DependencyInjection;
using Persistence.Client;
using Persistence.Client.Clients;
using Xunit;
namespace Persistence.IntegrationTests.Controllers
{
public class SetpointControllerTest : BaseIntegrationTest
{
2024-11-21 14:50:36 +05:00
private ISetpointClient setpointClient;
private class TestObject
{
public string? value1 { get; set; }
2024-11-18 15:05:12 +05:00
public int? value2 { get; set; }
}
public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory)
{
2024-11-21 14:50:36 +05:00
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
2024-11-21 14:50:36 +05:00
setpointClient = persistenceClientFactory.GetClient<ISetpointClient>();
}
[Fact]
2024-11-18 15:05:12 +05:00
public async Task GetCurrent_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetCurrent(setpointKeys);
2024-11-18 15:05:12 +05:00
//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 Save();
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetCurrent([setpointKey]);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey);
}
2024-11-18 15:05:12 +05:00
[Fact]
public async Task GetHistory_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
var historyMoment = DateTimeOffset.UtcNow;
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetHistory(setpointKeys, historyMoment);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
2024-11-18 15:05:12 +05:00
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetHistory_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
var historyMoment = DateTimeOffset.UtcNow;
historyMoment = historyMoment.AddDays(1);
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetHistory([setpointKey], historyMoment);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey);
}
[Fact]
2024-11-18 15:05:12 +05:00
public async Task GetLog_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetLog(setpointKeys);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
2024-11-18 15:05:12 +05:00
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetLog_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.GetLog([setpointKey]);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault().Value.FirstOrDefault()?.Key, setpointKey);
}
2024-11-18 15:05:12 +05:00
[Fact]
public async Task Save_returns_success()
2024-11-18 15:05:12 +05:00
{
await Save();
}
private async Task<Guid> Save()
{
//arrange
2024-11-18 15:05:12 +05:00
var setpointKey = Guid.NewGuid();
var setpointValue = new TestObject()
{
value1 = "1",
value2 = 2
};
//act
2024-11-21 14:50:36 +05:00
var response = await setpointClient.Save(setpointKey, setpointValue);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
return setpointKey;
2024-11-18 15:05:12 +05:00
}
}
}