#include #include #include namespace larra::xmpp { namespace { auto CreateTestData() { auto doc = std::make_unique(); auto node = doc->create_root_node("message"); node->set_attribute("from", "user1@server.i2p"); node->set_attribute("to", "user2@server.i2p"); node->set_attribute("type", "chat"); node->set_attribute("id", "1"); node->set_attribute("lang", "en", "xml"); auto bodyNode = node->add_child_element("body"); bodyNode->add_child_text("hello"); return doc; } auto CreateErrorTestData() { auto doc = std::make_unique(); auto node = doc->create_root_node("message"); node->set_attribute("from", "user1@server.i2p"); node->set_attribute("to", "user2@server.i2p"); node->set_attribute("type", "error"); auto error = node->add_child_element("error"); error->set_attribute("type", "auth"); auto notAuthorized = error->add_child_element("not-authorized"); notAuthorized->set_namespace_declaration("urn:ietf:params:xml:ns:xmpp-stanzas"); return doc; } const message::Message kMessage{ .from = {.username = "user1", .server = "server.i2p"}, .to = {.username = "user2", .server = "server.i2p"}, .type = message::type::kChat, .id = "1", .language = "en", .body = {{.content = "hello"}} // }; const message::Error kError{.from = {.username = "user1", .server = "server.i2p"}, // .to = {.username = "user2", .server = "server.i2p"}, .error = stanza::error::NotAuthorized{}}; } // namespace TEST(Parse, Message) { using T = Message; { auto doc = CreateTestData(); auto node = doc->get_root_node(); auto message = Serialization::Parse(node); EXPECT_EQ(message, static_cast(kMessage)); } auto doc = CreateErrorTestData(); auto node = doc->get_root_node(); auto message = Serialization::Parse(node); EXPECT_EQ(message, static_cast(kError)); } TEST(Serialize, Message) { { auto expected = R"( hello )"; xmlpp::Document doc; auto node = doc.create_root_node("message"); Serialization>::Serialize(node, kMessage); EXPECT_EQ(doc.write_to_string(), expected); } auto expected = R"( )"; xmlpp::Document doc; auto node = doc.create_root_node("message"); Serialization>::Serialize(node, kError); EXPECT_EQ(doc.write_to_string(), expected); } } // namespace larra::xmpp