<commit_before>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2014
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/test/unit_test.hpp>

#include <iostream>
#include <round/common/RPC.h>

using namespace std;
using namespace Round;

BOOST_AUTO_TEST_CASE(RPCMessageMethodTest) {
  RPC::JSON::Message rpcMsg;
  
  std::string ver;
  BOOST_CHECK(rpcMsg.getVersion(&ver));
  BOOST_CHECK_EQUAL(ver.compare(RPC::JSON::Message::VERSION), 0);

  const std::string TEST_METHOD = "hello";
  std::string method;
  BOOST_CHECK(!rpcMsg.getMethod(&method));
  BOOST_CHECK(rpcMsg.setMethod(TEST_METHOD));
  BOOST_CHECK(rpcMsg.getMethod(&method));
  BOOST_CHECK_EQUAL(method.compare(TEST_METHOD), 0);

  const std::string TEST_PARAMS = "{\"value\":1}";
  std::string params;
  BOOST_CHECK(!rpcMsg.getParams(&params));
  BOOST_CHECK(rpcMsg.setParams(TEST_PARAMS));
  BOOST_CHECK(rpcMsg.getParams(&params));
  BOOST_CHECK_EQUAL(params.compare(TEST_PARAMS), 0);

  const std::string TEST_ID = "12345";
  std::string id;
  BOOST_CHECK(!rpcMsg.getId(&id));
  BOOST_CHECK(rpcMsg.setId(TEST_ID));
  BOOST_CHECK(rpcMsg.getId(&id));
  BOOST_CHECK_EQUAL(id.compare(TEST_ID), 0);

  const std::string TEST_RESULT = "{\"value\":1}";
  std::string result;
  BOOST_CHECK(!rpcMsg.getResult(&params));
  BOOST_CHECK(rpcMsg.setResult(TEST_RESULT));
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);
  
  Error TEST_ERROR(12345, "12345");
  Error err;
  BOOST_CHECK(!rpcMsg.getError(&err));
  BOOST_CHECK(rpcMsg.setError(TEST_ERROR));
  BOOST_CHECK(rpcMsg.getError(&err));
  BOOST_CHECK(err.equals(TEST_ERROR));
  
  // Check again not to orverride.
  
  BOOST_CHECK_EQUAL(ver.compare(RPC::JSON::Message::VERSION), 0);
  
  BOOST_CHECK(rpcMsg.getMethod(&method));
  BOOST_CHECK_EQUAL(method.compare(TEST_METHOD), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&params));
  BOOST_CHECK_EQUAL(params.compare(TEST_PARAMS), 0);
  
  BOOST_CHECK(rpcMsg.getId(&id));
  BOOST_CHECK_EQUAL(id.compare(TEST_ID), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);

}

class TestMessageParser : public JSONParser {
  
public:
  
  TestMessageParser() {
  }

  JSONDictionary *createJSONDictionary() {
    return new RPC::JSON::Request();
  }
};

BOOST_AUTO_TEST_CASE(RPCRequestTest) {
  TestMessageParser jsonParser;
  RPC::JSON::Request *req;
  std::string inputStr, jsonStr;
  
  //inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"foobar, \"params\": \"bar\", \"baz]";
  //BOOST_CHECK(!jsonParser.parse(inputStr));
  
  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"id\": 1}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req);
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(!req->isNotify());
  req->toJSONString(&jsonStr);
  
  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"id\": \"1\"}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req);
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(!req->isNotify());
  req->toJSONString(&jsonStr);
  
  BOOST_CHECK(jsonParser.parse("{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42, 23], \"id\": 1}"));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req);
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(!req->isNotify());
  req->toJSONString(&jsonStr);
  
  BOOST_CHECK(jsonParser.parse("{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42, 23]}"));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req);
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(req->isNotify());
}
<commit_msg>* Added test cases for JSON-RPC.<commit_after>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2014
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/test/unit_test.hpp>

#include <iostream>
#include <round/common/RPC.h>

using namespace std;
using namespace Round;

BOOST_AUTO_TEST_CASE(RPCMessageMethodTest) {
  RPC::JSON::Message rpcMsg;
  
  std::string ver;
  BOOST_CHECK(rpcMsg.getVersion(&ver));
  BOOST_CHECK_EQUAL(ver.compare(RPC::JSON::Message::VERSION), 0);

  const std::string TEST_METHOD = "hello";
  std::string method;
  BOOST_CHECK(!rpcMsg.getMethod(&method));
  BOOST_CHECK(rpcMsg.setMethod(TEST_METHOD));
  BOOST_CHECK(rpcMsg.getMethod(&method));
  BOOST_CHECK_EQUAL(method.compare(TEST_METHOD), 0);

  const std::string TEST_PARAMS = "{\"value\":1}";
  std::string params;
  BOOST_CHECK(!rpcMsg.getParams(&params));
  BOOST_CHECK(rpcMsg.setParams(TEST_PARAMS));
  BOOST_CHECK(rpcMsg.getParams(&params));
  BOOST_CHECK_EQUAL(params.compare(TEST_PARAMS), 0);

  const std::string TEST_ID = "12345";
  std::string id;
  BOOST_CHECK(!rpcMsg.getId(&id));
  BOOST_CHECK(rpcMsg.setId(TEST_ID));
  BOOST_CHECK(rpcMsg.getId(&id));
  BOOST_CHECK_EQUAL(id.compare(TEST_ID), 0);

  const std::string TEST_RESULT = "{\"value\":1}";
  std::string result;
  BOOST_CHECK(!rpcMsg.getResult(&params));
  BOOST_CHECK(rpcMsg.setResult(TEST_RESULT));
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);
  
  Error TEST_ERROR(12345, "12345");
  Error err;
  BOOST_CHECK(!rpcMsg.getError(&err));
  BOOST_CHECK(rpcMsg.setError(TEST_ERROR));
  BOOST_CHECK(rpcMsg.getError(&err));
  BOOST_CHECK(err.equals(TEST_ERROR));
  
  // Check again not to orverride.
  
  BOOST_CHECK_EQUAL(ver.compare(RPC::JSON::Message::VERSION), 0);
  
  BOOST_CHECK(rpcMsg.getMethod(&method));
  BOOST_CHECK_EQUAL(method.compare(TEST_METHOD), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&params));
  BOOST_CHECK_EQUAL(params.compare(TEST_PARAMS), 0);
  
  BOOST_CHECK(rpcMsg.getId(&id));
  BOOST_CHECK_EQUAL(id.compare(TEST_ID), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);
  
  BOOST_CHECK(rpcMsg.getParams(&result));
  BOOST_CHECK_EQUAL(result.compare(TEST_RESULT), 0);

}

class TestMessageParser : public JSONParser {
  
public:
  
  TestMessageParser() {
  }

  JSONDictionary *createJSONDictionary() {
    return new RPC::JSON::Request();
  }
};

BOOST_AUTO_TEST_CASE(RPCRequestTest) {
  TestMessageParser jsonParser;
  RPC::JSON::Request *req;
  std::string inputStr, jsonStr;
  
  // rpc call with positional parameters:
  
  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42, 23], \"id\": 1}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(!req->isNotify());
  
  // rpc call with named parameters:

  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": {\"subtrahend\": 23, \"minuend\": 42}, \"id\": 3}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(!req->isNotify());
  req->toJSONString(&jsonStr);
  std::cout << inputStr << std::endl;
  std::cout << jsonStr << std::endl;
  
  // a Notification:

  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": \"update\", \"params\": [1,2,3,4,5]}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(req->isValid());
  BOOST_CHECK(req->isNotify());

  // rpc call with invalid JSON:

  inputStr = " {\"jsonrpc\": \"2.0\", \"method\": \"foobar, \"params\": \"bar\", \"baz]";
  BOOST_CHECK(!jsonParser.parse(inputStr));
  
  // rpc call with invalid Request object:
  
  inputStr = "{\"jsonrpc\": \"2.0\", \"method\": 1, \"params\": \"bar\"}";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(!req->isValid());
  
  //rpc call with an empty Array:
  
  inputStr = "[]";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(!req);
  
  // rpc call with an invalid Batch (but not empty):

  inputStr = "[1]";
  BOOST_CHECK(jsonParser.parse(inputStr));
  req = dynamic_cast<RPC::JSON::Request *>(jsonParser.getObject());
  BOOST_CHECK(!req);
}

{'fraction_non_alphanumeric': 0.21396265800765396, 'fraction_numerical': 0.012988519076887394, 'mean_word_length': 5.543247344461305, 'pattern_counts': {'":': 39, '<': 29, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6795066', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2927, 'n_words': 461}