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 Microsoft.Extensions.DependencyInjection; using NSubstitute; using Xunit; namespace AsbCloudInfrastructure.Tests.Services.SAUB; public class TelemetryDataSaubCacheTests { private const int idTelemetry = 1; private readonly IEnumerable fakeTelemetries = new[] { new TelemetryDataSaubDto() }; private readonly IServiceProvider serviceProviderMock = Substitute.For(); private readonly TelemetryDataCache telemetryDataCache; private readonly Type telemetryDataCacheType; public TelemetryDataSaubCacheTests() { serviceProviderMock.GetService().Returns(new BackgroundWorker(serviceProviderMock)); telemetryDataCache = TelemetryDataCache.GetInstance(serviceProviderMock); telemetryDataCacheType = telemetryDataCache.GetType(); } [Fact] public void AddRange_ShouldReturn_AddedElementToCache() { //arrange telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false); //act telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry); //assert Assert.Equal(lastTelemetry, fakeTelemetries.Last()); } [Fact] public void AddRange_ShouldReturn_NotAddedToCache() { //arrange telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true); //act telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry); //assert Assert.Null(lastTelemetry); } }