Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatatsu committed Feb 2, 2022
1 parent ab42260 commit f31211e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 30 deletions.
75 changes: 59 additions & 16 deletions packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import * as cdk from '@aws-cdk/core';
import * as iotevents from '../lib';

let stack: cdk.Stack;
let input: iotevents.IInput;
beforeEach(() => {
stack = new cdk.Stack();
input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input');
});

test('Default property', () => {
Expand Down Expand Up @@ -138,24 +140,50 @@ test('can set multiple events to State', () => {
});

test('can set states with transitions', () => {
// WHEN
// GIVEN
const firstState = new iotevents.State({
stateName: 'firstState',
onEnter: [{
eventName: 'test-eventName',
condition: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.currentInput(input),
}],
});
const secondState = new iotevents.State({
stateName: 'secondState',
});
const thirdState = new iotevents.State({
stateName: 'thirdState',
});

// WHEN
// transition as 1st -> 2nd
firstState.transitionTo(secondState, {
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('12.1'),
),
});
// transition as 1st -> 2nd, make duprecated transition with different condition
firstState.transitionTo(secondState, {
condition: iotevents.Expression.fromString('test-eventCondition-12'),
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('12.2'),
),
});
// transition as 2nd -> 1st, make circular reference
secondState.transitionTo(firstState, {
eventName: 'secondToFirst',
condition: iotevents.Expression.fromString('test-eventCondition-21'),
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('21'),
),
});
// transition as 2nd -> 3rd, to test recursive calling
secondState.transitionTo(thirdState, {
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('23'),
),
});

new iotevents.DetectorModel(stack, 'MyDetectorModel', {
Expand All @@ -169,23 +197,40 @@ test('can set states with transitions', () => {
{
StateName: 'firstState',
OnInput: {
TransitionEvents: [{
EventName: 'firstState_to_secondState',
NextState: 'secondState',
Condition: 'test-eventCondition-12',
}],
TransitionEvents: [
{
EventName: 'firstState_to_secondState',
NextState: 'secondState',
Condition: '$input.test-input.payload.temperature == 12.1',
},
{
EventName: 'firstState_to_secondState',
NextState: 'secondState',
Condition: '$input.test-input.payload.temperature == 12.2',
},
],
},
},
{
StateName: 'secondState',
OnInput: {
TransitionEvents: [{
EventName: 'secondToFirst',
NextState: 'firstState',
Condition: 'test-eventCondition-21',
}],
TransitionEvents: [
{
EventName: 'secondToFirst',
NextState: 'firstState',
Condition: '$input.test-input.payload.temperature == 21',
},
{
EventName: 'secondState_to_thirdState',
NextState: 'thirdState',
Condition: '$input.test-input.payload.temperature == 23',
},
],
},
},
{
StateName: 'thirdState',
},
],
},
});
Expand Down Expand Up @@ -248,7 +293,6 @@ test('cannot create without event', () => {
describe('Expression', () => {
test('currentInput', () => {
// WHEN
const input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input');
new iotevents.DetectorModel(stack, 'MyDetectorModel', {
initialState: new iotevents.State({
stateName: 'test-state',
Expand Down Expand Up @@ -277,7 +321,6 @@ describe('Expression', () => {

test('inputAttribute', () => {
// WHEN
const input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input');
new iotevents.DetectorModel(stack, 'MyDetectorModel', {
initialState: new iotevents.State({
stateName: 'test-state',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"Type": "AWS::IoTEvents::DetectorModel",
"Properties": {
"DetectorModelDefinition": {
"InitialStateName": "firstState",
"InitialStateName": "online",
"States": [
{
"OnEnter": {
Expand Down Expand Up @@ -78,12 +78,12 @@
]
]
},
"EventName": "firstState_to_secondState",
"NextState": "secondState"
"EventName": "online_to_offline",
"NextState": "offline"
}
]
},
"StateName": "firstState"
"StateName": "online"
},
{
"OnInput": {
Expand All @@ -101,12 +101,12 @@
]
]
},
"EventName": "secondState_to_firstState",
"NextState": "firstState"
"EventName": "offline_to_online",
"NextState": "online"
}
]
},
"StateName": "secondState"
"StateName": "offline"
}
]
},
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class TestStack extends cdk.Stack {
attributeJsonPaths: ['payload.deviceId', 'payload.temperature'],
});

const firstState = new iotevents.State({
stateName: 'firstState',
const onlineState = new iotevents.State({
stateName: 'online',
onEnter: [{
eventName: 'test-event',
// meaning `condition: 'currentInput("test_input") && $input.test_input.payload.temperature == 31.5'`
Expand All @@ -24,19 +24,19 @@ class TestStack extends cdk.Stack {
),
}],
});
const secondState = new iotevents.State({
stateName: 'secondState',
const offlineState = new iotevents.State({
stateName: 'offline',
});

// 1st => 2nd
firstState.transitionTo(secondState, {
onlineState.transitionTo(offlineState, {
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('12'),
),
});
// 2st => 1st
secondState.transitionTo(firstState, {
offlineState.transitionTo(onlineState, {
condition: iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
iotevents.Expression.fromString('21'),
Expand All @@ -48,7 +48,7 @@ class TestStack extends cdk.Stack {
description: 'test-detector-model-description',
evaluationMethod: iotevents.EventEvaluation.SERIAL,
detectorKey: 'payload.deviceId',
initialState: firstState,
initialState: onlineState,
});
}
}
Expand Down

0 comments on commit f31211e

Please sign in to comment.