]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/ceph_json_formattable: add unitest
authorYehuda Sadeh <yehuda@redhat.com>
Wed, 13 Dec 2017 11:39:24 +0000 (03:39 -0800)
committerYehuda Sadeh <yehuda@redhat.com>
Thu, 12 Apr 2018 22:38:36 +0000 (15:38 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/test/common/CMakeLists.txt
src/test/common/test_json_formattable.cc [new file with mode: 0644]

index 5272fbef46785dbeae8888288d9dbb554ea1b35b..25361405898f8fcfc51ff01ac359c42496816682 100644 (file)
@@ -55,6 +55,13 @@ add_executable(unittest_str_map
 add_ceph_unittest(unittest_str_map)
 target_link_libraries(unittest_str_map ceph-common)
 
+# unittest_json_formattable
+add_executable(unittest_json_formattable
+  test_json_formattable.cc
+  )
+add_ceph_unittest(unittest_json_formattable ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_json_formattable)
+target_link_libraries(unittest_json_formattable ceph-common global ${BLKID_LIBRARIES})
+
 # unittest_sharedptr_registry
 add_executable(unittest_sharedptr_registry
   test_sharedptr_registry.cc
diff --git a/src/test/common/test_json_formattable.cc b/src/test/common/test_json_formattable.cc
new file mode 100644 (file)
index 0000000..3161f75
--- /dev/null
@@ -0,0 +1,175 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
+ *
+ * Author: Loic Dachary <loic@dachary.org>
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#include <errno.h>
+#include <gtest/gtest.h>
+
+#include "common/ceph_json.h"
+
+#include <sstream>
+
+using namespace std;
+
+
+static void get_jf(const string& s, JSONFormattable *f)
+{
+  JSONParser p;
+  bool result = p.parse(s.c_str(), s.size());
+  ASSERT_EQ(true, result);
+  try {
+    decode_json_obj(*f, &p);
+  } catch (JSONDecoder::err& e) {
+    ASSERT_TRUE(0 == "Failed to decode JSON object");
+  }
+}
+
+TEST(formatable, str) {
+  JSONFormattable f;
+  get_jf("{ \"foo\": \"bar\" }", &f);
+  ASSERT_EQ((string)f["foo"], "bar");
+  ASSERT_EQ((string)f["fooz"], "");
+  ASSERT_EQ((string)f["fooz"]("lala"), "lala");
+}
+
+TEST(formatable, str2) {
+  JSONFormattable f;
+  get_jf("{ \"foo\": \"bar\" }", &f);
+  ASSERT_EQ((string)f["foo"], "bar");
+  ASSERT_EQ((string)f["fooz"], "");
+  ASSERT_EQ((string)f["fooz"]("lala"), "lala");
+
+  JSONFormattable f2;
+  get_jf("{ \"foo\": \"bar\", \"fooz\": \"zzz\" }", &f2);
+  ASSERT_EQ((string)f2["foo"], "bar");
+  ASSERT_NE((string)f2["fooz"], "");
+  ASSERT_EQ((string)f2["fooz"], "zzz");
+  ASSERT_EQ((string)f2["fooz"]("lala"), "zzz");
+
+}
+
+TEST(formatable, int) {
+  JSONFormattable f;
+  get_jf("{ \"foo\": 1 }", &f);
+  ASSERT_EQ((int)f["foo"], 1);
+  ASSERT_EQ((int)f["fooz"], 0);
+  ASSERT_EQ((int)f["fooz"](3), 3);
+
+  JSONFormattable f2;
+  get_jf("{ \"foo\": \"bar\", \"fooz\": \"123\" }", &f2);
+  ASSERT_EQ((string)f2["foo"], "bar");
+  ASSERT_NE((int)f2["fooz"], 0);
+  ASSERT_EQ((int)f2["fooz"], 123);
+  ASSERT_EQ((int)f2["fooz"](111), 123);
+}
+
+TEST(formatable, bool) {
+  JSONFormattable f;
+  get_jf("{ \"foo\": \"true\" }", &f);
+  ASSERT_EQ((bool)f["foo"], true);
+  ASSERT_EQ((bool)f["fooz"], false);
+  ASSERT_EQ((bool)f["fooz"](true), true);
+
+  JSONFormattable f2;
+  get_jf("{ \"foo\": \"false\" }", &f);
+  ASSERT_EQ((bool)f["foo"], false);
+}
+
+TEST(formatable, nested) {
+  JSONFormattable f;
+  get_jf("{ \"obj\": { \"foo\": 1, \"inobj\": { \"foo\": 2 } } }", &f);
+  ASSERT_EQ((int)f["foo"], 0);
+  ASSERT_EQ((int)f["obj"]["foo"], 1);
+  ASSERT_EQ((int)f["obj"]["inobj"]["foo"], 2);
+}
+
+TEST(formatable, array) {
+  JSONFormattable f;
+  get_jf("{ \"arr\": [ { \"foo\": 1, \"inobj\": { \"foo\": 2 } }," 
+         "{ \"foo\": 2 } ] }", &f);
+
+  int i = 1;
+  for (auto a : f.array()) {
+    ASSERT_EQ((int)a["foo"], i);
+    ++i;
+  }
+
+  JSONFormattable f2;
+  get_jf("{ \"arr\": [ 0, 1, 2, 3, 4 ]}", &f2);
+
+  i = 0;
+  for (auto a : f2.array()) {
+    ASSERT_EQ((int)a, i);
+    ++i;
+  }
+}
+
+TEST(formatable, bin_encode) {
+  JSONFormattable f, f2;
+  get_jf("{ \"arr\": [ { \"foo\": 1, \"bar\": \"aaa\", \"inobj\": { \"foo\": 2 } }," 
+         "{ \"foo\": 2, \"inobj\": { \"foo\": 3 } } ] }", &f);
+
+  int i = 1;
+  for (auto a : f.array()) {
+    ASSERT_EQ((int)a["foo"], i);
+    ASSERT_EQ((int)a["foo"]["inobj"], i + 1);
+    ASSERT_EQ((string)a["bar"], "aaa");
+    ++i;
+  }
+
+  bufferlist bl;
+  ::encode(f, bl);
+  bufferlist::iterator iter = bl.begin();
+  try {
+    ::decode(f2, iter);
+  } catch (buffer::error& err) {
+    ASSERT_TRUE(0 == "Failed to decode object");
+  }
+
+  i = 1;
+  for (auto a : f2.array()) {
+    ASSERT_EQ((int)a["foo"], i);
+    ASSERT_EQ((int)a["foo"]["inobj"], i + 1);
+    ASSERT_EQ((string)a["bar"], "aaa");
+    ++i;
+  }
+
+}
+
+TEST(formatable, json_encode) {
+  JSONFormattable f, f2;
+  get_jf("{ \"arr\": [ { \"foo\": 1, \"bar\": \"aaa\", \"inobj\": { \"foo\": 2 } }," 
+         "{ \"foo\": 2, \"inobj\": { \"foo\": 3 } } ] }", &f);
+
+  JSONFormatter formatter;
+  formatter.open_object_section("bla");
+  ::encode_json("f", f, &formatter);
+  formatter.close_section();
+
+  stringstream ss;
+  formatter.flush(ss);
+
+  get_jf(ss.str(), &f2);
+
+  int i = 1;
+  for (auto a : f2.array()) {
+    ASSERT_EQ((int)a["foo"], i);
+    ASSERT_EQ((int)a["foo"]["inobj"], i + 1);
+    ASSERT_EQ((string)a["bar"], "aaa");
+    ++i;
+  }
+
+}
+