diff --git a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs index 17d92d8d..ea6cc79e 100644 --- a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs @@ -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(); 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); } diff --git a/AsbCloudWebApi.IntegrationTests/Clients/ITelemetryControllerClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/ITelemetryControllerClient.cs new file mode 100644 index 00000000..94f4293e --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/ITelemetryControllerClient.cs @@ -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 GetTelemetriesInfoByLastData(CancellationToken token); + + [Post($"{BaseRoute}/{{uid}}/info")] + Task PostInfoAsync(string uid, [Body] TelemetryInfoDto info, CancellationToken token); + + [Post($"{BaseRoute}/{{uid}}/message")] + Task PostMessagesAsync(string uid, [Body] IEnumerable dtos, CancellationToken token); + + [Post($"{BaseRoute}/{{uid}}/event")] + Task PostEventsAsync(string uid, [Body] IEnumerable dtos, CancellationToken token); + + [Post($"{BaseRoute}/{{uid}}/user")] + Task PostUsersAsync(string uid, [Body] IEnumerable dtos, CancellationToken token); +} diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs new file mode 100644 index 00000000..e8abd06f --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs @@ -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 events = [new() { Id = 1, EventType = 1, IdCategory = 1, IdSound = 1, Message = "there is no spoon {tag1}", Tag = "tag1" }]; + private readonly IEnumerable users = [new TelemetryUserDto() { Id = 1, Level = 0, Name = "Neo", Patronymic = "Kianovich", Surname = "Theone" }]; + private readonly IEnumerable messages = [new TelemetryMessageDto() { Id = 100, IdEvent = 1, WellDepth = 5, Date = DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(5)), Arg0 = "3.14", IdTelemetryUser = 1 }]; + private readonly IEnumerable 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(string.Empty); + } + + [Fact] + public async Task GetTelemetriesInfoByLastData() + { + // Arrange + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.Set().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(); + + // act + var response = await client.PostUsersAsync(uid, users, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + var telemetryUserCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryUserCount); + } + + [Fact] + public async Task PostEventsAsync() + { + // arrange + dbContext.CleanupDbSet(); + + // act + var response = await client.PostEventsAsync(uid, events, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + var telemetryEventCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryEventCount); + } + + [Fact] + public async Task PostMessagesAsync() + { + // arrange + dbContext.CleanupDbSet(); + + // 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().Count(); + var telemetryEventCount = dbContext.Set().Count(); + var telemetryUserCount = dbContext.Set().Count(); + var telemetryMessageCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryEventCount); + Assert.Equal(1, telemetryUserCount); + Assert.Equal(1, telemetryMessageCount); + } +}