using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using AsbCloudApp.Data.SAUB; using AsbCloudDb.Model; using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Services.SAUB; using Bogus; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using Xunit; namespace AsbCloudWebApi.Tests.ServicesTests.SAUB; public class TelemetryDataSaubCacheTests { private readonly IEnumerable fakeTelemetries = new Faker() .RuleFor(t => t.DateTime, DateTime.UtcNow) .Generate(5) .OrderBy(t => t.DateTime); private readonly IServiceProvider serviceProvider = Substitute.For(); private readonly TelemetryDataCache telemetryDataCache; public TelemetryDataSaubCacheTests() { serviceProvider.GetService().Returns(new BackgroundWorker(serviceProvider)); telemetryDataCache = TelemetryDataCache.GetInstance(serviceProvider); } [Fact] public void AddRange_ShouldReturn_OneAddedElementToCache() { //arrange const int expectedCacheItemCount = 1; var idTelemetry = new Random().Next(1, 100); var telemetryDataCacheType = telemetryDataCache.GetType(); telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false); //act telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); //assert Assert.True(telemetryDataCache.CacheItemCount == expectedCacheItemCount); } [Fact] public void AddRange_ShouldReturn_ZeroAddedElementToCache() { //arrange const int expectedCacheItemCount = 0; var idTelemetry = new Random().Next(1, 100); var telemetryDataCacheType = telemetryDataCache.GetType(); telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true); //act telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); //assert Assert.True(telemetryDataCache.CacheItemCount == expectedCacheItemCount); } }