|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | +// clang-format off |
| 24 | +// this order is required |
| 25 | +#include <olp/dataservice/read/Types.h> |
| 26 | +#include "generated/parser/PartitionsParser.h" |
| 27 | +#include "JsonResultParser.h" |
| 28 | +#include "generated/serializer/PartitionsSerializer.h" |
| 29 | +#include "generated/serializer/JsonSerializer.h" |
| 30 | +#include "ReadDefaultResponses.h" |
| 31 | +#include "ExtendedApiResponse.h" |
| 32 | +// clang-format on |
| 33 | + |
| 34 | +namespace dr = olp::dataservice::read; |
| 35 | + |
| 36 | +TEST(JsonResultParserTest, ParseResult) { |
| 37 | + auto partitions = |
| 38 | + mockserver::ReadDefaultResponses::GeneratePartitionsResponse(); |
| 39 | + auto partitions_string = olp::serializer::serialize(partitions); |
| 40 | + |
| 41 | + { |
| 42 | + SCOPED_TRACE("Verify valid partitions responce"); |
| 43 | + auto str = std::stringstream(partitions_string); |
| 44 | + |
| 45 | + auto responce = olp::parser::parse_result<dr::PartitionsResponse>(str); |
| 46 | + ASSERT_TRUE(responce.IsSuccessful()); |
| 47 | + ASSERT_EQ(10u, responce.GetResult().GetPartitions().size()); |
| 48 | + } |
| 49 | + { |
| 50 | + SCOPED_TRACE("Verify corrupted with additional symbol"); |
| 51 | + auto str = std::stringstream(partitions_string + "_"); |
| 52 | + |
| 53 | + auto responce = olp::parser::parse_result<dr::PartitionsResponse>(str); |
| 54 | + ASSERT_FALSE(responce.IsSuccessful()); |
| 55 | + ASSERT_EQ(responce.GetError().GetErrorCode(), |
| 56 | + olp::client::ErrorCode::Unknown); |
| 57 | + ASSERT_EQ(responce.GetError().GetMessage(), "Fail parsing response."); |
| 58 | + } |
| 59 | + { |
| 60 | + SCOPED_TRACE("Verify merged responses"); |
| 61 | + auto partitions2 = |
| 62 | + mockserver::ReadDefaultResponses::GeneratePartitionsResponse(2); |
| 63 | + auto partitions_string2 = olp::serializer::serialize(partitions2); |
| 64 | + |
| 65 | + auto str = std::stringstream(partitions_string + partitions_string2); |
| 66 | + |
| 67 | + auto responce = olp::parser::parse_result<dr::PartitionsResponse>(str); |
| 68 | + ASSERT_FALSE(responce.IsSuccessful()); |
| 69 | + ASSERT_EQ(responce.GetError().GetErrorCode(), |
| 70 | + olp::client::ErrorCode::Unknown); |
| 71 | + ASSERT_EQ(responce.GetError().GetMessage(), "Fail parsing response."); |
| 72 | + } |
| 73 | + { |
| 74 | + SCOPED_TRACE("Verify missed last brace"); |
| 75 | + std::string json_input = |
| 76 | + partitions_string.substr(0, partitions_string.size() - 1); |
| 77 | + |
| 78 | + auto str = std::stringstream(json_input); |
| 79 | + auto responce = olp::parser::parse_result<dr::PartitionsResponse>(str); |
| 80 | + ASSERT_FALSE(responce.IsSuccessful()); |
| 81 | + ASSERT_EQ(responce.GetError().GetErrorCode(), |
| 82 | + olp::client::ErrorCode::Unknown); |
| 83 | + ASSERT_EQ(responce.GetError().GetMessage(), "Fail parsing response."); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +TEST(JsonResultParserTest, ExtendedResponse) { |
| 88 | + auto partitions = |
| 89 | + mockserver::ReadDefaultResponses::GeneratePartitionsResponse(); |
| 90 | + auto partitions_string = olp::serializer::serialize(partitions); |
| 91 | + using ExtendedResponse = |
| 92 | + dr::ExtendedApiResponse<dr::PartitionsResult, olp::client::ApiError, int>; |
| 93 | + |
| 94 | + { |
| 95 | + SCOPED_TRACE("Verify valid extended partitions responce"); |
| 96 | + auto str = std::stringstream(partitions_string); |
| 97 | + |
| 98 | + auto responce = olp::parser::parse_result<ExtendedResponse>(str, 100); |
| 99 | + ASSERT_TRUE(responce.IsSuccessful()); |
| 100 | + ASSERT_EQ(10u, responce.GetResult().GetPartitions().size()); |
| 101 | + ASSERT_EQ(100, responce.GetPayload()); |
| 102 | + } |
| 103 | + { |
| 104 | + SCOPED_TRACE("Verify corrupted with additional symbol"); |
| 105 | + auto str = std::stringstream(partitions_string + "_"); |
| 106 | + |
| 107 | + auto responce = olp::parser::parse_result<ExtendedResponse>(str, 100); |
| 108 | + ASSERT_FALSE(responce.IsSuccessful()); |
| 109 | + ASSERT_EQ(responce.GetError().GetErrorCode(), |
| 110 | + olp::client::ErrorCode::Unknown); |
| 111 | + ASSERT_EQ(responce.GetError().GetMessage(), "Fail parsing response."); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +TEST(JsonResultParserTest, WithArgs) { |
| 116 | + auto partitions = |
| 117 | + mockserver::ReadDefaultResponses::GeneratePartitionsResponse(); |
| 118 | + auto partitions_string = olp::serializer::serialize(partitions); |
| 119 | + |
| 120 | + struct Result { |
| 121 | + dr::PartitionsResult result; |
| 122 | + std::string data; |
| 123 | + int additional_data; |
| 124 | + }; |
| 125 | + |
| 126 | + using ResponseWithArgs = dr::Response<Result>; |
| 127 | + |
| 128 | + { |
| 129 | + SCOPED_TRACE("Verify valid partitions responce with some args"); |
| 130 | + auto str = std::stringstream(partitions_string); |
| 131 | + |
| 132 | + auto responce = |
| 133 | + olp::parser::parse_result<ResponseWithArgs, dr::PartitionsResult>( |
| 134 | + str, "data", 10); |
| 135 | + ASSERT_TRUE(responce.IsSuccessful()); |
| 136 | + ASSERT_EQ(10u, responce.GetResult().result.GetPartitions().size()); |
| 137 | + ASSERT_EQ("data", responce.GetResult().data); |
| 138 | + ASSERT_EQ(10, responce.GetResult().additional_data); |
| 139 | + } |
| 140 | + { |
| 141 | + SCOPED_TRACE("Verify corrupted with additional symbol"); |
| 142 | + auto str = std::stringstream(partitions_string + "_"); |
| 143 | + |
| 144 | + auto responce = |
| 145 | + olp::parser::parse_result<ResponseWithArgs, dr::PartitionsResult>( |
| 146 | + str, "data", 10); |
| 147 | + ASSERT_FALSE(responce.IsSuccessful()); |
| 148 | + ASSERT_EQ(responce.GetError().GetErrorCode(), |
| 149 | + olp::client::ErrorCode::Unknown); |
| 150 | + ASSERT_EQ(responce.GetError().GetMessage(), "Fail parsing response."); |
| 151 | + } |
| 152 | +} |
0 commit comments