ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 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 | |
| 19 | #include "test/cpp/util/channel_trace_proto_helper.h" |
| 20 | |
| 21 | #include <google/protobuf/text_format.h> |
| 22 | #include <google/protobuf/util/json_util.h> |
| 23 | |
| 24 | #include <grpc/grpc.h> |
| 25 | #include <grpc/support/log.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "src/proto/grpc/channelz/channelz.pb.h" |
| 29 | |
| 30 | namespace grpc { |
| 31 | namespace testing { |
| 32 | |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | // Generic helper that takes in a json string, converts it to a proto, and |
| 36 | // then back to json. This ensures that the json string was correctly formatted |
| 37 | // according to https://developers.google.com/protocol-buffers/docs/proto3#json |
| 38 | template <typename Message> |
| 39 | void VaidateProtoJsonTranslation(char* json_c_str) { |
| 40 | std::string json_str(json_c_str); |
| 41 | Message msg; |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 42 | google::protobuf::util::JsonParseOptions parse_options; |
| 43 | // If the following line is failing, then uncomment the last line of the |
| 44 | // comment, and uncomment the lines that print the two strings. You can |
| 45 | // then compare the output, and determine what fields are missing. |
| 46 | // |
ncteisen | b69f1f6 | 2018-05-09 10:38:44 -0700 | [diff] [blame] | 47 | // parse_options.ignore_unknown_fields = true; |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 48 | EXPECT_EQ(google::protobuf::util::JsonStringToMessage(json_str, &msg, |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 49 | parse_options), |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 50 | google::protobuf::util::Status::OK); |
| 51 | std::string proto_json_str; |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 52 | google::protobuf::util::JsonPrintOptions print_options; |
| 53 | // We usually do not want this to be true, however it can be helpful to |
| 54 | // uncomment and see the output produced then all fields are printed. |
| 55 | // print_options.always_print_primitive_fields = true; |
| 56 | EXPECT_EQ(google::protobuf::util::MessageToJsonString(msg, &proto_json_str, |
| 57 | print_options), |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 58 | google::protobuf::util::Status::OK); |
| 59 | // uncomment these to compare the the json strings. |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 60 | // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str()); |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 61 | // gpr_log(GPR_ERROR, "proto json: %s", proto_json_str.c_str()); |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 62 | EXPECT_EQ(json_str, proto_json_str); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 67 | void ValidateChannelTraceProtoJsonTranslation(char* json_c_str) { |
| 68 | VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 69 | } |
| 70 | |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 71 | void ValidateChannelProtoJsonTranslation(char* json_c_str) { |
| 72 | VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str); |
| 73 | } |
| 74 | |
| 75 | void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str) { |
| 76 | VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>( |
| 77 | json_c_str); |
ncteisen | 3b42f83 | 2018-03-19 13:22:35 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace testing |
| 81 | } // namespace grpc |