forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic-rule.test.ts
258 lines (216 loc) · 7.47 KB
/
topic-rule.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Template } from '@aws-cdk/assertions';
import * as cdk from '@aws-cdk/core';
import * as iot from '../lib';
test('Default property', () => {
const stack = new cdk.Stack();
new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
Actions: [],
Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'",
RuleDisabled: false,
},
});
});
test('can get topic rule name', () => {
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
new cdk.CfnResource(stack, 'Res', {
type: 'Test::Resource',
properties: {
TopicRuleName: topicRule.topicRuleName,
},
});
Template.fromStack(stack).hasResourceProperties('Test::Resource', {
TopicRuleName: { Ref: 'MyTopicRule4EC2091C' },
});
});
test('can get topic rule arn', () => {
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
new cdk.CfnResource(stack, 'Res', {
type: 'Test::Resource',
properties: {
TopicRuleArn: topicRule.topicRuleArn,
},
});
Template.fromStack(stack).hasResourceProperties('Test::Resource', {
TopicRuleArn: {
'Fn::GetAtt': ['MyTopicRule4EC2091C', 'Arn'],
},
});
});
test('can set physical name', () => {
// GIVEN
const stack = new cdk.Stack();
// WHEN
new iot.TopicRule(stack, 'MyTopicRule', {
topicRuleName: 'PhysicalName',
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
RuleName: 'PhysicalName',
});
});
test('can set description', () => {
const stack = new cdk.Stack();
new iot.TopicRule(stack, 'MyTopicRule', {
description: 'test-description',
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
Description: 'test-description',
},
});
});
test('can set ruleDisabled', () => {
const stack = new cdk.Stack();
new iot.TopicRule(stack, 'MyTopicRule', {
enabled: false,
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
RuleDisabled: true,
},
});
});
test.each([
['fromStringAsVer20151008', iot.IotSql.fromStringAsVer20151008, '2015-10-08'],
['fromStringAsVer20160323', iot.IotSql.fromStringAsVer20160323, '2016-03-23'],
['fromStringAsVerNewestUnstable', iot.IotSql.fromStringAsVerNewestUnstable, 'beta'],
])('can set sql with using %s', (_, factoryMethod, version) => {
const stack = new cdk.Stack();
new iot.TopicRule(stack, 'MyTopicRule', {
sql: factoryMethod("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
AwsIotSqlVersion: version,
Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'",
},
});
});
test.each([
['fromStringAsVer20151008', iot.IotSql.fromStringAsVer20151008],
['fromStringAsVer20160323', iot.IotSql.fromStringAsVer20160323],
['fromStringAsVerNewestUnstable', iot.IotSql.fromStringAsVerNewestUnstable],
])('Using %s fails when setting empty sql', (_, factoryMethod) => {
expect(() => {
factoryMethod('');
}).toThrow('IoT SQL string cannot be empty');
});
test('can set actions', () => {
const stack = new cdk.Stack();
const action1: iot.IAction = {
bind: () => ({
configuration: {
http: { url: 'http://example.com' },
},
}),
};
const action2: iot.IAction = {
bind: () => ({
configuration: {
lambda: { functionArn: 'test-functionArn' },
},
}),
};
new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
actions: [action1, action2],
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
Actions: [
{ Http: { Url: 'http://example.com' } },
{ Lambda: { FunctionArn: 'test-functionArn' } },
],
Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'",
},
});
});
test('can add an action', () => {
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
topicRule.addAction({
bind: () => ({
configuration: {
http: { url: 'http://example.com' },
},
}),
});
topicRule.addAction({
bind: () => ({
configuration: {
lambda: { functionArn: 'test-functionArn' },
},
}),
});
Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', {
TopicRulePayload: {
Actions: [
{ Http: { Url: 'http://example.com' } },
{ Lambda: { FunctionArn: 'test-functionArn' } },
],
Sql: "SELECT topic(2) as device_id, temperature FROM 'device/+/data'",
},
});
});
test('cannot add an action as empty object', () => {
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
const emptyKeysAction: iot.IAction = {
bind: () => ({
configuration: {},
}),
};
expect(() => {
topicRule.addAction(emptyKeysAction);
}).toThrow('An action property cannot be an empty object.');
});
test('cannot add an action that have multiple keys', () => {
const stack = new cdk.Stack();
const topicRule = new iot.TopicRule(stack, 'MyTopicRule', {
sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"),
});
const multipleKeysAction: iot.IAction = {
bind: () => ({
configuration: {
http: { url: 'http://example.com' },
lambda: { functionArn: 'test-functionArn' },
},
}),
};
expect(() => {
topicRule.addAction(multipleKeysAction);
}).toThrow('An action property cannot have multiple keys, received: http,lambda');
});
test('can import a TopicRule by ARN', () => {
const stack = new cdk.Stack();
const topicRuleArn = 'arn:aws:iot:ap-northeast-1:123456789012:rule/my-topic-rule-name';
const topicRule = iot.TopicRule.fromTopicRuleArn(stack, 'TopicRuleFromArn', topicRuleArn);
expect(topicRule).toMatchObject({
topicRuleArn,
topicRuleName: 'my-topic-rule-name',
});
});
test('fails importing a TopicRule by ARN if the ARN is missing the name of the TopicRule', () => {
const stack = new cdk.Stack();
const topicRuleArn = 'arn:aws:iot:ap-northeast-1:123456789012:rule/';
expect(() => {
iot.TopicRule.fromTopicRuleArn(stack, 'TopicRuleFromArn', topicRuleArn);
}).toThrow("Missing topic rule name in ARN: 'arn:aws:iot:ap-northeast-1:123456789012:rule/'");
});