def test_find_param_wildcard_searching(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_param_list1 = my_db.find_params("Device.Controller.*.Enable") found_param_list2 = my_db.find_params( "Device.Controller.*.STOMP.Username") found_param_list3 = my_db.find_params( "Device.Services.HomeAutomation.1.Camera.1.Pic.*.URL") found_param_list4 = my_db.find_params( "Device.Services.HomeAutomation.1.Camera.*.Pic.*.URL") assert len(found_param_list1) == 2 assert "Device.Controller.1.Enable" in found_param_list1 assert "Device.Controller.2.Enable" in found_param_list1 assert len(found_param_list2) == 2 assert "Device.Controller.1.STOMP.Username" in found_param_list2 assert "Device.Controller.2.STOMP.Username" in found_param_list2 assert len(found_param_list3) == 2 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.9.URL" in found_param_list3 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.10.URL" in found_param_list3 assert len(found_param_list4) == 5 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.9.URL" in found_param_list4 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.10.URL" in found_param_list4 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.10.URL" in found_param_list4 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.90.URL" in found_param_list4 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.100.URL" in found_param_list4
def test_find_impl_objects_static_table(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_objects_list1 = my_db.find_impl_objects("Device.Controller.{i}.", False) found_objects_list2 = my_db.find_impl_objects("Device.Subscription.", False) found_objects_list3 = my_db.find_impl_objects("Device.Services.", False) assert len(found_objects_list1) == 2 assert "Device.Controller.{i}.CoAP." in found_objects_list1 assert "Device.Controller.{i}.STOMP." in found_objects_list1 assert len(found_objects_list2) == 1 assert "Device.Subscription.{i}." in found_objects_list2 assert len(found_objects_list3) == 3 # assert "Device.Services.HomeAutomation." in found_objects_list3 assert "Device.Services.HomeAutomation.{i}." in found_objects_list3 # assert "Device.Services.HomeAutomation.{i}.Camera." in found_objects_list3 assert "Device.Services.HomeAutomation.{i}.Camera.{i}." in found_objects_list3 # assert "Device.Services.HomeAutomation.{i}.Camera..{i}.Pic." in found_objects_list3 assert "Device.Services.HomeAutomation.{i}.Camera.{i}.Pic.{i}." in found_objects_list3
def test_delete_instance(): file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): with mock.patch.object(agent_db.Database, '_save') as save_mock: my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") my_db.delete("Device.Services.HomeAutomation.1.Camera.2.Pic.100.") save_mock.assert_called_once_with()
def test_find_objects_instance_number_addressing_no_instance(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_instances_list1 = my_db.find_objects( "Device.Services.HomeAutomation.1.Camera.3.") assert len(found_instances_list1) == 0
def test_update_param(): file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): with mock.patch.object(agent_db.Database, '_save') as save_mock: my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") my_db.update("Device.LocalAgent.PeriodicInterval", 60) save_mock.assert_called_once_with()
def test_find_objects_static_path(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_objects_list1 = my_db.find_objects("Device.LocalAgent.") assert len(found_objects_list1) == 1 assert "Device.LocalAgent." in found_objects_list1
def test_find_impl_objects_invalid_obj(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") try: my_db.find_impl_objects("Device.NoSuchObj.", False) assert False, "NoSuchPathError Expected" except agent_db.NoSuchPathError: pass
def test_get_no_such_path(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") try: my_db.get("Device.NoSuchParam") assert False, "NoSuchPathError Expected" except agent_db.NoSuchPathError: pass
def test_find_impl_objects_full_path_next_level(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") try: my_db.find_impl_objects("Device.ControllerNumberOfEntries", True) assert False, "NoSuchPathError Expected" except agent_db.NoSuchPathError: pass
def test_find_objects_wildcard_searching_and_instance_number(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_instances_list1 = my_db.find_objects( "Device.Services.HomeAutomation.1.Camera.*.Pic.10.") assert len(found_instances_list1) == 2 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.10." in found_instances_list1 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.10." in found_instances_list1
def test_get_ip_addr(): ip_mock = mock.Mock() ip_mock.return_value = "10.99.12.8" file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): with mock.patch("agent.utils.IPAddr.get_ip_addr", ip_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") get_value1 = my_db.get("Device.LocalAgent.X_ARRIS-COM_IPAddr") assert get_value1 == "10.99.12.8"
def test_get_currrent_local_time(): time_mock = mock.Mock() time_mock.now.return_value = datetime.datetime(2016, 9, 20, 20, 15, 10) file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): with mock.patch("datetime.datetime", time_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") get_value1 = my_db.get("Device.Time.CurrentLocalTime") assert get_value1 == "2016-09-20T20:15:10-06:00"
def test_is_param_writable_static_path(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") is_param_writable1 = my_db.is_param_writable( "Device.ControllerNumberOfEntries") is_param_writable2 = my_db.is_param_writable( "Device.LocalAgent.PeriodicInterval") assert not is_param_writable1, "Attempted a 'readOnly' Parameter" assert is_param_writable2, "Attempted a 'readWrite' Parameter"
def test_delete_instance_no_such_path(): file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") my_db._save = mock.MagicMock() try: my_db.delete("Device.NoSuchPath.1.") assert False, "NoSuchPathError Expected" except agent_db.NoSuchPathError: pass
def test_get_affected_paths_for_get_three_layers(): endpoint_id = "ENDPOINT-ID" my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] partial_path = "Device.Services.HomeAutomation.*.Camera.*.Pic.*." with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") req_handler = request_handler.UspRequestHandler(endpoint_id, my_db) affected_path_list = req_handler._get_affected_paths_for_get( partial_path) assert len(affected_path_list) == 5, "expecting 5, found " + str( len(affected_path_list))
def __init__(self, dm_file, db_file, net_intf, cfg_file_name, debug=False): """Initialize the Abstract Agent""" self._service_map = {} self._periodic_handler_list = [] self._boot_notif_sender_list = [] self._cfg_file_name = cfg_file_name self._value_change_notif_poller = None self._logger = logging.getLogger(self.__class__.__name__) self._db = agent_db.Database(dm_file, db_file, net_intf) self._endpoint_id = self._db.get("Device.LocalAgent.EndpointID") self._load_services() self._msg_handler = request_handler.UspRequestHandler( self._endpoint_id, self._db, self._service_map, debug)
def test_get_affected_paths_for_get_partial_path(): endpoint_id = "ENDPOINT-ID" my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] partial_path = "Device.Subscription." with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") req_handler = request_handler.UspRequestHandler(endpoint_id, my_db) affected_path_list = req_handler._get_affected_paths_for_get( partial_path) assert len(affected_path_list) == 1, "expecting 1, found " + str( len(affected_path_list)) assert affected_path_list[0] == "Device.Subscription."
def test_find_param_static_path(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_param_list1 = my_db.find_params( "Device.ControllerNumberOfEntries") found_param_list2 = my_db.find_params( "Device.LocalAgent.SupportedProtocols") assert len(found_param_list1) == 1 assert "Device.ControllerNumberOfEntries" in found_param_list1 assert len(found_param_list2) == 1 assert "Device.LocalAgent.SupportedProtocols" in found_param_list2
def test_get_normal_param(): file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") get_value1 = my_db.get("Device.LocalAgent.Manufacturer") get_value2 = my_db.get("Device.Controller.1.Protocol") get_value3 = my_db.get("Device.Subscription.3.ID") get_value4 = my_db.get( "Device.Services.HomeAutomation.1.Camera.2.MaxNumberOfPics") assert get_value1 == "ARRIS" assert get_value2 == "STOMP" assert get_value3 == "sub-boot-coap" assert get_value4 == 30
def test_find_impl_objects_wildcard_searching_next_level(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_objects_list1 = my_db.find_impl_objects("Device.Controller.*.", True) found_objects_list2 = my_db.find_impl_objects( "Device.Services.HomeAutomation.*.", True) assert len(found_objects_list1) == 2 assert "Device.Controller.{i}.CoAP." in found_objects_list1 assert "Device.Controller.{i}.STOMP." in found_objects_list1 assert len(found_objects_list2) == 1 assert "Device.Services.HomeAutomation.{i}.Camera." in found_objects_list2
def test_find_instances_static_table(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_instances_list1 = my_db.find_instances("Device.Controller.") found_instances_list2 = my_db.find_instances("Device.Subscription.") assert len(found_instances_list1) == 2 assert "Device.Controller.1." in found_instances_list1 assert "Device.Controller.2." in found_instances_list1 assert len(found_instances_list2) == 4 assert "Device.Subscription.1." in found_instances_list2 assert "Device.Subscription.2." in found_instances_list2 assert "Device.Subscription.3." in found_instances_list2 assert "Device.Subscription.4." in found_instances_list2
def test_get_num_entries(): file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") get_value1 = my_db.get("Device.ControllerNumberOfEntries") get_value2 = my_db.get("Device.SubscriptionNumberOfEntries") get_value3 = my_db.get( "Device.Services.HomeAutomation.1.CameraNumberOfEntries") get_value4 = my_db.get( "Device.Services.HomeAutomation.1.Camera.2.PicNumberOfEntries") assert get_value1 == 2 assert get_value2 == 4 assert get_value3 == 2 assert get_value4 == 3
def test_is_param_writable_wildcard_searching(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") is_param_writable1 = my_db.is_param_writable( "Device.Controller.*.Enable") is_param_writable2 = my_db.is_param_writable( "Device.Controller.*.STOMP.Username") is_param_writable3 = my_db.is_param_writable( "Device.Services.HomeAutomation.1.Camera.1.Pic.*.URL") is_param_writable4 = my_db.is_param_writable( "Device.Services.HomeAutomation.1.Camera.*.Pic.*.URL") assert is_param_writable1, "Attempted 'readWrite' with wild-card for instance number" assert is_param_writable2, "Attempted 'readWrite' with wild-card for instance number and sub-object" assert not is_param_writable3, "Attempted 'readOnly' with multiple instance numbers and single wild-card" assert not is_param_writable4, "Attempted 'readOnly' with single instance number and multiple wild-cards"
def test_get_uptime(): time_mock = start_time_mock = mock.Mock() current_time_mock = mock.Mock() start_time_mock.return_value = time.mktime( datetime.datetime(2016, 9, 20, 20, 15, 10).timetuple()) current_time_mock.return_value = time.mktime( datetime.datetime(2016, 9, 21, 1, 16, 15).timetuple()) time_mock.side_effect = [ start_time_mock.return_value, current_time_mock.return_value ] file_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) file_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", file_mock): with mock.patch("time.time", time_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") get_value1 = my_db.get("Device.LocalAgent.UpTime") assert get_value1 == 18065
def test_find_param_partial_path(): my_mock = dm_mock = mock.mock_open(read_data=get_dm_file_contents()) db_mock = mock.mock_open(read_data=get_db_file_contents()) my_mock.side_effect = [dm_mock.return_value, db_mock.return_value] with mock.patch("builtins.open", my_mock): my_db = agent_db.Database("mock_dm.json", "mock_db.json", "intf") found_param_list1 = my_db.find_params("Device.LocalAgent.") found_param_list2 = my_db.find_params("Device.Services.") assert len(found_param_list1) == 13 assert "Device.LocalAgent.Manufacturer" in found_param_list1 assert "Device.LocalAgent.ManufacturerOUI" in found_param_list1 assert "Device.LocalAgent.ProductClass" in found_param_list1 assert "Device.LocalAgent.SerialNumber" in found_param_list1 assert "Device.LocalAgent.EndpointID" in found_param_list1 assert "Device.LocalAgent.ModelName" in found_param_list1 assert "Device.LocalAgent.HardwareVersion" in found_param_list1 assert "Device.LocalAgent.SoftwareVersion" in found_param_list1 assert "Device.LocalAgent.PeriodicInterval" in found_param_list1 assert "Device.LocalAgent.ProvisioningCode" in found_param_list1 assert "Device.LocalAgent.SupportedProtocols" in found_param_list1 assert "Device.LocalAgent.UpTime" in found_param_list1 assert "Device.LocalAgent.X_ARRIS-COM_IPAddr" in found_param_list1 assert len(found_param_list2) == 11 assert "Device.Services.HomeAutomationNumberOfEntries" in found_param_list2 assert "Device.Services.HomeAutomation.1.CameraNumberOfEntries" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.1.MaxNumberOfPics" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.1.PicNumberOfEntries" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.9.URL" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.1.Pic.10.URL" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.2.MaxNumberOfPics" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.2.PicNumberOfEntries" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.10.URL" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.90.URL" in found_param_list2 assert "Device.Services.HomeAutomation.1.Camera.2.Pic.100.URL" in found_param_list2