import { Processor } from '../src/processor'; describe('myTSTest', () => { const processor = new Processor('http://localhost:3000'); describe('decodePayload', () => { const properDataBase64 = 'eyJpZCI6ImE0Mzg4MTMyLTE0OTItMTFlYy1hMGIyLWM3OGZmYmQ2OTM0NyIsInBhcnRpdGlvbktleSI6IjJmY2U1M2YxLTFkYWYtNDI0Ny05MjQ1LTBhZDc4ZDBiMGQyZSIsInRpbWVzdGFtcCI6MTYzMTUzODA1OTQ1OSwidHlwZSI6ImJvb2tpbmdfcmVxdWVzdGVkIiwiYm9va2luZ19yZXF1ZXN0ZWQiOnsidGltZXN0YW1wIjoxNjMxNTM4MDU5NDU5LCJvcmRlcklkIjoxMDAxNywicHJvZHVjdF9wcm92aWRlciI6IlN0ZW5hIExpbmUifX0='; const properDecodedData = { id: 'a4388132-1492-11ec-a0b2-c78ffbd69347', partitionKey: '2fce53f1-1daf-4247-9245-0ad78d0b0d2e', timestamp: 1631538059459, type: 'booking_requested', booking_requested: { timestamp: 1631538059459, orderId: 10017, product_provider: 'Stena Line', }, }; it('should pass with proper data', () => { expect(processor.decodePayload(properDataBase64)).toEqual( properDecodedData ); }); it('should return null using an empty string', () => { expect(processor.decodePayload('')).toBeNull(); }); it('should return null when using non base64', () => { expect(processor.decodePayload('This is not in base64')).toBeNull(); }); }); describe('processEvent', () => { const kinesisData = { kinesis: { data: 'eyJpZCI6ImE0MzgwYzAyLTE0OTItMTFlYy1hMGIyLWM3OGZmYmQ2OTM0NyIsInBhcnRpdGlvbktleSI6ImRhZDNiODMwLTAyOWQtNGZlMy1iMDJkLTc1MDFjOTc4NzYwOCIsInRpbWVzdGFtcCI6MTYzMTUzODA1OTQ1NiwidHlwZSI6ImJvb2tpbmdfY29tcGxldGVkIiwiYm9va2luZ19jb21wbGV0ZWQiOnsidGltZXN0YW1wIjoxNjMxNTM4MDU5NDU2LCJwcm9kdWN0X3Byb3ZpZGVyIjoiQnJpdHRhbnkgRmVycmllcyIsIm9yZGVySWQiOjEyMzQ3OH19', partitionKey: 'dad3b830-029d-4fe3-b02d-7501c9787608', approximateArrivalTimestamp: 1631538059456, kinesisSchemaVersion: '1.0', sequenceNumber: 'dad3b830-029d-4fe3-b02d-7501c9787608', }, eventSource: 'aws:kinesis', eventID: 'shardId-000000000000:49545115243490985018280067714973144582180062593244200961', invokeIdentityArn: 'arn:aws:iam::EXAMPLE', eventVersion: '1.0', eventName: 'aws:kinesis:record', eventSourceARN: 'arn:aws:kinesis:EXAMPLE', awsRegion: 'us-east-1', }; const brokenData = { kinesis: { data: 'Data that will not decode', partitionKey: 'dad3b830-029d-4fe3-b02d-7501c9787608', approximateArrivalTimestamp: 1631538059456, kinesisSchemaVersion: '1.0', sequenceNumber: 'dad3b830-029d-4fe3-b02d-7501c9787608', }, }; const emptyData = { kinesis: { data: '', partitionKey: 'dad3b830-029d-4fe3-b02d-7501c9787608', approximateArrivalTimestamp: 1631538059456, kinesisSchemaVersion: '1.0', sequenceNumber: 'dad3b830-029d-4fe3-b02d-7501c9787608', }, }; // This data block has a type:"booking_notcompleted" const dataWrongTypeData = { kinesis: { data: 'eyJpZCI6ImE0MzgwYzAyLTE0OTItMTFlYy1hMGIyLWM3OGZmYmQ2OTM0NyIsInBhcnRpdGlvbktleSI6ImRhZDNiODMwLTAyOWQtNGZlMy1iMDJkLTc1MDFjOTc4NzYwOCIsInRpbWVzdGFtcCI6MTYzMTUzODA1OTQ1NiwidHlwZSI6ImJvb2tpbmdfbm90Y29tcGxldGVkIiwiYm9va2luZ19jb21wbGV0ZWQiOnsidGltZXN0YW1wIjoxNjMxNTM4MDU5NDU2LCJwcm9kdWN0X3Byb3ZpZGVyIjoiQnJpdHRhbnkgRmVycmllcyIsIm9yZGVySWQiOjEyMzQ3OH19', partitionKey: 'dad3b830-029d-4fe3-b02d-7501c9787608', approximateArrivalTimestamp: 1631538059456, kinesisSchemaVersion: '1.0', sequenceNumber: 'dad3b830-029d-4fe3-b02d-7501c9787608', }, }; const decodedData = { id: 'a4380c02-1492-11ec-a0b2-c78ffbd69347', partitionKey: 'dad3b830-029d-4fe3-b02d-7501c9787608', timestamp: 1631538059456, type: 'booking_completed', booking_completed: { timestamp: 1631538059456, product_provider: 'Brittany Ferries', orderId: 123478, }, }; const improperKinesisData = Object.assign({}, kinesisData, brokenData); const emptyKinesisData = Object.assign({}, kinesisData, emptyData); const wrongTypeKinesisData = Object.assign( {}, kinesisData, dataWrongTypeData ); it('should pass with proper data', () => { expect(processor.processEvent(kinesisData)).toBeUndefined(); }); it('should fail on data that did not decode correctly', () => { expect(processor.processEvent(improperKinesisData)).toBeNull(); }); it('should fail on on empty data', () => { expect(processor.processEvent(emptyKinesisData)).toBeNull(); }); it('should safely handle proper data but with an unwanted type', () => { expect(processor.processEvent(wrongTypeKinesisData)).toBeUndefined(); }); // Need this to get 100% coverage on the case statement it('cover all case steps', () => { const allTypes = ['booking_requested', 'not_a_case_option']; for (const testCase of allTypes) { const workData = Object.assign({}, decodedData, { type: testCase }); const workBlob = btoa(JSON.stringify(workData)); const tempKinesisData = Object.assign({}, kinesisData, { kinesis: { data: workBlob, }, }); expect(processor.processEvent(tempKinesisData)).toBeFalsy(); } }); }); describe('transformBookingRequestPayload', () => { const validData = { id: 'a4388132-1492-11ec-a0b2-c78ffbd69347', partitionKey: '2fce53f1-1daf-4247-9245-0ad78d0b0d2e', timestamp: 1631538059459, type: 'booking_requested', booking_requested: { timestamp: 1631538059459, orderId: 10017, product_provider: 'Stena Line', }, }; const invalidData = { id: 'a4388132-1492-11ec-a0b2-c78ffbd69347', partitionKey: '2fce53f1-1daf-4247-9245-0ad78d0b0d2e', timestamp: 1631538059459, type: 'booking_NOT_requested', booking_NOT_requested: { timestamp: 1631538059459, orderId: 10017, product_provider: 'Stena Square', }, }; const validTransformed = { product_order_id_buyer: 10017, timestamp: '2021-09-13T13:00:59.459Z', product_provider_buyer: 'Stena Line', }; it('should pass', () => { expect(processor.transformBookingRequestPayload(validData)).toEqual(validTransformed); }); it('should return a null for an item which does not contain a booking_request', () => { expect(processor.transformBookingRequestPayload(invalidData)).toBeNull(); }); }); describe('transformBookingRequestPayload', () => { const validTransformed = { product_order_id_buyer: 10017, timestamp: '2021-09-13T13:00:59.459Z', product_provider_buyer: 'Stena Line', }; const invalidTransformed = { not_product_order_id_buyer: 10017, timestamp: '2021-09-13T13:00:59.459Z', not_product_provider_buyer: 'Stena Line', }; it('should pass', async () => { expect(await processor.sendPayload(validTransformed)).toEqual({ status: 200, message: 'OK', }); }); it('should reject null data', async () => { expect(await processor.sendPayload(null)).toEqual({ status: 0, message: 'Empty Payload', }); }); it('should handle an error response from the server', async () => { // @ts-ignore expect(await processor.sendPayload(invalidTransformed)).toEqual({ status: 400, message: 'Bad Request', }); }); }); describe('Processor Construction', () => { it('should return an error if no url is passed on construction', () => { // @ts-ignore expect(() => new Processor()).toThrow(); }); }); });