channel_trace_proto_helper.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <gtest/gtest.h>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
29 
31 #include "src/core/lib/json/json.h"
32 #include "src/proto/grpc/channelz/channelz.pb.h"
33 
34 namespace grpc {
35 
36 namespace {
37 
38 // Generic helper that takes in a json string, converts it to a proto, and
39 // then back to json. This ensures that the json string was correctly formatted
40 // according to https://developers.google.com/protocol-buffers/docs/proto3#json
41 template <typename Message>
42 void VaidateProtoJsonTranslation(const std::string& json_str) {
43  Message msg;
44  grpc::protobuf::json::JsonParseOptions parse_options;
45  // If the following line is failing, then uncomment the last line of the
46  // comment, and uncomment the lines that print the two strings. You can
47  // then compare the output, and determine what fields are missing.
48  //
49  // parse_options.ignore_unknown_fields = true;
51  grpc::protobuf::json::JsonStringToMessage(json_str, &msg, parse_options);
52  EXPECT_TRUE(s.ok());
53  std::string proto_json_str;
54  grpc::protobuf::json::JsonPrintOptions print_options;
55  // We usually do not want this to be true, however it can be helpful to
56  // uncomment and see the output produced then all fields are printed.
57  // print_options.always_print_primitive_fields = true;
59  EXPECT_TRUE(s.ok());
60  // Parse JSON and re-dump to string, to make sure formatting is the
61  // same as what would be generated by our JSON library.
63  grpc_core::Json parsed_json =
64  grpc_core::Json::Parse(proto_json_str.c_str(), &error);
67  proto_json_str = parsed_json.Dump();
68  // uncomment these to compare the json strings.
69  // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str());
70  // gpr_log(GPR_ERROR, "proto json: %s", proto_json_str.c_str());
71  EXPECT_EQ(json_str, proto_json_str);
72 }
73 
74 } // namespace
75 
76 namespace testing {
77 
78 void ValidateChannelTraceProtoJsonTranslation(const char* json_c_str) {
79  VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
80 }
81 
82 void ValidateChannelProtoJsonTranslation(const char* json_c_str) {
83  VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
84 }
85 
87  const char* json_c_str) {
88  VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
89  json_c_str);
90 }
91 
92 void ValidateGetChannelResponseProtoJsonTranslation(const char* json_c_str) {
93  VaidateProtoJsonTranslation<grpc::channelz::v1::GetChannelResponse>(
94  json_c_str);
95 }
96 
97 void ValidateGetServerResponseProtoJsonTranslation(const char* json_c_str) {
98  VaidateProtoJsonTranslation<grpc::channelz::v1::GetServerResponse>(
99  json_c_str);
100 }
101 
102 void ValidateSubchannelProtoJsonTranslation(const char* json_c_str) {
103  VaidateProtoJsonTranslation<grpc::channelz::v1::Subchannel>(json_c_str);
104 }
105 
106 void ValidateServerProtoJsonTranslation(const char* json_c_str) {
107  VaidateProtoJsonTranslation<grpc::channelz::v1::Server>(json_c_str);
108 }
109 
111  VaidateProtoJsonTranslation<grpc::channelz::v1::GetServersResponse>(
112  json_c_str);
113 }
114 
115 } // namespace testing
116 } // namespace grpc
testing
Definition: aws_request_signer_test.cc:25
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation
void ValidateGetTopChannelsResponseProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:86
grpc::testing::ValidateServerProtoJsonTranslation
void ValidateServerProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:106
grpc_core::Json::type
Type type() const
Definition: src/core/lib/json/json.h:174
grpc
Definition: grpcpp/alarm.h:33
grpc_core::Json::Type::OBJECT
@ OBJECT
grpc::testing::ValidateChannelProtoJsonTranslation
void ValidateChannelProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:82
grpc::ASSERT_EQ
ASSERT_EQ(sizeof(valid_json), fwrite(valid_json, 1, sizeof(valid_json), creds_file))
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc::testing::ValidateChannelTraceProtoJsonTranslation
void ValidateChannelTraceProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:78
channel_trace_proto_helper.h
grpc::testing::ValidateGetChannelResponseProtoJsonTranslation
void ValidateGetChannelResponseProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:92
absl::FormatConversionChar::s
@ s
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
config.h
grpc::testing::ValidateGetServerResponseProtoJsonTranslation
void ValidateGetServerResponseProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:97
grpc::testing::ValidateSubchannelProtoJsonTranslation
void ValidateSubchannelProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:102
grpc.h
error.h
json.h
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
grpc::testing::ValidateGetServersResponseProtoJsonTranslation
void ValidateGetServersResponseProtoJsonTranslation(const char *json_c_str)
Definition: channel_trace_proto_helper.cc:110
grpc_core::Json::Parse
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
Definition: json_reader.cc:899
config_protobuf.h
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
grpc_error
Definition: error_internal.h:42
google::protobuf::util::MessageToJsonString
util::Status MessageToJsonString(const Message &message, std::string *output, const JsonOptions &options)
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/util/json_util.cc:242
google::protobuf::util::JsonStringToMessage
util::Status JsonStringToMessage(StringPiece input, Message *message, const JsonParseOptions &options)
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/util/json_util.cc:258
grpc_core::Json
Definition: src/core/lib/json/json.h:37
grpc_core::Json::Dump
std::string Dump(int indent=0) const
Definition: json_writer.cc:336
Message
Definition: protobuf/php/ext/google/protobuf/message.c:53
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52