- Modificado `InvoiceIngestionService` para usar `IServiceScopeFactory`, permitindo melhor gerenciamento do ciclo de vida de dependências. - Atualizações de pacotes em diversos projetos (`ComplianceNFs.Core`, `ComplianceNFs.Infrastructure`, `ComplianceNFs.Service`, `ComplianceNFs.Monitor`, `ComplianceNFs.Infrastructure.Tests`). - Alterado namespace de `AuditComplianceNFsTest.cs` para `ComplianceNFs.Infrastructure.Tests`. - Refatorações no `AttachmentRepository` e `ComplianceNFsDbContext` para simplificar código e melhorar legibilidade. - Atualizado teste `InvoiceIngestionServiceTests` para refletir mudanças no uso de escopos de serviço. - Melhorias gerais na arquitetura e alinhamento com boas práticas.
68 lines
2.7 KiB
C#
68 lines
2.7 KiB
C#
using System;
|
|
using System.Net.Mail;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Xunit;
|
|
using Moq;
|
|
using ComplianceNFs.Core.Application.Services;
|
|
using ComplianceNFs.Core.Ports;
|
|
using ComplianceNFs.Core.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace ComplianceNFs.Infrastructure.Tests
|
|
{
|
|
public class InvoiceIngestionServiceTests
|
|
{
|
|
[Fact]
|
|
public void OnNewMailReceived_ParsesXmlAttachmentAndSavesInvoice()
|
|
{
|
|
// Arrange
|
|
var mockMailListener = new Mock<IMailListener>();
|
|
var mockScopeFactory = new Mock<IServiceScopeFactory>();
|
|
var mockScope = new Mock<IServiceScope>();
|
|
var mockProvider = new Mock<IServiceProvider>();
|
|
var mockAttachmentRepo = new Mock<IAttachmentRepository>();
|
|
var mockXmlParser = new Mock<IXmlParser>();
|
|
var mockPdfParser = new Mock<IPdfParser>();
|
|
var mockLogger = new Mock<ILogger<InvoiceIngestionService>>();
|
|
|
|
mockProvider.Setup(x => x.GetService(typeof(IAttachmentRepository)))
|
|
.Returns(mockAttachmentRepo.Object);
|
|
mockScope.Setup(x => x.ServiceProvider).Returns(mockProvider.Object);
|
|
mockScopeFactory.Setup(x => x.CreateScope()).Returns(mockScope.Object);
|
|
|
|
|
|
var testParsed = new ParsedInvoice { CnpjComp = "123", NumeroNF = "456" };
|
|
mockXmlParser.Setup(x => x.Parse(It.IsAny<Stream>())).Returns(testParsed);
|
|
|
|
var service = new InvoiceIngestionService(
|
|
mockMailListener.Object,
|
|
mockScopeFactory.Object,
|
|
mockXmlParser.Object,
|
|
mockPdfParser.Object,
|
|
mockLogger.Object
|
|
);
|
|
|
|
var mail = new MailMessage
|
|
{
|
|
From = new MailAddress("test@supplier.com"),
|
|
Subject = "Test Invoice",
|
|
Headers = { ["Message-ID"] = "msgid", ["Date"] = DateTime.Now.ToString(), ["Conversation-ID"] = "conv-id" }
|
|
};
|
|
var xmlContent = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("<xml></xml>"));
|
|
var attachment = new Attachment(xmlContent, "invoice.xml");
|
|
mail.Attachments.Add(attachment);
|
|
|
|
// Act
|
|
// Simulate event
|
|
mockMailListener.Raise(m => m.NewMailReceived += null, mail);
|
|
|
|
// Assert
|
|
mockXmlParser.Verify(x => x.Parse(It.IsAny<Stream>()), Times.Once);
|
|
mockAttachmentRepo.Verify(x => x.SaveRawAsync(It.Is<EnergyInvoice>(inv => inv.CnpjComp == "123" && inv.Filename == "invoice.xml")), Times.Once);
|
|
}
|
|
}
|
|
}
|