userver: /data/code/userver/libraries/protobuf/tests/json/complex_to_json_test.cpp Source File
Loading...
Searching...
No Matches
complex_to_json_test.cpp
1#include <gtest/gtest.h>
2
3#include <google/protobuf/util/json_util.h>
4
5#include <userver/formats/json/serialize.hpp>
6#include <userver/protobuf/json/convert.hpp>
7#include <userver/utest/assert_macros.hpp>
8
9#include "utils.hpp"
10
11USERVER_NAMESPACE_BEGIN
12
13namespace protobuf::json::tests {
14
15/* NOTE: Uncomment when linkage is fixed (see comment in the convert.hpp)
16
17using ProtobufStringType =
18 decltype(std::declval<::google::protobuf::Reflection>()
19 .GetString(std::declval<const ::google::protobuf::Message&>(), nullptr));
20
21TEST(ComplexToJsonSuccessTest, Test) {
22 proto_json::messages::ComplexMessage message;
23 proto_json::messages::ComplexMessage::Bottom bottom_message;
24
25 for (int i = 1; i <= 2; ++i) {
26 auto inter = message.add_inters();
27 auto bottoms = inter->mutable_bottoms();
28 (*bottoms)["aaa"].set_field1(i * 10);
29 (*bottoms)["aaa"].mutable_field2()->set_seconds(i * 1);
30 (*bottoms)["aaa"].mutable_field2()->set_nanos(i * 2);
31 (*bottoms)["bbb"].set_field1(i * 20);
32 (*bottoms)["bbb"].mutable_field2()->set_seconds(i * 3);
33 (*bottoms)["bbb"].mutable_field2()->set_nanos(i * 4);
34 }
35
36 formats::json::ValueBuilder builder;
37 builder = message;
38
39 formats::json::Value json, expected_json, sample_json;
40 json = builder.ExtractValue();
41 UASSERT_NO_THROW((
42 expected_json = formats::json::FromString(
43 R"({
44 "inters": [
45 {
46 "bottoms": {
47 "aaa": {"field1":10, "field2":"1.000000002s"},
48 "bbb": {"field1":20, "field2":"3.000000004s"}
49 }
50 },
51 {
52 "bottoms": {
53 "aaa": {"field1":20, "field2":"2.000000004s"},
54 "bbb": {"field1":40, "field2":"6.000000008s"}
55 }
56 }
57 ]
58 })"
59 )
60 ));
61
62 ProtobufStringType sample_json_str;
63 ASSERT_TRUE(::google::protobuf::util::MessageToJsonString(message, &sample_json_str).ok());
64 UASSERT_NO_THROW((sample_json = formats::json::FromString(sample_json_str)));
65
66 EXPECT_EQ(json, expected_json);
67 EXPECT_EQ(expected_json, sample_json);
68
69 UASSERT_NO_THROW(
70 (bottom_message = json["inters"][1]["bottoms"]["aaa"].As<proto_json::messages::ComplexMessage::Bottom>())
71 );
72 EXPECT_EQ(bottom_message.field1(), std::uint32_t{20});
73 EXPECT_EQ(bottom_message.field2().seconds(), 2);
74 EXPECT_EQ(bottom_message.field2().nanos(), 4);
75}
76
77TEST(ComplexToJsonFailureTest, Test) {
78 proto_json::messages::ComplexMessage message;
79 proto_json::messages::ComplexMessage::Bottom bottom_message;
80
81 auto inter = message.add_inters();
82 auto bottoms = inter->mutable_bottoms();
83 (*bottoms)["aaa"].set_field1(10);
84 (*bottoms)["aaa"].mutable_field2()->set_seconds(1);
85 (*bottoms)["aaa"].mutable_field2()->set_nanos(-1);
86
87 formats::json::ValueBuilder builder;
88 EXPECT_PRINT_ERROR((builder = message), PrintErrorCode::kInvalidValue, "inters[0].bottoms['aaa'].field2");
89}
90*/
91
92} // namespace protobuf::json::tests
93
94USERVER_NAMESPACE_END