diff --git a/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts b/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts index 985f2d534d15f..817039a98eacf 100644 --- a/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts +++ b/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts @@ -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', () => { @@ -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', { @@ -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', + }, ], }, }); @@ -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', @@ -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', diff --git a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json index cbd8e691559e4..888869a41e68e 100644 --- a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json +++ b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json @@ -37,7 +37,7 @@ "Type": "AWS::IoTEvents::DetectorModel", "Properties": { "DetectorModelDefinition": { - "InitialStateName": "firstState", + "InitialStateName": "online", "States": [ { "OnEnter": { @@ -78,12 +78,12 @@ ] ] }, - "EventName": "firstState_to_secondState", - "NextState": "secondState" + "EventName": "online_to_offline", + "NextState": "offline" } ] }, - "StateName": "firstState" + "StateName": "online" }, { "OnInput": { @@ -101,12 +101,12 @@ ] ] }, - "EventName": "secondState_to_firstState", - "NextState": "firstState" + "EventName": "offline_to_online", + "NextState": "online" } ] }, - "StateName": "secondState" + "StateName": "offline" } ] }, diff --git a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts index a73d93191c91b..2c59cbfbed3be 100644 --- a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts +++ b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts @@ -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'` @@ -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'), @@ -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, }); } }