persistence/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs
2024-11-18 15:05:12 +05:00

148 lines
3.7 KiB
C#

using System.Net;
using System.Text.Json;
using Mapster;
using Persistence.IntegrationTests.Clients;
using Xunit;
namespace Persistence.IntegrationTests.Controllers
{
public class SetpointControllerTest : BaseIntegrationTest
{
private ISetpointClient client;
private class TestObject
{
public string? value1 { get; set; }
public int? value2 { get; set; }
}
public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory)
{
factory.ClientOptions.BaseAddress = new Uri($"http://localhost/api/Setpoint");
client = factory.GetHttpClient<ISetpointClient>(string.Empty);
}
[Fact]
public async Task GetCurrent_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
//act
var response = await client.GetCurrent(setpointKeys);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetHistory_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
var historyMoment = DateTimeOffset.Now.ToUniversalTime();
//act
var response = await client.GetHistory(setpointKeys, historyMoment);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task GetLog_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
//act
var response = await client.GetLog(setpointKeys);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Empty(response.Content);
}
[Fact]
public async Task Save_returns_success()
{
//arrange
var setpointKey = Guid.NewGuid();
var setpointValue = new TestObject()
{
value1 = "1",
value2 = 2
};
//act
var response = await client.Save(setpointKey, setpointValue);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
}
[Fact]
public async Task General_test_success()
{
//save
var setpointKey = Guid.NewGuid();
var setpointValue = new TestObject()
{
value1 = "1",
value2 = 2
};
var saveResponse = await client.Save(setpointKey, setpointValue);
Assert.Equal(HttpStatusCode.OK, saveResponse.StatusCode);
Assert.Equal(1, saveResponse.Content);
//current
var currentResponse = await client.GetCurrent([setpointKey]);
Assert.Equal(HttpStatusCode.OK, currentResponse.StatusCode);
var currentContent = currentResponse.Content;
Assert.NotNull(currentContent);
Assert.NotEmpty(currentContent);
var currentContentValue = currentContent.FirstOrDefault()?.Value?.ToString();
Assert.NotNull(currentContentValue);
Assert.NotEmpty(currentContentValue);
var testObjectValue = JsonSerializer.Deserialize<TestObject>(currentContentValue);
Assert.NotNull(testObjectValue);
Assert.Equal(setpointValue.value1, testObjectValue.value1);
Assert.Equal(setpointValue.value2, testObjectValue.value2);
//history
var historyMoment = DateTimeOffset.Now.ToUniversalTime();
var historyResponse = await client.GetHistory([setpointKey], historyMoment);
Assert.Equal(HttpStatusCode.OK, historyResponse.StatusCode);
Assert.NotNull(historyResponse.Content);
Assert.NotEmpty(historyResponse.Content);
//log
var logResponse = await client.GetLog([setpointKey]);
Assert.Equal(HttpStatusCode.OK, logResponse.StatusCode);
Assert.NotNull(logResponse.Content);
Assert.NotEmpty(logResponse.Content);
}
}
}