def test_implementation_get_formatted_data_no_data(self): """ Test case to check the implementation of _format_data for empty metric data. :return: None """ # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=False) # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) # Getting the parent child relationship array from registered metric # formatted_data = self.aws._format_data(registered_metric) formatted_data = get_formatted_data(registered_metric, False) # Check two dicts are equal or not self.assertEqual(None, formatted_data, "Check implementation of get_formatted_data")
def test_implementation_get_formatted_data_without_enclose_metatadata( self): """ Test case to check the output given by get_formatted_data method without enclosed meta_data option. RegisteredEdgeSystem->RegisteredMetric :return: None """ # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=False) # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) # Get current timestamp timestamp = getUTCmillis() registered_metric.values.put((timestamp, 10)) # Expected output without enclosed metadata expected_output = { "metric_name": registered_metric.ref_entity.name, "metric_data": [{ "value": 10, "timestamp": timestamp }], "unit": "null" } # Getting the parent child relationship array from registered metric formatted_data = get_formatted_data(registered_metric, False) # Convert json string to dict for the comparision formatted_json_data = json.loads(formatted_data) # Check two dicts are equal or not self.assertEqual( validate_json(formatted_json_data) == validate_json( expected_output), True, "Check implementation of get_formatted_data")
def setUp(self): """ Setup all required parameters required during the tests :return: None """ # EdgeSystem name self.edge_system = Dell5KEdgeSystem("TestEdgeSystem") # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=True)
def run(self, registry): import copy from liota.dccs.aws_iot import AWSIoT from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import QoSDetails from liota.lib.utilities.identity import Identity from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Get values from configuration file config_path = registry.get("package_conf") config = {} execfile(config_path + '/sampleProp.conf', config) # Encapsulates Identity identity = Identity(root_ca_cert=config['broker_root_ca_cert'], username=None, password=None, cert_file=config['edge_system_cert_file'], key_file=config['edge_system_key_file']) # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request' # Create and pass custom MqttMessagingAttributes object to MqttDccComms to have custom topic self.aws_iot = AWSIoT(MqttDccComms( edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, userdata=config['userdata'], protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout']), enclose_metadata=True) # Register edge system (gateway) aws_iot_edge_system = self.aws_iot.register(edge_system) registry.register("aws_iot", self.aws_iot) registry.register("aws_iot_edge_system", aws_iot_edge_system)
class PackageClass(LiotaPackage): """ This package creates a AWSIoT DCC object and registers edge system on AWSIoT to acquire "registered edge system", i.e. aws_iot_edge_system. """ def run(self, registry): import copy from liota.dccs.aws_iot import AWSIoT from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import QoSDetails from liota.lib.utilities.identity import Identity from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Get values from configuration file config_path = registry.get("package_conf") config = read_user_config(config_path + '/sampleProp.conf') # Encapsulates Identity identity = Identity(root_ca_cert=config['broker_root_ca_cert'], username=None, password=None, cert_file=config['edge_system_cert_file'], key_file=config['edge_system_key_file']) # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request' # Create and pass custom MqttMessagingAttributes object to MqttDccComms to have custom topic self.aws_iot = AWSIoT(MqttDccComms(edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout']), enclose_metadata=True) # Register edge system (gateway) aws_iot_edge_system = self.aws_iot.register(edge_system) registry.register("aws_iot", self.aws_iot) registry.register("aws_iot_edge_system", aws_iot_edge_system) def clean_up(self): self.aws_iot.comms.client.disconnect()
tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # AWSIoT broker doesn't support session persistence. So, always use "clean_session=True" # Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request' aws = AWSIoT(MqttDccComms( edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, userdata=config['userdata'], protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout']), enclose_metadata=True) # Registering EdgeSystem reg_edge_system = aws.register(edge_system) # Creating CPU Metric cpu_utilization = Metric(name="CPUUtilization", unit=None, interval=10, aggregation_size=2, sampling_function=read_cpu_utilization)
cert_file=config['edge_system_cert_file'], key_file=config['edge_system_key_file']) # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # AWSIoT broker doesn't support session persistence. So, always use "clean_session=True" # Custom Publish Topic for an EdgeSystem mqtt_msg_attr = MqttMessagingAttributes(pub_topic=config['CustomPubTopic']) aws = AWSIoT(MqttDccComms(edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout'], mqtt_msg_attr=mqtt_msg_attr), enclose_metadata=False) # Registering EdgeSystem reg_edge_system = aws.register(edge_system) # Creating CPU Metric cpu_utilization = Metric( name="CPUUtilization", unit=None, interval=10, aggregation_size=2, sampling_function=read_cpu_utilization )
class PackageClass(LiotaPackage): """ This package creates a AWSIoT DCC object and registers edge system on AWSIoT to acquire "registered edge system", i.e. aws_iot_edge_system. """ def run(self, registry): """ The execution function of a liota package. Establishes connection with AWSIoT DCC using MqttDccComms :param registry: the instance of ResourceRegistryPerPackage of the package :return: """ import copy from liota.dccs.aws_iot import AWSIoT from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import QoSDetails from liota.lib.utilities.identity import Identity from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Get values from configuration file config_path = registry.get("package_conf") config = read_user_config(config_path + '/sampleProp.conf') # Encapsulates Identity identity = Identity(root_ca_cert=config['broker_root_ca_cert'], username=None, password=None, cert_file=config['edge_system_cert_file'], key_file=config['edge_system_key_file']) # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request' # Create and pass custom MqttMessagingAttributes object to MqttDccComms to have custom topic self.aws_iot = AWSIoT(MqttDccComms(edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout']), enclose_metadata=True) # Register edge system (gateway) aws_iot_edge_system = self.aws_iot.register(edge_system) registry.register("aws_iot", self.aws_iot) registry.register("aws_iot_edge_system", aws_iot_edge_system) def clean_up(self): """ The clean up function of a liota package. Disconnects from AWSIoT DCC. :return: """ self.aws_iot.comms.client.disconnect()
class DCCUtilityTest(unittest.TestCase): """ Unit test cases for DCC Utility global functions """ def setUp(self): """ Setup all required parameters required during the tests :return: None """ # EdgeSystem name self.edge_system = Dell5KEdgeSystem("TestEdgeSystem") # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=True) def tearDown(self): """ Clean up all parameters used in test cases :return: None """ self.edge_system = Dell5KEdgeSystem("TestEdgeSystem") self.aws = None def test_implementation_get_formatted_data_no_data(self): """ Test case to check the implementation of _format_data for empty metric data. :return: None """ # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=False) # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) # Getting the parent child relationship array from registered metric # formatted_data = self.aws._format_data(registered_metric) formatted_data = get_formatted_data(registered_metric, False) # Check two dicts are equal or not self.assertEqual(None, formatted_data, "Check implementation of get_formatted_data") def test_implementation_get_formatted_data_with_enclose_metadata(self): """ Test case to check the implementation of get_formatted_data method with enclose_metadata option of AWSIoT class. RegisteredEdgeSystem->RegisteredMetric :return: None """ # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) timestamp = getUTCmillis() registered_metric.values.put((timestamp, 10)) expected_output = { "edge_system_name": registered_entity.ref_entity.name, "metric_name": registered_metric.ref_entity.name, "metric_data": [{ "value": 10, "timestamp": timestamp }], "unit": "null" } # Getting the parent child relationship array from registered metric formatted_data = get_formatted_data(registered_metric, True) formatted_json_data = json.loads(formatted_data) # Check two dicts are equal or not self.assertEqual( validate_json(formatted_json_data) == validate_json( expected_output), True, "Check implementation of get_formatted_data") def test_implementation_get_formatted_data_without_enclose_metatadata( self): """ Test case to check the output given by get_formatted_data method without enclosed meta_data option. RegisteredEdgeSystem->RegisteredMetric :return: None """ # Mocking the DataCenterComponent init method with mock.patch("liota.dccs.aws_iot.DataCenterComponent.__init__" ) as mocked_dcc: self.aws = AWSIoT("Mocked DCC", enclose_metadata=False) # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) # Get current timestamp timestamp = getUTCmillis() registered_metric.values.put((timestamp, 10)) # Expected output without enclosed metadata expected_output = { "metric_name": registered_metric.ref_entity.name, "metric_data": [{ "value": 10, "timestamp": timestamp }], "unit": "null" } # Getting the parent child relationship array from registered metric formatted_data = get_formatted_data(registered_metric, False) # Convert json string to dict for the comparision formatted_json_data = json.loads(formatted_data) # Check two dicts are equal or not self.assertEqual( validate_json(formatted_json_data) == validate_json( expected_output), True, "Check implementation of get_formatted_data") def test_implementation_get_formatted_data_with_enclose_metadata_device( self): """ Test case to test the implementation of get_formatted_data method with enclose_metadata option. RegisteredEdgeSystem->RegisteredDevice->RegisteredMetric :return: None """ # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating Simulated Device test_sensor = SimulatedDevice("TestSensor") # Registering Device and creating Parent-Child relationship reg_test_sensor = self.aws.register(test_sensor) self.aws.create_relationship(registered_entity, reg_test_sensor) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=ureg.degC, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(reg_test_sensor, registered_metric) # Get current timestamp timestamp = getUTCmillis() registered_metric.values.put((timestamp, 10)) # Expected output without enclosed metadata expected_output = { "edge_system_name": registered_entity.ref_entity.name, "metric_name": registered_metric.ref_entity.name, "device_name": reg_test_sensor.ref_entity.name, "metric_data": [{ "value": 10, "timestamp": timestamp }], "unit": "null" } unit_tuple = parse_unit(ureg.degC) if unit_tuple[0] is None: # Base and Derived Units expected_output['unit'] = unit_tuple[1] else: # Prefixed or non-SI Units expected_output['unit'] = unit_tuple[0] + unit_tuple[1] # Getting the parent child relationship array from registered metric formatted_data = get_formatted_data(registered_metric, True) # Convert json string to dict for the comparision formatted_json_data = json.loads(formatted_data) # Check two dicts are equal or not self.assertEqual( validate_json(formatted_json_data) == validate_json( expected_output), True, "Check implementation of get_formatted_data") def test_implementation_get_formatted_data_with_enclose_metadata_device_exception_flow( self): """ Test case to test the implementation of get_formatted_data method with enclose_metadata option. RegisteredEdgeSystem->RegisteredDevice->RegisteredMetric :return: None """ # Mock the get_formatted_data with mock.patch("liota.lib.utilities.dcc_utility.parse_unit" ) as mocked_parse_unit: # Method to be invoked on calling the parse_unit function mocked_parse_unit.side_effect = raise_exception # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating Simulated Device test_sensor = SimulatedDevice("TestSensor") # Registering Device and creating Parent-Child relationship reg_test_sensor = self.aws.register(test_sensor) self.aws.create_relationship(registered_entity, reg_test_sensor) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=ureg.degC, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(reg_test_sensor, registered_metric) # Get current timestamp timestamp = getUTCmillis() registered_metric.values.put((timestamp, 10)) # Expected output without enclosed metadata expected_output = { "edge_system_name": registered_entity.ref_entity.name, "metric_name": registered_metric.ref_entity.name, "device_name": reg_test_sensor.ref_entity.name, "metric_data": [{ "value": 10, "timestamp": timestamp }], "unit": "null" } # Getting the parent child relationship array from registered metric formatted_data = get_formatted_data(registered_metric, True) # Convert json string to dict for the comparision formatted_json_data = json.loads(formatted_data) # Check two dicts are equal or not self.assertEqual( validate_json(formatted_json_data) == validate_json( expected_output), True, "Check implementation of get_formatted_data") def test_validation_get_entity_hierarchy(self): """ Test case to check the validation of _get_entity_hierarchy method for Metric object. :return: None """ # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) # Checking whether implementation raising the TypeError for invalid input with self.assertRaises(TypeError): get_entity_hierarchy(test_metric) def test_implementation_get_entity_hierarchy(self): """ Test case to check get_entity_entity_hierarchy() for RegisteredEdgeSystem->RegisteredMetric :return: None """ # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=None, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(registered_entity, registered_metric) # Getting the parent child relationship array from registered metric entity_hierarchy = get_entity_hierarchy(registered_metric) self.assertSequenceEqual( [ registered_entity.ref_entity.name, registered_metric.ref_entity.name ], entity_hierarchy, "Check the implementation of _get_entity_hierarchy for " "RegisteredEdgeSystem->RegisteredMetric") def test_implementation_get_entity_hierarchy_device_metric(self): """ Test case to check get_entity_entity_hierarchy() for RegisteredEdgeSystem->RegisteredDevice->RegisteredMetric :return: None """ # Register the edge registered_entity = self.aws.register(self.edge_system) # Creating Simulated Device test_sensor = SimulatedDevice("TestSensor") # Registering Device and creating Parent-Child relationship reg_test_sensor = self.aws.register(test_sensor) self.aws.create_relationship(registered_entity, reg_test_sensor) # Creating test Metric test_metric = Metric(name="Test_Metric", unit=ureg.degC, interval=10, aggregation_size=2, sampling_function=sampling_function) registered_metric = self.aws.register(test_metric) # Creating the parent-child relationship self.aws.create_relationship(reg_test_sensor, registered_metric) # Getting the parent child relationship array from registered metric entity_hierarchy = get_entity_hierarchy(registered_metric) self.assertSequenceEqual( [ registered_entity.ref_entity.name, reg_test_sensor.ref_entity.name, registered_metric.ref_entity.name ], entity_hierarchy, "Check the implementation of _get_entity_hierarchy for " "RegisteredEdgeSystem->RegisteredDevice->RegisteredMetric")