From 83c9225aac478fba0790b4dd27a5b2bdef910c72 Mon Sep 17 00:00:00 2001 From: Marko Malenic Date: Fri, 9 Feb 2024 19:09:21 +1100 Subject: [PATCH] test: add event source unit tests --- .../stateful/eventSourceConstruct.test.ts | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/workload/stateful/eventSourceConstruct.test.ts diff --git a/test/workload/stateful/eventSourceConstruct.test.ts b/test/workload/stateful/eventSourceConstruct.test.ts new file mode 100644 index 000000000..3249f0bf2 --- /dev/null +++ b/test/workload/stateful/eventSourceConstruct.test.ts @@ -0,0 +1,77 @@ +import * as cdk from 'aws-cdk-lib'; +import { Template } from 'aws-cdk-lib/assertions'; +import { EventSource } from '../../../lib/workload/stateful/event_source/component'; + +let stack: cdk.Stack; + +function assert_common(template: Template) { + template.resourceCountIs('AWS::SQS::Queue', 2); + + template.hasResourceProperties('AWS::CloudWatch::Alarm', { + ComparisonOperator: 'GreaterThanThreshold', + EvaluationPeriods: 1, + Threshold: 0, + }); + + template.hasResourceProperties('AWS::Events::Rule', { + EventPattern: { + source: ['aws.s3'], + detail: { + bucket: { + name: ['bucket'], + }, + }, + }, + }); +} + +beforeEach(() => { + stack = new cdk.Stack(); +}); + +test('Test EventSource created props', () => { + new EventSource(stack, 'TestDatabaseConstruct', { + buckets: ['bucket'], + }); + const template = Template.fromStack(stack); + + assert_common(template); +}); + +test('Test EventSource created props with event types', () => { + new EventSource(stack, 'TestDatabaseConstruct', { + buckets: ['bucket'], + eventTypes: ['Object Created'], + }); + const template = Template.fromStack(stack); + + assert_common(template); + template.hasResourceProperties('AWS::Events::Rule', { + EventPattern: { + 'detail-type': ['Object Created'], + }, + }); +}); + +test('Test EventSource created props with prefix', () => { + new EventSource(stack, 'TestDatabaseConstruct', { + buckets: ['bucket'], + prefix: 'prefix', + }); + const template = Template.fromStack(stack); + + assert_common(template); + template.hasResourceProperties('AWS::Events::Rule', { + EventPattern: { + detail: { + object: { + key: [ + { + prefix: 'prefix', + }, + ], + }, + }, + }, + }); +});