def setup_app_instance(self, action, workflow_ctx):
        """Sets up an AppInstance for a device in an action

        Args:
            action (Action): The Action which has the Device
            workflow_ctx (WorkflowExecutionContext): The context of the workflow

        Returns:
            (tuple(app_name, device_id)): A tuple containing the app name for the Action, and the device_id int
        """
        if action.device_id:
            device_id = (action.app_name,
                         action.device_id.get_value(workflow_ctx.accumulator))
            if device_id not in self._instances:
                context = {
                    'workflow_execution_id': workflow_ctx.execution_id,
                    'workflow_id': workflow_ctx.id,
                    'workflow_name': workflow_ctx.name
                }
                self._instances[device_id] = AppInstance.create(
                    device_id[0], device_id[1], context)
                WalkoffEvent.CommonWorkflowSignal.send(
                    workflow_ctx.workflow,
                    event=WalkoffEvent.AppInstanceCreated)
                logger.debug(
                    'Created new app instance: App {0}, device {1}'.format(
                        *device_id))
            return device_id
        return None
Example #2
0
    def test_execute_with_accumulator_missing_action_callbacks(self):
        action = Action(app_name='HelloWorld',
                        action_name='Add Three',
                        name='helloWorld',
                        arguments=[
                            Argument('num1', reference='1'),
                            Argument('num2', reference='action2'),
                            Argument('num3', value='10.2')
                        ])
        accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionArgumentsInvalid))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                else:
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)
        action.execute(instance.instance, accumulator)

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Example #3
0
    def test_execute_sends_callbacks(self):
        action = Action(app_name='HelloWorld',
                        action_name='Add Three',
                        name='helloWorld',
                        arguments=[
                            Argument('num1', value='-5.6'),
                            Argument('num2', value='4.3'),
                            Argument('num3', value='10.2')
                        ])
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionExecutionSuccess))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                else:
                    self.assertIn('data', kwargs)
                    data = kwargs['data']
                    self.assertEqual(data['status'], 'Success')
                    self.assertAlmostEqual(data['result'], 8.9)
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})
        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Example #4
0
 def test_execute_with_complex_args(self):
     action = Action(app_name='HelloWorld',
                     action_name='Json Sample',
                     name='helloWorld',
                     arguments=[
                         Argument('json_in',
                                  value={
                                      'a':
                                      '-5.6',
                                      'b': {
                                          'a': '4.3',
                                          'b': 5.3
                                      },
                                      'c': ['1', '2', '3'],
                                      'd': [{
                                          'a': '',
                                          'b': 3
                                      }, {
                                          'a': '',
                                          'b': -1.5
                                      }, {
                                          'a': '',
                                          'b': -0.5
                                      }]
                                  })
                     ])
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 11.0)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Example #5
0
    def test_execute_action_which_raises_exception_sends_callbacks(self):
        action = Action(app_name='HelloWorld',
                        action_name='Buggy',
                        name='helloWorld')
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionExecutionError))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                elif kwargs['event'] == WalkoffEvent.ActionExecutionError:
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Example #6
0
 def test_execute_generates_uid(self):
     action = Action(app_name='HelloWorld', action_name='helloWorld')
     original_execution_uid = action.get_execution_uid()
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, {})
     self.assertNotEqual(action.get_execution_uid(), original_execution_uid)
Example #7
0
    def test_execute_multiple_triggers(self):
        triggers = [
            Condition('HelloWorld',
                      action_name='regMatch',
                      arguments=[Argument('regex', value='aaa')])
        ]
        action = Action(app_name='HelloWorld',
                        action_name='helloWorld',
                        triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        action.send_data_to_trigger({"data_in": {"data": 'a'}})

        trigger_taken = {'triggered': 0}
        trigger_not_taken = {'triggered': 0}

        def callback_is_sent(sender, **kwargs):
            if kwargs['event'] == WalkoffEvent.TriggerActionTaken:
                trigger_taken['triggered'] += 1
            elif kwargs['event'] == WalkoffEvent.TriggerActionNotTaken:
                action.send_data_to_trigger({"data_in": {"data": 'aaa'}})
                trigger_not_taken['triggered'] += 1

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})
        self.assertEqual(trigger_taken['triggered'], 1)
        self.assertEqual(trigger_not_taken['triggered'], 1)
Example #8
0
    def test_execute_default_return_success(self):
        action = Action(app_name='HelloWorld',
                        action_name='dummy action',
                        name='helloWorld',
                        arguments=[
                            Argument('status', value=True),
                            Argument('other', value=True)
                        ])
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionExecutionSuccess))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                else:
                    self.assertIn('data', kwargs)
                    data = kwargs['data']
                    self.assertEqual(data['status'], 'Success')
                    self.assertEqual(data['result'], None)
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Example #9
0
 def _make_app_instance(app_name='HelloWorld', device='device1', context=None):
     if not context:
         context = {
             'workflow_name': 'test_workflow',
             'workflow_id': uuid4(),
             'workflow_execution_id': uuid4()
         }
     return AppInstance.create(app_name, device, context)
Example #10
0
 def test_execute_action_which_raises_exception(self):
     action = Action(app_name='HelloWorld',
                     action_name='Buggy',
                     name='helloWorld')
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, {})
     self.assertIsNotNone(action.get_output())
Example #11
0
 def test_execute_no_args(self):
     action = Action(app_name='HelloWorld', action_name='helloWorld')
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     self.assertEqual(action.execute(instance.instance, {}),
                      ActionResult({'message': 'HELLO WORLD'}, 'Success'))
     self.assertEqual(action._output,
                      ActionResult({'message': 'HELLO WORLD'}, 'Success'))
Example #12
0
 def test_execute_global_action(self):
     action = Action(app_name='HelloWorld',
                     action_name='global2',
                     arguments=[Argument('arg1', value='something')])
     instance = AppInstance.create(app_name='HelloWorld', device_name='')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 'something')
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Example #13
0
 def _make_app_instance(app_name='HelloWorld',
                        device='device1',
                        context=None):
     if not context:
         context = {
             'workflow_name': 'test_workflow',
             'workflow_id': uuid4(),
             'workflow_execution_id': uuid4()
         }
     return AppInstance.create(app_name, device, context)
Example #14
0
 def setup_app_instance(self, action):
     device_id = (action.app_name, action.device_id)
     if device_id not in self._instances:
         self._instances[device_id] = AppInstance.create(
             action.app_name, action.device_id)
         WalkoffEvent.CommonWorkflowSignal.send(
             self, event=WalkoffEvent.AppInstanceCreated)
         logger.debug(
             'Created new app instance: App {0}, device {1}'.format(
                 action.app_name, action.device_id))
     return device_id
Example #15
0
 def test_execute_multiple_triggers(self):
     trigger = ConditionalExpression(
         'and',
         conditions=[
             Condition('HelloWorld',
                       action_name='regMatch',
                       arguments=[Argument('regex', value='aaa')])
         ])
     action = Action(app_name='HelloWorld',
                     action_name='helloWorld',
                     name='helloWorld',
                     trigger=trigger)
     AppInstance.create(app_name='HelloWorld', device_name='device1')
     self.assertFalse(action.execute_trigger({"data_in": {
         "data": 'a'
     }}, {}))
     self.assertTrue(
         action.execute_trigger({"data_in": {
             "data": 'aaa'
         }}, {}))
Example #16
0
 def test_execute_with_accumulator_missing_action(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', reference='1'),
                         Argument('num2', reference='action2'),
                         Argument('num3', value='10.2')
                     ])
     accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, accumulator)
Example #17
0
 def test_execute_with_args(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', value='-5.6'),
                         Argument('num2', value='4.3'),
                         Argument('num3', value='10.2')
                     ])
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Example #18
0
 def test_execute_with_accumulator_with_extra_actions(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', reference='1'),
                         Argument('num2', reference='action2'),
                         Argument('num3', value='10.2')
                     ])
     accumulator = {'1': '-5.6', 'action2': '4.3', '3': '45'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, accumulator)
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Example #19
0
    def setup_app_instance(self, action, workflow):
        """Sets up an AppInstance for a device in an action

        Args:
            action (Action): The Action which has the Device
            workflow (Workflow): The Workflow which has the Action

        Returns:
            (tuple(app_name, device_id)): A tuple containing the app name for the Action, and the device_id int
        """
        if action.device_id:
            device_id = (action.app_name, action.device_id.get_value(workflow.get_accumulator()))
            if device_id not in self._instances:
                self._instances[device_id] = AppInstance.create(*device_id)
                WalkoffEvent.CommonWorkflowSignal.send(workflow, event=WalkoffEvent.AppInstanceCreated)
                logger.debug('Created new app instance: App {0}, device {1}'.format(*device_id))
            return device_id
        return None
Example #20
0
    def test_execute_with_triggers(self):
        triggers = [
            Condition('HelloWorld',
                      action_name='regMatch',
                      arguments=[Argument('regex', value='aaa')])
        ]
        action = Action(app_name='HelloWorld',
                        action_name='helloWorld',
                        triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        action.send_data_to_trigger({"data_in": {"data": 'aaa'}})

        result = {'triggered': False}

        def callback_is_sent(sender, **kwargs):
            if kwargs['event'] == WalkoffEvent.TriggerActionTaken:
                result['triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)
        action.execute(instance.instance, {})
        self.assertTrue(result['triggered'])