- Added ComplianceNFs.Infrastructure.Tests project to the solution. - Implemented unit tests for AccessDbRepository, ArchivingService, AttachmentRepository, InvoiceIngestionService, MailListener, MonitorViewModel, and Worker. - Enhanced existing tests with additional assertions and mock setups. - Updated TODOs and roadmap documentation to reflect changes in service implementations and testing coverage. - Modified ComplianceNFs.sln to include new test project and adjust solution properties.
55 lines
2.0 KiB
C#
55 lines
2.0 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;
|
|
|
|
namespace ComplianceNFs.Infrastructure.Tests
|
|
{
|
|
public class InvoiceIngestionServiceTests
|
|
{
|
|
[Fact]
|
|
public void OnNewMailReceived_ParsesXmlAttachmentAndSavesInvoice()
|
|
{
|
|
// Arrange
|
|
var mockMailListener = new Mock<IMailListener>();
|
|
var mockAttachmentRepo = new Mock<IAttachmentRepository>();
|
|
var mockXmlParser = new Mock<IXmlParser>();
|
|
var mockPdfParser = new Mock<IPdfParser>();
|
|
|
|
var testParsed = new ParsedInvoice { CnpjComp = "123", NumeroNF = "456" };
|
|
mockXmlParser.Setup(x => x.Parse(It.IsAny<Stream>())).Returns(testParsed);
|
|
|
|
var service = new InvoiceIngestionService(
|
|
mockMailListener.Object,
|
|
mockAttachmentRepo.Object,
|
|
mockXmlParser.Object,
|
|
mockPdfParser.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);
|
|
}
|
|
}
|
|
}
|