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, }]; private readonly TelemetryInfoDto telemetryInfoDto = new() { TimeZoneId = timezone.TimezoneId, TimeZoneOffsetTotalHours = timezone.Hours, Cluster = "cluster1", }; 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 PostInfoAsync() { // arrange dbContext.CleanupDbSet(); // act var response = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); // Assert Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); var telemetriesCount = dbContext.Set().Count(); Assert.Equal(1, telemetriesCount); } [Fact] public async Task PostInfoAsync_twice_should_be_ok() { // arrange dbContext.CleanupDbSet(); // act _ = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); var response = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); // Assert Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); var telemetriesCount = dbContext.Set().Count(); Assert.Equal(1, telemetriesCount); } [Fact] public async Task PostUsersAsync() { // arrange dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // 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 PostUsers_twice_should_be_ok() { // arrange dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // act _ = await client.PostUsersAsync(uid, users, CancellationToken.None); 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(); dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // 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 PostEventsAsync_twice_should_be_ok() { // arrange dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // act _ = await client.PostEventsAsync(uid, events, CancellationToken.None); 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(); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // 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); } [Fact] public async Task PostMessagesAsync_twice_should_be_ok() { // arrange dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); dbContext.Set().Add(telemetry); dbContext.SaveChanges(); // act _ = await client.PostEventsAsync(uid, events, CancellationToken.None); _ = await client.PostUsersAsync(uid, users, CancellationToken.None); _ = await client.PostMessagesAsync(uid, messages, 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(2, telemetryMessageCount); } }