Fix TelemetryController.PostMessagesAsync(..) Time to utc.

This commit is contained in:
ngfrolov 2024-05-13 09:56:02 +05:00
parent 2a33eaa516
commit 04f0b4e275
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
3 changed files with 134 additions and 3 deletions

View File

@ -142,14 +142,13 @@ namespace AsbCloudInfrastructure.Services.SAUB
return Task.CompletedTask;
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var timezone = telemetryService.GetTimezone(telemetry.Id);
foreach (var dto in dtos)
{
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
entity.IdTelemetry = telemetry.Id;
entity.DateTime = dto.Date.ToOffset(TimeSpan.FromHours(timezone.Hours));
entity.DateTime = dto.Date.ToUniversalTime();
db.TelemetryMessages.Add(entity);
}

View File

@ -0,0 +1,23 @@
using AsbCloudApp.Data.SAUB;
using Refit;
namespace AsbCloudWebApi.IntegrationTests.Clients;
public interface ITelemetryControllerClient
{
private const string BaseRoute = "/api/telemetry";
[Get($"{BaseRoute}/Active")]
Task<IApiResponse> GetTelemetriesInfoByLastData(CancellationToken token);
[Post($"{BaseRoute}/{{uid}}/info")]
Task<IApiResponse> PostInfoAsync(string uid, [Body] TelemetryInfoDto info, CancellationToken token);
[Post($"{BaseRoute}/{{uid}}/message")]
Task<IApiResponse> PostMessagesAsync(string uid, [Body] IEnumerable<TelemetryMessageDto> dtos, CancellationToken token);
[Post($"{BaseRoute}/{{uid}}/event")]
Task<IApiResponse> PostEventsAsync(string uid, [Body] IEnumerable<EventDto> dtos, CancellationToken token);
[Post($"{BaseRoute}/{{uid}}/user")]
Task<IApiResponse> PostUsersAsync(string uid, [Body] IEnumerable<TelemetryUserDto> dtos, CancellationToken token);
}

View File

@ -0,0 +1,109 @@
using AsbCloudApp.Data.SAUB;
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.Clients;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class TelemetryControllerTest : BaseIntegrationTest
{
private ITelemetryControllerClient client;
static readonly string uid = DateTime.UtcNow.ToString("yyyyMMdd_HHmmssfff");
private static readonly SimpleTimezone timezone = new() {TimezoneId = "a", Hours = 5 };
private static readonly Telemetry telemetry = new Telemetry() {Id = 1, RemoteUid = uid, TimeZone = timezone, Info = new() };
private readonly IEnumerable<EventDto> events = [new() { Id = 1, EventType = 1, IdCategory = 1, IdSound = 1, Message = "there is no spoon {tag1}", Tag = "tag1" }];
private readonly IEnumerable<TelemetryUserDto> users = [new TelemetryUserDto() { Id = 1, Level = 0, Name = "Neo", Patronymic = "Kianovich", Surname = "Theone" }];
private readonly IEnumerable<TelemetryMessageDto> messages = [new TelemetryMessageDto() { Id = 100, IdEvent = 1, WellDepth = 5, Date = DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(5)), Arg0 = "3.14", IdTelemetryUser = 1 }];
private readonly IEnumerable<TelemetryDataSaub> telemetryDataSaubEntities = [new TelemetryDataSaub()
{
IdTelemetry = telemetry.Id,
DateTime = DateTimeOffset.UtcNow,
AxialLoad = 2,
WellDepth = 5,
BitDepth = 5,
BlockPosition = 5,
BlockSpeed = 5,
}];
public TelemetryControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<ITelemetryControllerClient>(string.Empty);
}
[Fact]
public async Task GetTelemetriesInfoByLastData()
{
// Arrange
dbContext.CleanupDbSet<Telemetry>();
dbContext.Set<Telemetry>().Add(telemetry);
dbContext.Set<TelemetryDataSaub>().AddRange(telemetryDataSaubEntities);
dbContext.SaveChanges();
// Act
var response = await client.GetTelemetriesInfoByLastData(CancellationToken.None);
// Assert
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task PostUsersAsync()
{
// arrange
dbContext.CleanupDbSet<Telemetry>();
// act
var response = await client.PostUsersAsync(uid, users, CancellationToken.None);
// Assert
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
var telemetriesCount = dbContext.Set<Telemetry>().Count();
var telemetryUserCount = dbContext.Set<TelemetryUser>().Count();
Assert.Equal(1, telemetriesCount);
Assert.Equal(1, telemetryUserCount);
}
[Fact]
public async Task PostEventsAsync()
{
// arrange
dbContext.CleanupDbSet<Telemetry>();
// act
var response = await client.PostEventsAsync(uid, events, CancellationToken.None);
// Assert
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
var telemetriesCount = dbContext.Set<Telemetry>().Count();
var telemetryEventCount = dbContext.Set<TelemetryEvent>().Count();
Assert.Equal(1, telemetriesCount);
Assert.Equal(1, telemetryEventCount);
}
[Fact]
public async Task PostMessagesAsync()
{
// arrange
dbContext.CleanupDbSet<Telemetry>();
// act
_ = await client.PostEventsAsync(uid, events, CancellationToken.None);
_ = await client.PostUsersAsync(uid, users, CancellationToken.None);
var response = await client.PostMessagesAsync(uid, messages, CancellationToken.None);
// Assert
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
var telemetriesCount = dbContext.Set<Telemetry>().Count();
var telemetryEventCount = dbContext.Set<TelemetryEvent>().Count();
var telemetryUserCount = dbContext.Set<TelemetryUser>().Count();
var telemetryMessageCount = dbContext.Set<TelemetryMessage>().Count();
Assert.Equal(1, telemetriesCount);
Assert.Equal(1, telemetryEventCount);
Assert.Equal(1, telemetryUserCount);
Assert.Equal(1, telemetryMessageCount);
}
}