/src/cpp/core/json/JsonRpcTests.cpp

http://github.com/rstudio/rstudio · C++ · 357 lines · 277 code · 66 blank · 14 comment · 32 complexity · daba5a37911717a2fe5b4b77d61d3037 MD5 · raw file

  1. /*
  2. * JsonRpcTests.cpp
  3. *
  4. * Copyright (C) 2021 by RStudio, PBC
  5. *
  6. * Unless you have received this program directly from RStudio pursuant
  7. * to the terms of a commercial license agreement with RStudio, then
  8. * this program is licensed to you under the terms of version 3 of the
  9. * GNU Affero General Public License. This program is distributed WITHOUT
  10. * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
  12. * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
  13. *
  14. */
  15. #include <tests/TestThat.hpp>
  16. #include <core/json/JsonRpc.hpp>
  17. namespace rstudio {
  18. namespace core {
  19. namespace tests {
  20. namespace {
  21. json::Object createObject()
  22. {
  23. json::Object object;
  24. object["a"] = true;
  25. object["b"] = false;
  26. object["c"] = 1000;
  27. object["d"] = (uint64_t) 18446744073709550615U;
  28. object["e"] = 246.9;
  29. object["f"] = std::string("Hello world");
  30. json::Array simpleArray;
  31. simpleArray.push_back(100);
  32. simpleArray.push_back(200);
  33. simpleArray.push_back(300);
  34. object["g"] = simpleArray;
  35. json::Array objectArray;
  36. json::Object obj1;
  37. obj1["a1"] = "a1";
  38. obj1["a2"] = 1;
  39. json::Object obj2;
  40. obj2["b1"] = "b1";
  41. obj2["b2"] = 2;
  42. objectArray.push_back(obj1);
  43. objectArray.push_back(obj2);
  44. object["h"] = objectArray;
  45. json::Object obj3;
  46. obj3["nestedValue"] = 9876.324;
  47. json::Object obj4;
  48. obj4["a"] = "Inner object a";
  49. json::Array innerArray;
  50. innerArray.push_back(1);
  51. innerArray.push_back(5);
  52. innerArray.push_back(6);
  53. obj4["b"] = innerArray;
  54. obj4["c"] = json::Value();
  55. obj3["inner"] = obj4;
  56. object["i"] = obj3;
  57. return object;
  58. }
  59. json::Value createValue()
  60. {
  61. json::Object obj = createObject();
  62. return std::move(obj);
  63. }
  64. json::Object s_object;
  65. } // anonymous namespace
  66. TEST_CASE("JsonRpc")
  67. {
  68. SECTION("Initialize")
  69. {
  70. json::Object objectA;
  71. objectA["1"] = 1;
  72. objectA["2"] = 2;
  73. json::Object objectC;
  74. objectC["1"] = "a";
  75. objectC["2"] = "b";
  76. s_object["a"] = objectA;
  77. s_object["b"] = 5;
  78. s_object["c"] = objectC;
  79. }
  80. SECTION("Null test")
  81. {
  82. std::string json = "{\"a\": 1, \"b\": null}";
  83. json::Value value;
  84. REQUIRE(!value.parse(json));
  85. REQUIRE(value.getType() == json::Type::OBJECT);
  86. json::Object obj = value.getObject();
  87. REQUIRE(obj["a"].getType() == json::Type::INTEGER);
  88. REQUIRE(obj["b"].getType() == json::Type::NULL_TYPE);
  89. std::string bVal;
  90. REQUIRE_FALSE(json::getOptionalParam(obj, "b", std::string("DEFAULT"), &bVal));
  91. REQUIRE(bVal == "DEFAULT");
  92. REQUIRE(json::typeAsString(obj["b"]) == "<Null>");
  93. }
  94. SECTION("readParams tests")
  95. {
  96. json::Array array;
  97. array.push_back(1);
  98. array.push_back(false);
  99. array.push_back("Hello there");
  100. int a;
  101. bool b;
  102. std::string c;
  103. Error error = json::readParams(array, &a, &b, &c);
  104. REQUIRE_FALSE(error);
  105. REQUIRE(a == 1);
  106. REQUIRE_FALSE(b);
  107. REQUIRE(c == "Hello there");
  108. error = json::readParams(array, &c, &b, &c);
  109. REQUIRE(error);
  110. error = json::readParams(array, &a, &a, &c);
  111. REQUIRE(error);
  112. error = json::readParams(array, &a, &b, &a);
  113. REQUIRE(error);
  114. a = 5;
  115. b = true;
  116. error = json::readParams(array, &a, &b);
  117. REQUIRE_FALSE(error);
  118. REQUIRE(a == 1);
  119. REQUIRE_FALSE(b);
  120. }
  121. SECTION("readObjectParam tests")
  122. {
  123. json::Array array;
  124. json::Object obj;
  125. obj["a"] = 1;
  126. obj["b"] = true;
  127. obj["c"] = "Hello there";
  128. array.push_back(obj);
  129. array.push_back(1);
  130. array.push_back(false);
  131. array.push_back(obj);
  132. int a;
  133. bool b;
  134. std::string c;
  135. Error error = json::readObjectParam(array, 0,
  136. "a", &a,
  137. "b", &b,
  138. "c", &c);
  139. REQUIRE_FALSE(error);
  140. REQUIRE(a == 1);
  141. REQUIRE(b);
  142. REQUIRE(c == "Hello there");
  143. error = json::readObjectParam(array, 0,
  144. "a", &b,
  145. "b", &b,
  146. "c", &c);
  147. REQUIRE(error);
  148. error = json::readObjectParam(array, 1,
  149. "a", &a,
  150. "b", &b,
  151. "c", &c);
  152. REQUIRE(error);
  153. error = json::readObjectParam(array, 3,
  154. "a", &a,
  155. "b", &b,
  156. "c", &c);
  157. REQUIRE_FALSE(error);
  158. }
  159. SECTION("Can serialize / deserialize complex json object with helpers")
  160. {
  161. json::Object object;
  162. object["a"] = true;
  163. object["b"] = false;
  164. object["c"] = 1000;
  165. object["d"] = (uint64_t)18446744073709550615U;
  166. object["e"] = 246.9;
  167. object["f"] = std::string("Hello world");
  168. json::Array simpleArray;
  169. simpleArray.push_back(100);
  170. simpleArray.push_back(200);
  171. simpleArray.push_back(300);
  172. object["g"] = simpleArray;
  173. json::Array objectArray;
  174. json::Object obj1;
  175. obj1["a1"] = "a1";
  176. obj1["a2"] = 1;
  177. json::Object obj2;
  178. obj2["b1"] = "b1";
  179. obj2["b2"] = 2;
  180. objectArray.push_back(obj1);
  181. objectArray.push_back(obj2);
  182. object["h"] = objectArray;
  183. json::Object obj3;
  184. obj3["nestedValue"] = 9876.324;
  185. json::Object obj4;
  186. obj4["a"] = "Inner object a";
  187. json::Array innerArray;
  188. innerArray.push_back(1);
  189. innerArray.push_back(5);
  190. innerArray.push_back(json::Value(6));
  191. obj4["b"] = innerArray;
  192. obj4["c"] = 3;
  193. obj3["inner"] = obj4;
  194. object["i"] = obj3;
  195. std::string json = object.write();
  196. json::Value value;
  197. REQUIRE(!value.parse(json));
  198. REQUIRE(value.getType() == json::Type::OBJECT);
  199. json::Object deserializedObject = value.getObject();
  200. bool a, b;
  201. int c;
  202. uint64_t d;
  203. double e;
  204. std::string f;
  205. json::Array g, h;
  206. json::Object i;
  207. Error error = json::readObject(deserializedObject,
  208. "a", a,
  209. "b", b,
  210. "c", c,
  211. "d", d,
  212. "e", e,
  213. "f", f,
  214. "g", g,
  215. "h", h,
  216. "i", i);
  217. REQUIRE_FALSE(error);
  218. REQUIRE(a);
  219. REQUIRE_FALSE(b);
  220. REQUIRE(c == 1000);
  221. REQUIRE(d == 18446744073709550615U);
  222. REQUIRE(e == Approx(246.9));
  223. REQUIRE(f == "Hello world");
  224. REQUIRE(g[0].getInt() == 100);
  225. REQUIRE(g[1].getInt() == 200);
  226. REQUIRE(g[2].getInt() == 300);
  227. int g1, g2, g3;
  228. error = json::readParams(g, &g1, &g2, &g3);
  229. REQUIRE_FALSE(error);
  230. REQUIRE(g1 == 100);
  231. REQUIRE(g2 == 200);
  232. REQUIRE(g3 == 300);
  233. json::Object h1, h2;
  234. error = json::readParams(h, &h1, &h2);
  235. REQUIRE_FALSE(error);
  236. std::string a1;
  237. int a2;
  238. error = json::readObject(h1,
  239. "a1", a1,
  240. "a2", a2);
  241. REQUIRE_FALSE(error);
  242. REQUIRE(a1 == "a1");
  243. REQUIRE(a2 == 1);
  244. std::string b1;
  245. int b2;
  246. error = json::readObject(h2,
  247. "b1", b1,
  248. "b2", b2);
  249. REQUIRE_FALSE(error);
  250. REQUIRE(b1 == "b1");
  251. REQUIRE(b2 == 2);
  252. double nestedValue;
  253. json::Object innerObj;
  254. error = json::readObject(i,
  255. "nestedValue", nestedValue,
  256. "inner", innerObj);
  257. REQUIRE_FALSE(error);
  258. REQUIRE(nestedValue == Approx(9876.324));
  259. std::string innerA;
  260. json::Array innerB;
  261. int innerC;
  262. error = json::readObject(innerObj,
  263. "a", innerA,
  264. "b", innerB,
  265. "c", innerC);
  266. REQUIRE_FALSE(error);
  267. REQUIRE(innerA == "Inner object a");
  268. REQUIRE(innerB.getSize() == 3);
  269. REQUIRE(innerB[0].getInt() == 1);
  270. REQUIRE(innerB[1].getInt() == 5);
  271. REQUIRE(innerB[2].getInt() == 6);
  272. REQUIRE(innerC == 3);
  273. }
  274. SECTION("Can set rpc response value from complex object")
  275. {
  276. json::Object object = createObject();
  277. json::JsonRpcResponse jsonRpcResponse;
  278. jsonRpcResponse.setResult(object);
  279. }
  280. SECTION("Can convert to value properly")
  281. {
  282. json::Object root;
  283. json::Value val = createValue();
  284. root["a"] = val;
  285. json::JsonRpcResponse jsonRpcResponse;
  286. jsonRpcResponse.setResult(root);
  287. }
  288. }
  289. } // namespace tests
  290. } // namespace core
  291. } // namespace rstudio