forked from ddrilling/AsbCloudServer
320 lines
9.9 KiB
C#
320 lines
9.9 KiB
C#
|
using AsbCloudApp.Data;
|
|||
|
using AsbCloudApp.Data.SAUB;
|
|||
|
using AsbCloudApp.Repositories;
|
|||
|
using AsbCloudApp.Requests;
|
|||
|
using AsbCloudApp.Services;
|
|||
|
using AsbCloudDb.Model;
|
|||
|
using AsbCloudInfrastructure.Repository;
|
|||
|
using AsbCloudInfrastructure.Services;
|
|||
|
using AsbCloudInfrastructure.Services.SAUB;
|
|||
|
using DocumentFormat.OpenXml.Office2010.ExcelAc;
|
|||
|
using NSubstitute;
|
|||
|
using Org.BouncyCastle.Asn1.Ocsp;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Xunit;
|
|||
|
|
|||
|
namespace AsbCloudInfrastructure.Tests.Services;
|
|||
|
|
|||
|
public class MessageServiceTest
|
|||
|
{
|
|||
|
private static readonly List<TelemetryBaseDto> telemetries = new List<TelemetryBaseDto>()
|
|||
|
{
|
|||
|
new TelemetryBaseDto()
|
|||
|
{
|
|||
|
Id = 1,
|
|||
|
TimeZone = new SimpleTimezoneDto()
|
|||
|
{
|
|||
|
Hours = 5
|
|||
|
}
|
|||
|
},
|
|||
|
new TelemetryBaseDto()
|
|||
|
{
|
|||
|
Id = 2,
|
|||
|
TimeZone = new SimpleTimezoneDto()
|
|||
|
{
|
|||
|
Hours = 5
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
private static readonly List<TelemetryEventDto> events = new List<TelemetryEventDto>()
|
|||
|
{
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 1,
|
|||
|
IdCategory = 1,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1"
|
|||
|
},
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 2,
|
|||
|
IdCategory = 1,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1"
|
|||
|
},
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 3,
|
|||
|
IdCategory = 1,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1"
|
|||
|
},
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 4,
|
|||
|
IdCategory = 2,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2"
|
|||
|
},
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 5,
|
|||
|
IdCategory = 2,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2"
|
|||
|
},
|
|||
|
new TelemetryEventDto()
|
|||
|
{
|
|||
|
Id = 6,
|
|||
|
IdCategory = 3,
|
|||
|
MessageTemplate = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3"
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3:
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private static readonly List<TelemetryMessageDto> messages = new List<TelemetryMessageDto>()
|
|||
|
{
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 1,
|
|||
|
IdEvent = 1,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 2,
|
|||
|
IdEvent = 1,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 3,
|
|||
|
IdEvent = 1,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 4,
|
|||
|
IdEvent = 2,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 5,
|
|||
|
IdEvent = 2,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 6,
|
|||
|
IdEvent = 2,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 7,
|
|||
|
IdEvent = 3,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 8,
|
|||
|
IdEvent = 3,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 9,
|
|||
|
IdEvent = 3,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 10,
|
|||
|
IdEvent = 4,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 11,
|
|||
|
IdEvent = 4,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 12,
|
|||
|
IdEvent = 5,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 13,
|
|||
|
IdEvent = 5,
|
|||
|
IdTelemetry = 1
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 14,
|
|||
|
IdEvent = 6,
|
|||
|
IdTelemetry = 2
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 15,
|
|||
|
IdEvent = 6,
|
|||
|
IdTelemetry = 2
|
|||
|
},
|
|||
|
new TelemetryMessageDto()
|
|||
|
{
|
|||
|
Id = 16,
|
|||
|
IdEvent = 6,
|
|||
|
IdTelemetry = 2
|
|||
|
},
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private readonly IMessageService messageServiceMock = Substitute.For<IMessageService>();
|
|||
|
private readonly IMessageRepository messageRepositoryMock = Substitute.For<IMessageRepository>();
|
|||
|
private readonly IEventRepository eventRepositoryMock = Substitute.For<IEventRepository>();
|
|||
|
private readonly ITelemetryService telemetryServiceMock = Substitute.For<ITelemetryService>();
|
|||
|
private readonly IAsbCloudDbContext db = Substitute.For<IAsbCloudDbContext>();
|
|||
|
|
|||
|
public MessageServiceTest()
|
|||
|
{
|
|||
|
messageServiceMock = new MessageService(messageRepositoryMock, telemetryServiceMock, eventRepositoryMock);
|
|||
|
|
|||
|
telemetryServiceMock
|
|||
|
.GetOrDefaultTelemetriesByIdsWells(Arg.Any<IEnumerable<int>>())
|
|||
|
.Returns(telemetries);
|
|||
|
|
|||
|
eventRepositoryMock
|
|||
|
.Get(Arg.Any<TelemetryEventRequest>(), Arg.Any<CancellationToken>())
|
|||
|
.Returns(events);
|
|||
|
|
|||
|
messageRepositoryMock
|
|||
|
.GetMessagesAsync(Arg.Any<MessageTelemetryRequest>(), Arg.Any<CancellationToken>())
|
|||
|
.Returns(messages);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetStatMessages_ShouldReturn_Success()
|
|||
|
{
|
|||
|
//act
|
|||
|
var baseRequest = new MessageRequestBase() { };
|
|||
|
var messageRequest = new MessageRequest(baseRequest, [1]);
|
|||
|
var result = await messageServiceMock.GetStat(messageRequest, CancellationToken.None);
|
|||
|
|
|||
|
Assert.Equal(6, result.Count());
|
|||
|
|
|||
|
var mCount_Telemetry_1_Category_1_Template_1_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdEvent == 1)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_1_Category_1_Template_1_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdCategory == 1)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual);
|
|||
|
|
|||
|
var mCount_Telemetry_1_Category_1_Template_2_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdEvent == 2)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_1_Category_1_Template_2_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdCategory == 1)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual);
|
|||
|
|
|||
|
var mCount_Telemetry_1_Category_1_Template_3_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdEvent == 3)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_1_Category_1_Template_3_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdCategory == 1)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual);
|
|||
|
|
|||
|
var mCount_Telemetry_1_Category_2_Template_2_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdEvent == 5)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_1_Category_2_Template_2_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdCategory == 2)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual);
|
|||
|
|
|||
|
var mCount_Telemetry_1_Category_2_Template_3_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdEvent == 6)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_1_Category_2_Template_3_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 1)
|
|||
|
.Where(x => x.IdCategory == 2)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual);
|
|||
|
|
|||
|
var mCount_Telemetry_2_Category_3_Template_1_Expected = messages
|
|||
|
.Where(x => x.IdTelemetry == 2)
|
|||
|
.Where(x => x.IdEvent == 6)
|
|||
|
.Count();
|
|||
|
var mCount_Telemetry_2_Category_3_Template_1_Actual = result
|
|||
|
.Where(x => x.IdTelemetry == 2)
|
|||
|
.Where(x => x.IdCategory == 3)
|
|||
|
.Where(x => x.MessageTemplate == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3")
|
|||
|
.Sum(x => x.MessagesCount);
|
|||
|
Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|