"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 ScheduledCouponListingCountRequest_1 = __importDefault(require("../../../in/request/scheduledTasks/ScheduledCouponListingCountRequest"));
const GetScheduledCouponListingRequest_1 = __importDefault(require("../../../in/request/scheduledTasks/GetScheduledCouponListingRequest"));
const ScheduledCouponListingCountQuery_1 = require("../../../../app/usecase/query/scheduledTasks/scheduledCouponListingCount/ScheduledCouponListingCountQuery");
const GetScheduledCouponListingQuery_1 = require("../../../../app/usecase/query/scheduledCouponListing/getScheduledCouponListing/GetScheduledCouponListingQuery");
const ScheduledCouponListing_1 = __importDefault(require("./ScheduledCouponListing"));
describe("ScheduledCouponListing", () => {
    let queryBusMock;
    let scheduledCouponListingCountRequest;
    let getScheduledCouponListingRequest;
    let handleValidationSpy;
    const mockSecurityId = "0.0.123456";
    const mockCount = 5;
    const mockScheduledCouponListing = { id: 1, date: "2023-01-01", amount: "100" };
    beforeEach(() => {
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        ScheduledCouponListing_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("scheduledCouponListingCount", () => {
        beforeEach(() => {
            scheduledCouponListingCountRequest = new ScheduledCouponListingCountRequest_1.default({ securityId: mockSecurityId });
        });
        it("should get scheduled coupon listing count successfully", async () => {
            const expectedResponse = new ScheduledCouponListingCountQuery_1.ScheduledCouponListingCountQueryResponse(mockCount);
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ScheduledCouponListing_1.default.scheduledCouponListingCount(scheduledCouponListingCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ScheduledCouponListingCountRequest", scheduledCouponListingCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new ScheduledCouponListingCountQuery_1.ScheduledCouponListingCountQuery(scheduledCouponListingCountRequest.securityId));
            expect(result).toEqual(mockCount);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ScheduledCouponListing_1.default.scheduledCouponListingCount(scheduledCouponListingCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ScheduledCouponListingCountRequest", scheduledCouponListingCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new ScheduledCouponListingCountQuery_1.ScheduledCouponListingCountQuery(scheduledCouponListingCountRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            scheduledCouponListingCountRequest = new ScheduledCouponListingCountRequest_1.default({
                securityId: "",
            });
            await expect(ScheduledCouponListing_1.default.scheduledCouponListingCount(scheduledCouponListingCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getScheduledCouponListing", () => {
        beforeEach(() => {
            getScheduledCouponListingRequest = new GetScheduledCouponListingRequest_1.default({
                securityId: mockSecurityId,
                pageIndex: 0,
                pageLength: 10,
            });
        });
        it("should get scheduled coupon listing successfully", async () => {
            const expectedResponse = new GetScheduledCouponListingQuery_1.GetScheduledCouponListingQueryResponse(mockScheduledCouponListing);
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ScheduledCouponListing_1.default.getScheduledCouponListing(getScheduledCouponListingRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetScheduledCouponListingRequest", getScheduledCouponListingRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetScheduledCouponListingQuery_1.GetScheduledCouponListingQuery(getScheduledCouponListingRequest.securityId, 0, 10));
            expect(result).toEqual(mockScheduledCouponListing);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ScheduledCouponListing_1.default.getScheduledCouponListing(getScheduledCouponListingRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetScheduledCouponListingRequest", getScheduledCouponListingRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetScheduledCouponListingQuery_1.GetScheduledCouponListingQuery(getScheduledCouponListingRequest.securityId, 0, 10));
        });
        it("should throw error if securityId is invalid", async () => {
            getScheduledCouponListingRequest = new GetScheduledCouponListingRequest_1.default({
                securityId: "",
                pageIndex: 0,
                pageLength: 10,
            });
            await expect(ScheduledCouponListing_1.default.getScheduledCouponListing(getScheduledCouponListingRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
