def addSensorAlert(self, nodeId: int, sensorId: int, state: int, dataJson: str, changeState: bool, hasLatestData: bool, dataType: int, sensorData: Any, logger: logging.Logger = None) -> bool: sensor_alert = SensorAlert() sensor_alert.nodeId = nodeId sensor_alert.sensorId = sensorId sensor_alert.state = state sensor_alert.changeState = changeState sensor_alert.hasLatestData = hasLatestData sensor_alert.dataType = dataType sensor_alert.sensorData = sensorData if dataJson == "": sensor_alert.hasOptionalData = False else: sensor_alert.hasOptionalData = True sensor_alert.optionalData = json.loads(dataJson) self._sensor_alerts.append(sensor_alert) return True
def add_sensor_alert(self, node_id: int, sensor_id: int, state: int, optional_data: Optional[Dict[str, Any]], change_state: bool, has_latest_data: bool, data_type: int, sensor_data: Any, logger: logging.Logger = None) -> bool: sensor_alert = SensorAlert() sensor_alert.nodeId = node_id sensor_alert.sensorId = sensor_id sensor_alert.state = state sensor_alert.changeState = change_state sensor_alert.hasLatestData = has_latest_data sensor_alert.dataType = data_type sensor_alert.sensorData = sensor_data sensor_alert.hasOptionalData = False sensor_alert.optionalData = optional_data if optional_data: sensor_alert.hasOptionalData = True self._sensor_alerts.append(sensor_alert) return True
def _create_instrumentation_dummy(self) -> Instrumentation: alert_level = AlertLevel() alert_level.level = 1 alert_level.name = "Instrumentation Alert Level" alert_level.triggerAlertTriggered = True alert_level.triggerAlertNormal = True alert_level.instrumentation_cmd = "dummy.py" alert_level.instrumentation_timeout = 10 sensor_alert = SensorAlert() sensor_alert.nodeId = 2 sensor_alert.sensorId = 3 sensor_alert.description = "Instrumentation Sensor Alert" sensor_alert.timeReceived = 1337 sensor_alert.alertDelay = 20 sensor_alert.state = 1 sensor_alert.hasOptionalData = True sensor_alert.optionalData = {"key1": "value1", "key2": "value2"} sensor_alert.changeState = False sensor_alert.alertLevels = [1] sensor_alert.triggeredAlertLevels = [1] sensor_alert.hasLatestData = False sensor_alert.dataType = SensorDataType.NONE sensor_alert.sensorData = None logger = logging.getLogger("Instrumentation Test Case") return Instrumentation(alert_level, sensor_alert, logger)
def test_process_output_invalid_sensor_alert_id(self): """ Tests an invalid sensor alert id output processing of an instrumentation script. """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) invalid_sensor_alert.sensorAlertId = sensor_alert.sensorAlertId + 1 # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(invalid_sensor_alert.convert_to_dict())) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)
def test_process_output_invalid_triggered_alert_levels(self): """ Tests an invalid triggered alert levels output processing of an instrumentation script. """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) invalid_sensor_alert.triggeredAlertLevels.append(22) # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(invalid_sensor_alert.convert_to_dict())) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)
def test_process_output_invalid_instrumentation_alert_level(self): """ Tests an invalid instrumentation alert level output processing of an instrumentation script. """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) arg = invalid_sensor_alert.convert_to_dict() arg["instrumentationAlertLevel"] = 22 # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(arg)) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)
def test_process_output_invalid_time_received(self): """ Tests an invalid time received output processing of an instrumentation script. """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) invalid_sensor_alert.timeReceived = sensor_alert.timeReceived + 1 arg = invalid_sensor_alert.convert_to_dict() arg["instrumentationAlertLevel"] = sensor_alert.alertLevels[0] # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(arg)) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)
def test_process_output_invalid_data_type(self): """ Tests an invalid data type output processing of an instrumentation script. """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) invalid_sensor_alert.dataType = SensorDataType.INT invalid_sensor_alert.sensorData = 1 arg = invalid_sensor_alert.convert_to_dict() arg["instrumentationAlertLevel"] = sensor_alert.alertLevels[0] # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(arg)) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)
def test_process_output_invalid_data_none(self): """ Tests an invalid data output processing of an instrumentation script (None expected, integer received). """ instrumentation = self._create_instrumentation_dummy() sensor_alert = instrumentation._sensor_alert sensor_alert.dataType = SensorDataType.NONE sensor_alert.sensorData = None invalid_sensor_alert = SensorAlert().deepcopy(sensor_alert) invalid_sensor_alert.sensorData = 1337 arg = invalid_sensor_alert.convert_to_dict() arg["instrumentationAlertLevel"] = sensor_alert.alertLevels[0] # Test instrumentation script output processing. was_success, new_sensor_alert = instrumentation._process_output( json.dumps(arg)) self.assertFalse(was_success) self.assertIsNone(new_sensor_alert)