blob: ee310784c21779b55ee6822c6c11c4c6c6d8040d [file] [log] [blame]
ncteisen3b42f832018-03-19 13:22:35 -07001/*
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
30namespace grpc {
31namespace testing {
32
ncteisenc3c6e062018-05-09 11:10:21 -070033namespace {
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
38template <typename Message>
39void VaidateProtoJsonTranslation(char* json_c_str) {
40 std::string json_str(json_c_str);
41 Message msg;
ncteisen3b42f832018-03-19 13:22:35 -070042 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 //
ncteisenb69f1f62018-05-09 10:38:44 -070047 // parse_options.ignore_unknown_fields = true;
ncteisen9a1bb052018-05-30 16:17:06 -070048 EXPECT_EQ(google::protobuf::util::JsonStringToMessage(json_str, &msg,
ncteisenc3c6e062018-05-09 11:10:21 -070049 parse_options),
ncteisen3b42f832018-03-19 13:22:35 -070050 google::protobuf::util::Status::OK);
51 std::string proto_json_str;
ncteisen9a1bb052018-05-30 16:17:06 -070052 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),
ncteisen3b42f832018-03-19 13:22:35 -070058 google::protobuf::util::Status::OK);
59 // uncomment these to compare the the json strings.
ncteisenc3c6e062018-05-09 11:10:21 -070060 // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str());
ncteisen3b42f832018-03-19 13:22:35 -070061 // gpr_log(GPR_ERROR, "proto json: %s", proto_json_str.c_str());
ncteisen9a1bb052018-05-30 16:17:06 -070062 EXPECT_EQ(json_str, proto_json_str);
ncteisenc3c6e062018-05-09 11:10:21 -070063}
64
65} // namespace
66
67void ValidateChannelTraceProtoJsonTranslation(char* tracer_json_c_str) {
68 VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(
69 tracer_json_c_str);
70}
71
72void ValidateChannelProtoJsonTranslation(char* channel_json_c_str) {
73 VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(channel_json_c_str);
ncteisen3b42f832018-03-19 13:22:35 -070074}
75
76} // namespace testing
77} // namespace grpc