Skip to content

Feature/cb2:14766: update smc-proh to only process insert and modify events #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading