|
| 1 | +import { Template, Match } from '@aws-cdk/assertions'; |
| 2 | +import * as iam from '@aws-cdk/aws-iam'; |
| 3 | +import * as iot from '@aws-cdk/aws-iot'; |
| 4 | +import * as kinesis from '@aws-cdk/aws-kinesis'; |
| 5 | +import * as cdk from '@aws-cdk/core'; |
| 6 | +import * as actions from '../../lib'; |
| 7 | + |
| 8 | +test('Default kinesis stream action', () => { |
| 9 | + // GIVEN |
| 10 | + const stack = new cdk.Stack(); |
| 11 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 12 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 13 | + }); |
| 14 | + const stream = kinesis.Stream.fromStreamArn(stack, 'MyStream', 'arn:aws:kinesis:xx-west-1:111122223333:stream/my-stream'); |
| 15 | + |
| 16 | + // WHEN |
| 17 | + topicRule.addAction(new actions.KinesisPutRecordAction(stream, { |
| 18 | + partitionKey: '${newuuid()}', |
| 19 | + })); |
| 20 | + |
| 21 | + // THEN |
| 22 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 23 | + TopicRulePayload: { |
| 24 | + Actions: [ |
| 25 | + { |
| 26 | + Kinesis: { |
| 27 | + StreamName: 'my-stream', |
| 28 | + PartitionKey: '${newuuid()}', |
| 29 | + RoleArn: { |
| 30 | + 'Fn::GetAtt': ['MyTopicRuleTopicRuleActionRoleCE2D05DA', 'Arn'], |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { |
| 39 | + AssumeRolePolicyDocument: { |
| 40 | + Statement: [ |
| 41 | + { |
| 42 | + Action: 'sts:AssumeRole', |
| 43 | + Effect: 'Allow', |
| 44 | + Principal: { |
| 45 | + Service: 'iot.amazonaws.com', |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + Version: '2012-10-17', |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 54 | + PolicyDocument: { |
| 55 | + Statement: [ |
| 56 | + { |
| 57 | + Action: 'kinesis:PutRecord', |
| 58 | + Effect: 'Allow', |
| 59 | + Resource: 'arn:aws:kinesis:xx-west-1:111122223333:stream/my-stream', |
| 60 | + }, |
| 61 | + ], |
| 62 | + Version: '2012-10-17', |
| 63 | + }, |
| 64 | + PolicyName: 'MyTopicRuleTopicRuleActionRoleDefaultPolicy54A701F7', |
| 65 | + Roles: [ |
| 66 | + { Ref: 'MyTopicRuleTopicRuleActionRoleCE2D05DA' }, |
| 67 | + ], |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +test('passes undefined to partitionKey if empty string is given', () => { |
| 72 | + // GIVEN |
| 73 | + const stack = new cdk.Stack(); |
| 74 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 75 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 76 | + }); |
| 77 | + const stream = kinesis.Stream.fromStreamArn(stack, 'MyStream', 'arn:aws:kinesis:xx-west-1:111122223333:stream/my-stream'); |
| 78 | + |
| 79 | + // WHEN |
| 80 | + topicRule.addAction(new actions.KinesisPutRecordAction(stream, { |
| 81 | + partitionKey: '', |
| 82 | + })); |
| 83 | + |
| 84 | + // THEN |
| 85 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 86 | + TopicRulePayload: { |
| 87 | + Actions: [ |
| 88 | + Match.objectLike({ Kinesis: { PartitionKey: Match.absent() } }), |
| 89 | + ], |
| 90 | + }, |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +test('can set role', () => { |
| 95 | + // GIVEN |
| 96 | + const stack = new cdk.Stack(); |
| 97 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 98 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 99 | + }); |
| 100 | + const stream = kinesis.Stream.fromStreamArn(stack, 'MyStream', 'arn:aws:kinesis:xx-west-1:111122223333:stream/my-stream'); |
| 101 | + const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::123456789012:role/ForTest'); |
| 102 | + |
| 103 | + // WHEN |
| 104 | + topicRule.addAction(new actions.KinesisPutRecordAction(stream, { |
| 105 | + partitionKey: '${newuuid()}', |
| 106 | + role, |
| 107 | + })); |
| 108 | + |
| 109 | + // THEN |
| 110 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 111 | + TopicRulePayload: { |
| 112 | + Actions: [ |
| 113 | + Match.objectLike({ Kinesis: { RoleArn: 'arn:aws:iam::123456789012:role/ForTest' } }), |
| 114 | + ], |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 119 | + PolicyName: 'MyRolePolicy64AB00A5', |
| 120 | + Roles: ['ForTest'], |
| 121 | + }); |
| 122 | +}); |
0 commit comments