def test_gen_range_modifier_json():
    # Minimal
    mi = ConfModCmd.gen_range_modifier_json("EmulatedDevice", "DeviceCount",
                                            ["10", "20", "30"], "40")
    assert mi != ""
    err_str, act_dict = json_utils.load_json(mi)
    assert err_str == ""
    pv_dict = {}
    pv_dict["start"] = ["10", "20", "30"]
    pv_dict["step"] = "40"
    exp_dict = {}
    exp_dict["objectName"] = "EmulatedDevice"
    exp_dict["propertyName"] = "DeviceCount"
    exp_dict["modifierType"] = "RANGE"
    exp_dict["propertyValueDict"] = pv_dict
    assert act_dict == exp_dict

    # Optional repeat, recycle, targetObjectStep, and reset
    mi = ConfModCmd.gen_range_modifier_json("EmulatedDevice", "DeviceCount",
                                            "10", "20", repeat=30,
                                            recycle=40, target_step="50",
                                            reset=True)
    assert mi != ""
    # res = json_utils.validate_json(
    #     mi, proc_func.get_range_modifier_json_schema())
    # assert res == ""
    err_str, act_dict = json_utils.load_json(mi)
    assert err_str == ""
    pv_dict = {}
    pv_dict["start"] = "10"
    pv_dict["step"] = "20"
    pv_dict["repeat"] = 30
    pv_dict["recycle"] = 40
    pv_dict["targetObjectStep"] = "50"
    pv_dict["resetOnNewTargetObject"] = True
    exp_dict = {}
    exp_dict["objectName"] = "EmulatedDevice"
    exp_dict["propertyName"] = "DeviceCount"
    exp_dict["modifierType"] = "RANGE"
    exp_dict["propertyValueDict"] = pv_dict
    assert act_dict == exp_dict
def test_check_obj_and_prop_names(stc):
    # No object or property specified
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "", "")
    assert err_str == ""
    assert obj == ""
    assert prop == ""

    # Object specified, no property specified
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "EmulatedDevice", "")
    assert err_str == ""
    assert obj == "EmulatedDevice"
    assert prop == ""

    # Object and property specified
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "EmulatedDevice", "DeviceCount")
    assert err_str == ""
    assert obj == "EmulatedDevice"
    assert prop == "DeviceCount"

    # Invalid object specified
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "InvalidObject", "")
    assert "Invalid object with ObjectName: InvalidObject" in err_str
    assert obj == "InvalidObject"
    assert prop == ""

    # Valid object, invalid property specified
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "EmulatedDevice", "InvalidProperty")
    assert "does not have a property called InvalidProperty" in err_str
    assert obj == "EmulatedDevice"
    assert prop == "InvalidProperty"

    # No object specified (no error caught)
    err_str, obj, prop = ConfModCmd.check_obj_and_prop_names(
        "", "InvalidProperty")
    assert err_str == ""
    assert obj == ""
    assert prop == "InvalidProperty"
def test_match_modifier_to_obj_and_prop_names(stc):
    # Create an StmPropertyModifier element
    root = etree.fromstring("<StmPropertyModifier ModifierInfo=\"\" />")
    assert root is not None
    pv_dict = {}
    pv_dict["start"] = "1.2.3.4"
    pv_dict["step"] = "0.0.0.1"
    pv_dict["repeat"] = 4
    pv_dict["recycle"] = 6
    pv_dict["targetObjectStep"] = "0.1.0.0"
    pv_dict["resetOnNewTargetObject"] = False
    m_dict = {}
    m_dict["objectName"] = "Ipv4If"
    m_dict["propertyName"] = "Address"
    m_dict["modifierType"] = "RANGE"
    m_dict["propertyValueDict"] = pv_dict
    root.set("ModifierInfo", json.dumps(m_dict))

    # Matches object and property
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "Ipv4If", "Address")
    assert err_str == ""
    assert res == root

    # Matches object, no property specified
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "Ipv4If", "")
    assert err_str == ""
    assert res == root

    # Matches object, doesn't match property
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "Ipv4If", "AddrList")
    assert err_str == ""
    assert res is None

    # No object, matches property
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "", "Address")
    assert err_str == ""
    assert res == root

    # No object, doesn't match property
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "", "AddrList")
    assert err_str == ""
    assert res is None

    # No object or property specified
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "", "")
    assert err_str == ""
    assert res == root

    # Missing node
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        None, "EmulatedDevice", "DeviceCount")
    assert "Invalid ElementTree element" in err_str
    assert res is None

    # Missing ModifierInfo
    root = etree.fromstring("<StmPropertyModifier />")
    assert root is not None
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "EmulatedDevice", "DeviceCount")
    assert "Missing ModifierInfo attribute" in err_str
    assert res is None

    # Invalid JSON/doesn't match schema
    root = etree.fromstring("<StmPropertyModifier ModifierInfo=\"{}\" />")
    assert root is not None
    err_str, res = ConfModCmd.match_modifier_to_obj_and_prop_names(
        root, "EmulatedDevice", "DeviceCount")
    assert "Failed to validate ModifierInfo JSON" in err_str
    assert res is None
def test_reset(stc):
    res = ConfModCmd.reset()
    assert res
def test_find_matching_modifiers(stc):
    # Create some StmPropertyModifier elements

    # Ipv4If - Address
    ipv4_addr = etree.fromstring("<StmPropertyModifier ModifierInfo=\"\" />")
    assert ipv4_addr is not None
    pv_dict = {}
    pv_dict["start"] = "1.2.3.4"
    pv_dict["step"] = "0.0.0.1"
    m_dict = {}
    m_dict["objectName"] = "Ipv4If"
    m_dict["propertyName"] = "Address"
    m_dict["modifierType"] = "RANGE"
    m_dict["propertyValueDict"] = pv_dict
    ipv4_addr.set("ModifierInfo", json.dumps(m_dict))

    # Ipv4If - AddrList
    ipv4_addr_list = etree.fromstring(
        "<StmPropertyModifier ModifierInfo=\"\" />")
    assert ipv4_addr_list is not None
    pv_dict = {}
    pv_dict["start"] = ["1.2.3.4"]
    pv_dict["step"] = ["0.0.0.1"]
    m_dict = {}
    m_dict["objectName"] = "Ipv4If"
    m_dict["propertyName"] = "AddrList"
    m_dict["modifierType"] = "RANGE"
    m_dict["propertyValueDict"] = pv_dict
    ipv4_addr_list.set("ModifierInfo", json.dumps(m_dict))

    # EmulatedDevice - DeviceCount
    dev_count = etree.fromstring(
        "<StmPropertyModifier ModifierInfo=\"\" />")
    assert dev_count is not None
    pv_dict = {}
    pv_dict["start"] = "100"
    pv_dict["step"] = "30"
    m_dict = {}
    m_dict["objectName"] = "EmulatedDevice"
    m_dict["propertyName"] = "DeviceCount"
    m_dict["modifierType"] = "RANGE"
    m_dict["propertyValueDict"] = pv_dict
    dev_count.set("ModifierInfo", json.dumps(m_dict))

    mod_list = [ipv4_addr, ipv4_addr_list, dev_count]

    # Test with nothing in the list
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        [], "EmulatedDevice", "DeviceCount")
    assert err_str == ""
    assert match_list == []

    # Test with match on nothing
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        mod_list, "", "")
    assert err_str == ""
    assert len(match_list) == 3
    assert ipv4_addr in match_list
    assert ipv4_addr_list in match_list
    assert dev_count in match_list

    # Test with match on just EmulatedDevice (no property)
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        mod_list, "EmulatedDevice", "")
    assert err_str == ""
    assert len(match_list) == 1
    assert dev_count in match_list

    # Test with match on DeviceCount (no object)
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        mod_list, "EmulatedDevice", "")
    assert err_str == ""
    assert len(match_list) == 1
    assert dev_count in match_list

    # Test with match on Ipv4If (no object)
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        mod_list, "Ipv4If", "")
    assert err_str == ""
    assert len(match_list) == 2
    assert ipv4_addr in match_list
    assert ipv4_addr_list in match_list

    # Test with match on BgpRouterConfig and AsNum
    err_str, match_list = ConfModCmd.find_matching_modifiers(
        mod_list, "BgpRouterConfig", "AsNum")
    assert err_str == ""
    assert match_list == []
def test_validate(stc):
    res = ConfModCmd.validate("", "", "", "", "", "", "", "", "", "", "", "")
    assert res == ""