Skip to content

Commit

Permalink
Feature/cb2:14766: update smc-proh to only process insert and modify …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
cb-cs authored Oct 29, 2024
1 parent eb95ee1 commit 8858382
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/utils/ExtractTestResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/ExtractTestResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
);
});
});
29 changes: 29 additions & 0 deletions tests/unit/Handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 8858382

Please sign in to comment.