"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 SetImpactDataCommand_1 = require("../../../../app/usecase/command/interestRates/setImpactData/SetImpactDataCommand");
const SetInterestRateCommand_1 = require("../../../../app/usecase/command/interestRates/setInterestRate/SetInterestRateCommand");
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../../core/validation/ValidationError");
const ts_jest_1 = require("@golevelup/ts-jest");
const GetImpactDataQuery_1 = require("../../../../app/usecase/query/interestRates/getImpactData/GetImpactDataQuery");
const GetInterestRateQuery_1 = require("../../../../app/usecase/query/interestRates/getInterestRate/GetInterestRateQuery");
const LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
const DataFixture_1 = require("../../../../../__tests__/fixtures/shared/DataFixture");
const GetInterestRateRequest_1 = __importDefault(require("../../request/interestRates/GetInterestRateRequest"));
const SetImpactDataRequest_1 = __importDefault(require("../../request/interestRates/SetImpactDataRequest"));
const SetInterestRateRequest_1 = __importDefault(require("../../request/interestRates/SetInterestRateRequest"));
const GetImpactDataRequest_1 = __importDefault(require("../../request/kpiLinkedRate/GetImpactDataRequest"));
const KpiLinkedRate_1 = __importDefault(require("./KpiLinkedRate"));
describe("KpiLinkedRate", () => {
    let commandBusMock;
    let queryBusMock;
    let setInterestRateRequest;
    let setImpactDataRequest;
    let getInterestRateRequest;
    let getImpactDataRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const expectedSetInterestRateResponse = {
        payload: true,
        transactionId: transactionId,
    };
    const expectedSetImpactDataResponse = {
        payload: true,
        transactionId: transactionId,
    };
    const expectedGetInterestRateResponse = {
        maxRate: "10.5",
        baseRate: "5.5",
        minRate: "1.5",
        startPeriod: "1640995200",
        startRate: "4.5",
        missedPenalty: "2.5",
        reportPeriod: "30",
        rateDecimals: 8,
    };
    const expectedGetImpactDataResponse = {
        maxDeviationCap: "100.5",
        baseLine: "50.0",
        maxDeviationFloor: "10.5",
        impactDataDecimals: 8,
        adjustmentPrecision: "5.0",
    };
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        KpiLinkedRate_1.default.commandBus = commandBusMock;
        KpiLinkedRate_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("setInterestRate", () => {
        beforeEach(() => {
            setInterestRateRequest = new SetInterestRateRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
                maxRate: "10.5",
                baseRate: "5.5",
                minRate: "1.5",
                startPeriod: "1640995200",
                startRate: "4.5",
                missedPenalty: "2.5",
                reportPeriod: "30",
                rateDecimals: 8,
            });
        });
        it("should set interest rate successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedSetInterestRateResponse);
            const result = await KpiLinkedRate_1.default.setInterestRate(setInterestRateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetInterestRateRequest", setInterestRateRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetInterestRateCommand_1.SetInterestRateCommand(setInterestRateRequest.securityId, setInterestRateRequest.maxRate, setInterestRateRequest.baseRate, setInterestRateRequest.minRate, setInterestRateRequest.startPeriod, setInterestRateRequest.startRate, setInterestRateRequest.missedPenalty, setInterestRateRequest.reportPeriod, setInterestRateRequest.rateDecimals));
            expect(result).toEqual(expectedSetInterestRateResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(KpiLinkedRate_1.default.setInterestRate(setInterestRateRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetInterestRateRequest", setInterestRateRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetInterestRateCommand_1.SetInterestRateCommand(setInterestRateRequest.securityId, setInterestRateRequest.maxRate, setInterestRateRequest.baseRate, setInterestRateRequest.minRate, setInterestRateRequest.startPeriod, setInterestRateRequest.startRate, setInterestRateRequest.missedPenalty, setInterestRateRequest.reportPeriod, setInterestRateRequest.rateDecimals));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(KpiLinkedRate_1.default.setInterestRate(setInterestRateRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetInterestRateRequest", setInterestRateRequest);
            expect(commandBusMock.execute).not.toHaveBeenCalled();
        });
    });
    describe("setImpactData", () => {
        beforeEach(() => {
            setImpactDataRequest = new SetImpactDataRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
                maxDeviationCap: "100.5",
                baseLine: "50.0",
                maxDeviationFloor: "10.5",
                impactDataDecimals: 8,
                adjustmentPrecision: "5.0",
            });
        });
        it("should set impact data successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedSetImpactDataResponse);
            const result = await KpiLinkedRate_1.default.setImpactData(setImpactDataRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetImpactDataRequest", setImpactDataRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetImpactDataCommand_1.SetImpactDataCommand(setImpactDataRequest.securityId, setImpactDataRequest.maxDeviationCap, setImpactDataRequest.baseLine, setImpactDataRequest.maxDeviationFloor, setImpactDataRequest.impactDataDecimals, setImpactDataRequest.adjustmentPrecision));
            expect(result).toEqual(expectedSetImpactDataResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(KpiLinkedRate_1.default.setImpactData(setImpactDataRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetImpactDataRequest", setImpactDataRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetImpactDataCommand_1.SetImpactDataCommand(setImpactDataRequest.securityId, setImpactDataRequest.maxDeviationCap, setImpactDataRequest.baseLine, setImpactDataRequest.maxDeviationFloor, setImpactDataRequest.impactDataDecimals, setImpactDataRequest.adjustmentPrecision));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(KpiLinkedRate_1.default.setImpactData(setImpactDataRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetImpactDataRequest", setImpactDataRequest);
            expect(commandBusMock.execute).not.toHaveBeenCalled();
        });
    });
    describe("getInterestRate", () => {
        beforeEach(() => {
            getInterestRateRequest = new GetInterestRateRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
            });
        });
        it("should get interest rate successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedGetInterestRateResponse);
            const result = await KpiLinkedRate_1.default.getInterestRate(getInterestRateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetInterestRateRequest", getInterestRateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetInterestRateQuery_1.GetInterestRateQuery(getInterestRateRequest.securityId));
            expect(result).toEqual(expectedGetInterestRateResponse);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(KpiLinkedRate_1.default.getInterestRate(getInterestRateRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetInterestRateRequest", getInterestRateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetInterestRateQuery_1.GetInterestRateQuery(getInterestRateRequest.securityId));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(KpiLinkedRate_1.default.getInterestRate(getInterestRateRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetInterestRateRequest", getInterestRateRequest);
            expect(queryBusMock.execute).not.toHaveBeenCalled();
        });
    });
    describe("getImpactData", () => {
        beforeEach(() => {
            getImpactDataRequest = new GetImpactDataRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
            });
        });
        it("should get impact data successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedGetImpactDataResponse);
            const result = await KpiLinkedRate_1.default.getImpactData(getImpactDataRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetImpactDataRequest", getImpactDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetImpactDataQuery_1.GetImpactDataQuery(getImpactDataRequest.securityId));
            expect(result).toEqual(expectedGetImpactDataResponse);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(KpiLinkedRate_1.default.getImpactData(getImpactDataRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetImpactDataRequest", getImpactDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetImpactDataQuery_1.GetImpactDataQuery(getImpactDataRequest.securityId));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(KpiLinkedRate_1.default.getImpactData(getImpactDataRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetImpactDataRequest", getImpactDataRequest);
            expect(queryBusMock.execute).not.toHaveBeenCalled();
        });
    });
});
