From 7f24e910d514d08b9e434ae88741346ec3a14394 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi <dreamorosi@gmail.com> Date: Tue, 27 Dec 2022 14:47:44 +0100 Subject: [PATCH] feat: completed SSMProvider implementation --- packages/parameters/src/SSMProvider.ts | 11 +++- .../tests/unit/getParametersByName.test.ts | 60 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 packages/parameters/tests/unit/getParametersByName.test.ts diff --git a/packages/parameters/src/SSMProvider.ts b/packages/parameters/src/SSMProvider.ts index bc38a7265f..b469896e0c 100644 --- a/packages/parameters/src/SSMProvider.ts +++ b/packages/parameters/src/SSMProvider.ts @@ -56,7 +56,7 @@ class SSMProvider extends BaseProvider { * @param {Record<string, unknown>[]} parameters - List of parameter names, and any optional overrides * */ - public async getParametersByName(parameters: Record<string, SSMGetParametersByNameOptionsInterface>, options: SSMGetParametersByNameOptionsInterface): Promise<Record<string, unknown>> { + public async getParametersByName(parameters: Record<string, SSMGetParametersByNameOptionsInterface>, options?: SSMGetParametersByNameOptionsInterface): Promise<Record<string, unknown>> { const configs = { ...{ decrypt: false, maxAge: DEFAULT_MAX_AGE_SECS, @@ -372,8 +372,17 @@ const getParameters = (path: string, options?: SSMGetMultipleOptionsInterface): return DEFAULT_PROVIDERS.ssm.getMultiple(path, options); }; +const getParametersByName = (parameters: Record<string, SSMGetParametersByNameOptionsInterface>, options?: SSMGetParametersByNameOptionsInterface): Promise<Record<string, unknown>> => { + if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) { + DEFAULT_PROVIDERS.ssm = new SSMProvider(); + } + + return (DEFAULT_PROVIDERS.ssm as SSMProvider).getParametersByName(parameters, options); +}; + export { SSMProvider, getParameter, getParameters, + getParametersByName, }; \ No newline at end of file diff --git a/packages/parameters/tests/unit/getParametersByName.test.ts b/packages/parameters/tests/unit/getParametersByName.test.ts new file mode 100644 index 0000000000..420d8fd6a1 --- /dev/null +++ b/packages/parameters/tests/unit/getParametersByName.test.ts @@ -0,0 +1,60 @@ +/** + * Test getParametersByName function + * + * @group unit/parameters/SSMProvider/getParametersByName/function + */ +import { DEFAULT_PROVIDERS } from '../../src/BaseProvider'; +import { SSMProvider, getParametersByName } from '../../src/SSMProvider'; + +describe('Function: getParametersByName', () => { + + beforeEach(() => { + jest.clearAllMocks(); + }); + + test('when called and a default provider doesn\'t exist, it instantiates one and returns the value', async () => { + + // Prepare + const parameters = { + '/foo/bar': { + maxAge: 1000, + }, + '/foo/baz': { + maxAge: 2000, + } + }; + const getParametersByNameSpy = jest.spyOn(SSMProvider.prototype, 'getParametersByName').mockImplementation(); + + // Act + await getParametersByName(parameters); + + // Assess + expect(getParametersByNameSpy).toHaveBeenCalledWith(parameters, undefined); + + }); + + test('when called and a default provider exists, it uses it and returns the value', async () => { + + // Prepare + const provider = new SSMProvider(); + DEFAULT_PROVIDERS.ssm = provider; + const parameters = { + '/foo/bar': { + maxAge: 1000, + }, + '/foo/baz': { + maxAge: 2000, + } + }; + const getParametersByNameSpy = jest.spyOn(provider, 'getParametersByName').mockImplementation(); + + // Act + await getParametersByName(parameters); + + // Assess + expect(getParametersByNameSpy).toHaveBeenCalledWith(parameters, undefined); + expect(DEFAULT_PROVIDERS.ssm).toBe(provider); + + }); + +}); \ No newline at end of file