1
+ //
2
+ // Created by sajith on 6/22/22.
3
+ //
4
+
5
+ #include " parsers/XmlParser.h"
6
+
7
+ #include < gtest/gtest.h>
8
+ #include < optional>
9
+ #include < string>
10
+
11
+ #include " types/Report.h"
12
+
13
+ struct XmlParserTest : testing::Test
14
+ {
15
+ parsers::XmlParser sut;
16
+ };
17
+
18
+ TEST_F (XmlParserTest, whenValidData_parseReportReturnsReport)
19
+ {
20
+ const std::string xmlRequest{" <report>"
21
+ " <payer>2</payer>"
22
+ " <tax>VAT</tax>"
23
+ " <amount>10</amount>"
24
+ " <year>2020</year>"
25
+ " </report>" };
26
+
27
+ const std::optional<types::Report> parserReport = sut.parseReport (xmlRequest);
28
+ const types::Report expectedReport{2 , " VAT" , 10 , 2020 };
29
+ ASSERT_TRUE (parserReport);
30
+ ASSERT_EQ (parserReport, expectedReport);
31
+ }
32
+
33
+
34
+
35
+ TEST_F (XmlParserTest, whenMissingFields_parseReportReturnsNull)
36
+ {
37
+ const std::string xmlReport = " <report><payer>2</payer><year>2020</year></report>" ;
38
+ ASSERT_EQ (sut.parseReport (xmlReport), std::nullopt );
39
+ }
40
+
41
+ TEST_F (XmlParserTest, whenEmptyReport_parseReportReturnsNull)
42
+ {
43
+ const std::string xmlReport = " " ;
44
+ ASSERT_EQ (sut.parseReport (xmlReport), std::nullopt );
45
+ }
46
+
47
+ TEST_F (XmlParserTest, whenInvalidXml_parseReportReturnsNull)
48
+ {
49
+ const std::string xmlReport = " <<report>>" ;
50
+ ASSERT_EQ (sut.parseReport (xmlReport), std::nullopt );
51
+ }
52
+
53
+ TEST_F (XmlParserTest, whenNumericDataInvalid_parseReportReturnsNull)
54
+ {
55
+ const std::string xmlReport = " <report><payer>Two</payer><tax>VAT</tax><amount>One"
56
+ " </amount><year>Three</year></report>" ;
57
+ ASSERT_EQ (sut.parseReport (xmlReport), std::nullopt );
58
+ }
59
+
60
+ TEST_F (XmlParserTest, whenValidData_parseCredentialsReturnsUser)
61
+ {
62
+ const std::string xmlReport = " <credentials><login>Jhon Doe</login>"
63
+ " <password>123</password></credentials>" ;
64
+ const std::optional<types::User> parsedUser = sut.parseCredentials (xmlReport);
65
+ ASSERT_TRUE (parsedUser);
66
+ ASSERT_EQ (parsedUser->login .value , std::string (" Jhon Doe" ));
67
+ ASSERT_EQ (parsedUser->password .value , std::string (" 123" ));
68
+ }
69
+
70
+ TEST_F (XmlParserTest, whenMissingFields_parseCredentialsReturnsNull)
71
+ {
72
+ const std::string xmlReport = " <credentials><login>Jhon Doe</login></credentials>" ;
73
+ const std::optional<types::User> parsedUser = sut.parseCredentials (xmlReport);
74
+ ASSERT_EQ (parsedUser, std::nullopt );
75
+ }
0 commit comments