Popraw poniższy test napisany w C#, aby wykonywał się prawidłowo. /// TEST [Test] public…
- User
Popraw poniższy test napisany w C#, aby wykonywał się prawidłowo.
/// TEST
[Test] public async Task PrintSaleConfirmationAsync_ValidInput_SuccessfulPrint() { // Arrange var response = new FirstDataStartResponse { AcceptorTransactionIdentifier = "123456", TransactionNumber = 1, TID = "TID001", MID = "MID001", CardAcceptorName = "Test Merchant", MaskedCardNumber = "************1234", CardName = "VISA", TransactionCurrencyCode = "PLN", TransactionAmount = 100.00m, MaskedExpiryDateOfCard = "12/24", UnmaskedExpiryDateOfCard = "12/24" }; string ip = "192.168.1.1"; int port = 9100; var mockFiscalPrinterService = new Mock<FiscalPrinterService>(); mockFiscalPrinterService.Setup(x => x.SendCommandToFiscalPrinter(It.IsAny<string[]>(), ip, port)) .ReturnsAsync(string.Empty); // Simulate successful command execution // Act await mockFiscalPrinterService.Object.PrintSaleConfirmationAsync(response, ip, port); // Assert mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formstart"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl1"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl2"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl4"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl6"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl8"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl9"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl31"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl35"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formline" && a[2] == "fl70"), ip, port), Times.Once); mockFiscalPrinterService.Verify(x => x.SendCommandToFiscalPrinter(It.Is<string[]>(a => a[0] == "formend"), ip, port), Times.Once); }/// KOD
public async Task PrintSaleConfirmationAsync(FirstDataStartResponse response, string ip, int port) { if (response == null) { Logger.Error("Response cannot be null."); throw new ArgumentNullException(nameof(response)); }
Logger.Info($"Printing sale confirmation for transaction {response.AcceptorTransactionIdentifier} on printer at {ip}:{port}"); var semaphore = GetPrinterSemaphore(ip, port); await semaphore.WaitAsync(); try { var printRules = !string.IsNullOrEmpty(response.MaskingRulesPrintouts) ? response.MaskingRulesPrintouts.ToMaskingRulesPrintoutsDecoder(MaskingRulesPrintoutsDecoder.PrintType.CardholderCopy) : null; var isDCC = response.DCCExchangeRate.HasValue; var isApproved = response.IsApproved; var expiryDate = (printRules?.PrintB4 == true || printRules?.PrintB5 == true) ? response.MaskedExpiryDateOfCard : response.UnmaskedExpiryDateOfCard; var cardNumber = printRules?.PrintB3 == true ? response.MaskedCardNumber?.Trim() : response.UnmaskedCardNumber?.Trim(); if (!string.IsNullOrEmpty(cardNumber) && !string.IsNullOrEmpty(expiryDate)) { await PrintFormHeader(isApproved, response, ip, port); await PrintTransactionDetails(isDCC, response, cardNumber, expiryDate, ip, port); await PrintAdditionalInfo(response, ip, port); await PrintFormFooter(ip, port); Logger.Info($"Sale confirmation printed successfully for transaction {response.AcceptorTransactionIdentifier}"); } else { Logger.Error("Failed to retrieve card number or expiry date."); throw new InvalidOperationException("Card number or expiry date is missing."); } } catch (Exception ex) { Logger.Error(ex, $"Error printing sale confirmation for transaction {response.AcceptorTransactionIdentifier}"); throw; } finally { semaphore.Release(); } - Assistant