"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 SsiManagement_1 = __importDefault(require("./SsiManagement"));
const SsiFixture_1 = require("../../../../__tests__/fixtures/ssi/SsiFixture");
const AddIssuerCommand_1 = require("../../../app/usecase/command/security/ssi/addIssuer/AddIssuerCommand");
const SetRevocationRegistryAddressCommand_1 = require("../../../app/usecase/command/security/ssi/setRevocationRegistryAddress/SetRevocationRegistryAddressCommand");
const RemoveIssuerCommand_1 = require("../../../app/usecase/command/security/ssi/removeIssuer/RemoveIssuerCommand");
const GetRevocationRegistryAddressQuery_1 = require("../../../app/usecase/query/security/ssi/getRevocationRegistryAddress/GetRevocationRegistryAddressQuery");
const GetIssuerListCountQuery_1 = require("../../../app/usecase/query/security/ssi/getIssuerListCount/GetIssuerListCountQuery");
const GetIssuerListMembersQuery_1 = require("../../../app/usecase/query/security/ssi/getIssuerListMembers/GetIssuerListMembersQuery");
const IsIssuerQuery_1 = require("../../../app/usecase/query/security/ssi/isIssuer/IsIssuerQuery");
describe("Ssi Management", () => {
    let commandBusMock;
    let queryBusMock;
    let setRevocationRegistryAddressRequest;
    let addIssuerRequest;
    let removeIssuerRequest;
    let getRevocationRegistryAddressRequest;
    let getIssuerListCountRequest;
    let getIssuerListMembersRequest;
    let isIssuerRequest;
    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");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        SsiManagement_1.default.commandBus = commandBusMock;
        SsiManagement_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("addIssuer", () => {
        addIssuerRequest = new request_1.AddIssuerRequest(SsiFixture_1.AddIssuerRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should  add issuer successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.addIssuer(addIssuerRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("AddIssuerRequest", addIssuerRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddIssuerCommand_1.AddIssuerCommand(addIssuerRequest.securityId, addIssuerRequest.issuerId));
            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(SsiManagement_1.default.addIssuer(addIssuerRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("AddIssuerRequest", addIssuerRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddIssuerCommand_1.AddIssuerCommand(addIssuerRequest.securityId, addIssuerRequest.issuerId));
        });
        it("should throw error if securityId is invalid", async () => {
            addIssuerRequest = new request_1.AddIssuerRequest({
                ...SsiFixture_1.AddIssuerRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.addIssuer(addIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if issuerId is invalid", async () => {
            addIssuerRequest = new request_1.AddIssuerRequest({
                ...SsiFixture_1.AddIssuerRequestFixture.create({
                    issuerId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.addIssuer(addIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("setRevocationRegistryAddress", () => {
        setRevocationRegistryAddressRequest = new request_1.SetRevocationRegistryAddressRequest(SsiFixture_1.SetRevocationRegistryAddressRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should set revocation registry successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.setRevocationRegistryAddress(setRevocationRegistryAddressRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetRevocationRegistryAddressRequest", setRevocationRegistryAddressRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetRevocationRegistryAddressCommand_1.SetRevocationRegistryAddressCommand(setRevocationRegistryAddressRequest.securityId, setRevocationRegistryAddressRequest.revocationRegistryId));
            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(SsiManagement_1.default.setRevocationRegistryAddress(setRevocationRegistryAddressRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetRevocationRegistryAddressRequest", setRevocationRegistryAddressRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetRevocationRegistryAddressCommand_1.SetRevocationRegistryAddressCommand(setRevocationRegistryAddressRequest.securityId, setRevocationRegistryAddressRequest.revocationRegistryId));
        });
        it("should throw error if securityId is invalid", async () => {
            setRevocationRegistryAddressRequest = new request_1.SetRevocationRegistryAddressRequest({
                ...SsiFixture_1.SetRevocationRegistryAddressRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.setRevocationRegistryAddress(setRevocationRegistryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if revocationRegistryId is invalid", async () => {
            setRevocationRegistryAddressRequest = new request_1.SetRevocationRegistryAddressRequest({
                ...SsiFixture_1.SetRevocationRegistryAddressRequestFixture.create({
                    revocationRegistryId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.setRevocationRegistryAddress(setRevocationRegistryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("removeIssuer", () => {
        removeIssuerRequest = new request_1.RemoveIssuerRequest(SsiFixture_1.RemoveIssuerRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should remove issuer successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.removeIssuer(removeIssuerRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveIssuerRequest", removeIssuerRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveIssuerCommand_1.RemoveIssuerCommand(removeIssuerRequest.securityId, removeIssuerRequest.issuerId));
            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(SsiManagement_1.default.removeIssuer(removeIssuerRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveIssuerRequest", removeIssuerRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveIssuerCommand_1.RemoveIssuerCommand(removeIssuerRequest.securityId, removeIssuerRequest.issuerId));
        });
        it("should throw error if securityId is invalid", async () => {
            removeIssuerRequest = new request_1.RemoveIssuerRequest({
                ...SsiFixture_1.RemoveIssuerRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.removeIssuer(removeIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if issuerId is invalid", async () => {
            removeIssuerRequest = new request_1.RemoveIssuerRequest({
                ...SsiFixture_1.RemoveIssuerRequestFixture.create({
                    issuerId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.removeIssuer(removeIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getRevocationRegistryAddress", () => {
        getRevocationRegistryAddressRequest = new request_1.GetRevocationRegistryAddressRequest(SsiFixture_1.GetRevocationRegistryAddressRequestFixture.create());
        const expectedResponse = {
            payload: transactionId,
        };
        it("should get revocation registry successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.getRevocationRegistryAddress(getRevocationRegistryAddressRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRevocationRegistryAddressRequest", getRevocationRegistryAddressRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRevocationRegistryAddressQuery_1.GetRevocationRegistryAddressQuery(getRevocationRegistryAddressRequest.securityId));
            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(SsiManagement_1.default.getRevocationRegistryAddress(getRevocationRegistryAddressRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRevocationRegistryAddressRequest", getRevocationRegistryAddressRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRevocationRegistryAddressQuery_1.GetRevocationRegistryAddressQuery(getRevocationRegistryAddressRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getRevocationRegistryAddressRequest = new request_1.GetRevocationRegistryAddressRequest({
                ...SsiFixture_1.GetRevocationRegistryAddressRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.getRevocationRegistryAddress(getRevocationRegistryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getIssuerListCount", () => {
        getIssuerListCountRequest = new request_1.GetIssuerListCountRequest(SsiFixture_1.GetIssuerListCountRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get revocation registry successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.getIssuerListCount(getIssuerListCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetIssuerListCountRequest", getIssuerListCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetIssuerListCountQuery_1.GetIssuerListCountQuery(getIssuerListCountRequest.securityId));
            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(SsiManagement_1.default.getIssuerListCount(getIssuerListCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetIssuerListCountRequest", getIssuerListCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetIssuerListCountQuery_1.GetIssuerListCountQuery(getIssuerListCountRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getIssuerListCountRequest = new request_1.GetIssuerListCountRequest({
                ...SsiFixture_1.GetIssuerListCountRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.getIssuerListCount(getIssuerListCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getIssuerListMembers", () => {
        getIssuerListMembersRequest = new request_1.GetIssuerListMembersRequest(SsiFixture_1.GetIssuerListMembersRequestFixture.create());
        const expectedResponse = {
            payload: [transactionId],
        };
        it("should get issuer list members successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.getIssuerListMembers(getIssuerListMembersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetIssuerListMembersRequest", getIssuerListMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetIssuerListMembersQuery_1.GetIssuerListMembersQuery(getIssuerListMembersRequest.securityId, getIssuerListMembersRequest.start, getIssuerListMembersRequest.end));
            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(SsiManagement_1.default.getIssuerListMembers(getIssuerListMembersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetIssuerListMembersRequest", getIssuerListMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetIssuerListMembersQuery_1.GetIssuerListMembersQuery(getIssuerListMembersRequest.securityId, getIssuerListMembersRequest.start, getIssuerListMembersRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getIssuerListMembersRequest = new request_1.GetIssuerListMembersRequest({
                ...SsiFixture_1.GetIssuerListMembersRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.getIssuerListMembers(getIssuerListMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if start is invalid", async () => {
            getIssuerListMembersRequest = new request_1.GetIssuerListMembersRequest({
                ...SsiFixture_1.GetIssuerListMembersRequestFixture.create({
                    start: -1,
                }),
            });
            await expect(SsiManagement_1.default.getIssuerListMembers(getIssuerListMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if end is invalid", async () => {
            getIssuerListMembersRequest = new request_1.GetIssuerListMembersRequest({
                ...SsiFixture_1.GetIssuerListMembersRequestFixture.create({
                    end: -1,
                }),
            });
            await expect(SsiManagement_1.default.getIssuerListMembers(getIssuerListMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isIssuer", () => {
        isIssuerRequest = new request_1.IsIssuerRequest(SsiFixture_1.IsIssuerRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should check is issuer successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await SsiManagement_1.default.isIssuer(isIssuerRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsIssuerRequest", isIssuerRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsIssuerQuery_1.IsIssuerQuery(isIssuerRequest.securityId, isIssuerRequest.issuerId));
            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(SsiManagement_1.default.isIssuer(isIssuerRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsIssuerRequest", isIssuerRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsIssuerQuery_1.IsIssuerQuery(isIssuerRequest.securityId, isIssuerRequest.issuerId));
        });
        it("should throw error if securityId is invalid", async () => {
            isIssuerRequest = new request_1.IsIssuerRequest({
                ...SsiFixture_1.IsIssuerRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.isIssuer(isIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if issuerId is invalid", async () => {
            isIssuerRequest = new request_1.IsIssuerRequest({
                ...SsiFixture_1.IsIssuerRequestFixture.create({
                    issuerId: "invalid",
                }),
            });
            await expect(SsiManagement_1.default.isIssuer(isIssuerRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
