"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 ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../core/validation/ValidationError");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const CancelCouponCommand_1 = require("../../../app/usecase/command/bond/coupon/cancel/CancelCouponCommand");
const SetCouponCommand_1 = require("../../../app/usecase/command/bond/coupon/set/SetCouponCommand");
const GetCouponForQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponFor/GetCouponForQuery");
const GetCouponAmountForQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponAmountFor/GetCouponAmountForQuery");
const GetCouponQuery_1 = require("../../../app/usecase/query/bond/coupons/getCoupon/GetCouponQuery");
const GetCouponCountQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponCount/GetCouponCountQuery");
const GetCouponsOrderedListQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponsOrderedList/GetCouponsOrderedListQuery");
const GetCouponsOrderedListTotalQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponsOrderedListTotal/GetCouponsOrderedListTotalQuery");
const GetCouponHoldersQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponHolders/GetCouponHoldersQuery");
const GetTotalCouponHoldersQuery_1 = require("../../../app/usecase/query/bond/coupons/getTotalCouponHolders/GetTotalCouponHoldersQuery");
const GetCouponFromOrderedListAtQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponFromOrderedListAt/GetCouponFromOrderedListAtQuery");
const CancelCouponRequest_1 = __importDefault(require("../request/bond/CancelCouponRequest"));
const SetCouponRequest_1 = __importDefault(require("../request/bond/SetCouponRequest"));
const GetCouponForRequest_1 = __importDefault(require("../request/bond/GetCouponForRequest"));
const GetCouponRequest_1 = __importDefault(require("../request/bond/GetCouponRequest"));
const GetAllCouponsRequest_1 = __importDefault(require("../request/bond/GetAllCouponsRequest"));
const GetCouponsOrderedListRequest_1 = __importDefault(require("../request/bond/GetCouponsOrderedListRequest"));
const GetCouponsOrderedListTotalRequest_1 = __importDefault(require("../request/bond/GetCouponsOrderedListTotalRequest"));
const GetCouponFromOrderedListAtRequest_1 = __importDefault(require("../request/bond/GetCouponFromOrderedListAtRequest"));
const request_1 = require("../request");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
const BondFixture_1 = require("../../../../__tests__/fixtures/bond/BondFixture");
const SecurityDate_1 = require("../../../domain/context/shared/SecurityDate");
const RateStatus_1 = require("../../../domain/context/bond/RateStatus");
const _1 = require("@faker-js/faker/.");
const Coupon_1 = __importDefault(require("./Coupon"));
describe("Coupon", () => {
    let commandBusMock;
    let queryBusMock;
    let setCouponRequest;
    let cancelCouponRequest;
    let getCouponForRequest;
    let getCouponRequest;
    let getAllCouponsRequest;
    let getCouponsOrderedListRequest;
    let getCouponHoldersRequest;
    let getTotalCouponHoldersRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        getAllCouponsRequest = new GetAllCouponsRequest_1.default(BondFixture_1.GetAllCouponsRequestFixture.create());
        getCouponsOrderedListRequest = new GetCouponsOrderedListRequest_1.default(BondFixture_1.GetCouponsOrderedListRequestFixture.create());
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Coupon_1.default.commandBus = commandBusMock;
        Coupon_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("setCoupon", () => {
        setCouponRequest = new SetCouponRequest_1.default(BondFixture_1.SetCouponRequestFixture.create());
        it("should set coupon successfully", async () => {
            const expectedResponse = {
                payload: 1,
                transactionId: transactionId,
            };
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.setCoupon(setCouponRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetCouponRequest", setCouponRequest);
            expect(commandBusMock.execute).toHaveBeenCalledTimes(1);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetCouponCommand_1.SetCouponCommand(setCouponRequest.securityId, setCouponRequest.recordTimestamp, setCouponRequest.executionTimestamp, setCouponRequest.rate, setCouponRequest.startTimestamp, setCouponRequest.endTimestamp, setCouponRequest.fixingTimestamp, RateStatus_1.CastRateStatus.fromNumber(setCouponRequest.rateStatus)));
            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(Coupon_1.default.setCoupon(setCouponRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetCouponRequest", setCouponRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetCouponCommand_1.SetCouponCommand(setCouponRequest.securityId, setCouponRequest.recordTimestamp, setCouponRequest.executionTimestamp, setCouponRequest.rate, setCouponRequest.startTimestamp, setCouponRequest.endTimestamp, setCouponRequest.fixingTimestamp, RateStatus_1.CastRateStatus.fromNumber(setCouponRequest.rateStatus)));
        });
        it("should throw error if recordTimestamp is invalid", async () => {
            setCouponRequest = new SetCouponRequest_1.default({
                ...BondFixture_1.SetCouponRequestFixture.create(),
                recordTimestamp: (Math.ceil(new Date().getTime() / 1000) - 100).toString(),
            });
            await expect(Coupon_1.default.setCoupon(setCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if executionTimestamp is invalid", async () => {
            const time = _1.faker.date.past().getTime();
            setCouponRequest = new SetCouponRequest_1.default({
                ...BondFixture_1.SetCouponRequestFixture.create(),
                recordTimestamp: time.toString(),
                executionTimestamp: (time - 10).toString(),
            });
            await expect(Coupon_1.default.setCoupon(setCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if securityId is invalid", async () => {
            setCouponRequest = new SetCouponRequest_1.default({
                ...BondFixture_1.SetCouponRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.setCoupon(setCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if rate is invalid", async () => {
            setCouponRequest = new SetCouponRequest_1.default({
                ...BondFixture_1.SetCouponRequestFixture.create(),
                rate: "invalid",
            });
            await expect(Coupon_1.default.setCoupon(setCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("cancelCoupon", () => {
        beforeEach(() => {
            cancelCouponRequest = new CancelCouponRequest_1.default(BondFixture_1.CancelCouponRequestFixture.create());
        });
        it("should cancel coupon successfully", async () => {
            const expectedResponse = {
                payload: true,
                transactionId: transactionId,
            };
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.cancelCoupon(cancelCouponRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("CancelCouponRequest", cancelCouponRequest);
            expect(commandBusMock.execute).toHaveBeenCalledTimes(1);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new CancelCouponCommand_1.CancelCouponCommand(cancelCouponRequest.securityId, cancelCouponRequest.couponId));
            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(Coupon_1.default.cancelCoupon(cancelCouponRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("CancelCouponRequest", cancelCouponRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new CancelCouponCommand_1.CancelCouponCommand(cancelCouponRequest.securityId, cancelCouponRequest.couponId));
        });
        it("should throw error if securityId is invalid", async () => {
            cancelCouponRequest = new CancelCouponRequest_1.default({
                ...BondFixture_1.CancelCouponRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.cancelCoupon(cancelCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCouponFor", () => {
        beforeEach(() => {
            getCouponForRequest = new GetCouponForRequest_1.default(BondFixture_1.GetCouponForRequestFixture.create());
        });
        it("should get coupon for successfully", async () => {
            const coupon = BondFixture_1.CouponFixture.create();
            const expectedResponse = {
                tokenBalance: BigInt(1000),
                nominalValue: BigInt(500),
                decimals: 2,
                recordDateReached: false,
                coupon: coupon,
                couponAmount: {
                    numerator: "10",
                    denominator: "4",
                    recordDateReached: true,
                },
                isDisabled: false,
            };
            queryBusMock.execute.mockResolvedValue({ couponFor: expectedResponse });
            const result = await Coupon_1.default.getCouponFor(getCouponForRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponForRequest", getCouponForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponForQuery_1.GetCouponForQuery(getCouponForRequest.targetId, getCouponForRequest.securityId, getCouponForRequest.couponId));
            expect(result).toEqual(expect.objectContaining({
                tokenBalance: expectedResponse.tokenBalance.toString(),
                nominalValue: expectedResponse.nominalValue.toString(),
                decimals: expectedResponse.decimals.toString(),
                recordDateReached: expectedResponse.recordDateReached,
                coupon: expect.objectContaining({
                    recordDate: new Date(coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    executionDate: new Date(coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    rate: coupon.rate.toString(),
                }),
                couponAmount: expect.objectContaining({
                    numerator: expectedResponse.couponAmount.numerator,
                    denominator: expectedResponse.couponAmount.denominator,
                    recordDateReached: expectedResponse.couponAmount.recordDateReached,
                }),
                isDisabled: expectedResponse.isDisabled,
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponFor(getCouponForRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponForRequest", getCouponForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponForQuery_1.GetCouponForQuery(getCouponForRequest.targetId, getCouponForRequest.securityId, getCouponForRequest.couponId));
        });
        it("should throw error if targetId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                targetId: "invalid",
            });
            await expect(Coupon_1.default.getCouponFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if securityId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.getCouponFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if couponId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                couponId: 0,
            });
            await expect(Coupon_1.default.getCouponFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCouponAmountFor", () => {
        beforeEach(() => {
            getCouponForRequest = new GetCouponForRequest_1.default(BondFixture_1.GetCouponForRequestFixture.create());
        });
        it("should get coupon amount for successfully", async () => {
            const expectedResponse = {
                numerator: "10",
                denominator: "4",
                recordDateReached: true,
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.getCouponAmountFor(getCouponForRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponForRequest", getCouponForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponAmountForQuery_1.GetCouponAmountForQuery(getCouponForRequest.targetId, getCouponForRequest.securityId, getCouponForRequest.couponId));
            expect(result).toEqual(expect.objectContaining({
                numerator: expectedResponse.numerator,
                denominator: expectedResponse.denominator,
                recordDateReached: expectedResponse.recordDateReached,
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponAmountFor(getCouponForRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponForRequest", getCouponForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponAmountForQuery_1.GetCouponAmountForQuery(getCouponForRequest.targetId, getCouponForRequest.securityId, getCouponForRequest.couponId));
        });
        it("should throw error if targetId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                targetId: "invalid",
            });
            await expect(Coupon_1.default.getCouponAmountFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if securityId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.getCouponAmountFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if couponId is invalid", async () => {
            getCouponForRequest = new GetCouponForRequest_1.default({
                ...BondFixture_1.GetCouponForRequestFixture.create(),
                couponId: 0,
            });
            await expect(Coupon_1.default.getCouponAmountFor(getCouponForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCoupon", () => {
        getCouponRequest = new GetCouponRequest_1.default(BondFixture_1.GetCouponRequestFixture.create());
        it("should get coupon successfully", async () => {
            const expectedResponse = {
                coupon: BondFixture_1.CouponFixture.create(),
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.getCoupon(getCouponRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponRequest", getCouponRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponQuery_1.GetCouponQuery(getCouponRequest.securityId, getCouponRequest.couponId));
            expect(result).toEqual(expect.objectContaining({
                couponId: getCouponRequest.couponId,
                recordDate: new Date(expectedResponse.coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                executionDate: new Date(expectedResponse.coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                rate: expectedResponse.coupon.rate.toString(),
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCoupon(getCouponRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponRequest", getCouponRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponQuery_1.GetCouponQuery(getCouponRequest.securityId, getCouponRequest.couponId));
        });
        it("should throw error if securityId is invalid", async () => {
            getCouponRequest = new GetCouponRequest_1.default({
                ...BondFixture_1.GetCouponRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.getCoupon(getCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if couponId is invalid", async () => {
            getCouponRequest = new GetCouponRequest_1.default({
                ...BondFixture_1.GetCouponRequestFixture.create(),
                couponId: 0,
            });
            await expect(Coupon_1.default.getCoupon(getCouponRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getAllCoupons", () => {
        getAllCouponsRequest = new GetAllCouponsRequest_1.default(BondFixture_1.GetAllCouponsRequestFixture.create());
        it("should get all coupons successfully", async () => {
            const expectedResponse = {
                payload: 1,
            };
            const expectedResponse2 = {
                coupon: BondFixture_1.CouponFixture.create(),
            };
            queryBusMock.execute.mockResolvedValueOnce(expectedResponse).mockResolvedValueOnce(expectedResponse2);
            const result = await Coupon_1.default.getAllCoupons(getAllCouponsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAllCouponsRequest", getAllCouponsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(2);
            expect(queryBusMock.execute).toHaveBeenNthCalledWith(1, new GetCouponCountQuery_1.GetCouponCountQuery(getAllCouponsRequest.securityId));
            expect(queryBusMock.execute).toHaveBeenNthCalledWith(2, new GetCouponQuery_1.GetCouponQuery(getAllCouponsRequest.securityId, 1));
            expect(result).toEqual(expect.arrayContaining([
                {
                    couponId: 1,
                    recordDate: new Date(expectedResponse2.coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    executionDate: new Date(expectedResponse2.coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    rate: expectedResponse2.coupon.rate.toString(),
                    rateDecimals: expectedResponse2.coupon.rateDecimals,
                    startDate: new Date(expectedResponse2.coupon.startTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    endDate: new Date(expectedResponse2.coupon.endTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    fixingDate: new Date(expectedResponse2.coupon.fixingTimeStamp * SecurityDate_1.ONE_THOUSAND),
                    rateStatus: RateStatus_1.CastRateStatus.toNumber(expectedResponse2.coupon.rateStatus),
                    isDisabled: expectedResponse2.coupon.isDisabled,
                },
            ]));
        });
        it("should return empty array if count is 0", async () => {
            const expectedResponse = {
                payload: 0,
            };
            queryBusMock.execute.mockResolvedValueOnce(expectedResponse);
            const result = await Coupon_1.default.getAllCoupons(getAllCouponsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAllCouponsRequest", getAllCouponsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponCountQuery_1.GetCouponCountQuery(getAllCouponsRequest.securityId));
            expect(result).toStrictEqual([]);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getAllCoupons(getAllCouponsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAllCouponsRequest", getAllCouponsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponCountQuery_1.GetCouponCountQuery(getAllCouponsRequest.securityId));
        });
    });
    describe("getCouponsOrderedList", () => {
        it("should get coupons ordered list successfully", async () => {
            const expectedResponse = [1, 2, 3, 4, 5];
            queryBusMock.execute.mockResolvedValue({ payload: expectedResponse });
            const result = await Coupon_1.default.getCouponsOrderedList(getCouponsOrderedListRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponsOrderedListRequest", getCouponsOrderedListRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponsOrderedListQuery_1.GetCouponsOrderedListQuery(getCouponsOrderedListRequest.securityId, getCouponsOrderedListRequest.pageIndex, getCouponsOrderedListRequest.pageLength));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponsOrderedList(getCouponsOrderedListRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponsOrderedListRequest", getCouponsOrderedListRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponsOrderedListQuery_1.GetCouponsOrderedListQuery(getCouponsOrderedListRequest.securityId, getCouponsOrderedListRequest.pageIndex, getCouponsOrderedListRequest.pageLength));
        });
        it("should throw error if securityId is invalid", async () => {
            const invalidRequest = new GetCouponsOrderedListRequest_1.default({
                securityId: "invalid",
                pageIndex: 0,
                pageLength: 10,
            });
            await expect(Coupon_1.default.getCouponsOrderedList(invalidRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should work with mocked query handler", async () => {
            const expectedResponse = [10, 20, 30];
            const mockHandler = {
                execute: jest.fn().mockResolvedValue({ payload: expectedResponse }),
            };
            queryBusMock.execute.mockImplementation((query) => {
                if (query instanceof GetCouponsOrderedListQuery_1.GetCouponsOrderedListQuery) {
                    return mockHandler.execute(query);
                }
                return Promise.resolve({});
            });
            const result = await Coupon_1.default.getCouponsOrderedList(getCouponsOrderedListRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponsOrderedListRequest", getCouponsOrderedListRequest);
            expect(mockHandler.execute).toHaveBeenCalledWith(new GetCouponsOrderedListQuery_1.GetCouponsOrderedListQuery(getCouponsOrderedListRequest.securityId, getCouponsOrderedListRequest.pageIndex, getCouponsOrderedListRequest.pageLength));
            expect(result).toEqual(expectedResponse);
        });
    });
    describe("getCouponsOrderedListTotal", () => {
        it("should get coupons ordered list total successfully", async () => {
            const securityId = DataFixture_1.HederaIdPropsFixture.create().value;
            const request = new GetCouponsOrderedListTotalRequest_1.default({ securityId });
            queryBusMock.execute.mockResolvedValue({ payload: 5 });
            const result = await Coupon_1.default.getCouponsOrderedListTotal(request);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetCouponsOrderedListTotalRequest", request);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponsOrderedListTotalQuery_1.GetCouponsOrderedListTotalQuery(securityId));
            expect(result).toEqual(5);
        });
        it("should throw an error if query execution fails", async () => {
            const securityId = DataFixture_1.HederaIdPropsFixture.create().value;
            const request = new GetCouponsOrderedListTotalRequest_1.default({ securityId });
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponsOrderedListTotal(request)).rejects.toThrow("Query execution failed");
        });
        it("should throw error if securityId is invalid", async () => {
            const request = new GetCouponsOrderedListTotalRequest_1.default({ securityId: "invalid" });
            await expect(Coupon_1.default.getCouponsOrderedListTotal(request)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCouponHolders", () => {
        getCouponHoldersRequest = new request_1.GetCouponHoldersRequest(BondFixture_1.GetCouponHoldersQueryFixture.create());
        it("should get coupon holders successfully", async () => {
            const expectedResponse = {
                payload: [transactionId],
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.getCouponHolders(getCouponHoldersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetCouponHoldersRequest.name, getCouponHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponHoldersQuery_1.GetCouponHoldersQuery(getCouponHoldersRequest.securityId, getCouponHoldersRequest.couponId, getCouponHoldersRequest.start, getCouponHoldersRequest.end));
            expect(result).toStrictEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponHolders(getCouponHoldersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetCouponHoldersRequest.name, getCouponHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponHoldersQuery_1.GetCouponHoldersQuery(getCouponHoldersRequest.securityId, getCouponHoldersRequest.couponId, getCouponHoldersRequest.start, getCouponHoldersRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getCouponHoldersRequest = new request_1.GetCouponHoldersRequest({
                ...BondFixture_1.GetCouponHoldersQueryFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.getCouponHolders(getCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if couponId is invalid", async () => {
            getCouponHoldersRequest = new request_1.GetCouponHoldersRequest({
                ...BondFixture_1.GetCouponHoldersQueryFixture.create(),
                couponId: -1,
            });
            await expect(Coupon_1.default.getCouponHolders(getCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if start is invalid", async () => {
            getCouponHoldersRequest = new request_1.GetCouponHoldersRequest({
                ...BondFixture_1.GetCouponHoldersQueryFixture.create(),
                start: -1,
            });
            await expect(Coupon_1.default.getCouponHolders(getCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if end is invalid", async () => {
            getCouponHoldersRequest = new request_1.GetCouponHoldersRequest({
                ...BondFixture_1.GetCouponHoldersQueryFixture.create(),
                end: -1,
            });
            await expect(Coupon_1.default.getCouponHolders(getCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getTotalCouponHolders", () => {
        getTotalCouponHoldersRequest = new request_1.GetTotalCouponHoldersRequest(BondFixture_1.GetTotalCouponHoldersRequestFixture.create());
        it("should get total coupon holders successfully", async () => {
            const expectedResponse = {
                payload: 1,
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Coupon_1.default.getTotalCouponHolders(getTotalCouponHoldersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetTotalCouponHoldersRequest.name, getTotalCouponHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetTotalCouponHoldersQuery_1.GetTotalCouponHoldersQuery(getTotalCouponHoldersRequest.securityId, getTotalCouponHoldersRequest.couponId));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getTotalCouponHolders(getTotalCouponHoldersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetTotalCouponHoldersRequest.name, getTotalCouponHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetTotalCouponHoldersQuery_1.GetTotalCouponHoldersQuery(getTotalCouponHoldersRequest.securityId, getTotalCouponHoldersRequest.couponId));
        });
        it("should throw error if securityId is invalid", async () => {
            getTotalCouponHoldersRequest = new request_1.GetTotalCouponHoldersRequest({
                ...BondFixture_1.GetTotalCouponHoldersRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Coupon_1.default.getTotalCouponHolders(getTotalCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if couponId is invalid", async () => {
            getTotalCouponHoldersRequest = new request_1.GetTotalCouponHoldersRequest({
                ...BondFixture_1.GetTotalCouponHoldersRequestFixture.create(),
                couponId: -1,
            });
            await expect(Coupon_1.default.getTotalCouponHolders(getTotalCouponHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCouponFromOrderedListAt", () => {
        it("should get coupon from ordered list at position successfully", async () => {
            const securityId = DataFixture_1.HederaIdPropsFixture.create().value;
            const pos = 3;
            const request = new GetCouponFromOrderedListAtRequest_1.default({ securityId, pos });
            queryBusMock.execute.mockResolvedValue({ couponId: 7 });
            const result = await Coupon_1.default.getCouponFromOrderedListAt(request);
            expect(handleValidationSpy).toHaveBeenCalledWith(GetCouponFromOrderedListAtRequest_1.default.name, request);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetCouponFromOrderedListAtQuery_1.GetCouponFromOrderedListAtQuery(securityId, pos));
            expect(result).toEqual(7);
        });
        it("should throw an error if query execution fails", async () => {
            const securityId = DataFixture_1.HederaIdPropsFixture.create().value;
            const request = new GetCouponFromOrderedListAtRequest_1.default({ securityId, pos: 1 });
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Coupon_1.default.getCouponFromOrderedListAt(request)).rejects.toThrow("Query execution failed");
        });
        it("should throw error if securityId is invalid", async () => {
            const request = new GetCouponFromOrderedListAtRequest_1.default({ securityId: "invalid", pos: 1 });
            await expect(Coupon_1.default.getCouponFromOrderedListAt(request)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
