<commit_before>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Copyright (c) 2020 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#include "MockRpcConnection.h"

namespace opflexagent {

void ResponseDict::init() {
    uint64_t j = 1001;
    for (unsigned int i = 0 ; i < no_of_span_msgs; i++, j++) {
        d[i].GetAllocator().Clear();
        d[i].Parse(response[i].c_str());
        dict.emplace(j, i);
    }
    j = 2001;
    for (unsigned int i = no_of_span_msgs ; i < (no_of_span_msgs + no_of_netflow_msgs); i++, j++) {
        d[i].GetAllocator().Clear();
        d[i].Parse(response[i].c_str());
        dict.emplace(j, i);
    }
}

ResponseDict& ResponseDict::Instance() {
    static ResponseDict inst;
    if (!inst.isInitialized) {
        inst.init();
        inst.isInitialized = true;
    }
    return inst;
}

void MockRpcConnection::sendTransaction(const uint64_t& reqId, const list<JsonRpcTransactMessage>& requests, Transaction* trans) {
    ResponseDict& rDict = ResponseDict::Instance();
    auto itr = rDict.dict.find(reqId);
    if (itr != rDict.dict.end()) {
        trans->handleTransaction(reqId, rDict.d[itr->second]);
    } else {
        LOG(DEBUG) << "No response found for req " << reqId;
    }
}

}<commit_msg>Better mock JSONRPC requests in UT<commit_after>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Copyright (c) 2020 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#include "MockRpcConnection.h"

namespace opflexagent {

void ResponseDict::init() {
    uint64_t j = 1001;
    for (unsigned int i = 0 ; i < no_of_span_msgs; i++, j++) {
        d[i].GetAllocator().Clear();
        d[i].Parse(response[i].c_str());
        dict.emplace(j, i);
    }
    j = 2001;
    for (unsigned int i = no_of_span_msgs ; i < (no_of_span_msgs + no_of_netflow_msgs); i++, j++) {
        d[i].GetAllocator().Clear();
        d[i].Parse(response[i].c_str());
        dict.emplace(j, i);
    }
}

ResponseDict& ResponseDict::Instance() {
    static ResponseDict inst;
    if (!inst.isInitialized) {
        inst.init();
        inst.isInitialized = true;
    }
    return inst;
}

void MockRpcConnection::sendTransaction(const uint64_t& reqId, const list<JsonRpcTransactMessage>& requests, Transaction* trans) {
    // prepare request
    std::shared_ptr<TransactReq> transactReq = std::make_shared<TransactReq>(TransactReq(requests, reqId));
    yajr::rpc::MethodName method(transactReq->getMethod().c_str());
    opflex::jsonrpc::PayloadWrapper wrapper(transactReq.get());
    ::yajr::internal::StringQueue sq;
    yajr::rpc::SendHandler sendHandler(sq);
    wrapper(sendHandler);

    // fake the response
    ResponseDict& rDict = ResponseDict::Instance();
    auto itr = rDict.dict.find(reqId);
    if (itr != rDict.dict.end()) {
        trans->handleTransaction(reqId, rDict.d[itr->second]);
    } else {
        LOG(DEBUG) << "No response found for req " << reqId;
    }
}

}
{'fraction_non_alphanumeric': 0.14897360703812318, 'fraction_numerical': 0.012903225806451613, 'mean_word_length': 3.301387137452711, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211340', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1121, 'n_words': 267}