"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 Security_1 = __importDefault(require("../../../in/security/Security"));
const ProtectedPartitionsFixture_1 = require("../../../../../__tests__/fixtures/protectedPartitions/ProtectedPartitionsFixture");
const PartitionsProtectedQuery_1 = require("../../../../app/usecase/query/security/protectedPartitions/arePartitionsProtected/PartitionsProtectedQuery");
const ProtectPartitionsCommand_1 = require("../../../../app/usecase/command/security/operations/protectPartitions/ProtectPartitionsCommand");
const UnprotectPartitionsCommand_1 = require("../../../../app/usecase/command/security/operations/unprotectPartitions/UnprotectPartitionsCommand");
const GetNounceQuery_1 = require("../../../../app/usecase/query/security/protectedPartitions/getNounce/GetNounceQuery");
describe("Protected Partitions", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let getNounceRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        mirrorNodeMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Security_1.default.commandBus = commandBusMock;
        Security_1.default.queryBus = queryBusMock;
        Security_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("protectPartitions", () => {
        const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest(ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should protect partitions successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.protectPartitions(partitionsProtectedRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ProtectPartitionsCommand_1.ProtectPartitionsCommand(partitionsProtectedRequest.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(Security_1.default.protectPartitions(partitionsProtectedRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ProtectPartitionsCommand_1.ProtectPartitionsCommand(partitionsProtectedRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest({
                ...ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.protectPartitions(partitionsProtectedRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("unprotectPartitions", () => {
        const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest(ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should protect partitions successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.unprotectPartitions(partitionsProtectedRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UnprotectPartitionsCommand_1.UnprotectPartitionsCommand(partitionsProtectedRequest.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(Security_1.default.unprotectPartitions(partitionsProtectedRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UnprotectPartitionsCommand_1.UnprotectPartitionsCommand(partitionsProtectedRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest({
                ...ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.unprotectPartitions(partitionsProtectedRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("arePartitionsProtected", () => {
        const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest(ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should check are partitions protected successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.arePartitionsProtected(partitionsProtectedRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new PartitionsProtectedQuery_1.PartitionsProtectedQuery(partitionsProtectedRequest.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(Security_1.default.arePartitionsProtected(partitionsProtectedRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PartitionsProtectedRequest", partitionsProtectedRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new PartitionsProtectedQuery_1.PartitionsProtectedQuery(partitionsProtectedRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const partitionsProtectedRequest = new request_1.PartitionsProtectedRequest({
                ...ProtectedPartitionsFixture_1.PartitionsProtectedRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.arePartitionsProtected(partitionsProtectedRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getNounce", () => {
        getNounceRequest = new request_1.GetNounceRequest(ProtectedPartitionsFixture_1.GetNounceRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get nonce successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getNounce(getNounceRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNounceRequest", getNounceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNounceQuery_1.GetNounceQuery(getNounceRequest.securityId, getNounceRequest.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(Security_1.default.getNounce(getNounceRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetNounceRequest", getNounceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetNounceQuery_1.GetNounceQuery(getNounceRequest.securityId, getNounceRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getNounceRequest = new request_1.GetNounceRequest({
                ...ProtectedPartitionsFixture_1.GetNounceRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getNounce(getNounceRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getNounceRequest = new request_1.GetNounceRequest({
                ...ProtectedPartitionsFixture_1.GetNounceRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getNounce(getNounceRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
