local M = {}

function M.mock_json_data()
    --
    -- this testcase is to verify if the mock is trimming double quotes when json object is being returned as response
    --

    -- setting mock
    local test_utils = require("test_utils")
    local value = "NEXT_COMMAND_REPLY HERAMOCK_OBJECT_MOCK_META CORRELATION_ID_START_HERA_MOCK 16:3 CORRELATION_ID,3:3 1,3:3 8,3:3 0,3:3 0,CORRELATION_ID_END_HERA_MOCK DS_CODE_START_HERA_MOCK 9:3 DS_CODE,3:3 1,3:3 8,3:3 0,3:3 0,DS_CODE_END_HERA_MOCK DS_TXN_ID_START_HERA_MOCK 11:3 DS_TXN_ID,3:3 1,3:3 8,3:3 0,3:3 0,DS_TXN_ID_END_HERA_MOCK ERROR_CODE_START_HERA_MOCK 12:3 ERROR_CODE,3:3 1,3:3 8,3:3 0,3:3 0,ERROR_CODE_END_HERA_MOCK ERROR_COMPONENT_START_HERA_MOCK 17:3 ERROR_COMPONENT,3:3 1,3:3 8,3:3 0,3:3 0,ERROR_COMPONENT_END_HERA_MOCK ERROR_DESCRIPTION_START_HERA_MOCK 19:3 ERROR_DESCRIPTION,3:3 1,3:3 8,3:3 0,3:3 0,ERROR_DESCRIPTION_END_HERA_MOCK ERROR_DETAIL_START_HERA_MOCK 14:3 ERROR_DETAIL,3:3 1,3:3 8,3:3 0,3:3 0,ERROR_DETAIL_END_HERA_MOCK ERROR_MSG_TYPE_START_HERA_MOCK 16:3 ERROR_MSG_TYPE,3:3 1,3:3 8,3:3 0,3:3 0,ERROR_MSG_TYPE_END_HERA_MOCK EXT_SNO_START_HERA_MOCK 9:3 EXT_SNO,3:3 1,3:3 8,3:3 0,3:3 0,EXT_SNO_END_HERA_MOCK INT_SNO_START_HERA_MOCK 9:3 INT_SNO,3:3 1,3:3 8,3:3 0,3:3 0,INT_SNO_END_HERA_MOCK PREP_TXN_INFO_1_START_HERA_MOCK 17:3 PREP_TXN_INFO_1,3:3 1,3:3 8,3:3 0,3:3 0,PREP_TXN_INFO_1_END_HERA_MOCK PREP_TXN_TUID_START_HERA_MOCK 15:3 PREP_TXN_TUID,3:3 1,3:3 8,3:3 0,3:3 0,PREP_TXN_TUID_END_HERA_MOCK STATUS_START_HERA_MOCK 8:3 STATUS,3:3 1,3:3 8,3:3 0,3:3 0,STATUS_END_HERA_MOCK THREEDS_SERVER_TXN_ID_START_HERA_MOCK 23:3 THREEDS_SERVER_TXN_ID,3:3 1,3:3 8,3:3 0,3:3 0,THREEDS_SERVER_TXN_ID_END_HERA_MOCK TIME_CREATED_MS_START_HERA_MOCK 17:3 TIME_CREATED_MS,3:3 2,3:3 0,3:3 0,5:3 129,TIME_CREATED_MS_END_HERA_MOCK TIME_UPDATED_MS_START_HERA_MOCK 17:3 TIME_UPDATED_MS,3:3 2,3:3 0,3:3 0,5:3 129,TIME_UPDATED_MS_END_HERA_MOCK  NEXT_NEWSTRING CORRELATION_ID_START_HERA_MOCK 5ea50d6610bbd CORRELATION_ID_END_HERA_MOCK DS_CODE_START_HERA_MOCK FIME DS_CODE_END_HERA_MOCK DS_TXN_ID_START_HERA_MOCK a0549f7e-18fe-4ba6-a1ed-f3b94c52033a DS_TXN_ID_END_HERA_MOCK ERROR_CODE_START_HERA_MOCK 0 ERROR_CODE_END_HERA_MOCK ERROR_COMPONENT_START_HERA_MOCK  ERROR_COMPONENT_END_HERA_MOCK ERROR_DESCRIPTION_START_HERA_MOCK  ERROR_DESCRIPTION_END_HERA_MOCK ERROR_DETAIL_START_HERA_MOCK  ERROR_DETAIL_END_HERA_MOCK ERROR_MSG_TYPE_START_HERA_MOCK  ERROR_MSG_TYPE_END_HERA_MOCK EXT_SNO_START_HERA_MOCK FELQxqoy7jTBO6kJWf7S EXT_SNO_END_HERA_MOCK INT_SNO_START_HERA_MOCK 05ffb342-460a-4ce4-b513-0488f4e59108 INT_SNO_END_HERA_MOCK PREP_TXN_INFO_1_START_HERA_MOCK {\"dataProcessingRetryable\":true,\"prevSuccessRunTuid\":\"\",\"batchMode\":\"REFRESH_PREP_DATA\",\"refresh\":true} PREP_TXN_INFO_1_END_HERA_MOCK PREP_TXN_TUID_START_HERA_MOCK 11ED-A2C0-8A8389C4-A798-2E5D0DF0B0D8 PREP_TXN_TUID_END_HERA_MOCK STATUS_START_HERA_MOCK SUCCESS STATUS_END_HERA_MOCK THREEDS_SERVER_TXN_ID_START_HERA_MOCK 8c948ffa-be8e-4224-ba1a-ce69277624f3 THREEDS_SERVER_TXN_ID_END_HERA_MOCK TIME_CREATED_MS_START_HERA_MOCK 1675318335967 TIME_CREATED_MS_END_HERA_MOCK TIME_UPDATED_MS_START_HERA_MOCK 1675318339170 TIME_UPDATED_MS_END_HERA_MOCK  NEXT_NEWSTRING";

    test_utils.add_mock("ThreedsPrepTxnData.GROUP_BY_STATUS", value)

    local status, up_sock, sock_id = test_utils.make_connection()
    if not status then
        return "FAILED"
    end

    if status then
        local request = "585:0 25:2006 CorrId=6867b798f9bfe,513:25 SELECT /* ThreedsPrepTxnData.GROUP_BY_STATUS */ DS_CODE, EXT_SNO, INT_SNO, T1.STATUS, T1.PREP_TXN_TUID, THREEDS_SERVER_TXN_ID, DS_TXN_ID, CORRELATION_ID, ERROR_CODE, ERROR_COMPONENT, ERROR_DESCRIPTION, ERROR_DETAIL, ERROR_MSG_TYPE, TIME_CREATED_MS, TIME_UPDATED_MS, PREP_TXN_INFO_1 FROM THREEDS_PREP_TXN_DATA T1 JOIN (SELECT STATUS, MAX(PREP_TXN_TUID) AS PREP_TXN_TUID FROM THREEDS_PREP_TXN_DATA WHERE DS_CODE = :ds_code GROUP BY STATUS) T2 ON T1.PREP_TXN_TUID = T2.PREP_TXN_TUID ORDER BY T1.PREP_TXN_TUID DESC,9:2 ds_code,6:3 FIME,1:4,2:22,3:7 0,,"
        status, value = test_utils.send_to_server(up_sock, request, sock_id)
        if not status then
            return "FAILED"
        end

        local found = false
        for cnt = 4, 1, -1
        do
            status, value = test_utils.read_from_server(up_sock, sock_id)
            if not status then
                return "FAILED"
            end

            if cnt == 2 then
                if string.find(value, "\"dataProcessingRetryable") then
                    found = true
                end
            end
        end
        if found then
            return "PASSED"
        end
    end


    return "FAILED"
end

function M.test(name, in_table)
    local test = "mock_based_on_data"
    local out_table = {}
    if name ~= "all" and M[name] ~= nil then
        out_table[test .. "." .. name] = M[name]()
    elseif name == "all" or name == test then
        out_table[test .. ".mock_json_data"] = M.mock_json_data()
    end
    local test_utils = require("test_utils")
    return test_utils.merge_tables(out_table, in_table)
end

return M