From 88583826703da1498039d4ba650a66c89bdbd398 Mon Sep 17 00:00:00 2001 From: Craig Broady <79261988+cb-cs@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:58:22 +0000 Subject: [PATCH] Feature/cb2:14766: update smc-proh to only process insert and modify events (#25) * feat(cb2-14766): added try catch for unmarshall * feat(cb2-14766): added if to handle eventName * feat(cb2-14766): added unit tests for coverage * feat(cb2-14766): removed if and added test for try catch --- src/utils/ExtractTestResults.ts | 9 ++++++++- tests/unit/ExtractTestResults.test.ts | 12 +++++++++++ tests/unit/Handler.test.ts | 29 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/utils/ExtractTestResults.ts b/src/utils/ExtractTestResults.ts index 4e2e81f..a1b9402 100644 --- a/src/utils/ExtractTestResults.ts +++ b/src/utils/ExtractTestResults.ts @@ -27,7 +27,14 @@ import { ValidationUtil } from './ValidationUtil'; * @returns MCRequest[] - an array of MCRequest interface, contains formatted test data */ export const extractMCTestResults = (record: DynamoDBRecord): MCRequest[] => { - const testResultUnmarshall: TestResultSchema = unmarshall(record.dynamodb.NewImage as any) as TestResultSchema; + let testResultUnmarshall: TestResultSchema; + + try { + testResultUnmarshall = unmarshall(record.dynamodb.NewImage as any) as TestResultSchema; + } catch (error) { + throw new Error(`Error unmarshalling test result: ${error}`); + } + logger.info( `Processing testResultId: ${JSON.stringify( testResultUnmarshall.testResultId, diff --git a/tests/unit/ExtractTestResults.test.ts b/tests/unit/ExtractTestResults.test.ts index cba5256..b3a31f4 100644 --- a/tests/unit/ExtractTestResults.test.ts +++ b/tests/unit/ExtractTestResults.test.ts @@ -46,4 +46,16 @@ describe('extractTestResults', () => { expect(e.body.errors[0]).toEqual('"vin" is required'); } }); + + it('should throw an error if there is an issue unmarshalling the DynamoDB record', () => { + const invalidDynamoDBRecord = { + dynamodb: { + NewImage: 'invalid data', + }, + } as unknown as DynamoDBRecord; + + expect(() => extractMCTestResults(invalidDynamoDBRecord)).toThrow( + 'Error unmarshalling test result:' + ); + }); }); diff --git a/tests/unit/Handler.test.ts b/tests/unit/Handler.test.ts index 77ffd61..4395526 100644 --- a/tests/unit/Handler.test.ts +++ b/tests/unit/Handler.test.ts @@ -148,6 +148,35 @@ describe('Application entry', () => { }); }); + it('should handle an error that does not have message', async () => { + process.env.SEND_TO_SMC = 'TRUE'; + + const eventWithError: SQSEvent = { ...event }; + eventWithError.Records[0].body = 'invalid JSON to cause error'; + + jest.spyOn(JSON, 'parse').mockImplementationOnce(() => { + const error = new Error('CustomError with no message'); + delete error.message; + throw error; + }); + + const expectedResponse = { + batchItemFailures: [ + { itemIdentifier: eventWithError.Records[0].messageId }, + ], + }; + + const expectedLog = JSON.stringify(eventWithError.Records[0]); + + await handler(eventWithError, null, (error, result) => { + expect(error).toBeNull(); + expect(result).toEqual(expectedResponse); + expect(infoLogSpy).not.toHaveBeenCalled(); + expect(logger.error).toHaveBeenCalledWith(`Error processing record: ${expectedLog}`); + expect(logger.error).toHaveBeenCalledWith(expect.any(SyntaxError)); + }); + }); + it('should add only 1 record to batchItemFailures if one of two records fails', async () => { process.env.SEND_TO_SMC = 'TRUE';