Skip to content

Commit 0d4f7dd

Browse files
author
Liubov Didkivska
authored
Tests for parse_result (#1042)
Json parser returns error if parse fail. Add test to check parsing success with valid response and fails with invalid json. Relates-To: OLPEDGE-2147 Signed-off-by: Liubov Didkivska <ext-liubov.didkivska@here.com>
1 parent a921428 commit 0d4f7dd

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

olp-cpp-sdk-dataservice-read/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(OLP_SDK_DATASERVICE_READ_TEST_SOURCES
2222
CatalogRepositoryTest.cpp
2323
DataCacheRepositoryTest.cpp
2424
DataRepositoryTest.cpp
25+
JsonResultParserTest.cpp
2526
MetadataApiTest.cpp
2627
ParserTest.cpp
2728
PartitionsCacheRepositoryTest.cpp
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)