"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts_jest_1 = require("@golevelup/ts-jest");
const LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../../core/validation/ValidationError");
const DataFixture_1 = require("../../../../../__tests__/fixtures/shared/DataFixture");
const Security_1 = __importDefault(require("../../../in/security/Security"));
const NominalValueFixture_1 = require("../../../../../__tests__/fixtures/nominalValue/NominalValueFixture");
const request_1 = require("../../request");
const SetNominalValueCommand_1 = require("../../../../app/usecase/command/security/nominalValue/setNominalValue/SetNominalValueCommand");
const GetNominalValueQuery_1 = require("../../../../app/usecase/query/security/nominalValue/getNominalValue/GetNominalValueQuery");
const GetNominalValueDecimalsQuery_1 = require("../../../../app/usecase/query/security/nominalValue/getNominalValueDecimals/GetNominalValueDecimalsQuery");
const BigDecimal_1 = __importDefault(require("../../../../domain/context/shared/BigDecimal"));
describe("NominalValue", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        mirrorNodeMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Security_1.default.commandBus = commandBusMock;
        Security_1.default.queryBus = queryBusMock;
        Security_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("setNominalValue", () => {
        const setNominalValueRequest = new request_1.SetNominalValueRequest(NominalValueFixture_1.SetNominalValueRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should set nominal value successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.setNominalValue(setNominalValueRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetNominalValueRequest", setNominalValueRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetNominalValueCommand_1.SetNominalValueCommand(setNominalValueRequest.securityId, setNominalValueRequest.nominalValue, setNominalValueRequest.nominalValueDecimals));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.setNominalValue(setNominalValueRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetNominalValueRequest", setNominalValueRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetNominalValueCommand_1.SetNominalValueCommand(setNominalValueRequest.securityId, setNominalValueRequest.nominalValue, setNominalValueRequest.nominalValueDecimals));
        });
        it("should throw ValidationError if securityId is invalid", async () => {
            const invalidRequest = new request_1.SetNominalValueRequest({
                ...NominalValueFixture_1.SetNominalValueRequestFixture.create({ securityId: "invalid" }),
            });
            await expect(Security_1.default.setNominalValue(invalidRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw ValidationError if nominalValue is empty", async () => {
            const invalidRequest = new request_1.SetNominalValueRequest({
                ...NominalValueFixture_1.SetNominalValueRequestFixture.create({ nominalValue: "" }),
            });
            await expect(Security_1.default.setNominalValue(invalidRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getNominalValue", () => {
        const getNominalValueRequest = new request_1.GetNominalValueRequest(NominalValueFixture_1.GetNominalValueRequestFixture.create());
        it("should get nominal value successfully", async () => {
            const nominalValue = new BigDecimal_1.default("1000");
            queryBusMock.execute.mockResolvedValue({ payload: nominalValue });
            const result = await Security_1.default.getNominalValue(getNominalValueRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNominalValueRequest", getNominalValueRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNominalValueQuery_1.GetNominalValueQuery(getNominalValueRequest.securityId));
            expect(result).toEqual(nominalValue.toString());
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getNominalValue(getNominalValueRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNominalValueRequest", getNominalValueRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNominalValueQuery_1.GetNominalValueQuery(getNominalValueRequest.securityId));
        });
        it("should throw ValidationError if securityId is invalid", async () => {
            const invalidRequest = new request_1.GetNominalValueRequest({
                ...NominalValueFixture_1.GetNominalValueRequestFixture.create({ securityId: "invalid" }),
            });
            await expect(Security_1.default.getNominalValue(invalidRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getNominalValueDecimals", () => {
        const getNominalValueDecimalsRequest = new request_1.GetNominalValueDecimalsRequest(NominalValueFixture_1.GetNominalValueDecimalsRequestFixture.create());
        it("should get nominal value decimals successfully", async () => {
            const decimals = 6;
            queryBusMock.execute.mockResolvedValue({ payload: decimals });
            const result = await Security_1.default.getNominalValueDecimals(getNominalValueDecimalsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNominalValueDecimalsRequest", getNominalValueDecimalsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNominalValueDecimalsQuery_1.GetNominalValueDecimalsQuery(getNominalValueDecimalsRequest.securityId));
            expect(result).toBe(decimals);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getNominalValueDecimals(getNominalValueDecimalsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNominalValueDecimalsRequest", getNominalValueDecimalsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNominalValueDecimalsQuery_1.GetNominalValueDecimalsQuery(getNominalValueDecimalsRequest.securityId));
        });
        it("should throw ValidationError if securityId is invalid", async () => {
            const invalidRequest = new request_1.GetNominalValueDecimalsRequest({
                ...NominalValueFixture_1.GetNominalValueDecimalsRequestFixture.create({ securityId: "invalid" }),
            });
            await expect(Security_1.default.getNominalValueDecimals(invalidRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
