ComplianceNFs/ComplianceNFs.Infrastructure.Tests/InvoiceIngestionServiceTests.cs
Giuliano Paschoalino e6b2180c94 feat(logging): Implement robust logging across infrastructure, application, and UI layers
- Added Microsoft.Extensions.Logging to various projects for enhanced logging capabilities.
- Updated AccessDbRepository and AttachmentRepository to include logging for database operations.
- Integrated logging into MailListener for better error handling and operational insights.
- Modified tests to utilize mocks for ILogger to ensure logging behavior is tested.
- Enhanced App.xaml.cs and MainWindow.xaml.cs to log application startup and initialization events.
- Created LoggingBootstrapper to configure logging services in the WPF application.
- Updated TODOs-and-Roadmap.md to reflect the addition of logging features.
2025-06-18 17:38:52 -03:00

58 lines
2.1 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;
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 mockLogger = new Mock<ILogger<InvoiceIngestionService>>();
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,
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);
}
}
}