"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 request_1 = require("../request");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
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 KycFixture_1 = require("../../../../__tests__/fixtures/kyc/KycFixture");
const Kyc_1 = __importDefault(require("./Kyc"));
const ActivateInternalKycCommand_1 = require("../../../app/usecase/command/security/kyc/activateInternalKyc/ActivateInternalKycCommand");
const DeactivateInternalKycCommand_1 = require("../../../app/usecase/command/security/kyc/deactivateInternalKyc/DeactivateInternalKycCommand");
const IsInternalKycActivatedQuery_1 = require("../../../app/usecase/query/security/kyc/isInternalKycActivated/IsInternalKycActivatedQuery");
const GrantKycCommand_1 = require("../../../app/usecase/command/security/kyc/grantKyc/GrantKycCommand");
const RevokeKycCommand_1 = require("../../../app/usecase/command/security/kyc/revokeKyc/RevokeKycCommand");
const GetKycForQuery_1 = require("../../../app/usecase/query/security/kyc/getKycFor/GetKycForQuery");
const GetKycAccountsCountQuery_1 = require("../../../app/usecase/query/security/kyc/getKycAccountsCount/GetKycAccountsCountQuery");
const GetKycAccountsDataQuery_1 = require("../../../app/usecase/query/security/kyc/getKycAccountsData/GetKycAccountsDataQuery");
const GetKycStatusForQuery_1 = require("../../../app/usecase/query/security/kyc/getKycStatusFor/GetKycStatusForQuery");
describe("Kyc", () => {
    let commandBusMock;
    let queryBusMock;
    let grantKycRequest;
    let revokeKycRequest;
    let getKycAccountsCountRequest;
    let getKycForRequest;
    let getKycAccountsDataRequest;
    let getKycStatusForRequest;
    let activateInternalKycRequest;
    let deactivateInternalKycRequest;
    let isInternalKycActivatedRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const expectedResponse = {
        payload: true,
        transactionId: transactionId,
    };
    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(() => { });
        Kyc_1.default.commandBus = commandBusMock;
        Kyc_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("grantKyc", () => {
        grantKycRequest = new request_1.GrantKycRequest(KycFixture_1.GrantKycRequestFixture.create());
        it("should grant kyc successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.grantKyc(grantKycRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GrantKycRequest", grantKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new GrantKycCommand_1.GrantKycCommand(grantKycRequest.securityId, grantKycRequest.targetId, grantKycRequest.vcBase64));
            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(Kyc_1.default.grantKyc(grantKycRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GrantKycRequest", grantKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new GrantKycCommand_1.GrantKycCommand(grantKycRequest.securityId, grantKycRequest.targetId, grantKycRequest.vcBase64));
        });
        it("should throw error if securityId is invalid", async () => {
            grantKycRequest = new request_1.GrantKycRequest({
                ...KycFixture_1.GrantKycRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.grantKyc(grantKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            grantKycRequest = new request_1.GrantKycRequest({
                ...KycFixture_1.GrantKycRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Kyc_1.default.grantKyc(grantKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if vcBase64 is invalid", async () => {
            grantKycRequest = new request_1.GrantKycRequest({
                ...KycFixture_1.GrantKycRequestFixture.create({
                    vcBase64: "inv@lid",
                }),
            });
            await expect(Kyc_1.default.grantKyc(grantKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("revokeKyc", () => {
        revokeKycRequest = new request_1.RevokeKycRequest(KycFixture_1.RevokeKycRequestFixture.create());
        it("should revoke kyc successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.revokeKyc(revokeKycRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("RevokeKycRequest", revokeKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RevokeKycCommand_1.RevokeKycCommand(revokeKycRequest.securityId, revokeKycRequest.targetId));
            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(Kyc_1.default.revokeKyc(revokeKycRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("RevokeKycRequest", revokeKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RevokeKycCommand_1.RevokeKycCommand(revokeKycRequest.securityId, revokeKycRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            revokeKycRequest = new request_1.RevokeKycRequest({
                ...KycFixture_1.RevokeKycRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.revokeKyc(revokeKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            revokeKycRequest = new request_1.RevokeKycRequest({
                ...KycFixture_1.RevokeKycRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Kyc_1.default.revokeKyc(revokeKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getKycFor", () => {
        getKycForRequest = new request_1.GetKycForRequest(KycFixture_1.GetKycForRequestFixture.create());
        const expectedResponse = {
            payload: KycFixture_1.KycFixture.create(),
        };
        it("should get kyc for successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.getKycFor(getKycForRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycForRequest", getKycForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycForQuery_1.GetKycForQuery(getKycForRequest.securityId, getKycForRequest.targetId));
            expect(result).toEqual(expect.objectContaining({
                validFrom: expectedResponse.payload.validFrom,
                validTo: expectedResponse.payload.validTo,
                vcId: expectedResponse.payload.vcId,
                issuer: expectedResponse.payload.issuer,
                status: expectedResponse.payload.status,
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kyc_1.default.getKycFor(getKycForRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycForRequest", getKycForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycForQuery_1.GetKycForQuery(getKycForRequest.securityId, getKycForRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getKycForRequest = new request_1.GetKycForRequest({
                ...KycFixture_1.GetKycForRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycFor(getKycForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getKycForRequest = new request_1.GetKycForRequest({
                ...KycFixture_1.GetKycForRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycFor(getKycForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getKycAccountsCount", () => {
        getKycAccountsCountRequest = new request_1.GetKycAccountsCountRequest(KycFixture_1.GetKycAccountsCountRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get kyc accounts count successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.getKycAccountsCount(getKycAccountsCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycAccountsCountRequest", getKycAccountsCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycAccountsCountQuery_1.GetKycAccountsCountQuery(getKycAccountsCountRequest.securityId, getKycAccountsCountRequest.kycStatus));
            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(Kyc_1.default.getKycAccountsCount(getKycAccountsCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycAccountsCountRequest", getKycAccountsCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycAccountsCountQuery_1.GetKycAccountsCountQuery(getKycAccountsCountRequest.securityId, getKycAccountsCountRequest.kycStatus));
        });
        it("should throw error if securityId is invalid", async () => {
            getKycAccountsCountRequest = new request_1.GetKycAccountsCountRequest({
                ...KycFixture_1.GetKycAccountsCountRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycAccountsCount(getKycAccountsCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if kycStatus is invalid", async () => {
            getKycAccountsCountRequest = new request_1.GetKycAccountsCountRequest({
                ...KycFixture_1.GetKycAccountsCountRequestFixture.create({
                    kycStatus: -1,
                }),
            });
            await expect(Kyc_1.default.getKycAccountsCount(getKycAccountsCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getKycAccountsData", () => {
        getKycAccountsDataRequest = new request_1.GetKycAccountsDataRequest(KycFixture_1.GetKycAccountsDataRequestFixture.create());
        const expectedResponse = {
            payload: [KycFixture_1.KycAccountDataFixture.create()],
        };
        it("should get kyc accounts data successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycAccountsData", getKycAccountsDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycAccountsDataQuery_1.GetKycAccountsDataQuery(getKycAccountsDataRequest.securityId, getKycAccountsDataRequest.kycStatus, getKycAccountsDataRequest.start, getKycAccountsDataRequest.end));
            expect(result).toEqual(expect.arrayContaining([
                {
                    account: expectedResponse.payload[0].account,
                    validFrom: expectedResponse.payload[0].validFrom,
                    validTo: expectedResponse.payload[0].validTo,
                    vcId: expectedResponse.payload[0].vcId,
                    issuer: expectedResponse.payload[0].issuer,
                    status: expectedResponse.payload[0].status,
                },
            ]));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycAccountsData", getKycAccountsDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycAccountsDataQuery_1.GetKycAccountsDataQuery(getKycAccountsDataRequest.securityId, getKycAccountsDataRequest.kycStatus, getKycAccountsDataRequest.start, getKycAccountsDataRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getKycAccountsDataRequest = new request_1.GetKycAccountsDataRequest({
                ...KycFixture_1.GetKycAccountsDataRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if kycStatus is invalid", async () => {
            getKycAccountsDataRequest = new request_1.GetKycAccountsDataRequest({
                ...KycFixture_1.GetKycAccountsDataRequestFixture.create({
                    kycStatus: -1,
                }),
            });
            await expect(Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if start is invalid", async () => {
            getKycAccountsDataRequest = new request_1.GetKycAccountsDataRequest({
                ...KycFixture_1.GetKycAccountsDataRequestFixture.create({
                    start: -1,
                }),
            });
            await expect(Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if end is invalid", async () => {
            getKycAccountsDataRequest = new request_1.GetKycAccountsDataRequest({
                ...KycFixture_1.GetKycAccountsDataRequestFixture.create({
                    end: -1,
                }),
            });
            await expect(Kyc_1.default.getKycAccountsData(getKycAccountsDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getKycStatusFor", () => {
        getKycStatusForRequest = new request_1.GetKycStatusForRequest(KycFixture_1.GetKycStatusForRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get kyc status for successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.getKycStatusFor(getKycStatusForRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycStatusForRequest", getKycStatusForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycStatusForQuery_1.GetKycStatusForQuery(getKycStatusForRequest.securityId, getKycStatusForRequest.targetId));
            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(Kyc_1.default.getKycStatusFor(getKycStatusForRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetKycStatusForRequest", getKycStatusForRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetKycStatusForQuery_1.GetKycStatusForQuery(getKycStatusForRequest.securityId, getKycStatusForRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getKycStatusForRequest = new request_1.GetKycStatusForRequest({
                ...KycFixture_1.GetKycStatusForRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycStatusFor(getKycStatusForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getKycStatusForRequest = new request_1.GetKycStatusForRequest({
                ...KycFixture_1.GetKycStatusForRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Kyc_1.default.getKycStatusFor(getKycStatusForRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("activateInternalKyc", () => {
        activateInternalKycRequest = new request_1.ActivateInternalKycRequest(KycFixture_1.ActivateInternalKycRequestFixture.create());
        it("should update activate internal kyc successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.activateInternalKyc(activateInternalKycRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ActivateInternalKycRequest", activateInternalKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ActivateInternalKycCommand_1.ActivateInternalKycCommand(activateInternalKycRequest.securityId));
            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(Kyc_1.default.activateInternalKyc(activateInternalKycRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ActivateInternalKycRequest", activateInternalKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ActivateInternalKycCommand_1.ActivateInternalKycCommand(activateInternalKycRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            activateInternalKycRequest = new request_1.ActivateInternalKycRequest({
                ...KycFixture_1.ActivateInternalKycRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.activateInternalKyc(activateInternalKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("deactivateInternalKyc", () => {
        deactivateInternalKycRequest = new request_1.DeactivateInternalKycRequest(KycFixture_1.DeactivateInternalKycRequestFixture.create());
        it("should update deactivate internal kyc successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kyc_1.default.deactivateInternalKyc(deactivateInternalKycRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("DeactivateInternalKycRequest", deactivateInternalKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new DeactivateInternalKycCommand_1.DeactivateInternalKycCommand(deactivateInternalKycRequest.securityId));
            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(Kyc_1.default.deactivateInternalKyc(deactivateInternalKycRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("DeactivateInternalKycRequest", deactivateInternalKycRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new DeactivateInternalKycCommand_1.DeactivateInternalKycCommand(deactivateInternalKycRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            deactivateInternalKycRequest = new request_1.DeactivateInternalKycRequest({
                ...KycFixture_1.DeactivateInternalKycRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.activateInternalKyc(activateInternalKycRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isInternalKycActivated", () => {
        isInternalKycActivatedRequest = new request_1.IsInternalKycActivatedRequest(KycFixture_1.IsInternalKycActivatedQueryFixture.create());
        const expectedQueryResponse = {
            payload: true,
        };
        it("should check if internal kyc is activated", async () => {
            queryBusMock.execute.mockResolvedValue(expectedQueryResponse);
            const result = await Kyc_1.default.isInternalKycActivated(isInternalKycActivatedRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsInternalKycActivatedRequest", isInternalKycActivatedRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsInternalKycActivatedQuery_1.IsInternalKycActivatedQuery(isInternalKycActivatedRequest.securityId));
            expect(result).toEqual(expectedQueryResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kyc_1.default.isInternalKycActivated(isInternalKycActivatedRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsInternalKycActivatedRequest", isInternalKycActivatedRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsInternalKycActivatedQuery_1.IsInternalKycActivatedQuery(isInternalKycActivatedRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            isInternalKycActivatedRequest = new request_1.IsInternalKycActivatedRequest({
                ...KycFixture_1.IsInternalKycActivatedQueryFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Kyc_1.default.isInternalKycActivated(isInternalKycActivatedRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
