def test_run_pass_in_json(stc):
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogInfo("begin test_run")

    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    common_data_path = stc_sys.GetApplicationCommonDataPath()

    # Create exposed properties for the ports
    project = stc_sys.GetObject('Project')
    left_tag = tag_utils.get_tag_object('Left_Port_Group')
    ep_cfg = ctor.Create('ExposedConfig', project)
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'LeftPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(left_tag, RelationType('ScriptableExposedProperty'))

    meth_name = "RFC2544THROUGHPUT_SAMPLE_BASIC"
    test_name = "test_run"

    # Clean up the fake installed methodology (if it exists)
    if os.path.exists(os.path.join(common_data_path,
                                   mgr_const.MM_TEST_METH_DIR,
                                   meth_name)):
        meth_man_utils.methodology_rmdir(meth_name)

    # Create a fake installed methodology
    home_dir = meth_man_utils.get_methodology_home_dir()
    assert home_dir is not None
    meth_dir = meth_man_utils.methodology_mkdir(meth_name)
    assert meth_dir is not None
    test_dir = meth_man_utils.methodology_test_case_mkdir(meth_name, test_name)
    assert test_dir is not None

    # Add a fake sequence file
    seq_file = os.path.join(meth_dir, mgr_const.MM_SEQUENCER_FILE_NAME)
    f = open(seq_file, "w")
    f.write("<?xml version=\"1.0\" encoding=\"windows-1252\"?>")
    f.close()

    # Add a fake TXML file
    meta_file = os.path.join(meth_dir, mgr_const.MM_META_FILE_NAME)
    f = open(meta_file, "w")
    data = UnitTestUtils.gen_test_info_header("unit test meth disp name",
                                              meth_name,
                                              "unit test meth test case",
                                              "")
    data = data + UnitTestUtils.UTU_FOOTER
    f.write(data)
    f.close()

    # Initialize the methodology manager by calling update
    cmd = ctor.CreateCommand(PKG +
                             ".UpdateTestMethodologyManagerCommand")
    cmd.Execute()
    cmd.MarkDelete()

    # meth_man = meth_man_utils.get_meth_manager()
    # assert meth_man
    # test_meth_obj_list = meth_man.GetObjects("StmMethodology")
    # assert len(test_meth_obj_list) > 0

    # test_meth = None
    # for test_meth_obj in test_meth_obj_list:
    #     act_meth_key = test_meth_obj.Get("MethodologyKey")
    #     plLogger.LogDebug("meth_name: " + test_meth_obj.Get("Name"))
    #     plLogger.LogDebug("meth_key: " + test_meth_obj.Get("MethodologyKey"))
    #     if act_meth_key == meth_name:
    #         test_meth = test_meth_obj
    #         break

    # Add MethodologyGroupCommand to the sequencer
    meth_group_cmd = ctor.Create(PKG + ".MethodologyGroupCommand", sequencer)
    sequencer.SetCollection("CommandList", [meth_group_cmd.GetObjectHandle()])
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json == ""

    cmd = ctor.Create(PKG + ".RunMethodologyTestCommand", sequencer)
    RunCmd.get_this_cmd = MagicMock(return_value=cmd)
    RunCmd.load_config = MagicMock()
    # TODO: set it up so we don't have to mock this function
    RunCmd.set_active_test_case = MagicMock()

    # Call run with all empty
    # res = RunCmd.run("", "", "", "", False)
    # assert not res
    # assert 'MethodologyJson does not conform to the schema' in cmd.Get("Status")
    # assert cmd.Get("OutputTestCaseKey") == ''
    # cmd.Set("Status", '')

    # Call run with invalid meth key
    # res = RunCmd.run("", "", "blah", "", False)
    # assert not res
    # assert 'MethodologyJson does not conform to the schema' in cmd.Get("Status")
    # assert cmd.Get("OutputTestCaseKey") == ''
    # cmd.Set("Status", '')

    # Call run with empty json
    # res = RunCmd.run("", "", meth_name, "", False)
    # assert not res
    # assert 'MethodologyJson does not conform to the schema' in cmd.Get("Status")
    # assert cmd.Get("OutputTestCaseKey") == ''
    # cmd.Set("Status", '')

    # Call run with invalid json
    # res = RunCmd.run("", "", meth_name, "invalid json", False)
    # assert not res
    # assert 'MethodologyJson does not conform to the schema' in cmd.Get("Status")
    # assert cmd.Get("OutputTestCaseKey") == ''
    # cmd.Set("Status", '')

    # Call the run function with valid meth key and json
    res = RunCmd.run("", "", meth_name, get_sample_2544_json_basic(), False)
    assert res
    assert cmd.Get("Status") == ''
    assert cmd.Get("OutputTestCaseKey") == meth_name + '-1'

    # The KeyValueJson should be populated after running the command
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json != ""
    err_str, key_value_dict = json_utils.load_json(key_value_json)
    assert err_str == ""
    assert key_value_dict is not None

    assert len(key_value_dict.items()) == 1
    assert key_value_dict["AddressStartValue"] == "1.1.1.1"

    # Clean up the fake installed methodology
    if os.path.exists(os.path.join(common_data_path,
                                   mgr_const.MM_TEST_METH_DIR,
                                   meth_name)):
        meth_man_utils.methodology_rmdir(meth_name)
def test_run(stc):
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogInfo("begin test_run")

    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    common_data_path = stc_sys.GetApplicationCommonDataPath()

    # Create exposed properties for the ports
    project = stc_sys.GetObject('Project')
    left_tag = tag_utils.get_tag_object('Left_Port_Group')
    right_tag = tag_utils.get_tag_object('Right_Port_Group')
    ep_cfg = ctor.Create('ExposedConfig', project)
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'LeftPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(left_tag, RelationType('ScriptableExposedProperty'))
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'RightPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(right_tag, RelationType('ScriptableExposedProperty'))

    meth_name = "test_RunMethodologyTestCommand_test_run"
    test_name = "test_run"

    # Clean up the fake installed methodology (if it exists)
    if os.path.exists(os.path.join(common_data_path,
                                   mgr_const.MM_TEST_METH_DIR,
                                   meth_name)):
        meth_man_utils.methodology_rmdir(meth_name)

    # Create a fake installed methodology
    home_dir = meth_man_utils.get_methodology_home_dir()
    assert home_dir is not None
    meth_dir = meth_man_utils.methodology_mkdir(meth_name)
    assert meth_dir is not None
    test_dir = meth_man_utils.methodology_test_case_mkdir(meth_name, test_name)
    assert test_dir is not None

    cmd = ctor.Create(PKG + ".RunMethodologyTestCommand", sequencer)
    RunCmd.get_this_cmd = MagicMock(return_value=cmd)
    RunCmd.load_config = MagicMock()
    # TODO: set it up so we don't have to mock this function
    RunCmd.set_active_test_case = MagicMock()

    # Create a valid JSON file
    json_content = get_sample_2544_json()
    meta_json_file = os.path.join(test_dir, mgr_const.MM_META_JSON_FILE_NAME)
    f = open(meta_json_file, "w")
    f.write(json_content)
    f.close()

    meth_man = stc_sys.GetObject("StmMethodologyManager")
    if meth_man is None:
        meth_man = ctor.Create("StmMethodologyManager", stc_sys)
    assert meth_man
    test_meth = ctor.Create("StmMethodology", meth_man)
    test_meth.Set("MethodologyKey", meth_name)
    test_case = ctor.Create("StmTestCase", test_meth)
    assert test_case is not None
    assert test_case.IsTypeOf("StmTestCase")
    test_case.Set("Path", test_dir)
    test_case.Set("TestCaseKey", test_name)

    # Add MethodologyGroupCommand to the sequencer
    meth_group_cmd = ctor.Create(PKG + ".MethodologyGroupCommand", sequencer)
    sequencer.SetCollection("CommandList", [meth_group_cmd.GetObjectHandle()])
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json == ""

    # Call the run function with invalid StmTestCase handle
    res = RunCmd.run("", test_meth.GetObjectHandle(), "", "", False)
    assert not res
    assert 'Was unable to find StmTestCase with handle' in cmd.Get("Status")
    cmd.Set("Status", '')

    # Call the run function with missing proc function script file
    res = RunCmd.run("", test_case.GetObjectHandle(), "", "", False)
    assert not res
    assert cmd.Get("Status") == 'Failed to find script: RunMethodologyTestCommandTestScript.py'
    cmd.Set("Status", '')

    # Create the proc function script file
    err_str, meta = json_utils.load_json(json_content)
    assert err_str == ""
    script_filename = meta["processing_functions"][0]["script_filename"]
    script_full_path = write_testscript(script_filename)
    assert script_full_path is not None

    # Call the run function with valid StmTestCaseHandle
    res = RunCmd.run("", test_case.GetObjectHandle(), "", "", False)
    assert res
    assert cmd.Get("Status") == ''

    # The KeyValueJson should be populated after running the command
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json != ""
    err_str, key_value_dict = json_utils.load_json(key_value_json)
    assert err_str == ""
    assert key_value_dict is not None

    assert len(key_value_dict.items()) == 8
    assert key_value_dict["CommandAddressStartValue"] == "198.18.1.2"
    assert key_value_dict["CommandAddressStepValue"] == "0.0.1.0"
    assert key_value_dict["TrafficDuration"] == "60"
    vlist = key_value_dict["ObjectIteratorCommand.ValueList"]
    assert type(vlist) is list
    sitems = set(vlist) & set(["64", "128", "256", "512", "1024", "1280", "1518"])
    assert len(sitems) == 7
    assert key_value_dict["cmd1.bgp.count"] == "3"

    # Clear out KeyValueJson in the meth group command
    meth_group_cmd.Set("KeyValueJson", "")
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json == ""

    # Call the run function with invalid test case key
    res = RunCmd.run("InvalidTestCaseKey", 0, "", "", False)
    assert not res
    assert cmd.Get("Status") == 'Test case with key InvalidTestCaseKey not found'
    cmd.Set("Status", '')

    # Call the run function with valid test case key
    res = RunCmd.run(test_name, 0, "", "", False)
    assert res
    assert cmd.Get("Status") == ''

    # The KeyValueJson should be populated after running the command
    key_value_json = meth_group_cmd.Get("KeyValueJson")
    assert key_value_json != ""
    err_str, key_value_dict = json_utils.load_json(key_value_json)
    assert err_str == ""
    assert key_value_dict is not None
    assert len(key_value_dict.items()) == 8

    assert remove_testscript(script_full_path)

    # Clean up the fake installed methodology
    if os.path.exists(os.path.join(common_data_path,
                                   mgr_const.MM_TEST_METH_DIR,
                                   meth_name)):
        meth_man_utils.methodology_rmdir(meth_name)