UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

1,152 lines (954 loc) 51.3 kB
import { getGatewayJson, handleTestWarnings, handleTestAssets, handleTestProjects, writeArchive, handleNonDeploymentForAssets, handleDeploymentForAssets, handleDeploymentForProjects, handleNonDeploymentForProjects, testAssetsForEndpoint } from './test-action-helper.js'; import { executeDeployment, prepareGatewayJson } from '../../deployers/project/projects-deployer.js'; import { showWarning, showInfo, showError } from '../../helpers/common/message-helper.js'; import { testAssets, testProjects, combineTestAsset, buildAndDeployAssets, addEndpointToZip, processEndpointFromResponse, createJSONBuffer, APIEndpoints, findProjectForApi, updateEndpointZip, formattedEndpoints} from '../../helpers/apim/test-helper.js'; import { createBuildZip } from '../../helpers/common/fs-helper.js'; import { buildAssets, getOutputPath } from './../helpers/build-action-helper.js'; import { TestOptionsModel } from '../../model/studio/command-options/test-options-model.js'; import { API_DETAILS_MISSING, CREATED_BUILD_ZIP, CREATED_TEST_ZIP, ENDPOINT_TEST_SUCCESS, ERROR_PROCESSING_ENDPOINT, IGNORE_NAMES_OPT, IGNORE_PROJECT_ARG, MULTIPLE_PROJECTS_NOT_ALLOWED, NO_VALID_ENDPOINT_FOUND, RETRY_TEST_COMMAND } from '../../constants/message-constants.js'; import { GatewaysJson } from '@apic/studio-shared'; import { GatewayResponseAPI } from '../../model/studio/deploy-response-model.js'; import { getGatewayEndpoints } from '../../configure/endpoints/config.js'; import validateEndpoint from '../../validators/endpoint-validator.js'; jest.mock('../../deployers/project/projects-deployer.js', () => ({ prepareGatewayJson: jest.fn(), executeDeployment:jest.fn() })); jest.mock('../../validators/endpoint-validator.js', () => ({ __esModule: true, default: jest.fn(), })); jest.mock('../../configure/endpoints/config.js', () => ({ getGatewayEndpoints: jest.fn(), })); jest.mock('../../helpers/common/message-helper.js', () => ({ showWarning: jest.fn(), showInfo: jest.fn(), showError:jest.fn() })); jest.mock('../../helpers/apim/test-helper.js', () => ({ testAssets: jest.fn(), testProjects: jest.fn(), combineTestAsset: jest.fn(), createJSONBuffer: jest.fn().mockReturnValue(Buffer.from('mock-endpoint-file')), buildAndDeployAssets: jest.fn(), processEndpointFromResponse: jest.fn(), addEndpointToZip: jest.fn(), findProjectForApi:jest.fn(), updateEndpointZip:jest.fn(), formattedEndpoints:jest.fn() })); jest.mock('../../helpers/common/fs-helper.js', () => ({ createBuildZip: jest.fn(), })); jest.mock('./../helpers/build-action-helper.js', () => ({ getOutputPath: jest.fn(), buildAssets:jest.fn() })); jest.mock('@apic/studio-build', () => ({ processProjectBuild: jest.fn(), })); jest.mock('env-paths', () => { return jest.fn((name: string) => ({ data: `/mock/path/${name}-data`, config: `/mock/path/${name}-config`, cache: `/mock/path/${name}-cache`, log: `/mock/path/${name}-log`, temp: `/mock/path/${name}-temp`, })); }); describe('Test action helper functions', () => { describe('handleDeploymentForProjects', () => { const localDirMock = 'localDir'; const gatewayJsonMock = {} as GatewaysJson; const optionsMock = { localDir: 'rootDir', all: true, target: 'target', username: 'username', password:'xyz', project: 'project', names: '', debug: false, deploy: true } as TestOptionsModel; const mockZipBuffer = Buffer.from('test'); const mockApiReference = { api1: 'ref1', api2: 'ref2' }; beforeEach(() => { jest.clearAllMocks(); }); it('should call buildAndDeployAssets, processEndpointFromResponse, and addEndpointToZip with correct parameters', async () => { const mockDeployResponse = { buildBuffer: Buffer.from('mockBuildBuffer'), deploymentResult: [{ someKey: 'someValue' }], }; const mockEndpointFile = Buffer.from('mockEndpointFile'); (buildAndDeployAssets as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleDeploymentForProjects(optionsMock,gatewayJsonMock, localDirMock, mockZipBuffer,mockApiReference); expect(buildAndDeployAssets).toHaveBeenCalledWith(localDirMock, mockApiReference, gatewayJsonMock); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse.deploymentResult); expect(addEndpointToZip).toHaveBeenCalledWith(mockZipBuffer, mockEndpointFile); // expect(result).toEqual(mockZipBuffer); expect(result).toEqual({ buildZipBuffer: expect.any(Buffer), testZipBuffer: mockZipBuffer }); }); it('should return undefined when gateway details are missing', async () => { const optionsWithoutGateway = { ...optionsMock, target: '', username: '', password: '' }; const result = await handleDeploymentForProjects(optionsWithoutGateway, gatewayJsonMock, localDirMock, mockZipBuffer,mockApiReference); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); }); describe('handleDeploymentForAssets', () => { const projectsMock = 'project1'; const localDirMock = 'localDir'; const gatewayJsonMock = {} as GatewaysJson; const optionsMock = { archive: 'archive', localDir: 'rootDir', all: true, target: 'target', username: 'username', password:'xyz', project: 'project', names: 'names', debug: false, deploy: true } as TestOptionsModel; const mockZipBuffer = Buffer.from('test'); const mockApiReference = 'asset1'; const mockZipBufferToBuild = Buffer.from('mockZipBufferToBuild'); const mockFinalZipBuffer = Buffer.from('mockFinalZipBuffer'); beforeEach(() => { jest.clearAllMocks(); }); it('should call buildAssets, executeDeployment, processEndpointFromResponse, and addEndpointToZip with correct parameters', async () => { const mockDeployResponse = { someKey: 'someValue' } as unknown as GatewayResponseAPI[]; const mockEndpointFile = Buffer.from('mockEndpointFile'); (buildAssets as jest.Mock).mockResolvedValue(mockZipBufferToBuild); (executeDeployment as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); // Call the function const result = await handleDeploymentForAssets( optionsMock, gatewayJsonMock, localDirMock, mockApiReference, projectsMock, mockZipBuffer ); // Assertions to verify if the correct functions are called with correct parameters expect(buildAssets).toHaveBeenCalledWith(mockApiReference, localDirMock, projectsMock); expect(executeDeployment).toHaveBeenCalledWith(gatewayJsonMock, mockZipBufferToBuild); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse); expect(addEndpointToZip).toHaveBeenCalledWith(mockZipBuffer, mockEndpointFile); // Validate the result expect(result).toEqual({ buildZipBuffer: mockZipBufferToBuild, // Adjust if necessary testZipBuffer: mockZipBuffer, }); }); it('should return undefined when gateway details are missing', async () => { const optionsWithoutGateway = { ...optionsMock, target: '', username: '', password: '' }; const result = await handleDeploymentForAssets(optionsWithoutGateway, gatewayJsonMock, localDirMock, mockApiReference, projectsMock, mockZipBuffer); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); }); describe('getGatewayJson', () => { const mockOptions = { target: 'testTarget', username: 'testUser', password: 'xyz', authToken: '', deploy: true, } as TestOptionsModel; test('should call prepareGatewayJson with correct parameters', async () => { const mockGatewayPassword = 'xyz'; const mockGatewayJson = {}; (prepareGatewayJson as jest.Mock).mockResolvedValue(mockGatewayJson); const result = await getGatewayJson(mockOptions, mockGatewayPassword); expect(prepareGatewayJson).toHaveBeenCalledWith( mockOptions.target, mockOptions.username, mockGatewayPassword, mockOptions.deploy, false ); expect(result).toEqual(mockGatewayJson); }); test('should handle errors from prepareGatewayJson', async () => { const mockGatewayPassword = 'xyz'; const mockError = new Error('Failed to prepare gateway JSON'); (prepareGatewayJson as jest.Mock).mockRejectedValue(mockError); await expect(getGatewayJson(mockOptions, mockGatewayPassword)) .rejects .toThrow('Failed to prepare gateway JSON'); }); test('should handle different deployment scenarios (deploy = false)', async () => { const mockOptionsWithoutDeploy = { target: 'testTarget', username: 'testUser', password: 'xyz', deploy: false, } as TestOptionsModel; const mockGatewayPassword = 'xyz'; const mockGatewayJson = {}; (prepareGatewayJson as jest.Mock).mockResolvedValue(mockGatewayJson); const result = await getGatewayJson(mockOptionsWithoutDeploy, mockGatewayPassword); expect(prepareGatewayJson).toHaveBeenCalledWith( mockOptionsWithoutDeploy.target, mockOptionsWithoutDeploy.username, mockGatewayPassword, mockOptionsWithoutDeploy.deploy, false ); expect(result).toEqual(mockGatewayJson); }); test('should handle empty target parameter', async () => { const mockOptionsEmptyTarget = { target: '', username: 'testUser', password: 'xyz', deploy: true, } as TestOptionsModel; const mockGatewayPassword = 'xyz'; const mockGatewayJson = {}; (prepareGatewayJson as jest.Mock).mockResolvedValue(mockGatewayJson); const result = await getGatewayJson(mockOptionsEmptyTarget, mockGatewayPassword); expect(prepareGatewayJson).toHaveBeenCalledWith( mockOptionsEmptyTarget.target, mockOptionsEmptyTarget.username, mockGatewayPassword, mockOptionsEmptyTarget.deploy, false ); expect(result).toEqual(mockGatewayJson); }); }); describe('handleTestWarnings', () => { beforeEach(() => { jest.clearAllMocks(); }); test('should show warnings based on projects and options', () => { const mockProjects = 'projectA,projectB'; const mockOptions = { all: true, names: 'someName', } as TestOptionsModel; handleTestWarnings(mockProjects, mockOptions); expect(showWarning).toHaveBeenCalledWith(IGNORE_PROJECT_ARG); expect(showWarning).toHaveBeenCalledWith(IGNORE_NAMES_OPT); }); test('should not show warnings if all options are valid', () => { const mockProjects = 'projectA,projectB'; const mockOptions = { all: false, names: 'someName', } as TestOptionsModel; handleTestWarnings(mockProjects, mockOptions); expect(showWarning).not.toHaveBeenCalled(); }); test('should not show warnings if projects and names are valid and all is false', () => { const mockProjects = 'projectA,projectB'; const mockOptions = { all: false, names: 'someName', } as TestOptionsModel; handleTestWarnings(mockProjects, mockOptions); expect(showWarning).not.toHaveBeenCalled(); }); }); describe('handleTestAssets', () => { const optionsMock = { deploy: false, all: false, names: '' }as TestOptionsModel; const projectsMock = 'project1'; const localDirMock = 'localDir'; const mockGatewayJson = {} as GatewaysJson; beforeEach(() => { jest.clearAllMocks(); }); it('should handle multiple projects error', async () => { const result = await handleTestAssets(optionsMock, 'project1,project2', 'localDir', { gateways: [], overwrite: '', skip: '' }); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); expect(showError).toHaveBeenCalledWith(MULTIPLE_PROJECTS_NOT_ALLOWED); }); it('should call handleNonDeploymentForAssets when deploy is false', async () => { const mockZipBuffer = Buffer.from('mock zip data'); const apiReferenceMock = ''; (testAssets as jest.Mock).mockResolvedValue({ zipBuffer: mockZipBuffer, apiReference: apiReferenceMock }); const result = await handleTestAssets(optionsMock, projectsMock, localDirMock, mockGatewayJson); expect(testAssets).toHaveBeenCalledWith(localDirMock, projectsMock, optionsMock.names); const nonDeploymentResult = await handleNonDeploymentForAssets(apiReferenceMock, optionsMock, mockGatewayJson, localDirMock, projectsMock, mockZipBuffer); expect(result).toEqual(nonDeploymentResult); }); it('should call handleDeploymentForAssets when deploy is true', async () => { optionsMock.deploy = true; const mockZipBuffer = Buffer.from('mock zip data'); const apiReferenceMock = { /* mock api reference */ }; (testAssets as jest.Mock).mockResolvedValue({ zipBuffer: mockZipBuffer, apiReference: apiReferenceMock }); const result = await handleTestAssets(optionsMock, projectsMock, localDirMock, mockGatewayJson); expect(testAssets).toHaveBeenCalledWith(localDirMock, projectsMock, optionsMock.names); const deploymentResult = await handleDeploymentForAssets(optionsMock, mockGatewayJson, localDirMock, apiReferenceMock, projectsMock, mockZipBuffer); expect(result).toEqual(deploymentResult); }); test('should handle failure from testAssets gracefully', async () => { const mockOptions = { deploy: true, all: false, names: 'someName', } as TestOptionsModel; const mockProjects = 'projectA'; const mockLocalDir = 'localDir'; const mockGatewayJson = {} as GatewaysJson; (testAssets as jest.Mock).mockRejectedValue(new Error('Failed to retrieve test assets')); await expect(handleTestAssets(mockOptions, mockProjects, mockLocalDir, mockGatewayJson)) .rejects .toThrow('Failed to retrieve test assets'); }); test('should call testAssets with correct parameters', async () => { const mockOptions = { deploy: true, all: false, names: 'someName', } as TestOptionsModel; const mockProjects = 'projectA'; const mockLocalDir = 'localDir'; const mockGatewayJson = {} as GatewaysJson; const mockTestAssetsResult = { zipBuffer: Buffer.from('mockZipBuffer'), apiReference: 'apiRef' }; (testAssets as jest.Mock).mockResolvedValue(mockTestAssetsResult); await handleTestAssets(mockOptions, mockProjects, mockLocalDir, mockGatewayJson); expect(testAssets).toHaveBeenCalledWith(mockLocalDir, mockProjects, mockOptions.names); }); }); describe('writeArchive', () => { test('should write both test and build archives and show info when buildZipBuffer is provided', async () => { const mockProjects = 'projectA'; const mockAll = false; const mockNames = 'someName'; const mockTestZipBuffer = Buffer.from('mockTestZipBuffer'); const mockBuildZipBuffer = Buffer.from('mockBuildZipBuffer'); const mockTestZipFileName = 'mockTestZipFileName'; const mockBuildZipFileName = 'mockBuildZipFileName'; (getOutputPath as jest.Mock) .mockResolvedValueOnce(mockBuildZipFileName) .mockResolvedValueOnce(mockTestZipFileName); (createBuildZip as jest.Mock).mockReturnValue(true); await writeArchive(mockProjects, mockAll, mockNames, mockTestZipBuffer, mockBuildZipBuffer); expect(getOutputPath).toHaveBeenCalledWith(mockProjects, mockAll, mockNames, 'build'); expect(getOutputPath).toHaveBeenCalledWith(mockProjects, mockAll, mockNames, 'test'); expect(createBuildZip).toHaveBeenCalledWith(mockTestZipBuffer, mockTestZipFileName); expect(createBuildZip).toHaveBeenCalledWith(mockBuildZipBuffer, mockBuildZipFileName); expect(showInfo).toHaveBeenCalledWith(ENDPOINT_TEST_SUCCESS); expect(showInfo).toHaveBeenCalledWith(CREATED_TEST_ZIP + mockTestZipFileName); expect(showInfo).toHaveBeenCalledWith(CREATED_BUILD_ZIP + mockBuildZipFileName); }); test('should write only test archive and show info when buildZipBuffer is undefined', async () => { const mockProjects = 'projectA'; const mockAll = false; const mockNames = 'someName'; const mockTestZipBuffer = Buffer.from('mockTestZipBuffer'); const mockTestZipFileName = 'mockTestZipFileName'; (getOutputPath as jest.Mock).mockResolvedValueOnce(mockTestZipFileName); (createBuildZip as jest.Mock).mockReturnValue(true); await writeArchive(mockProjects, mockAll, mockNames, mockTestZipBuffer, undefined); expect(getOutputPath).toHaveBeenCalledWith(mockProjects, mockAll, mockNames, 'test'); expect(createBuildZip).toHaveBeenCalledWith(mockTestZipBuffer, mockTestZipFileName); expect(showInfo).toHaveBeenCalledWith(ENDPOINT_TEST_SUCCESS); expect(showInfo).toHaveBeenCalledWith(CREATED_TEST_ZIP + mockTestZipFileName); }); test('should handle errors from getOutputPath', async () => { const mockProjects = 'projectA'; const mockAll = false; const mockNames = 'someName'; const mockTestZipBuffer = Buffer.from('mockTestZipBuffer'); const mockError = new Error('Failed to get output path'); (getOutputPath as jest.Mock).mockRejectedValue(mockError); await expect(writeArchive(mockProjects, mockAll, mockNames, mockTestZipBuffer, undefined)) .rejects .toThrow('Failed to get output path'); }); test('should handle empty parameters', async () => { const mockProjects = ''; const mockAll = false; const mockNames = ''; const mockTestZipBuffer = Buffer.from(''); const mockZipFileName = 'mockZipFileName'; (getOutputPath as jest.Mock).mockResolvedValue(mockZipFileName); (createBuildZip as jest.Mock).mockReturnValue(true); await writeArchive(mockProjects, mockAll, mockNames, mockTestZipBuffer, undefined); expect(getOutputPath).toHaveBeenCalledWith(mockProjects, mockAll, mockNames, 'test'); expect(createBuildZip).toHaveBeenCalledWith(mockTestZipBuffer, mockZipFileName); expect(showInfo).toHaveBeenCalledWith(ENDPOINT_TEST_SUCCESS); expect(showInfo).toHaveBeenCalledWith(CREATED_TEST_ZIP + mockZipFileName); }); test('should handle undefined parameters', async () => { const mockProjects = undefined; const mockAll = undefined; const mockNames = undefined; const mockTestZipBuffer = Buffer.from(''); const mockZipFileName = 'mockZipFileName'; (getOutputPath as jest.Mock).mockResolvedValue(mockZipFileName); (createBuildZip as jest.Mock).mockReturnValue(true); await writeArchive(mockProjects as any, mockAll as any, mockNames as any, mockTestZipBuffer, undefined); expect(getOutputPath).toHaveBeenCalledWith(mockProjects, mockAll, mockNames, 'test'); expect(createBuildZip).toHaveBeenCalledWith(mockTestZipBuffer, mockZipFileName); expect(showInfo).toHaveBeenCalledWith(ENDPOINT_TEST_SUCCESS); expect(showInfo).toHaveBeenCalledWith(CREATED_TEST_ZIP + mockZipFileName); }); }); describe('handleTestProjects', () => { const projectsMock = 'project1,project2'; const localDirMock = 'localDir'; const gatewayJsonMock = {} as GatewaysJson; const optionsMock = { archive: 'archive', localDir: 'rootDir', all: true, target: 'target', username: 'username', authToken: '', project: 'project', names: 'names', debug: false, deploy: true } as TestOptionsModel; const mockZipBuffer = Buffer.from('test'); const mockApiReference = { api1: 'ref1', api2: 'ref2' }; beforeEach(() => { jest.clearAllMocks(); }); it('should handle deployment for projects', async () => { const mockTestProject = { projectName: 'testProject1', asset: 'assetName', }; const mockZipBuffer = Buffer.from('mocked-zip-buffer'); const mockApiReference = { api1: 'ref1', api2: 'ref2' }; (testProjects as jest.Mock).mockResolvedValue(mockTestProject); (combineTestAsset as jest.Mock).mockResolvedValue({ zipBuffer: mockZipBuffer, apiReference: mockApiReference }); const result = await handleTestProjects({ ...optionsMock, deploy: true }, projectsMock, localDirMock, gatewayJsonMock); expect(testProjects).toHaveBeenCalledWith(optionsMock.all, localDirMock, projectsMock); expect(combineTestAsset).toHaveBeenCalledWith(localDirMock, mockTestProject); const deploymentResult = await handleDeploymentForProjects( { ...optionsMock, deploy: true }, gatewayJsonMock, localDirMock, mockZipBuffer, mockTestProject ); expect(result).toEqual(deploymentResult); }); it('should handle non-deployment for projects', async () => { const mockTestProject = { projectName: 'testProject1', asset: 'assetName', }; const mockZipBuffer = Buffer.from('mocked-zip-buffer'); const mockApiReference = { api1: 'ref1', api2: 'ref2' }; (testProjects as jest.Mock).mockResolvedValue(mockTestProject); (combineTestAsset as jest.Mock).mockResolvedValue({ zipBuffer: mockZipBuffer, apiReference: mockApiReference }); // Call the function under test const result = await handleTestProjects({ ...optionsMock, deploy: false }, projectsMock, localDirMock, gatewayJsonMock); // Assertions for non-deployment case expect(testProjects).toHaveBeenCalledWith(optionsMock.all, localDirMock, projectsMock); expect(combineTestAsset).toHaveBeenCalledWith(localDirMock, mockTestProject); const nonDeploymentResult = await handleNonDeploymentForProjects( Object.values(mockApiReference).join(','), { ...optionsMock, deploy: false }, gatewayJsonMock, localDirMock, mockZipBuffer, mockApiReference, ); expect(result).toEqual(nonDeploymentResult); }); it('should handle case where testProjects fails', async () => { (testProjects as jest.Mock).mockRejectedValue(new Error('Test projects error')); await expect(handleTestProjects(optionsMock, projectsMock, localDirMock, gatewayJsonMock)).rejects.toThrow('Test projects error'); }); it('should handle case where combineTestAsset fails', async () => { const mockTestProject = { /* mock test project data */ }; (testProjects as jest.Mock).mockResolvedValue(mockTestProject); (combineTestAsset as jest.Mock).mockRejectedValue(new Error('Combine asset error')); await expect(handleTestProjects(optionsMock, projectsMock, localDirMock, gatewayJsonMock)).rejects.toThrow('Combine asset error'); }); }); describe('handleNonDeploymentForProjects', () => { const optionsMock = { archive: 'archive', localDir: 'rootDir', all: true, target: 'target', username: 'username', project: 'project', authToken: '', names: 'names', debug: false, deploy: false } as TestOptionsModel; const gatewayJsonMock = {} as GatewaysJson; const localDirMock = 'localDir'; const finalZipBufferMock = Buffer.from('finalZipBuffer'); beforeEach(() => { jest.clearAllMocks(); }); afterEach(() => { jest.restoreAllMocks(); }); it('should handle found endpoints correctly and call addEndpointToZip', async () => { const apiReferencesStringMock = 'apiRefString'; const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock =''; const apiReferenceMock = 'apiRefMock'; (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsMock, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); const endpointFile = createJSONBuffer(foundEndpointsMock); const expectedZipBuffer = addEndpointToZip(finalZipBufferMock, endpointFile); expect(getGatewayEndpoints).toHaveBeenCalledWith(apiReferencesStringMock); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock, Buffer.from('mockEndpointBuffer')); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: expectedZipBuffer }); }); it('should handle found endpoints and not found endpoints and return undefined if no gateway details', async () => { const apiReferencesStringMock = 'apiRefString'; const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock = ['api2']; const apiReferenceMock = 'apiRefMock'; (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsMock, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); expect(getGatewayEndpoints).toHaveBeenCalledWith(apiReferencesStringMock); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock, Buffer.from('mockEndpointBuffer')); expect(showError).toHaveBeenCalledWith(API_DETAILS_MISSING); expect(showError).toHaveBeenCalledWith(RETRY_TEST_COMMAND); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); it('should handle found === not endpoints, and call deployAndAddEndpointForProjects', async () => { const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock = 'api1'; const apiReferencesStringMock = 'api1'; const optionsWithGateway = { ...optionsMock, password: 'xyz' }; const apiReferenceMock = 'apiRefMock'; (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); // const mockDeployResponse = { someKey: 'someValue' }; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBuffer = Buffer.from('mockZipBuffer'); const mockDeployResponse = { buildBuffer: Buffer.from('mockBuildBuffer'), deploymentResult: [{ someKey: 'someValue' }], }; (buildAndDeployAssets as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsWithGateway, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); expect(getGatewayEndpoints).toHaveBeenCalledWith(apiReferencesStringMock); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock, Buffer.from('mockEndpointBuffer')); expect(buildAndDeployAssets).toHaveBeenCalledWith(localDirMock, apiReferenceMock, gatewayJsonMock); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse.deploymentResult); expect(addEndpointToZip).toHaveBeenCalledWith(mockZipBuffer, mockEndpointFile); expect(result).toEqual({ buildZipBuffer: expect.any(Buffer), testZipBuffer: mockZipBuffer }); }); it('should handle found !== not found endpoints, call handleNotFoundApisForProjects', async () => { const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock = 'api2'; const apiReferencesStringMock = 'api1'; const notFoundApisListMock = ['api2']; const projectApisMapMock = { api2: 'project1' }; const mockDeployResponse = { buildBuffer: Buffer.from('mockBuildBuffer'), deploymentResult: [{ api2: 'response1' }], }; const mockEndpointFile = Buffer.from('mockEndpointFile'); const optionsWithGateway = { ...optionsMock, password: 'xyz' }; const mockUpdatedZipBuffer = Buffer.from('mockUpdatedZipBuffer'); const apiReferenceMock = 'apiRefMock'; (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointFile')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); (findProjectForApi as jest.Mock).mockReturnValue(projectApisMapMock); (buildAndDeployAssets as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(Buffer.from('newEndpointFile')); (updateEndpointZip as jest.Mock).mockReturnValue(Buffer.from('mockUpdatedZipBuffer')); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsWithGateway, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); expect(getGatewayEndpoints).toHaveBeenCalledWith(apiReferencesStringMock); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock, mockEndpointFile); expect(findProjectForApi).toHaveBeenCalledWith(apiReferenceMock, notFoundApisListMock); expect(buildAndDeployAssets).toHaveBeenCalledWith(localDirMock, projectApisMapMock, gatewayJsonMock); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse.deploymentResult); expect(updateEndpointZip).toHaveBeenCalled(); expect(result).toEqual({ buildZipBuffer: expect.any(Buffer), testZipBuffer: mockUpdatedZipBuffer }); }); it('should handle if no results found, call deployAndAddEndpointForProjects', async () => { const apiReferencesStringMock = 'api1'; const optionsWithGateway = { ...optionsMock, password: 'xyz' }; // const mockDeployResponse = { someKey: 'someValue' }; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBuffer = Buffer.from('mockZipBuffer'); const gatewayJsonMock = {} as GatewaysJson; const mockDeployResponse = { buildBuffer: Buffer.from('mockBuildBuffer'), deploymentResult: [{ someKey: 'someValue' }], }; const projectApisMapMock = { api2: 'project1' }; const apiReferenceMock = 'apiRefMock'; (buildAndDeployAssets as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsWithGateway, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); console.log('Passing to buildAndDeployAssets:', localDirMock, projectApisMapMock, gatewayJsonMock); expect(buildAndDeployAssets).toHaveBeenCalled(); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse.deploymentResult); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock,mockEndpointFile); expect(result).toEqual({ buildZipBuffer: expect.any(Buffer), testZipBuffer: expect.any(Buffer) }); }); it('should handle if no results found and no gateway details then return undefined', async () => { const apiReferencesStringMock = 'api1'; const mockDeployResponse = { someKey: 'someValue' }; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBuffer = Buffer.from('mockZipBuffer'); const apiReferenceMock = 'apiRefMock'; (buildAndDeployAssets as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForProjects( apiReferencesStringMock, optionsMock, gatewayJsonMock, localDirMock, finalZipBufferMock, apiReferenceMock ); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); }); describe('handleNonDeploymentForAssets', () => { const localDirMock = 'localDir'; const gatewayJsonMock = {} as GatewaysJson; const optionsMock = { archive: 'archive', localDir: 'rootDir', all: true, target: 'target', username: 'username', authToken: '', project: 'project', names: 'names', password: 'xyz', debug: false, deploy: false } as TestOptionsModel; const mockZipBuffer = Buffer.from('test'); beforeEach(() => { jest.clearAllMocks(); }); it('should return undefined when no gateway details and result is null ', async () => { const mockApiReference = 'apiRef'; const result = await handleNonDeploymentForAssets( mockApiReference, { ...optionsMock, password: '' }, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); it('should handle found endpoints correctly and call addEndpointToZip', async () => { const mockApiReference = 'apiRef'; const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock =''; const finalZipBufferMock = Buffer.from('finalZipBuffer'); (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const result = await handleNonDeploymentForAssets( mockApiReference, optionsMock, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); const endpointFile = createJSONBuffer(foundEndpointsMock); const expectedZipBuffer = addEndpointToZip(finalZipBufferMock, endpointFile); expect(getGatewayEndpoints).toHaveBeenCalledWith(mockApiReference); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(finalZipBufferMock, Buffer.from('mockEndpointBuffer')); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: expectedZipBuffer }); }); it('should handle undefined if found endpoints and nofoundApis but no gateway info ', async () => { const mockApiReference = 'apiRef'; const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock =['api1']; const finalZipBufferMock = Buffer.from('finalZipBuffer'); (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const result = await handleNonDeploymentForAssets( mockApiReference, { ...optionsMock, password: '' }, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); expect(getGatewayEndpoints).toHaveBeenCalledWith(mockApiReference); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalled(); expect(showError).toHaveBeenCalledWith(API_DETAILS_MISSING); expect(showError).toHaveBeenCalledWith(RETRY_TEST_COMMAND); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); it('should handle found === not endpoints, and call deployAndAddEndpointForAssets', async () => { const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock = 'api1'; const mockApiReference = 'api1'; const finalZipBufferMock = Buffer.from('finalZipBuffer'); (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const mockDeployResponse = { someKey: 'someValue' } as unknown as GatewayResponseAPI[]; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBufferToBuild = Buffer.from('mockZipBufferToBuild'); (buildAssets as jest.Mock).mockResolvedValue(mockZipBufferToBuild); (executeDeployment as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForAssets( mockApiReference, optionsMock, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); const endpointFile = createJSONBuffer(foundEndpointsMock); const expectedZipBuffer = addEndpointToZip(finalZipBufferMock, endpointFile); expect(getGatewayEndpoints).toHaveBeenCalledWith(mockApiReference); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(expectedZipBuffer, Buffer.from('mockEndpointBuffer')); expect(buildAssets).toHaveBeenCalledWith(notFoundApisMock, localDirMock, 'projectsMock'); expect(executeDeployment).toHaveBeenCalledWith(gatewayJsonMock, mockZipBufferToBuild); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse); expect(addEndpointToZip).toHaveBeenCalled(); expect(result).toEqual({ buildZipBuffer: mockZipBufferToBuild, testZipBuffer: mockZipBuffer }); }); it('should handle found !== not endpoints, and call handleNotFoundApisForAssets', async () => { const foundEndpointsMock = { endpoint1: 'data1', endpoint2: 'data2' } as unknown as APIEndpoints; const notFoundApisMock = 'api1'; const mockApiReference = 'api2'; const finalZipBufferMock = Buffer.from('finalZipBuffer'); (getGatewayEndpoints as jest.Mock).mockResolvedValue({ foundEndpoints: foundEndpointsMock, notFoundApis: notFoundApisMock }); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.from('mockEndpointBuffer')); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('updatedZipBuffer')); const mockDeployResponse = { someKey: 'someValue' } as unknown as GatewayResponseAPI[]; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBufferToBuild = Buffer.from('mockZipBufferToBuild'); (buildAssets as jest.Mock).mockResolvedValue(mockZipBufferToBuild); (executeDeployment as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (updateEndpointZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForAssets( mockApiReference, optionsMock, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); const endpointFile = createJSONBuffer(foundEndpointsMock); const expectedZipBuffer = addEndpointToZip(finalZipBufferMock, endpointFile); expect(getGatewayEndpoints).toHaveBeenCalledWith(mockApiReference); expect(createJSONBuffer).toHaveBeenCalledWith(foundEndpointsMock); expect(addEndpointToZip).toHaveBeenCalledWith(expect.any(Buffer), expect.any(Buffer)); expect(buildAssets).toHaveBeenCalledWith(notFoundApisMock, localDirMock, 'projectsMock'); expect(executeDeployment).toHaveBeenCalledWith(gatewayJsonMock, mockZipBufferToBuild); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse); expect(updateEndpointZip).toHaveBeenCalled(); expect(result).toEqual({ buildZipBuffer: mockZipBufferToBuild, testZipBuffer: mockZipBuffer }); }); it('should handle if no results found, call deployAndAddEndpointForAssets', async () => { const mockApiReference = 'api1'; const mockDeployResponse = { someKey: 'someValue' } as unknown as GatewayResponseAPI[]; const mockEndpointFile = Buffer.from('mockEndpointFile'); const mockZipBufferToBuild = Buffer.from('mockZipBufferToBuild'); (buildAssets as jest.Mock).mockResolvedValue(mockZipBufferToBuild); (executeDeployment as jest.Mock).mockResolvedValue(mockDeployResponse); (processEndpointFromResponse as jest.Mock).mockReturnValue(mockEndpointFile); (addEndpointToZip as jest.Mock).mockReturnValue(mockZipBuffer); const result = await handleNonDeploymentForAssets( mockApiReference, optionsMock, gatewayJsonMock, localDirMock, 'projectsMock', mockZipBuffer, ); expect(buildAssets).toHaveBeenCalledWith(mockApiReference, localDirMock, 'projectsMock'); expect(executeDeployment).toHaveBeenCalledWith(gatewayJsonMock, mockZipBufferToBuild); expect(processEndpointFromResponse).toHaveBeenCalledWith(mockDeployResponse); expect(addEndpointToZip).toHaveBeenCalledWith(mockZipBuffer,mockEndpointFile); expect(result).toEqual({ buildZipBuffer: mockZipBufferToBuild, testZipBuffer: mockZipBuffer }); }); }); describe('handleDeploymentForProjects', () => { const localDirMock = 'localDir'; const gatewayJsonMock = {} as GatewaysJson; const optionsMock = { localDir: 'rootDir', all: true, target: 'target', username: 'username', password:'xyz', authToken: '', project: 'project', names: '', debug: false, deploy: true } as TestOptionsModel; const mockZipBuffer = Buffer.from('test'); const mockApiReference = { api1: 'ref1', api2: 'ref2' }; beforeEach(() => { jest.clearAllMocks(); }); it('should return undefined when gateway details are missing', async () => { const optionsWithoutGateway = { ...optionsMock, target: '', username: '', password: '' }; const result = await handleDeploymentForProjects(optionsWithoutGateway, gatewayJsonMock, localDirMock, mockZipBuffer,mockApiReference); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); }); describe('handleTestAssetsForEndpoints', () => { const projectsMock = 'project1'; const localDirMock = 'localDir'; const optionsMock = { archive: '', localDir: 'rootDir', all: false, target: '', username: '', password:'', authToken: '', project: 'project', names: 'names', debug: false, deploy: true, endpoints: 'http://apigateway-7476bd68bc-q8rs5:5555/gateway/PradeeRid/1.0' } as TestOptionsModel; beforeEach(() => { jest.clearAllMocks(); }); it('should return undefined if handleMultipleProjectsError returns true', async () => { const result = await testAssetsForEndpoint(optionsMock, 'project1,', localDirMock); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); expect(showError).toHaveBeenCalledWith(MULTIPLE_PROJECTS_NOT_ALLOWED); }); it('should return undefined if validateEndpoint returns false', async () => { const mockTestAssetsResult = { zipBuffer: Buffer.from('mockZipBuffer'), apiReference: 'apiRef' }; const mockedValidateResponse = false; const optionsWithGateway = { ...optionsMock, endpoint: 'eatd' }; (testAssets as jest.Mock).mockResolvedValue(mockTestAssetsResult); (validateEndpoint as jest.Mock).mockResolvedValue(mockedValidateResponse); const result = await testAssetsForEndpoint(optionsWithGateway, projectsMock, localDirMock); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); expect(showError).toHaveBeenCalledWith(` ${ERROR_PROCESSING_ENDPOINT} ${NO_VALID_ENDPOINT_FOUND}`); }); it('should return undefined if no mapped endpoints are found', async () => { const mockApiReference = 'dev:PradeeRid:1.0'; const mockTestAssetsResult = { zipBuffer: Buffer.from('mockZipBuffer'), apiReference: mockApiReference }; (testAssets as jest.Mock).mockResolvedValue(mockTestAssetsResult); (validateEndpoint as jest.Mock).mockResolvedValue([]); const mockEndpointBuffer = Buffer.from('endpoint'); (createJSONBuffer as jest.Mock).mockReturnValue(mockEndpointBuffer); (addEndpointToZip as jest.Mock).mockReturnValue(Buffer.from('finalBuffer')); await testAssetsForEndpoint(optionsMock, projectsMock, localDirMock); expect(showError).toHaveBeenCalled(); }); it('should return undefined if createJSONBuffer returns an empty buffer', async () => { const mockTestAssetsResult = { zipBuffer: Buffer.from('mockZipBuffer'), apiReference: 'dev:PradeeRid:1.0' }; const optionsWithGateway = { ...optionsMock, endpoint: 'http://apigateway-7476bd68bc-q8rs5:5555/gateway/PradeeRid/1.0' }; (testAssets as jest.Mock).mockResolvedValue(mockTestAssetsResult); (validateEndpoint as jest.Mock).mockResolvedValue(['validEndpoint']); (createJSONBuffer as jest.Mock).mockReturnValue(Buffer.alloc(0)); const result = await testAssetsForEndpoint(optionsWithGateway, projectsMock, localDirMock); expect(result).toEqual({ buildZipBuffer: undefined, testZipBuffer: undefined }); }); it('should call addEndpointToZip and return buffer', async () => { const mockTestAssetsResult = { zipBuffer: Buffer.from('mockZipBuffer'), apiReference: 'dev:PradeeRid:1.0' }; (testAssets as jest.Mock).mockResolvedValue(mockTestAssetsResult); (vali