2023-10-24 10:55:50 +05:00
|
|
|
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 AsbCloudWebApi.Tests.ServicesTests.SAUB;
|
|
|
|
|
|
|
|
public class TelemetryDataSaubCacheTests
|
|
|
|
{
|
2023-10-24 16:44:24 +05:00
|
|
|
private const int idTelemetry = 1;
|
2023-10-24 10:55:50 +05:00
|
|
|
|
2023-10-24 16:44:24 +05:00
|
|
|
private readonly IEnumerable<TelemetryDataSaubDto> fakeTelemetries = new[]
|
|
|
|
{
|
|
|
|
new TelemetryDataSaubDto()
|
|
|
|
};
|
2023-10-24 10:55:50 +05:00
|
|
|
|
2023-10-24 16:44:24 +05:00
|
|
|
private readonly IServiceProvider serviceProviderMock = Substitute.For<IServiceProvider>();
|
|
|
|
|
|
|
|
private readonly TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
|
|
|
|
private readonly Type telemetryDataCacheType;
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
public TelemetryDataSaubCacheTests()
|
|
|
|
{
|
2023-10-24 16:44:24 +05:00
|
|
|
serviceProviderMock.GetService<BackgroundWorker>().Returns(new BackgroundWorker(serviceProviderMock));
|
2023-10-24 10:55:50 +05:00
|
|
|
|
2023-10-24 16:44:24 +05:00
|
|
|
telemetryDataCache = TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(serviceProviderMock);
|
|
|
|
|
|
|
|
telemetryDataCacheType = telemetryDataCache.GetType();
|
2023-10-24 10:55:50 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2023-10-24 16:44:24 +05:00
|
|
|
public void AddRange_ShouldReturn_AddedElementToCache()
|
2023-10-24 10:55:50 +05:00
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false);
|
|
|
|
|
|
|
|
//act
|
|
|
|
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
|
2023-10-24 16:44:24 +05:00
|
|
|
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
//assert
|
2023-10-24 16:44:24 +05:00
|
|
|
Assert.Equal(lastTelemetry, fakeTelemetries.Last());
|
2023-10-24 10:55:50 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2023-10-24 16:44:24 +05:00
|
|
|
public void AddRange_ShouldReturn_NotAddedToCache()
|
2023-10-24 10:55:50 +05:00
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true);
|
2023-10-24 16:44:24 +05:00
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
//act
|
|
|
|
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
|
2023-10-24 16:44:24 +05:00
|
|
|
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
//assert
|
2023-10-24 16:44:24 +05:00
|
|
|
Assert.NotEqual(lastTelemetry, fakeTelemetries.Last());
|
2023-10-24 10:55:50 +05:00
|
|
|
}
|
|
|
|
}
|