|
| 1 | +import { SNSClient } from '@aws-sdk/client-sns'; |
| 2 | +import * as pino from 'pino'; |
| 3 | +import { createHandler } from '../mi-updates-transformer'; |
| 4 | +import { KinesisStreamEvent, Context, KinesisStreamRecordPayload } from 'aws-lambda'; |
| 5 | +import { mockDeep } from 'jest-mock-extended'; |
| 6 | +import { Deps } from '../deps'; |
| 7 | +import { EnvVars } from '../env'; |
| 8 | +import { MI } from '@internal/datastore'; |
| 9 | +import { mapMIToCloudEvent } from '../mappers/mi-mapper'; |
| 10 | + |
| 11 | +// Make crypto return consistent values, since we're calling it in both prod and test code and comparing the values |
| 12 | +const realCrypto = jest.requireActual('crypto'); |
| 13 | +const randomBytes: Record<string, any> = {'8': realCrypto.randomBytes(8), '16': realCrypto.randomBytes(16)} |
| 14 | +jest.mock('crypto', () => ({ |
| 15 | + randomUUID: () => '4616b2d9-b7a5-45aa-8523-fa7419626b69', |
| 16 | + randomBytes: (size: number) => randomBytes[String(size)] |
| 17 | +})); |
| 18 | + |
| 19 | +describe('mi-updates-transformer Lambda', () => { |
| 20 | + |
| 21 | + const mockedDeps: jest.Mocked<Deps> = { |
| 22 | + snsClient: { send: jest.fn()} as unknown as SNSClient, |
| 23 | + logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger, |
| 24 | + env: { |
| 25 | + EVENT_PUB_SNS_TOPIC_ARN: 'arn:aws:sns:region:account:topic', |
| 26 | + } as unknown as EnvVars |
| 27 | + } as Deps; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.useFakeTimers(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.useRealTimers(); |
| 35 | + }) |
| 36 | + |
| 37 | + it('processes Kinesis events and publishes them to SNS', async () => { |
| 38 | + |
| 39 | + const handler = createHandler(mockedDeps); |
| 40 | + const miEvents = generateMIEvents(1); |
| 41 | + const expectedEntries = [expect.objectContaining({Message: JSON.stringify(mapMIToCloudEvent(miEvents[0]))})]; |
| 42 | + |
| 43 | + await handler(generateKinesisEvent(miEvents), mockDeep<Context>(), jest.fn()); |
| 44 | + |
| 45 | + expect(mockedDeps.snsClient.send).toHaveBeenCalledWith(expect.objectContaining({ |
| 46 | + input: expect.objectContaining({ |
| 47 | + TopicArn: 'arn:aws:sns:region:account:topic', |
| 48 | + PublishBatchRequestEntries: expectedEntries |
| 49 | + }) |
| 50 | + })); |
| 51 | + }); |
| 52 | + |
| 53 | + it ('batches mutiple records into a single call to SNS', async () => { |
| 54 | + |
| 55 | + const handler = createHandler(mockedDeps); |
| 56 | + const miEvents = generateMIEvents(10); |
| 57 | + const expectedEntries = miEvents.map(miEvent => |
| 58 | + expect.objectContaining({Message: JSON.stringify(mapMIToCloudEvent(miEvent))})); |
| 59 | + |
| 60 | + await handler(generateKinesisEvent(miEvents), mockDeep<Context>(), jest.fn()); |
| 61 | + |
| 62 | + expect(mockedDeps.snsClient.send).toHaveBeenCalledWith(expect.objectContaining({ |
| 63 | + input: expect.objectContaining({ |
| 64 | + TopicArn: 'arn:aws:sns:region:account:topic', |
| 65 | + PublishBatchRequestEntries: expectedEntries |
| 66 | + }) |
| 67 | + })); |
| 68 | + }); |
| 69 | + |
| 70 | + |
| 71 | + it('splits more than 10 records into multiple SNS calls', async () => { |
| 72 | + |
| 73 | + const handler = createHandler(mockedDeps); |
| 74 | + const miEvents = generateMIEvents(21); |
| 75 | + const expectedEntries = [ |
| 76 | + miEvents.slice(0, 10).map(miEvent => |
| 77 | + expect.objectContaining({Message: JSON.stringify(mapMIToCloudEvent(miEvent))})), |
| 78 | + miEvents.slice(10, 20).map(miEvent => |
| 79 | + expect.objectContaining({Message: JSON.stringify(mapMIToCloudEvent(miEvent))})), |
| 80 | + miEvents.slice(20).map(miEvent => |
| 81 | + expect.objectContaining({Message: JSON.stringify(mapMIToCloudEvent(miEvent))})), |
| 82 | + ]; |
| 83 | + |
| 84 | + await handler(generateKinesisEvent(miEvents), mockDeep<Context>(), jest.fn()); |
| 85 | + |
| 86 | + expect(mockedDeps.snsClient.send).toHaveBeenNthCalledWith(1, |
| 87 | + expect.objectContaining({ |
| 88 | + input: expect.objectContaining({ |
| 89 | + TopicArn: 'arn:aws:sns:region:account:topic', |
| 90 | + PublishBatchRequestEntries: expectedEntries[0] |
| 91 | + })})); |
| 92 | + expect(mockedDeps.snsClient.send).toHaveBeenNthCalledWith(2, |
| 93 | + expect.objectContaining({ |
| 94 | + input: expect.objectContaining({ |
| 95 | + TopicArn: 'arn:aws:sns:region:account:topic', |
| 96 | + PublishBatchRequestEntries: expectedEntries[1] |
| 97 | + }) |
| 98 | + }) |
| 99 | + ); |
| 100 | + |
| 101 | + expect(mockedDeps.snsClient.send).toHaveBeenNthCalledWith(3, |
| 102 | + expect.objectContaining({ |
| 103 | + input: expect.objectContaining({ |
| 104 | + TopicArn: 'arn:aws:sns:region:account:topic', |
| 105 | + PublishBatchRequestEntries: expectedEntries[2] |
| 106 | + }) |
| 107 | + }) |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + function generateKinesisEvent(miEvents: Object[]): KinesisStreamEvent { |
| 112 | + const records = miEvents |
| 113 | + .map(mi => Buffer.from(JSON.stringify(mi), 'utf-8').toString('base64')) |
| 114 | + .map(data => ({ kinesis: { data }} as unknown as KinesisStreamRecordPayload)); |
| 115 | + return { Records: records } as unknown as KinesisStreamEvent; |
| 116 | + } |
| 117 | + function generateMIEvents(numMIEvents: number): MI[] { |
| 118 | + return Array.from(Array(numMIEvents).keys()) |
| 119 | + .map(i => ({ |
| 120 | + id: String(i + 1), |
| 121 | + lineItem: 'lineItem' + (i + 1), |
| 122 | + timestamp: new Date().toISOString(), |
| 123 | + quantity: 100 + i, |
| 124 | + supplierId: 'supplier' + (i + 1), |
| 125 | + createdAt: new Date().toISOString(), |
| 126 | + updatedAt: new Date().toISOString(), |
| 127 | + ttl: Math.floor(Date.now() / 1000) + 3600, |
| 128 | + specificationId: 'spec1', |
| 129 | + groupId: 'group1', |
| 130 | + stockRemaining: 500 - i, |
| 131 | + })); |
| 132 | + } |
| 133 | +}); |
0 commit comments