1 #include <UnitTest++/UnitTest++.h>
9 #include "JsonSerializer.h"
10 #include "StringUtils.h"
12 #include "SerializerTestDefines.h"
14 SUITE(ArticleJsonSerializerTests
)
16 using namespace WikiWalker
;
17 using namespace WikiWalker::CollectionUtils
;
19 TEST(WriteUnanalyzedArticleWithoutLinks_LinksIsNull
)
22 std::ostringstream oss
;
24 CollectionUtils::add(ac
, std::make_shared
<Article
>("Farm"));
26 atj
.serialize(ac
, oss
);
28 auto serString
= oss
.str();
29 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
31 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
33 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
34 R
"({"Farm
":{"forward_links
":null}})") !=
38 TEST(WriteAnalyzedArticleWithoutLinks_LinksIsEmptyArray
)
41 std::ostringstream oss
;
44 auto a
= std::make_shared
<Article
>("Farm");
45 CollectionUtils::add(ac
, a
);
48 atj
.serialize(ac
, oss
);
50 auto serString
= oss
.str();
51 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
53 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
55 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
56 R
"({"Farm
":{"forward_links
":[]}})") !=
60 TEST(WriteArticleWithOneLink
)
63 std::ostringstream oss
;
66 // yes, only a is inserted, since we want to emulate article-only
67 auto a
= std::make_shared
<Article
>("Farm");
68 CollectionUtils::add(ac
, a
);
70 auto linked
= std::make_shared
<Article
>("Animal");
73 atj
.serialize(ac
, oss
);
75 auto serString
= oss
.str();
76 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
78 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
80 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
81 R
"({"Farm
":{"forward_links
":["Animal
"]}})") !=
85 TEST(WriteArticleWithMultipleLinks
)
88 std::ostringstream oss
;
91 // yes, only a is inserted, since we want to emulate article-only
92 auto a
= std::make_shared
<Article
>("Farm");
93 CollectionUtils::add(ac
, a
);
95 auto al1
= std::make_shared
<Article
>("Animal"),
96 al2
= std::make_shared
<Article
>("Pig"),
97 al3
= std::make_shared
<Article
>("Equality");
103 atj
.serialize(ac
, oss
);
105 auto serString
= oss
.str();
106 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
108 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
110 CHECK(serString
.find(
111 WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
112 R
"({"Farm
":{"forward_links
":["Animal
","Pig
","Equality
"]}})") !=
116 TEST(WriteEmptyArticleCollection
)
119 ArticleCollection ac
;
120 std::ostringstream oss
;
122 atj
.serialize(ac
, oss
);
124 auto serString
= oss
.str();
125 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
127 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
129 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
"{}") !=
133 TEST(WriteArticleCollection_OneUnanalyzedArticleWithoutLinks_LinksIsNull
)
136 ArticleCollection ac
;
137 std::ostringstream oss
;
139 auto linked
= std::make_shared
<Article
>("Foo");
140 CollectionUtils::add(ac
, linked
);
142 atj
.serialize(ac
, oss
);
144 auto serString
= oss
.str();
145 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
147 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
149 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
150 R
"({"Foo
":{"forward_links
":null}})") !=
154 TEST(WriteArticleCollection_OneAnalyzedArticleWithoutLinks_LinksIsEmptyArray
)
157 ArticleCollection ac
;
158 std::ostringstream oss
;
160 auto a
= std::make_shared
<Article
>("Foo");
162 CollectionUtils::add(ac
, a
);
164 atj
.serialize(ac
, oss
);
166 auto serString
= oss
.str();
167 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
169 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
171 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
172 R
"({"Foo
":{"forward_links
":[]}})") !=
177 WriteArticleCollection_MultipleArticles_WithMultipleLinks_MatchesExpected
)
180 ArticleCollection ac
;
181 std::ostringstream oss
;
183 auto a
= std::make_shared
<Article
>("Foo");
184 auto b
= std::make_shared
<Article
>("Bar");
185 auto c
= std::make_shared
<Article
>("Baz");
189 CollectionUtils::add(ac
, a
);
190 CollectionUtils::add(ac
, b
);
191 CollectionUtils::add(ac
, c
);
193 atj
.serialize(ac
, oss
);
195 auto serString
= oss
.str();
196 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
198 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
200 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
202 R
"("Bar
":{"forward_links
":["Foo
","Baz
"]},)"
203 R
"("Baz
":{"forward_links
":null},)"
204 R
"("Foo
":{"forward_links
":["Bar
"]})"
205 "}") != std::string::npos
);
208 TEST(SerializeArticleWithOnlyNullptr_NullptrWillBeSkipped
)
211 std::ostringstream oss
;
212 ArticleCollection ac
;
214 // yes, only a is inserted, since we want to emulate article-only
215 auto a
= std::make_shared
<Article
>("Farm");
216 CollectionUtils::add(ac
, a
);
219 // will be skipped on serialization, since it'll become nullptr
220 auto linked
= std::make_shared
<Article
>("Animal");
224 atj
.serialize(ac
, oss
);
226 auto serString
= oss
.str();
227 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
229 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
231 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
232 R
"({"Farm
":{"forward_links
":[]}})") !=
236 TEST(SerializeArticleWithValidArticleAndANullptr_NullptrWillBeSkipped
)
239 std::ostringstream oss
;
240 ArticleCollection ac
;
242 // yes, only a is inserted, since we want to emulate article-only
243 auto a
= std::make_shared
<Article
>("Farm");
244 CollectionUtils::add(ac
, a
);
247 // will be skipped on serialization, since it'll become nullptr
248 auto linked
= std::make_shared
<Article
>("Animal");
252 auto linked2
= std::make_shared
<Article
>("Barn");
255 atj
.serialize(ac
, oss
);
257 auto serString
= oss
.str();
258 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1
) !=
260 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2
) !=
262 CHECK(serString
.find(WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
263 R
"({"Farm
":{"forward_links
":["Barn
"]}})") !=