/Users/deen/code/yugabyte-db/src/yb/util/jsonreader-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include <string> |
33 | | #include <vector> |
34 | | |
35 | | #include <gtest/gtest.h> |
36 | | |
37 | | #include "yb/gutil/integral_types.h" |
38 | | #include "yb/util/jsonreader.h" |
39 | | #include "yb/util/status.h" |
40 | | #include "yb/util/test_macros.h" |
41 | | |
42 | | using rapidjson::Value; |
43 | | using std::string; |
44 | | using std::vector; |
45 | | using strings::Substitute; |
46 | | |
47 | | namespace yb { |
48 | | |
49 | 1 | TEST(JsonReaderTest, Corrupt) { |
50 | 1 | JsonReader r(""); |
51 | 1 | Status s = r.Init(); |
52 | 1 | ASSERT_TRUE(s.IsCorruption()); |
53 | 1 | ASSERT_STR_CONTAINS( |
54 | 1 | s.ToString(), "JSON text is corrupt: The document is empty."); |
55 | 1 | } |
56 | | |
57 | 1 | TEST(JsonReaderTest, Empty) { |
58 | 1 | JsonReader r("{}"); |
59 | 1 | ASSERT_OK(r.Init()); |
60 | 1 | JsonReader r2("[]"); |
61 | 1 | ASSERT_OK(r2.Init()); |
62 | | |
63 | | // Not found. |
64 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsNotFound()); |
65 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsNotFound()); |
66 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsNotFound()); |
67 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "foo", nullptr).IsNotFound()); |
68 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "foo", nullptr).IsNotFound()); |
69 | 1 | } |
70 | | |
71 | 1 | TEST(JsonReaderTest, Basic) { |
72 | 1 | JsonReader r("{ \"foo\" : \"bar\" }"); |
73 | 1 | ASSERT_OK(r.Init()); |
74 | 1 | string foo; |
75 | 1 | ASSERT_OK(r.ExtractString(r.root(), "foo", &foo)); |
76 | 1 | ASSERT_EQ("bar", foo); |
77 | | |
78 | | // Bad types. |
79 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument()); |
80 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument()); |
81 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "foo", nullptr).IsInvalidArgument()); |
82 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "foo", nullptr).IsInvalidArgument()); |
83 | 1 | } |
84 | | |
85 | 1 | TEST(JsonReaderTest, LessBasic) { |
86 | 1 | string doc = Substitute( |
87 | 1 | "{ \"small\" : 1, \"big\" : $0, \"null\" : null, \"empty\" : \"\" }", kint64max); |
88 | 1 | JsonReader r(doc); |
89 | 1 | ASSERT_OK(r.Init()); |
90 | 1 | int32_t small; |
91 | 1 | ASSERT_OK(r.ExtractInt32(r.root(), "small", &small)); |
92 | 1 | ASSERT_EQ(1, small); |
93 | 1 | int64_t big; |
94 | 1 | ASSERT_OK(r.ExtractInt64(r.root(), "big", &big)); |
95 | 1 | ASSERT_EQ(kint64max, big); |
96 | 1 | string str; |
97 | 1 | ASSERT_OK(r.ExtractString(r.root(), "null", &str)); |
98 | 1 | ASSERT_EQ("", str); |
99 | 1 | ASSERT_OK(r.ExtractString(r.root(), "empty", &str)); |
100 | 1 | ASSERT_EQ("", str); |
101 | | |
102 | | // Bad types. |
103 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), "small", nullptr).IsInvalidArgument()); |
104 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "small", nullptr).IsInvalidArgument()); |
105 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "small", nullptr).IsInvalidArgument()); |
106 | | |
107 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "big", nullptr).IsInvalidArgument()); |
108 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), "big", nullptr).IsInvalidArgument()); |
109 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "big", nullptr).IsInvalidArgument()); |
110 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "big", nullptr).IsInvalidArgument()); |
111 | | |
112 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "null", nullptr).IsInvalidArgument()); |
113 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "null", nullptr).IsInvalidArgument()); |
114 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "null", nullptr).IsInvalidArgument()); |
115 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "null", nullptr).IsInvalidArgument()); |
116 | | |
117 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "empty", nullptr).IsInvalidArgument()); |
118 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "empty", nullptr).IsInvalidArgument()); |
119 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "empty", nullptr).IsInvalidArgument()); |
120 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "empty", nullptr).IsInvalidArgument()); |
121 | 1 | } |
122 | | |
123 | 1 | TEST(JsonReaderTest, Objects) { |
124 | 1 | JsonReader r("{ \"foo\" : { \"1\" : 1 } }"); |
125 | 1 | ASSERT_OK(r.Init()); |
126 | | |
127 | 1 | const Value* foo = nullptr; |
128 | 1 | ASSERT_OK(r.ExtractObject(r.root(), "foo", &foo)); |
129 | 1 | ASSERT_TRUE(foo); |
130 | | |
131 | 1 | int32_t one; |
132 | 1 | ASSERT_OK(r.ExtractInt32(foo, "1", &one)); |
133 | 1 | ASSERT_EQ(1, one); |
134 | | |
135 | | // Bad types. |
136 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument()); |
137 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument()); |
138 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsInvalidArgument()); |
139 | 1 | ASSERT_TRUE(r.ExtractObjectArray(r.root(), "foo", nullptr).IsInvalidArgument()); |
140 | 1 | } |
141 | | |
142 | 1 | TEST(JsonReaderTest, TopLevelArray) { |
143 | 1 | JsonReader r("[ { \"name\" : \"foo\" }, { \"name\" : \"bar\" } ]"); |
144 | 1 | ASSERT_OK(r.Init()); |
145 | | |
146 | 1 | vector<const Value*> objs; |
147 | 1 | ASSERT_OK(r.ExtractObjectArray(r.root(), nullptr, &objs)); |
148 | 1 | ASSERT_EQ(2, objs.size()); |
149 | 1 | string name; |
150 | 1 | ASSERT_OK(r.ExtractString(objs[0], "name", &name)); |
151 | 1 | ASSERT_EQ("foo", name); |
152 | 1 | ASSERT_OK(r.ExtractString(objs[1], "name", &name)); |
153 | 1 | ASSERT_EQ("bar", name); |
154 | | |
155 | | // Bad types. |
156 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), nullptr, NULL).IsInvalidArgument()); |
157 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), nullptr, NULL).IsInvalidArgument()); |
158 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), nullptr, NULL).IsInvalidArgument()); |
159 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), nullptr, NULL).IsInvalidArgument()); |
160 | 1 | } |
161 | | |
162 | 1 | TEST(JsonReaderTest, NestedArray) { |
163 | 1 | JsonReader r("{ \"foo\" : [ { \"val\" : 0 }, { \"val\" : 1 }, { \"val\" : 2 } ] }"); |
164 | 1 | ASSERT_OK(r.Init()); |
165 | | |
166 | 1 | vector<const Value*> foo; |
167 | 1 | ASSERT_OK(r.ExtractObjectArray(r.root(), "foo", &foo)); |
168 | 1 | ASSERT_EQ(3, foo.size()); |
169 | 1 | int i = 0; |
170 | 3 | for (const Value* v : foo) { |
171 | 3 | int32_t number; |
172 | 3 | ASSERT_OK(r.ExtractInt32(v, "val", &number)); |
173 | 3 | ASSERT_EQ(i, number); |
174 | 3 | i++; |
175 | 3 | } |
176 | | |
177 | | // Bad types. |
178 | 1 | ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument()); |
179 | 1 | ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument()); |
180 | 1 | ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsInvalidArgument()); |
181 | 1 | ASSERT_TRUE(r.ExtractObject(r.root(), "foo", nullptr).IsInvalidArgument()); |
182 | 1 | } |
183 | | |
184 | | } // namespace yb |