| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "src/core/lib/json/json_object_loader.h" |
| 16 |
|
| 17 |
#include <cstdint> |
| 18 |
|
| 19 |
#include "absl/status/status.h" |
| 20 |
#include "gmock/gmock.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
|
| 23 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 24 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 25 |
|
| 26 |
namespace grpc_core { |
| 27 |
namespace { |
| 28 |
|
| 29 |
template <typename T> |
| 30 |
absl::StatusOr<T> Parse(absl::string_view json, |
| 31 |
const JsonArgs& args = JsonArgs()) { |
| 32 |
auto parsed = Json::Parse(json); |
| 33 |
if (!parsed.ok()) return parsed.status(); |
| 34 |
return LoadFromJson<T>(*parsed, args); |
| 35 |
} |
| 36 |
|
| 37 |
// |
| 38 |
// Signed integer tests |
| 39 |
// |
| 40 |
|
| 41 |
template <typename T> |
| 42 |
class SignedIntegerTest : public ::testing::Test {}; |
| 43 |
|
| 44 |
TYPED_TEST_SUITE_P(SignedIntegerTest); |
| 45 |
|
| 46 |
TYPED_TEST_P(SignedIntegerTest, IntegerFields) { |
| 47 |
struct TestStruct { |
| 48 |
TypeParam value = 0; |
| 49 |
TypeParam optional_value = 0; |
| 50 |
absl::optional<TypeParam> absl_optional_value; |
| 51 |
std::unique_ptr<TypeParam> unique_ptr_value; |
| 52 |
|
| 53 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 54 |
static const auto* loader = |
| 55 |
JsonObjectLoader<TestStruct>() |
| 56 |
.Field("value", &TestStruct::value) |
| 57 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 58 |
.OptionalField("absl_optional_value", |
| 59 |
&TestStruct::absl_optional_value) |
| 60 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 61 |
.Finish(); |
| 62 |
return loader; |
| 63 |
} |
| 64 |
}; |
| 65 |
// Positive number. |
| 66 |
auto test_struct = Parse<TestStruct>("{\"value\": 5}"); |
| 67 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 68 |
EXPECT_EQ(test_struct->value, 5); |
| 69 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 70 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 71 |
// Negative number. |
| 72 |
test_struct = Parse<TestStruct>("{\"value\": -5}"); |
| 73 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 74 |
EXPECT_EQ(test_struct->value, -5); |
| 75 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 76 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 77 |
// Encoded in a JSON string. |
| 78 |
test_struct = Parse<TestStruct>("{\"value\": \"5\"}"); |
| 79 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 80 |
EXPECT_EQ(test_struct->value, 5); |
| 81 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 82 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 83 |
// Fails to parse number from JSON string. |
| 84 |
test_struct = Parse<TestStruct>("{\"value\": \"foo\"}"); |
| 85 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 86 |
EXPECT_EQ( |
| 87 |
test_struct.status().message(), |
| 88 |
"errors validating JSON: [field:value error:failed to parse number]") |
| 89 |
<< test_struct.status(); |
| 90 |
// Fails if required field is not present. |
| 91 |
test_struct = Parse<TestStruct>("{}"); |
| 92 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 93 |
EXPECT_EQ(test_struct.status().message(), |
| 94 |
"errors validating JSON: [field:value error:field not present]") |
| 95 |
<< test_struct.status(); |
| 96 |
// Optional fields present. |
| 97 |
test_struct = Parse<TestStruct>( |
| 98 |
"{\"value\": 5, \"optional_value\": 7, " |
| 99 |
"\"absl_optional_value\": 9, \"unique_ptr_value\": 11}"); |
| 100 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 101 |
EXPECT_EQ(test_struct->value, 5); |
| 102 |
EXPECT_EQ(test_struct->optional_value, 7); |
| 103 |
EXPECT_EQ(test_struct->absl_optional_value, 9); |
| 104 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 105 |
EXPECT_EQ(*test_struct->unique_ptr_value, 11); |
| 106 |
// Wrong JSON type. |
| 107 |
test_struct = Parse<TestStruct>( |
| 108 |
"{\"value\": [], \"optional_value\": {}, " |
| 109 |
"\"absl_optional_value\": true, \"unique_ptr_value\": false}"); |
| 110 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 111 |
EXPECT_EQ(test_struct.status().message(), |
| 112 |
"errors validating JSON: [" |
| 113 |
"field:absl_optional_value error:is not a number; " |
| 114 |
"field:optional_value error:is not a number; " |
| 115 |
"field:unique_ptr_value error:is not a number; " |
| 116 |
"field:value error:is not a number]") |
| 117 |
<< test_struct.status(); |
| 118 |
} |
| 119 |
|
| 120 |
REGISTER_TYPED_TEST_SUITE_P(SignedIntegerTest, IntegerFields); |
| 121 |
|
| 122 |
using IntegerTypes = ::testing::Types<int32_t, int64_t>; |
| 123 |
INSTANTIATE_TYPED_TEST_SUITE_P(My, SignedIntegerTest, IntegerTypes); |
| 124 |
|
| 125 |
// |
| 126 |
// Unsigned integer tests |
| 127 |
// |
| 128 |
|
| 129 |
template <typename T> |
| 130 |
class UnsignedIntegerTest : public ::testing::Test {}; |
| 131 |
|
| 132 |
TYPED_TEST_SUITE_P(UnsignedIntegerTest); |
| 133 |
|
| 134 |
TYPED_TEST_P(UnsignedIntegerTest, IntegerFields) { |
| 135 |
struct TestStruct { |
| 136 |
TypeParam value = 0; |
| 137 |
TypeParam optional_value = 0; |
| 138 |
absl::optional<TypeParam> absl_optional_value; |
| 139 |
std::unique_ptr<TypeParam> unique_ptr_value; |
| 140 |
|
| 141 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 142 |
static const auto* loader = |
| 143 |
JsonObjectLoader<TestStruct>() |
| 144 |
.Field("value", &TestStruct::value) |
| 145 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 146 |
.OptionalField("absl_optional_value", |
| 147 |
&TestStruct::absl_optional_value) |
| 148 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 149 |
.Finish(); |
| 150 |
return loader; |
| 151 |
} |
| 152 |
}; |
| 153 |
// Positive number. |
| 154 |
auto test_struct = Parse<TestStruct>("{\"value\": 5}"); |
| 155 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 156 |
EXPECT_EQ(test_struct->value, 5); |
| 157 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 158 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 159 |
// Negative number. |
| 160 |
test_struct = Parse<TestStruct>("{\"value\": -5}"); |
| 161 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 162 |
EXPECT_EQ(test_struct.status().message(), |
| 163 |
"errors validating JSON: [" |
| 164 |
"field:value error:failed to parse non-negative number]") |
| 165 |
<< test_struct.status(); |
| 166 |
// Encoded in a JSON string. |
| 167 |
test_struct = Parse<TestStruct>("{\"value\": \"5\"}"); |
| 168 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 169 |
EXPECT_EQ(test_struct->value, 5); |
| 170 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 171 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 172 |
// Fails to parse number from JSON string. |
| 173 |
test_struct = Parse<TestStruct>("{\"value\": \"foo\"}"); |
| 174 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 175 |
EXPECT_EQ(test_struct.status().message(), |
| 176 |
"errors validating JSON: [" |
| 177 |
"field:value error:failed to parse non-negative number]") |
| 178 |
<< test_struct.status(); |
| 179 |
// Fails if required field is not present. |
| 180 |
test_struct = Parse<TestStruct>("{}"); |
| 181 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 182 |
EXPECT_EQ(test_struct.status().message(), |
| 183 |
"errors validating JSON: [field:value error:field not present]") |
| 184 |
<< test_struct.status(); |
| 185 |
// Optional fields present. |
| 186 |
test_struct = Parse<TestStruct>( |
| 187 |
"{\"value\": 5, \"optional_value\": 7, " |
| 188 |
"\"absl_optional_value\": 9, \"unique_ptr_value\": 11}"); |
| 189 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 190 |
EXPECT_EQ(test_struct->value, 5); |
| 191 |
EXPECT_EQ(test_struct->optional_value, 7); |
| 192 |
ASSERT_TRUE(test_struct->absl_optional_value.has_value()); |
| 193 |
EXPECT_EQ(*test_struct->absl_optional_value, 9); |
| 194 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 195 |
EXPECT_EQ(*test_struct->unique_ptr_value, 11); |
| 196 |
// Wrong JSON type. |
| 197 |
test_struct = Parse<TestStruct>( |
| 198 |
"{\"value\": [], \"optional_value\": {}, " |
| 199 |
"\"absl_optional_value\": true, \"unique_ptr_value\": false}"); |
| 200 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 201 |
EXPECT_EQ(test_struct.status().message(), |
| 202 |
"errors validating JSON: [" |
| 203 |
"field:absl_optional_value error:is not a number; " |
| 204 |
"field:optional_value error:is not a number; " |
| 205 |
"field:unique_ptr_value error:is not a number; " |
| 206 |
"field:value error:is not a number]") |
| 207 |
<< test_struct.status(); |
| 208 |
} |
| 209 |
|
| 210 |
REGISTER_TYPED_TEST_SUITE_P(UnsignedIntegerTest, IntegerFields); |
| 211 |
|
| 212 |
using UnsignedIntegerTypes = ::testing::Types<uint32_t, uint64_t>; |
| 213 |
INSTANTIATE_TYPED_TEST_SUITE_P(My, UnsignedIntegerTest, UnsignedIntegerTypes); |
| 214 |
|
| 215 |
// |
| 216 |
// Floating-point tests |
| 217 |
// |
| 218 |
|
| 219 |
template <typename T> |
| 220 |
class FloatingPointTest : public ::testing::Test {}; |
| 221 |
|
| 222 |
TYPED_TEST_SUITE_P(FloatingPointTest); |
| 223 |
|
| 224 |
TYPED_TEST_P(FloatingPointTest, FloatFields) { |
| 225 |
struct TestStruct { |
| 226 |
TypeParam value = 0; |
| 227 |
TypeParam optional_value = 0; |
| 228 |
absl::optional<TypeParam> absl_optional_value; |
| 229 |
std::unique_ptr<TypeParam> unique_ptr_value; |
| 230 |
|
| 231 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 232 |
static const auto* loader = |
| 233 |
JsonObjectLoader<TestStruct>() |
| 234 |
.Field("value", &TestStruct::value) |
| 235 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 236 |
.OptionalField("absl_optional_value", |
| 237 |
&TestStruct::absl_optional_value) |
| 238 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 239 |
.Finish(); |
| 240 |
return loader; |
| 241 |
} |
| 242 |
}; |
| 243 |
// Positive number. |
| 244 |
auto test_struct = Parse<TestStruct>("{\"value\": 5.2}"); |
| 245 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 246 |
EXPECT_NEAR(test_struct->value, 5.2, 0.0001); |
| 247 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 248 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 249 |
// Negative number. |
| 250 |
test_struct = Parse<TestStruct>("{\"value\": -5.2}"); |
| 251 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 252 |
EXPECT_NEAR(test_struct->value, -5.2, 0.0001); |
| 253 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 254 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 255 |
// Encoded in a JSON string. |
| 256 |
test_struct = Parse<TestStruct>("{\"value\": \"5.2\"}"); |
| 257 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 258 |
EXPECT_NEAR(test_struct->value, 5.2, 0.0001); |
| 259 |
EXPECT_EQ(test_struct->optional_value, 0); |
| 260 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 261 |
// Fails to parse number from JSON string. |
| 262 |
test_struct = Parse<TestStruct>("{\"value\": \"foo\"}"); |
| 263 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 264 |
EXPECT_EQ(test_struct.status().message(), |
| 265 |
"errors validating JSON: [" |
| 266 |
"field:value error:failed to parse floating-point number]") |
| 267 |
<< test_struct.status(); |
| 268 |
// Fails if required field is not present. |
| 269 |
test_struct = Parse<TestStruct>("{}"); |
| 270 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 271 |
EXPECT_EQ(test_struct.status().message(), |
| 272 |
"errors validating JSON: [field:value error:field not present]") |
| 273 |
<< test_struct.status(); |
| 274 |
// Optional fields present. |
| 275 |
test_struct = Parse<TestStruct>( |
| 276 |
"{\"value\": 5.2, \"optional_value\": 7.5, " |
| 277 |
"\"absl_optional_value\": 9.8, \"unique_ptr_value\": 11.5}"); |
| 278 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 279 |
EXPECT_NEAR(test_struct->value, 5.2, 0.0001); |
| 280 |
EXPECT_NEAR(test_struct->optional_value, 7.5, 0.0001); |
| 281 |
ASSERT_TRUE(test_struct->absl_optional_value.has_value()); |
| 282 |
EXPECT_NEAR(*test_struct->absl_optional_value, 9.8, 0.0001); |
| 283 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 284 |
EXPECT_NEAR(*test_struct->unique_ptr_value, 11.5, 0.0001); |
| 285 |
// Wrong JSON type. |
| 286 |
test_struct = Parse<TestStruct>( |
| 287 |
"{\"value\": [], \"optional_value\": {}, " |
| 288 |
"\"absl_optional_value\": true, \"unique_ptr_value\": false}"); |
| 289 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 290 |
EXPECT_EQ(test_struct.status().message(), |
| 291 |
"errors validating JSON: [" |
| 292 |
"field:absl_optional_value error:is not a number; " |
| 293 |
"field:optional_value error:is not a number; " |
| 294 |
"field:unique_ptr_value error:is not a number; " |
| 295 |
"field:value error:is not a number]") |
| 296 |
<< test_struct.status(); |
| 297 |
} |
| 298 |
|
| 299 |
REGISTER_TYPED_TEST_SUITE_P(FloatingPointTest, FloatFields); |
| 300 |
|
| 301 |
using FloatingPointTypes = ::testing::Types<float, double>; |
| 302 |
INSTANTIATE_TYPED_TEST_SUITE_P(My, FloatingPointTest, FloatingPointTypes); |
| 303 |
|
| 304 |
// |
| 305 |
// Boolean tests |
| 306 |
// |
| 307 |
|
| 308 |
TEST(JsonObjectLoader, BooleanFields) { |
| 309 |
struct TestStruct { |
| 310 |
bool value = false; |
| 311 |
bool optional_value = true; |
| 312 |
absl::optional<bool> absl_optional_value; |
| 313 |
std::unique_ptr<bool> unique_ptr_value; |
| 314 |
|
| 315 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 316 |
static const auto* loader = |
| 317 |
JsonObjectLoader<TestStruct>() |
| 318 |
.Field("value", &TestStruct::value) |
| 319 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 320 |
.OptionalField("absl_optional_value", |
| 321 |
&TestStruct::absl_optional_value) |
| 322 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 323 |
.Finish(); |
| 324 |
return loader; |
| 325 |
} |
| 326 |
}; |
| 327 |
// True. |
| 328 |
auto test_struct = Parse<TestStruct>("{\"value\": true}"); |
| 329 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 330 |
EXPECT_EQ(test_struct->value, true); |
| 331 |
EXPECT_EQ(test_struct->optional_value, true); // Unmodified. |
| 332 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 333 |
// False. |
| 334 |
test_struct = Parse<TestStruct>("{\"value\": false}"); |
| 335 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 336 |
EXPECT_EQ(test_struct->value, false); |
| 337 |
EXPECT_EQ(test_struct->optional_value, true); // Unmodified. |
| 338 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 339 |
// Fails if required field is not present. |
| 340 |
test_struct = Parse<TestStruct>("{}"); |
| 341 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 342 |
EXPECT_EQ(test_struct.status().message(), |
| 343 |
"errors validating JSON: [field:value error:field not present]") |
| 344 |
<< test_struct.status(); |
| 345 |
// Optional fields present. |
| 346 |
test_struct = Parse<TestStruct>( |
| 347 |
"{\"value\": true, \"optional_value\": false," |
| 348 |
"\"absl_optional_value\": true, \"unique_ptr_value\": false}"); |
| 349 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 350 |
EXPECT_EQ(test_struct->value, true); |
| 351 |
EXPECT_EQ(test_struct->optional_value, false); |
| 352 |
EXPECT_EQ(test_struct->absl_optional_value, true); |
| 353 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 354 |
EXPECT_EQ(*test_struct->unique_ptr_value, false); |
| 355 |
// Wrong JSON type. |
| 356 |
test_struct = Parse<TestStruct>( |
| 357 |
"{\"value\": [], \"optional_value\": {}, " |
| 358 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": \"foo\"}"); |
| 359 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 360 |
EXPECT_EQ(test_struct.status().message(), |
| 361 |
"errors validating JSON: [" |
| 362 |
"field:absl_optional_value error:is not a boolean; " |
| 363 |
"field:optional_value error:is not a boolean; " |
| 364 |
"field:unique_ptr_value error:is not a boolean; " |
| 365 |
"field:value error:is not a boolean]") |
| 366 |
<< test_struct.status(); |
| 367 |
} |
| 368 |
|
| 369 |
// |
| 370 |
// String tests |
| 371 |
// |
| 372 |
|
| 373 |
TEST(JsonObjectLoader, StringFields) { |
| 374 |
struct TestStruct { |
| 375 |
std::string value; |
| 376 |
std::string optional_value; |
| 377 |
absl::optional<std::string> absl_optional_value; |
| 378 |
std::unique_ptr<std::string> unique_ptr_value; |
| 379 |
|
| 380 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 381 |
static const auto* loader = |
| 382 |
JsonObjectLoader<TestStruct>() |
| 383 |
.Field("value", &TestStruct::value) |
| 384 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 385 |
.OptionalField("absl_optional_value", |
| 386 |
&TestStruct::absl_optional_value) |
| 387 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 388 |
.Finish(); |
| 389 |
return loader; |
| 390 |
} |
| 391 |
}; |
| 392 |
// Valid string. |
| 393 |
auto test_struct = Parse<TestStruct>("{\"value\": \"foo\"}"); |
| 394 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 395 |
EXPECT_EQ(test_struct->value, "foo"); |
| 396 |
EXPECT_EQ(test_struct->optional_value, ""); |
| 397 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 398 |
// Fails if required field is not present. |
| 399 |
test_struct = Parse<TestStruct>("{}"); |
| 400 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 401 |
EXPECT_EQ(test_struct.status().message(), |
| 402 |
"errors validating JSON: [field:value error:field not present]") |
| 403 |
<< test_struct.status(); |
| 404 |
// Optional fields present. |
| 405 |
test_struct = Parse<TestStruct>( |
| 406 |
"{\"value\": \"foo\", \"optional_value\": \"bar\"," |
| 407 |
"\"absl_optional_value\": \"baz\", \"unique_ptr_value\": \"quux\"}"); |
| 408 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 409 |
EXPECT_EQ(test_struct->value, "foo"); |
| 410 |
EXPECT_EQ(test_struct->optional_value, "bar"); |
| 411 |
EXPECT_EQ(test_struct->absl_optional_value, "baz"); |
| 412 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 413 |
EXPECT_EQ(*test_struct->unique_ptr_value, "quux"); |
| 414 |
// Wrong JSON type. |
| 415 |
test_struct = Parse<TestStruct>( |
| 416 |
"{\"value\": [], \"optional_value\": {}, " |
| 417 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": true}"); |
| 418 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 419 |
EXPECT_EQ(test_struct.status().message(), |
| 420 |
"errors validating JSON: [" |
| 421 |
"field:absl_optional_value error:is not a string; " |
| 422 |
"field:optional_value error:is not a string; " |
| 423 |
"field:unique_ptr_value error:is not a string; " |
| 424 |
"field:value error:is not a string]") |
| 425 |
<< test_struct.status(); |
| 426 |
} |
| 427 |
|
| 428 |
// |
| 429 |
// Duration tests |
| 430 |
// |
| 431 |
|
| 432 |
TEST(JsonObjectLoader, DurationFields) { |
| 433 |
struct TestStruct { |
| 434 |
Duration value = Duration::Zero(); |
| 435 |
Duration optional_value = Duration::Zero(); |
| 436 |
absl::optional<Duration> absl_optional_value; |
| 437 |
std::unique_ptr<Duration> unique_ptr_value; |
| 438 |
|
| 439 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 440 |
static const auto* loader = |
| 441 |
JsonObjectLoader<TestStruct>() |
| 442 |
.Field("value", &TestStruct::value) |
| 443 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 444 |
.OptionalField("absl_optional_value", |
| 445 |
&TestStruct::absl_optional_value) |
| 446 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 447 |
.Finish(); |
| 448 |
return loader; |
| 449 |
} |
| 450 |
}; |
| 451 |
// Valid duration string. |
| 452 |
auto test_struct = Parse<TestStruct>("{\"value\": \"3s\"}"); |
| 453 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 454 |
EXPECT_EQ(test_struct->value, Duration::Seconds(3)); |
| 455 |
EXPECT_EQ(test_struct->optional_value, Duration::Zero()); |
| 456 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 457 |
// Invalid duration strings. |
| 458 |
test_struct = Parse<TestStruct>( |
| 459 |
"{\"value\": \"3sec\", \"optional_value\": \"foos\"," |
| 460 |
"\"absl_optional_value\": \"1.0123456789s\"}"); |
| 461 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 462 |
EXPECT_EQ(test_struct.status().message(), |
| 463 |
"errors validating JSON: [" |
| 464 |
"field:absl_optional_value error:" |
| 465 |
"Not a duration (too many digits after decimal); " |
| 466 |
"field:optional_value error:" |
| 467 |
"Not a duration (not a number of seconds); " |
| 468 |
"field:value error:Not a duration (no s suffix)]") |
| 469 |
<< test_struct.status(); |
| 470 |
test_struct = Parse<TestStruct>("{\"value\": \"315576000001s\"}"); |
| 471 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 472 |
EXPECT_EQ(test_struct.status().message(), |
| 473 |
"errors validating JSON: [" |
| 474 |
"field:value error:seconds must be in the range [0, 315576000000]]") |
| 475 |
<< test_struct.status(); |
| 476 |
test_struct = Parse<TestStruct>("{\"value\": \"3.xs\"}"); |
| 477 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 478 |
EXPECT_EQ(test_struct.status().message(), |
| 479 |
"errors validating JSON: [" |
| 480 |
"field:value error:Not a duration (not a number of nanoseconds)]") |
| 481 |
<< test_struct.status(); |
| 482 |
// Fails if required field is not present. |
| 483 |
test_struct = Parse<TestStruct>("{}"); |
| 484 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 485 |
EXPECT_EQ(test_struct.status().message(), |
| 486 |
"errors validating JSON: [field:value error:field not present]") |
| 487 |
<< test_struct.status(); |
| 488 |
// Optional fields present. |
| 489 |
test_struct = Parse<TestStruct>( |
| 490 |
"{\"value\": \"3s\", \"optional_value\": \"3.2s\", " |
| 491 |
"\"absl_optional_value\": \"10s\", \"unique_ptr_value\": \"11s\"}"); |
| 492 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 493 |
EXPECT_EQ(test_struct->value, Duration::Seconds(3)); |
| 494 |
EXPECT_EQ(test_struct->optional_value, Duration::Milliseconds(3200)); |
| 495 |
EXPECT_EQ(test_struct->absl_optional_value, Duration::Seconds(10)); |
| 496 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 497 |
EXPECT_EQ(*test_struct->unique_ptr_value, Duration::Seconds(11)); |
| 498 |
// Wrong JSON type. |
| 499 |
test_struct = Parse<TestStruct>( |
| 500 |
"{\"value\": [], \"optional_value\": {}, " |
| 501 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": true}"); |
| 502 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 503 |
EXPECT_EQ(test_struct.status().message(), |
| 504 |
"errors validating JSON: [" |
| 505 |
"field:absl_optional_value error:is not a string; " |
| 506 |
"field:optional_value error:is not a string; " |
| 507 |
"field:unique_ptr_value error:is not a string; " |
| 508 |
"field:value error:is not a string]") |
| 509 |
<< test_struct.status(); |
| 510 |
} |
| 511 |
|
| 512 |
// |
| 513 |
// Json::Object tests |
| 514 |
// |
| 515 |
|
| 516 |
TEST(JsonObjectLoader, JsonObjectFields) { |
| 517 |
struct TestStruct { |
| 518 |
Json::Object value; |
| 519 |
Json::Object optional_value; |
| 520 |
absl::optional<Json::Object> absl_optional_value; |
| 521 |
std::unique_ptr<Json::Object> unique_ptr_value; |
| 522 |
|
| 523 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 524 |
static const auto* loader = |
| 525 |
JsonObjectLoader<TestStruct>() |
| 526 |
.Field("value", &TestStruct::value) |
| 527 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 528 |
.OptionalField("absl_optional_value", |
| 529 |
&TestStruct::absl_optional_value) |
| 530 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 531 |
.Finish(); |
| 532 |
return loader; |
| 533 |
} |
| 534 |
}; |
| 535 |
// Valid object. |
| 536 |
auto test_struct = Parse<TestStruct>("{\"value\": {\"a\":1}}"); |
| 537 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 538 |
EXPECT_EQ(Json{test_struct->value}.Dump(), "{\"a\":1}"); |
| 539 |
EXPECT_EQ(Json{test_struct->optional_value}.Dump(), "{}"); |
| 540 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 541 |
// Fails if required field is not present. |
| 542 |
test_struct = Parse<TestStruct>("{}"); |
| 543 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 544 |
EXPECT_EQ(test_struct.status().message(), |
| 545 |
"errors validating JSON: [field:value error:field not present]") |
| 546 |
<< test_struct.status(); |
| 547 |
// Optional fields present. |
| 548 |
test_struct = Parse<TestStruct>( |
| 549 |
"{\"value\": {\"a\":1}, \"optional_value\": {\"b\":2}, " |
| 550 |
"\"absl_optional_value\": {\"c\":3}, \"unique_ptr_value\": {\"d\":4}}"); |
| 551 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 552 |
EXPECT_EQ(Json{test_struct->value}.Dump(), "{\"a\":1}"); |
| 553 |
EXPECT_EQ(Json{test_struct->optional_value}.Dump(), "{\"b\":2}"); |
| 554 |
ASSERT_TRUE(test_struct->absl_optional_value.has_value()); |
| 555 |
EXPECT_EQ(Json{*test_struct->absl_optional_value}.Dump(), "{\"c\":3}"); |
| 556 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 557 |
EXPECT_EQ(Json{*test_struct->unique_ptr_value}.Dump(), "{\"d\":4}"); |
| 558 |
// Wrong JSON type. |
| 559 |
test_struct = Parse<TestStruct>( |
| 560 |
"{\"value\": [], \"optional_value\": true, " |
| 561 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": \"foo\"}"); |
| 562 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 563 |
EXPECT_EQ(test_struct.status().message(), |
| 564 |
"errors validating JSON: [" |
| 565 |
"field:absl_optional_value error:is not an object; " |
| 566 |
"field:optional_value error:is not an object; " |
| 567 |
"field:unique_ptr_value error:is not an object; " |
| 568 |
"field:value error:is not an object]") |
| 569 |
<< test_struct.status(); |
| 570 |
} |
| 571 |
|
| 572 |
// |
| 573 |
// map<> tests |
| 574 |
// |
| 575 |
|
| 576 |
TEST(JsonObjectLoader, MapFields) { |
| 577 |
struct TestStruct { |
| 578 |
std::map<std::string, int32_t> value; |
| 579 |
std::map<std::string, std::string> optional_value; |
| 580 |
absl::optional<std::map<std::string, bool>> absl_optional_value; |
| 581 |
std::unique_ptr<std::map<std::string, int32_t>> unique_ptr_value; |
| 582 |
|
| 583 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 584 |
static const auto* loader = |
| 585 |
JsonObjectLoader<TestStruct>() |
| 586 |
.Field("value", &TestStruct::value) |
| 587 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 588 |
.OptionalField("absl_optional_value", |
| 589 |
&TestStruct::absl_optional_value) |
| 590 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 591 |
.Finish(); |
| 592 |
return loader; |
| 593 |
} |
| 594 |
}; |
| 595 |
// Valid map. |
| 596 |
auto test_struct = Parse<TestStruct>("{\"value\": {\"a\":1}}"); |
| 597 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 598 |
EXPECT_THAT(test_struct->value, |
| 599 |
::testing::ElementsAre(::testing::Pair("a", 1))); |
| 600 |
EXPECT_THAT(test_struct->optional_value, ::testing::ElementsAre()); |
| 601 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 602 |
// Fails if required field is not present. |
| 603 |
test_struct = Parse<TestStruct>("{}"); |
| 604 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 605 |
EXPECT_EQ(test_struct.status().message(), |
| 606 |
"errors validating JSON: [field:value error:field not present]") |
| 607 |
<< test_struct.status(); |
| 608 |
// Optional fields present. |
| 609 |
test_struct = Parse<TestStruct>( |
| 610 |
"{\"value\": {\"a\":1}, \"optional_value\": {\"b\":\"foo\"}, " |
| 611 |
"\"absl_optional_value\": {\"c\":true}, " |
| 612 |
"\"unique_ptr_value\": {\"d\":4}}"); |
| 613 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 614 |
EXPECT_THAT(test_struct->value, |
| 615 |
::testing::ElementsAre(::testing::Pair("a", 1))); |
| 616 |
EXPECT_THAT(test_struct->optional_value, |
| 617 |
::testing::ElementsAre(::testing::Pair("b", "foo"))); |
| 618 |
ASSERT_TRUE(test_struct->absl_optional_value.has_value()); |
| 619 |
EXPECT_THAT(*test_struct->absl_optional_value, |
| 620 |
::testing::ElementsAre(::testing::Pair("c", true))); |
| 621 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 622 |
EXPECT_THAT(*test_struct->unique_ptr_value, |
| 623 |
::testing::ElementsAre(::testing::Pair("d", 4))); |
| 624 |
// Wrong JSON type. |
| 625 |
test_struct = Parse<TestStruct>( |
| 626 |
"{\"value\": [], \"optional_value\": true, " |
| 627 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": \"foo\"}"); |
| 628 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 629 |
EXPECT_EQ(test_struct.status().message(), |
| 630 |
"errors validating JSON: [" |
| 631 |
"field:absl_optional_value error:is not an object; " |
| 632 |
"field:optional_value error:is not an object; " |
| 633 |
"field:unique_ptr_value error:is not an object; " |
| 634 |
"field:value error:is not an object]") |
| 635 |
<< test_struct.status(); |
| 636 |
// Wrong JSON type for map value. |
| 637 |
test_struct = Parse<TestStruct>( |
| 638 |
"{\"value\": {\"a\":\"foo\"}, \"optional_value\": {\"b\":true}, " |
| 639 |
"\"absl_optional_value\": {\"c\":1}}"); |
| 640 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 641 |
EXPECT_EQ(test_struct.status().message(), |
| 642 |
"errors validating JSON: [" |
| 643 |
"field:absl_optional_value[\"c\"] error:is not a boolean; " |
| 644 |
"field:optional_value[\"b\"] error:is not a string; " |
| 645 |
"field:value[\"a\"] error:failed to parse number]") |
| 646 |
<< test_struct.status(); |
| 647 |
} |
| 648 |
|
| 649 |
// |
| 650 |
// vector<> tests |
| 651 |
// |
| 652 |
|
| 653 |
TEST(JsonObjectLoader, VectorFields) { |
| 654 |
struct TestStruct { |
| 655 |
std::vector<int32_t> value; |
| 656 |
std::vector<std::string> optional_value; |
| 657 |
absl::optional<std::vector<bool>> absl_optional_value; |
| 658 |
std::unique_ptr<std::vector<int32_t>> unique_ptr_value; |
| 659 |
|
| 660 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 661 |
static const auto* loader = |
| 662 |
JsonObjectLoader<TestStruct>() |
| 663 |
.Field("value", &TestStruct::value) |
| 664 |
.OptionalField("optional_value", &TestStruct::optional_value) |
| 665 |
.OptionalField("absl_optional_value", |
| 666 |
&TestStruct::absl_optional_value) |
| 667 |
.OptionalField("unique_ptr_value", &TestStruct::unique_ptr_value) |
| 668 |
.Finish(); |
| 669 |
return loader; |
| 670 |
} |
| 671 |
}; |
| 672 |
// Valid map. |
| 673 |
auto test_struct = Parse<TestStruct>("{\"value\": [1, 2, 3]}"); |
| 674 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 675 |
EXPECT_THAT(test_struct->value, ::testing::ElementsAre(1, 2, 3)); |
| 676 |
EXPECT_THAT(test_struct->optional_value, ::testing::ElementsAre()); |
| 677 |
EXPECT_FALSE(test_struct->absl_optional_value.has_value()); |
| 678 |
// Fails if required field is not present. |
| 679 |
test_struct = Parse<TestStruct>("{}"); |
| 680 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 681 |
EXPECT_EQ(test_struct.status().message(), |
| 682 |
"errors validating JSON: [field:value error:field not present]") |
| 683 |
<< test_struct.status(); |
| 684 |
// Optional fields present. |
| 685 |
test_struct = Parse<TestStruct>( |
| 686 |
"{\"value\": [4, 5, 6], \"optional_value\": [\"foo\", \"bar\"], " |
| 687 |
"\"absl_optional_value\": [true, false, true], " |
| 688 |
"\"unique_ptr_value\": [1, 2, 3]}"); |
| 689 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 690 |
EXPECT_THAT(test_struct->value, ::testing::ElementsAre(4, 5, 6)); |
| 691 |
EXPECT_THAT(test_struct->optional_value, |
| 692 |
::testing::ElementsAre("foo", "bar")); |
| 693 |
ASSERT_TRUE(test_struct->absl_optional_value.has_value()); |
| 694 |
EXPECT_THAT(*test_struct->absl_optional_value, |
| 695 |
::testing::ElementsAre(true, false, true)); |
| 696 |
ASSERT_NE(test_struct->unique_ptr_value, nullptr); |
| 697 |
EXPECT_THAT(*test_struct->unique_ptr_value, ::testing::ElementsAre(1, 2, 3)); |
| 698 |
// Wrong JSON type. |
| 699 |
test_struct = Parse<TestStruct>( |
| 700 |
"{\"value\": {}, \"optional_value\": true, " |
| 701 |
"\"absl_optional_value\": 1, \"unique_ptr_value\": \"foo\"}"); |
| 702 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 703 |
EXPECT_EQ(test_struct.status().message(), |
| 704 |
"errors validating JSON: [" |
| 705 |
"field:absl_optional_value error:is not an array; " |
| 706 |
"field:optional_value error:is not an array; " |
| 707 |
"field:unique_ptr_value error:is not an array; " |
| 708 |
"field:value error:is not an array]") |
| 709 |
<< test_struct.status(); |
| 710 |
// Wrong JSON type for map value. |
| 711 |
test_struct = Parse<TestStruct>( |
| 712 |
"{\"value\": [\"foo\", \"bar\"], \"optional_value\": [true, false], " |
| 713 |
"\"absl_optional_value\": [1, 2]}"); |
| 714 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 715 |
EXPECT_EQ(test_struct.status().message(), |
| 716 |
"errors validating JSON: [" |
| 717 |
"field:absl_optional_value[0] error:is not a boolean; " |
| 718 |
"field:absl_optional_value[1] error:is not a boolean; " |
| 719 |
"field:optional_value[0] error:is not a string; " |
| 720 |
"field:optional_value[1] error:is not a string; " |
| 721 |
"field:value[0] error:failed to parse number; " |
| 722 |
"field:value[1] error:failed to parse number]") |
| 723 |
<< test_struct.status(); |
| 724 |
} |
| 725 |
|
| 726 |
// |
| 727 |
// Nested struct tests |
| 728 |
// |
| 729 |
|
| 730 |
TEST(JsonObjectLoader, NestedStructFields) { |
| 731 |
struct NestedStruct { |
| 732 |
int32_t inner = 0; |
| 733 |
|
| 734 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 735 |
static const auto* loader = JsonObjectLoader<NestedStruct>() |
| 736 |
.Field("inner", &NestedStruct::inner) |
| 737 |
.Finish(); |
| 738 |
return loader; |
| 739 |
} |
| 740 |
}; |
| 741 |
struct TestStruct { |
| 742 |
NestedStruct outer; |
| 743 |
NestedStruct optional_outer; |
| 744 |
absl::optional<NestedStruct> absl_optional_outer; |
| 745 |
std::unique_ptr<NestedStruct> unique_ptr_outer; |
| 746 |
|
| 747 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 748 |
static const auto* loader = |
| 749 |
JsonObjectLoader<TestStruct>() |
| 750 |
.Field("outer", &TestStruct::outer) |
| 751 |
.OptionalField("optional_outer", &TestStruct::optional_outer) |
| 752 |
.OptionalField("absl_optional_outer", |
| 753 |
&TestStruct::absl_optional_outer) |
| 754 |
.OptionalField("unique_ptr_outer", &TestStruct::unique_ptr_outer) |
| 755 |
.Finish(); |
| 756 |
return loader; |
| 757 |
} |
| 758 |
}; |
| 759 |
// Valid nested struct. |
| 760 |
auto test_struct = Parse<TestStruct>("{\"outer\": {\"inner\": 1}}"); |
| 761 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 762 |
EXPECT_EQ(test_struct->outer.inner, 1); |
| 763 |
EXPECT_EQ(test_struct->optional_outer.inner, 0); |
| 764 |
EXPECT_FALSE(test_struct->absl_optional_outer.has_value()); |
| 765 |
// Fails if required field is not present. |
| 766 |
test_struct = Parse<TestStruct>("{}"); |
| 767 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 768 |
EXPECT_EQ(test_struct.status().message(), |
| 769 |
"errors validating JSON: [field:outer error:field not present]") |
| 770 |
<< test_struct.status(); |
| 771 |
// Fails if inner required field is not present. |
| 772 |
test_struct = Parse<TestStruct>("{\"outer\": {}}"); |
| 773 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 774 |
EXPECT_EQ( |
| 775 |
test_struct.status().message(), |
| 776 |
"errors validating JSON: [field:outer.inner error:field not present]") |
| 777 |
<< test_struct.status(); |
| 778 |
// Optional fields present. |
| 779 |
test_struct = Parse<TestStruct>( |
| 780 |
"{\"outer\": {\"inner\":1}, \"optional_outer\": {\"inner\":2}, " |
| 781 |
"\"absl_optional_outer\": {\"inner\":3}, " |
| 782 |
"\"unique_ptr_outer\": {\"inner\":4}}"); |
| 783 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 784 |
EXPECT_EQ(test_struct->outer.inner, 1); |
| 785 |
EXPECT_EQ(test_struct->optional_outer.inner, 2); |
| 786 |
ASSERT_TRUE(test_struct->absl_optional_outer.has_value()); |
| 787 |
EXPECT_EQ(test_struct->absl_optional_outer->inner, 3); |
| 788 |
ASSERT_NE(test_struct->unique_ptr_outer, nullptr); |
| 789 |
EXPECT_EQ(test_struct->unique_ptr_outer->inner, 4); |
| 790 |
// Wrong JSON type. |
| 791 |
test_struct = Parse<TestStruct>( |
| 792 |
"{\"outer\": \"foo\", \"optional_outer\": true, " |
| 793 |
"\"absl_optional_outer\": 1, \"unique_ptr_outer\": \"foo\"}"); |
| 794 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 795 |
EXPECT_EQ(test_struct.status().message(), |
| 796 |
"errors validating JSON: [" |
| 797 |
"field:absl_optional_outer error:is not an object; " |
| 798 |
"field:optional_outer error:is not an object; " |
| 799 |
"field:outer error:is not an object; " |
| 800 |
"field:unique_ptr_outer error:is not an object]") |
| 801 |
<< test_struct.status(); |
| 802 |
// Wrong JSON type for inner value. |
| 803 |
test_struct = Parse<TestStruct>( |
| 804 |
"{\"outer\": {\"inner\":\"foo\"}, \"optional_outer\": {\"inner\":true}, " |
| 805 |
"\"absl_optional_outer\": {\"inner\":[]}}"); |
| 806 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 807 |
EXPECT_EQ(test_struct.status().message(), |
| 808 |
"errors validating JSON: [" |
| 809 |
"field:absl_optional_outer.inner error:is not a number; " |
| 810 |
"field:optional_outer.inner error:is not a number; " |
| 811 |
"field:outer.inner error:failed to parse number]") |
| 812 |
<< test_struct.status(); |
| 813 |
} |
| 814 |
|
| 815 |
TEST(JsonObjectLoader, BareString) { |
| 816 |
auto parsed = Parse<std::string>("\"foo\""); |
| 817 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 818 |
EXPECT_EQ(*parsed, "foo"); |
| 819 |
} |
| 820 |
|
| 821 |
TEST(JsonObjectLoader, BareDuration) { |
| 822 |
auto parsed = Parse<Duration>("\"1.5s\""); |
| 823 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 824 |
EXPECT_EQ(*parsed, Duration::Milliseconds(1500)); |
| 825 |
} |
| 826 |
|
| 827 |
TEST(JsonObjectLoader, BareSignedInteger) { |
| 828 |
auto parsed = Parse<int32_t>("5"); |
| 829 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 830 |
EXPECT_EQ(*parsed, 5); |
| 831 |
} |
| 832 |
|
| 833 |
TEST(JsonObjectLoader, BareUnsignedInteger) { |
| 834 |
auto parsed = Parse<uint32_t>("5"); |
| 835 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 836 |
EXPECT_EQ(*parsed, 5); |
| 837 |
} |
| 838 |
|
| 839 |
TEST(JsonObjectLoader, BareFloat) { |
| 840 |
auto parsed = Parse<float>("5.2"); |
| 841 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 842 |
EXPECT_NEAR(*parsed, 5.2, 0.001); |
| 843 |
} |
| 844 |
|
| 845 |
TEST(JsonObjectLoader, BareBool) { |
| 846 |
auto parsed = Parse<bool>("true"); |
| 847 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 848 |
EXPECT_TRUE(*parsed); |
| 849 |
} |
| 850 |
|
| 851 |
TEST(JsonObjectLoader, BareUniquePtr) { |
| 852 |
auto parsed = Parse<std::unique_ptr<uint32_t>>("3"); |
| 853 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 854 |
ASSERT_NE(*parsed, nullptr); |
| 855 |
EXPECT_EQ(**parsed, 3); |
| 856 |
} |
| 857 |
|
| 858 |
TEST(JsonObjectLoader, BareVector) { |
| 859 |
auto parsed = Parse<std::vector<int32_t>>("[1, 2, 3]"); |
| 860 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 861 |
EXPECT_THAT(*parsed, ::testing::ElementsAre(1, 2, 3)); |
| 862 |
} |
| 863 |
|
| 864 |
TEST(JsonObjectLoader, BareMap) { |
| 865 |
auto parsed = |
| 866 |
Parse<std::map<std::string, int32_t>>("{\"a\":1, \"b\":2, \"c\":3}"); |
| 867 |
ASSERT_TRUE(parsed.ok()) << parsed.status(); |
| 868 |
EXPECT_THAT(*parsed, ::testing::ElementsAre(::testing::Pair("a", 1), |
| 869 |
::testing::Pair("b", 2), |
| 870 |
::testing::Pair("c", 3))); |
| 871 |
} |
| 872 |
|
| 873 |
TEST(JsonObjectLoader, IgnoresUnsupportedFields) { |
| 874 |
struct TestStruct { |
| 875 |
int32_t a = 0; |
| 876 |
|
| 877 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 878 |
static const auto* loader = |
| 879 |
JsonObjectLoader<TestStruct>().Field("a", &TestStruct::a).Finish(); |
| 880 |
return loader; |
| 881 |
} |
| 882 |
}; |
| 883 |
auto test_struct = Parse<TestStruct>("{\"a\": 3, \"b\":false}"); |
| 884 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 885 |
EXPECT_EQ(test_struct->a, 3); |
| 886 |
} |
| 887 |
|
| 888 |
TEST(JsonObjectLoader, IgnoresDisabledFields) { |
| 889 |
class FakeJsonArgs : public JsonArgs { |
| 890 |
public: |
| 891 |
FakeJsonArgs() = default; |
| 892 |
|
| 893 |
bool IsEnabled(absl::string_view key) const override { |
| 894 |
return key != "disabled"; |
| 895 |
} |
| 896 |
}; |
| 897 |
struct TestStruct { |
| 898 |
int32_t a = 0; |
| 899 |
int32_t b = 0; |
| 900 |
int32_t c = 0; |
| 901 |
|
| 902 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 903 |
static const auto* loader = |
| 904 |
JsonObjectLoader<TestStruct>() |
| 905 |
.Field("a", &TestStruct::a, "disabled") |
| 906 |
.OptionalField("b", &TestStruct::b, "disabled") |
| 907 |
.OptionalField("c", &TestStruct::c, "enabled") |
| 908 |
.Finish(); |
| 909 |
return loader; |
| 910 |
} |
| 911 |
}; |
| 912 |
// Fields "a" and "b" have the wrong types, but we ignore them, |
| 913 |
// because they're disabled. |
| 914 |
auto test_struct = |
| 915 |
Parse<TestStruct>("{\"a\":false, \"b\":false, \"c\":1}", FakeJsonArgs()); |
| 916 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 917 |
EXPECT_EQ(test_struct->a, 0); |
| 918 |
EXPECT_EQ(test_struct->b, 0); |
| 919 |
EXPECT_EQ(test_struct->c, 1); |
| 920 |
} |
| 921 |
|
| 922 |
TEST(JsonObjectLoader, PostLoadHook) { |
| 923 |
struct TestStruct { |
| 924 |
int32_t a = 0; |
| 925 |
|
| 926 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 927 |
static const auto* loader = JsonObjectLoader<TestStruct>() |
| 928 |
.OptionalField("a", &TestStruct::a) |
| 929 |
.Finish(); |
| 930 |
return loader; |
| 931 |
} |
| 932 |
|
| 933 |
void JsonPostLoad(const Json& /*source*/, const JsonArgs& /*args*/, |
| 934 |
ValidationErrors* /*errors*/) { |
| 935 |
++a; |
| 936 |
} |
| 937 |
}; |
| 938 |
auto test_struct = Parse<TestStruct>("{\"a\": 1}"); |
| 939 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 940 |
EXPECT_EQ(test_struct->a, 2); |
| 941 |
test_struct = Parse<TestStruct>("{}"); |
| 942 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 943 |
EXPECT_EQ(test_struct->a, 1); |
| 944 |
} |
| 945 |
|
| 946 |
TEST(JsonObjectLoader, CustomValidationInPostLoadHook) { |
| 947 |
struct TestStruct { |
| 948 |
int32_t a = 0; |
| 949 |
|
| 950 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 951 |
static const auto* loader = |
| 952 |
JsonObjectLoader<TestStruct>().Field("a", &TestStruct::a).Finish(); |
| 953 |
return loader; |
| 954 |
} |
| 955 |
|
| 956 |
void JsonPostLoad(const Json& /*source*/, const JsonArgs& /*args*/, |
| 957 |
ValidationErrors* errors) { |
| 958 |
ValidationErrors::ScopedField field(errors, ".a"); |
| 959 |
if (!errors->FieldHasErrors() && a <= 0) { |
| 960 |
errors->AddError("must be greater than 0"); |
| 961 |
} |
| 962 |
} |
| 963 |
}; |
| 964 |
// Value greater than 0. |
| 965 |
auto test_struct = Parse<TestStruct>("{\"a\": 1}"); |
| 966 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 967 |
EXPECT_EQ(test_struct->a, 1); |
| 968 |
// Value 0, triggers custom validation. |
| 969 |
test_struct = Parse<TestStruct>("{\"a\": 0}"); |
| 970 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 971 |
EXPECT_EQ(test_struct.status().message(), |
| 972 |
"errors validating JSON: [field:a error:must be greater than 0]") |
| 973 |
<< test_struct.status(); |
| 974 |
// Invalid type, generates built-in parsing error, so custom |
| 975 |
// validation will not generate a new error. |
| 976 |
test_struct = Parse<TestStruct>("{\"a\": []}"); |
| 977 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 978 |
EXPECT_EQ(test_struct.status().message(), |
| 979 |
"errors validating JSON: [field:a error:is not a number]") |
| 980 |
<< test_struct.status(); |
| 981 |
} |
| 982 |
|
| 983 |
TEST(JsonObjectLoader, LoadRefCountedFromJson) { |
| 984 |
struct TestStruct : public RefCounted<TestStruct> { |
| 985 |
int32_t a = 0; |
| 986 |
|
| 987 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 988 |
static const auto* loader = |
| 989 |
JsonObjectLoader<TestStruct>().Field("a", &TestStruct::a).Finish(); |
| 990 |
return loader; |
| 991 |
} |
| 992 |
}; |
| 993 |
// Valid. |
| 994 |
{ |
| 995 |
absl::string_view json_str = "{\"a\":1}"; |
| 996 |
auto json = Json::Parse(json_str); |
| 997 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 998 |
absl::StatusOr<RefCountedPtr<TestStruct>> test_struct = |
| 999 |
LoadRefCountedFromJson<TestStruct>(*json, JsonArgs()); |
| 1000 |
ASSERT_TRUE(test_struct.ok()) << test_struct.status(); |
| 1001 |
EXPECT_EQ((*test_struct)->a, 1); |
| 1002 |
} |
| 1003 |
// Invalid. |
| 1004 |
{ |
| 1005 |
absl::string_view json_str = "{\"a\":\"foo\"}"; |
| 1006 |
auto json = Json::Parse(json_str); |
| 1007 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 1008 |
absl::StatusOr<RefCountedPtr<TestStruct>> test_struct = |
| 1009 |
LoadRefCountedFromJson<TestStruct>(*json, JsonArgs()); |
| 1010 |
EXPECT_EQ(test_struct.status().code(), absl::StatusCode::kInvalidArgument); |
| 1011 |
EXPECT_EQ(test_struct.status().message(), |
| 1012 |
"errors validating JSON: [field:a error:failed to parse number]") |
| 1013 |
<< test_struct.status(); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
TEST(JsonObjectLoader, LoadFromJsonWithValidationErrors) { |
| 1018 |
struct TestStruct { |
| 1019 |
int32_t a = 0; |
| 1020 |
|
| 1021 |
static const JsonLoaderInterface* JsonLoader(const JsonArgs&) { |
| 1022 |
static const auto* loader = |
| 1023 |
JsonObjectLoader<TestStruct>().Field("a", &TestStruct::a).Finish(); |
| 1024 |
return loader; |
| 1025 |
} |
| 1026 |
}; |
| 1027 |
// Valid. |
| 1028 |
{ |
| 1029 |
absl::string_view json_str = "{\"a\":1}"; |
| 1030 |
auto json = Json::Parse(json_str); |
| 1031 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 1032 |
ValidationErrors errors; |
| 1033 |
TestStruct test_struct = |
| 1034 |
LoadFromJson<TestStruct>(*json, JsonArgs(), &errors); |
| 1035 |
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors"); |
| 1036 |
EXPECT_EQ(test_struct.a, 1); |
| 1037 |
} |
| 1038 |
// Invalid. |
| 1039 |
{ |
| 1040 |
absl::string_view json_str = "{\"a\":\"foo\"}"; |
| 1041 |
auto json = Json::Parse(json_str); |
| 1042 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 1043 |
ValidationErrors errors; |
| 1044 |
LoadFromJson<TestStruct>(*json, JsonArgs(), &errors); |
| 1045 |
absl::Status status = errors.status("errors validating JSON"); |
| 1046 |
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument); |
| 1047 |
EXPECT_EQ(status.message(), |
| 1048 |
"errors validating JSON: [field:a error:failed to parse number]") |
| 1049 |
<< status; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
TEST(JsonObjectLoader, LoadJsonObjectField) { |
| 1054 |
absl::string_view json_str = "{\"int\":1}"; |
| 1055 |
auto json = Json::Parse(json_str); |
| 1056 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 1057 |
// Load a valid field. |
| 1058 |
{ |
| 1059 |
ValidationErrors errors; |
| 1060 |
auto value = LoadJsonObjectField<int32_t>(json->object_value(), JsonArgs(), |
| 1061 |
"int", &errors); |
| 1062 |
ASSERT_TRUE(value.has_value()) << errors.status("unexpected errors"); |
| 1063 |
EXPECT_EQ(*value, 1); |
| 1064 |
EXPECT_TRUE(errors.ok()); |
| 1065 |
} |
| 1066 |
// An optional field that is not present. |
| 1067 |
{ |
| 1068 |
ValidationErrors errors; |
| 1069 |
auto value = LoadJsonObjectField<int32_t>(json->object_value(), JsonArgs(), |
| 1070 |
"not_present", &errors, |
| 1071 |
/*required=*/false); |
| 1072 |
EXPECT_FALSE(value.has_value()); |
| 1073 |
EXPECT_TRUE(errors.ok()); |
| 1074 |
} |
| 1075 |
// A required field that is not present. |
| 1076 |
{ |
| 1077 |
ValidationErrors errors; |
| 1078 |
auto value = LoadJsonObjectField<int32_t>(json->object_value(), JsonArgs(), |
| 1079 |
"not_present", &errors); |
| 1080 |
EXPECT_FALSE(value.has_value()); |
| 1081 |
auto status = errors.status("errors validating JSON"); |
| 1082 |
EXPECT_THAT(status.code(), absl::StatusCode::kInvalidArgument); |
| 1083 |
EXPECT_EQ(status.message(), |
| 1084 |
"errors validating JSON: [" |
| 1085 |
"field:not_present error:field not present]") |
| 1086 |
<< status; |
| 1087 |
} |
| 1088 |
// Value has the wrong type. |
| 1089 |
{ |
| 1090 |
ValidationErrors errors; |
| 1091 |
auto value = LoadJsonObjectField<std::string>(json->object_value(), |
| 1092 |
JsonArgs(), "int", &errors); |
| 1093 |
EXPECT_FALSE(value.has_value()); |
| 1094 |
auto status = errors.status("errors validating JSON"); |
| 1095 |
EXPECT_THAT(status.code(), absl::StatusCode::kInvalidArgument); |
| 1096 |
EXPECT_EQ(status.message(), |
| 1097 |
"errors validating JSON: [field:int error:is not a string]") |
| 1098 |
<< status; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
} // namespace |
| 1103 |
} // namespace grpc_core |
| 1104 |
|
| 1105 |
int main(int argc, char** argv) { |
| 1106 |
::testing::InitGoogleTest(&argc, argv); |
| 1107 |
return RUN_ALL_TESTS(); |
| 1108 |
} |