4 #include <UnitTest++/UnitTest++.h>
7 #include "JsonSerializer.h"
8 #include "WalkerException.h"
10 #include "SerializerTestDefines.h"
12 #define WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE \
13 "{" WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_1 \
14 "," WW_TEST_JSONSERIALIZER_PROTOCOL_HEADER_2 \
15 "," WW_TEST_JSONSERIALIZER_PROTOCOL_COLLECTION_KEY
17 SUITE(JsonDeserializerTests
)
19 using namespace WikiWalker
;
20 using namespace WikiWalker::CollectionUtils
;
22 TEST(Deserialize_ArticleCollection_OneArticleWithoutLinks_Unanalyzed
)
24 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
25 R
"({"Farm
":{"forward_links
":null}})"
27 std::istringstream
json(jsonString
);
31 deser
.deserialize(ac
, json
);
33 CHECK_EQUAL(1, ac
.size());
35 auto a
= CollectionUtils::get(ac
, "Farm");
37 CHECK_EQUAL(false, a
->analyzed());
40 TEST(Deserialize_ArticleCollection_OneArticleWithoutLinks_Analyzed
)
42 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
43 R
"({"Farm
":{"forward_links
":[]}})"
45 std::istringstream
json(jsonString
);
49 deser
.deserialize(ac
, json
);
51 CHECK_EQUAL(1, ac
.size());
53 auto a
= CollectionUtils::get(ac
, "Farm");
56 CHECK_EQUAL(true, a
->analyzed());
57 CHECK_EQUAL(0, a
->countLinks());
60 TEST(Deserialize_ArticleCollection_OneArticleWithOneLink
)
62 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
63 R
"({"Farm
":{"forward_links
":["Animal
"]}})"
65 std::istringstream
json(jsonString
);
69 deser
.deserialize(ac
, json
);
71 /* Deserialized state doesn't exactly match serialized one.
72 * This is intended. Only-link articles become new articles in the
75 CHECK_EQUAL(2, ac
.size());
77 auto a
= CollectionUtils::get(ac
, "Farm");
80 CHECK_EQUAL(1, a
->countLinks());
83 TEST(Deserialize_ArticleCollection_OneArticleWithMultipleLinks
)
85 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
86 R
"({"Farm
":{"forward_links
":["Animal
","Pig
","Equality
"]}})"
88 std::istringstream
json(jsonString
);
92 deser
.deserialize(ac
, json
);
94 // see comment in Deserialize_ArticleCollection_OneArticleWithOneLink
95 CHECK_EQUAL(4, ac
.size());
97 auto a
= CollectionUtils::get(ac
, "Farm");
100 CHECK_EQUAL(3, a
->countLinks());
103 TEST(Deserialize_ArticleCollection_Empty
)
105 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
108 std::istringstream
json(jsonString
);
110 ArticleCollection ac
;
111 JsonSerializer deser
;
112 deser
.deserialize(ac
, json
);
113 CHECK_EQUAL(0, ac
.size());
116 TEST(Deserialize_ArticleCollection_MultipleArticlesWithMultipleLinks
)
118 std::string jsonString
= WW_TEST_JSONSERIALIZER_PROTOCOL_COMPLETE
121 R
"({"forward_links
":["Animal
","Pig
","Equality
"]},)"
123 R
"({"forward_links
":["Cat
","Pig
","Dog
"]})"
126 std::istringstream
json(jsonString
);
128 ArticleCollection ac
;
129 JsonSerializer deser
;
130 deser
.deserialize(ac
, json
);
132 // see comment in Deserialize_ArticleCollection_OneArticleWithOneLink
133 CHECK_EQUAL(6, ac
.size());
135 for(std::string a
: {"Farm", "Animal", "Pig", "Equality", "Cat", "Dog"}) {
136 auto x
= CollectionUtils::get(ac
, a
);
140 auto a
= CollectionUtils::get(ac
, "Farm");
142 CHECK_EQUAL(3, a
->countLinks());
145 TEST(Deserialize_ArticleCollection_WrongProgramName
)
147 std::string jsonString
=
148 R
"({"program
":"wikwalker
","scheme
-version
":2,)"
149 R
"("ArticleCollection
":)"
150 R
"({"Farm
":{"forward_links
":null}})"
153 std::istringstream
json(jsonString
);
155 ArticleCollection ac
;
156 JsonSerializer deser
;
157 CHECK_THROW(deser
.deserialize(ac
, json
), WalkerException
);
160 TEST(Deserialize_ArticleCollection_WrongVersion
)
162 std::string jsonString
=
163 R
"({"program
":"wikiwalker
","scheme
-version
":3,)"
164 R
"("ArticleCollection
":)"
165 R
"({"Farm
":{"forward_links
":null}})"
168 std::istringstream
json(jsonString
);
170 ArticleCollection ac
;
171 JsonSerializer deser
;
172 CHECK_THROW(deser
.deserialize(ac
, json
), WalkerException
);