Esempio n. 1
0
    def test_to_dict(self):
        # Create primitive event
        p_timestamp = EpochNSClock.generate_timestamp()
        p_event = PrimitiveEvent(timestamp=p_timestamp, data=P_DATA)
        p_hist = "p_hist"

        # Create composite event
        c_timestamp = EpochNSClock.generate_timestamp()
        c_history = BoboHistory()
        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=C_NAME,
                                 history=c_history,
                                 data=C_DATA)
        c_hist = "c_hist"

        # Create list of events for history
        events = {p_hist: [p_event], c_hist: [c_event]}

        # Create history and add events
        history = BoboHistory(events=events)

        self.assertDictEqual(history.to_dict(), {
            p_hist: [p_event.to_dict()],
            c_hist: [c_event.to_dict()]
        })
Esempio n. 2
0
 def on_handler_final(self, nfa_name: str, run_id: str,
                      event: CompositeEvent):
     if not self._is_cancelled:
         self._queue_final.put_nowait({
             bdc.NFA_NAME: nfa_name,
             bdc.RUN_ID: run_id,
             bdc.EVENT: event.to_dict()
         })
Esempio n. 3
0
    def test_action(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        # create dict representation of action event
        a_dict = {
            ActionEvent.TIMESTAMP: a_timestamp,
            ActionEvent.NAME: a_name,
            ActionEvent.SUCCESS: a_success,
            ActionEvent.FOR_EVENT: a_for_event.to_dict(),
            ActionEvent.EXCEPTION: a_exception,
            ActionEvent.DESCRIPTION: a_description,
            ActionEvent.DATA: a_data,
            ActionEvent.EVENT_ID: a_event_id
        }

        # build actual action event from dict
        a_event_1 = BoboRuleBuilder.action(a_dict)
        a_event_2 = BoboRuleBuilder.event(a_dict)

        for a_event in [a_event_1, a_event_2]:
            self.assertEqual(a_event.timestamp, a_timestamp)
            self.assertEqual(a_event.name, a_name)
            self.assertEqual(a_event.success, a_success)
            self.assertEqual(a_event.exception, a_exception)
            self.assertEqual(a_event.description, a_description)
            self.assertDictEqual(a_event.data, a_data)
            self.assertEqual(a_event.event_id, a_event_id)

            c_event = a_event.for_event
            self.assertIsInstance(c_event, CompositeEvent)
            self.assertEqual(c_event.timestamp, a_for_event.timestamp)
            self.assertEqual(c_event.name, a_for_event.name)
            self.assertIsNone(c_event.history.first)
            self.assertDictEqual(c_event.data, a_for_event.data)
            self.assertEqual(c_event.event_id, a_for_event.event_id)
Esempio n. 4
0
    def test_to_dict(self):
        c_timestamp = EpochNSClock.generate_timestamp()
        c_name = "c_name"
        c_history = BoboHistory()
        c_data = {"c_key": "c_value"}

        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=c_name,
                                 history=c_history,
                                 data=c_data)

        self.assertDictEqual(
            c_event.to_dict(), {
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: c_history.to_dict(),
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_event.event_id
            })
Esempio n. 5
0
    def test_action_none(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: None,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: None,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: None,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: None,
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: None
            })