def test_fetch_digital_twin_triggered_on_start(self): emtpy_queue(queue=self.test_queue) device_manager = DeviceManager(queue=self.test_queue, thread_event=self.default_event) device_manager.running = False device_manager.start() msg = self.test_queue.get() self.assertEqual(msg.event, "digital_twin") self.assertEqual(msg.subject, "fetch_digital_twin")
def test_store_remote_digital_twin(self): emtpy_queue(queue=self.test_queue) device_manager = DeviceManager(queue=self.test_queue, thread_event=self.default_event) device_manager.start() moch_dt = ["abc"] notify_msg = ObserverMessage(event="digital_twin", subject="retrieved_digital_twin", data=moch_dt) device_manager.notify(msg=notify_msg) sleep(0.5) self.assertEqual(device_manager.remote_digital_twin, moch_dt) device_manager.running = False device_manager.notify(msg=ObserverMessage(data="", event=""))
def test_fetch_digital_twin(self, mock_mongo, mock_get_doc_data, mock_adapter): mock_mongo.return_value = None mock_get_doc_data.return_value = None mock_adapter.return_value = "test" db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event) emtpy_queue(queue=self.test_queue) db_handler._fetch_digital_twin() item = self.test_queue.get() correct_msg = ObserverMessage(event="digital_twin", data="test", subject="retrieved_digital_twin") correct_msg.source = "DbHandler" self.assertEqual(correct_msg, item)
def test_log_mqtt_traffic(self, mock_mqtt_client): """ Testing logging_tests of a mqtt message""" mock_mqtt_client.return_value = None topic = 'test' payload = '1-2-three' mqtt_gateway = MqttGateway(queue=self.test_queue, thread_event=self.default_event) emtpy_queue(queue=self.test_queue) mqtt_gateway._log_mqtt_traffic(topic=topic, payload=payload) result = self.test_queue.get() self.assertEqual(result.event, "iot_traffic") self.assertEqual(result.data['source'], "MqttGateway") self.assertEqual(result.data['topic'], topic) self.assertEqual(result.data['payload'], payload)
def test_handle_state_change(self, mock_mqtt_client): """ Testing handling of a state changed message""" mock_mqtt_client.return_value = None mqtt_gateway = MqttGateway(queue=self.test_queue, thread_event=self.default_event) mock_iot_message = IotMessage() mock_iot_message.event = 'state' mock_iot_message.device_id = 'test_device' mock_iot_message.payload = {'state': True} emtpy_queue(queue=self.test_queue) mqtt_gateway._handle_state_change(msg=mock_iot_message) result = self.test_queue.get() self.assertEqual(result.event, "device_state_changed") self.assertEqual(result.data['device_id'], mock_iot_message.device_id) self.assertEqual(result.data['event_type'], mock_iot_message.event) self.assertEqual(result.data['state'], True)
def test_on_message_invalid_payload(self, mock_mqtt_client): """ Testing handling on message e""" mock_mqtt_client.return_value = None mqtt_gateway = MqttGateway(queue=self.test_queue, thread_event=self.default_event) topic = 'iot/test_topic' data = {'device': 'dev_001', 'event_t': 'unknown_event', 'state': True} json_data = dumps(data) emtpy_queue(queue=self.test_queue) mqtt_gateway.on_message(topic=topic, payload=json_data) iot_traffic_event = self.test_queue.get() self.assertEqual(iot_traffic_event.event, "iot_traffic") self.assertEqual(iot_traffic_event.data['source'], "MqttGateway") self.assertEqual(iot_traffic_event.data['topic'], topic) self.assertEqual(iot_traffic_event.data['payload'], json_data) self.assertTrue(self.test_queue.empty())
def test_run_for_3_times_poll_interval(self, mock_fetch_host_data): """ Testing main loop, we leave the main loop after x time and count the messages placed on the queue""" data = { 'timestamp': datetime.now(), 'temperature': 12.147, 'cpu_load': 15.786 } mock_fetch_host_data.return_value = data interval = 2 mock_interval_property = PropertyMock(return_value=interval) mock_running_property = PropertyMock(return_value=True) type(self.health_monitor).update_time_sec = mock_interval_property type(self.health_monitor).running = mock_running_property emtpy_queue(queue=self.test_queue) self.health_monitor.start() sleep(interval * 3) mock_running_property = PropertyMock(return_value=False) type(self.health_monitor).running = mock_running_property result = self.test_queue.qsize() == 3 or self.test_queue.qsize() == 4 self.assertTrue(result)