Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
9.4 kB
1
Indexable
Never
using System.Reflection;
using SGS.Application.Commands.Transfusions;
using SGS.Domain.Common.Enums;
using SGS.Domain.Entities.Articles;
using SGS.Domain.Entities.Locations;
using SGS.Domain.Entities.Partners;
using SGS.Domain.Entities.SKUs;
using SGS.Domain.Entities.SKUSerialCodesForTrackings;
using SGS.Domain.Entities.Users;
using SGS.Reporting.Application.TransfusionReports;
using SGS.Test.Integration.Common;
using SGS.Test.Integration.Helpers;
using Xunit;

namespace SGS.Test.Integration.ReportsTests.TransfusionReportTests;

public class TransfusionReportTest : BaseTest
{
    private Location _location;
    private Article _article;
    private SKUSerialCodesForTracking _sKUSerial;
    private Partner _partner;
    private SKU _sKU;
    private User _workerAssistant;

    public TransfusionReportTest(Fixture fixture) : base(fixture)
    {
    }

    private async Task SetUp()
    {
        var setupHelper = new SetupHelper();
        _location = await setupHelper.SetupLocation(HttpClient);
        _article = await setupHelper.SetupArticle(HttpClient);
        _sKUSerial = await setupHelper.SetupSKUSerialCodeForTracking(HttpClient);
        _partner = await setupHelper.SetupPartner(HttpClient);
        _sKU = await setupHelper.SetupSKU(HttpClient);
        _workerAssistant = await setupHelper.SetupUser(HttpClient);
    }

    [Fact]
    public async Task GetTransfusionOneSourceTwoDestinationReport_Success()
    {
        // Arrange
        await SetUp();
        var lines = new List<CreateTransfusion.CommandLine>();
        lines.Add(new CreateTransfusion.CommandLine(1, _sKUSerial.Id, "Seal Code Old 1", "Seal Code New 1",
            DateTime.Today, 20000,
            4000, 16000, 21000, 3000, "Lot 1", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));
        lines.Add(new CreateTransfusion.CommandLine(2, _sKUSerial.Id, "Seal Code Old 2", "Seal Code New 2",
            DateTime.Today, 7000,
            6000, 1000, 8200, 18200, "Lot 2", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));
        lines.Add(new CreateTransfusion.CommandLine(3, _sKUSerial.Id, "Seal Code Old 3", "Seal Code New 3",
            DateTime.Today, 6500,
            5100, 1400, 6400, 13600, "Lot 3", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));


        var responseMessageCreate = await HttpClient.CreateTransfusion(_location.Code,
            _article.Id, _workerAssistant.Id, TransfusionDirection.MultipleSource,
            true, "Note 1", lines);
        var createResponse = await ApiCallerHelper.ParseResponse<CreateTransfusion.Response>(responseMessageCreate);

        // Act
        var responseMessage = await HttpClient.GetTransfusionReport(createResponse.Id, 21);
        var reportData = await responseMessage.Content.ReadAsByteArrayAsync();

        await File.WriteAllBytesAsync(
            @$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\{responseMessage.Content.Headers.ContentDisposition?.FileName ?? "test.pdf"}"
            , reportData);
    }

    [Fact]
    public async Task GetTransfusionOneDestinationTwoSourceReport_Success()
    {
        // Arrange
        await SetUp();
        var lines = new List<CreateTransfusion.CommandLine>();
        lines.Add(new CreateTransfusion.CommandLine(1, _sKUSerial.Id, "Seal Code Old 1", "Seal Code New 1",
            DateTime.Today, 7000,
            6000, 1000, 8200, 18200, "Lot 1", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));
        lines.Add(new CreateTransfusion.CommandLine(2, _sKUSerial.Id, "Seal Code Old 2", "Seal Code New 2",
            DateTime.Today, 20000,
            4000, 16000, 21000, 3000, "Lot 2", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));
        lines.Add(new CreateTransfusion.CommandLine(3, _sKUSerial.Id, "Seal Code Old 3", "Seal Code New 3",
            DateTime.Today, 6500,
            5100, 1400, 6400, 13600, "Lot 3", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));


        var responseMessageCreate = await HttpClient.CreateTransfusion(_location.Code
            , _article.Id, _workerAssistant.Id, TransfusionDirection.MultipleSource,
            true, "Note 1", lines);
        var createResponse = await ApiCallerHelper.ParseResponse<CreateTransfusion.Response>(responseMessageCreate);

        // Act
        var responseMessage = await HttpClient.GetTransfusionReport(createResponse.Id, 21);
        var reportData = await responseMessage.Content.ReadAsByteArrayAsync();

        await File.WriteAllBytesAsync(
            @$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\{responseMessage.Content.Headers.ContentDisposition?.FileName ?? "test.pdf"}"
            , reportData);
    }

    [Fact]
    public async Task GetTransfusionOneSourceTwoDestinationPostedReport_Success()
    {
        // Arrange
        await SetUp();
        var lines = new List<CreateTransfusion.CommandLine>();
        lines.Add(new CreateTransfusion.CommandLine(1, _sKUSerial.Id, "Seal Code Old 1", "Seal Code New 1",
            DateTime.Today, 20000,
            4000, 16000, 21000, 3000, "Lot 1", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));
        lines.Add(new CreateTransfusion.CommandLine(2, _sKUSerial.Id, "Seal Code Old 2", "Seal Code New 2",
            DateTime.Today, 7000,
            6000, 1000, 8200, 18200, "Lot 2", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));
        lines.Add(new CreateTransfusion.CommandLine(3, _sKUSerial.Id, "Seal Code Old 3", "Seal Code New 3",
            DateTime.Today, 6500,
            5100, 1400, 6400, 13600, "Lot 3", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));


        var responseMessageCreate = await HttpClient.CreateTransfusion(_location.Code,
            _article.Id, _workerAssistant.Id, TransfusionDirection.MultipleSource,
            true, "Note 1", lines);
        var createResponse = await ApiCallerHelper.ParseResponse<CreateTransfusion.Response>(responseMessageCreate);

        // Act
        var postResponseMessage = await HttpClient.PostTransfusion(createResponse.Id);
        var postResponse = await ApiCallerHelper.ParseResponse<PostTransfusion.Response>(postResponseMessage);

        var responseMessage = await HttpClient.GetTransfusionReport(postResponse.Id, 21);
        var reportData = await responseMessage.Content.ReadAsByteArrayAsync();

        await File.WriteAllBytesAsync(
            @$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\{responseMessage.Content.Headers.ContentDisposition?.FileName ?? "test.pdf"}"
            , reportData);
    }

    [Fact]
    public async Task GetTransfusionOneDestinationTwoSourcePostedReport_Success()
    {
        // Arrange
        await SetUp();
        var lines = new List<CreateTransfusion.CommandLine>();
        lines.Add(new CreateTransfusion.CommandLine(1, _sKUSerial.Id, "Seal Code Old 1", "Seal Code New 1",
            DateTime.Today, 7000,
            6000, 1000, 8200, 18200, "Lot 1", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));
        lines.Add(new CreateTransfusion.CommandLine(2, _sKUSerial.Id, "Seal Code Old 2", "Seal Code New 2",
            DateTime.Today, 20000,
            4000, 16000, 21000, 3000, "Lot 2", _partner.Id, _sKU.Id, TransfusionLineType.Destination, false));
        lines.Add(new CreateTransfusion.CommandLine(3, _sKUSerial.Id, "Seal Code Old 3", "Seal Code New 3",
            DateTime.Today, 6500,
            5100, 1400, 6400, 13600, "Lot 3", _partner.Id, _sKU.Id, TransfusionLineType.Source, false));


        var responseMessageCreate = await HttpClient.CreateTransfusion(_location.Code
            , _article.Id, _workerAssistant.Id, TransfusionDirection.MultipleSource,
            true, "Note 1", lines);
        var createResponse = await ApiCallerHelper.ParseResponse<CreateTransfusion.Response>(responseMessageCreate);

        // Act
        var postResponseMessage = await HttpClient.PostTransfusion(createResponse.Id);
        var postResponse = await ApiCallerHelper.ParseResponse<PostTransfusion.Response>(postResponseMessage);

        var responseMessage = await HttpClient.GetTransfusionReport(postResponse.Id, 21);
        var reportData = await responseMessage.Content.ReadAsByteArrayAsync();

        await File.WriteAllBytesAsync(
            @$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\{responseMessage.Content.Headers.ContentDisposition?.FileName ?? "test.pdf"}"
            , reportData);
    }
}

internal static class TransfusionReportHelper
{
    private const string url = "api/report/";
    private const string BaseUrlWithSlash = "api/Transfusion/";


    public static async Task<HttpResponseMessage> GetTransfusionReport(this HttpClient client, Guid id,
        int queryType)
    {
        var token = await client.Login();
        return await client.CallApi(HttpMethod.Post, url + $"{queryType}", token, new GetTransfusionReport.Query(id));
    }

    public static async Task<HttpResponseMessage> PostTransfusion(this HttpClient client, Guid id)
    {
        var token = await client.Login();
        return await client.CallApi<object>(HttpMethod.Post, BaseUrlWithSlash + "Post/" + id, token);
    }
}