diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2017-07-07 21:09:17 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2017-07-18 15:05:09 (GMT) |
commit | f0db4d39912e773208e9db2a3da3b68ac92ba17b (patch) | |
tree | cb6a455ba7b21d2218b3f9f2040af3bf30a39004 /Swiften/Parser | |
parent | f21c9448c5187493fd08574962d88c2db1405512 (diff) | |
download | swift-f0db4d39912e773208e9db2a3da3b68ac92ba17b.zip swift-f0db4d39912e773208e9db2a3da3b68ac92ba17b.tar.bz2 |
Add MIXUpdateSubscription Element, its Parser and Serializer
Updates MIXJoin Element list of subscriptions to an unordered_set.
Remove redundant MIXSubscribe Element.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Added tests for MIXUpdateSubscription Parser and Serializer based on examples in XEP 0369, which passes.
Tested on Ubuntu 16.04 LTS.
Change-Id: Ied06269cf329b4fdffdde4ace67ebffa4089fbde
Diffstat (limited to 'Swiften/Parser')
-rw-r--r-- | Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp | 2 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/MIXJoinParser.cpp | 8 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/MIXSubscribeParser.cpp | 36 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.cpp | 49 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.h (renamed from Swiften/Parser/PayloadParsers/MIXSubscribeParser.h) | 9 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/UnitTest/MIXJoinParserTest.cpp | 48 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/UnitTest/MIXUpdateSubscriptionParserTest.cpp | 49 | ||||
-rw-r--r-- | Swiften/Parser/SConscript | 2 |
8 files changed, 130 insertions, 73 deletions
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 8f57704..43c4ebb 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -56,6 +56,7 @@ #include <Swiften/Parser/PayloadParsers/MIXRegisterNickParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXDestroyParser.h> +#include <Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.h> #include <Swiften/Parser/PayloadParsers/MIXJoinParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXPayloadParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXUserPreferenceParser.h> @@ -143,6 +144,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(std::make_shared<MIXRegisterNickParserFactory>()); factories_.push_back(std::make_shared<MIXSetNickParserFactory>()); factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXCreateParser> >("create", "urn:xmpp:mix:0")); + factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXUpdateSubscriptionParser> >("update-subscription", "urn:xmpp:mix:0")); factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXUserPreferenceParser> >("user-preference", "urn:xmpp:mix:0")); factories_.push_back(std::make_shared<MIXPayloadParserFactory>()); factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXLeaveParser> >("leave", "urn:xmpp:mix:0")); diff --git a/Swiften/Parser/PayloadParsers/MIXJoinParser.cpp b/Swiften/Parser/PayloadParsers/MIXJoinParser.cpp index 0c09c44..6e72f90 100644 --- a/Swiften/Parser/PayloadParsers/MIXJoinParser.cpp +++ b/Swiften/Parser/PayloadParsers/MIXJoinParser.cpp @@ -9,7 +9,6 @@ #include <boost/optional.hpp> #include <Swiften/Parser/PayloadParserFactory.h> -#include <Swiften/Parser/PayloadParsers/MIXSubscribeParser.h> #include <Swiften/Parser/PayloadParsers/FormParser.h> using namespace Swift; @@ -36,7 +35,9 @@ void MIXJoinParser::handleStartElement(const std::string& element, const std::st if (level_ == 1) { if (element == "subscribe" && ns == "urn:xmpp:mix:0") { - currentPayloadParser_ = std::make_shared<MIXSubscribeParser>(); + if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("node")) { + getPayloadInternal()->addSubscription(*attributeValue); + } } if (element == "x" && ns == "jabber:x:data") { currentPayloadParser_ = std::make_shared<FormParser>(); @@ -57,9 +58,6 @@ void MIXJoinParser::handleEndElement(const std::string& element, const std::stri } if (level_ == 1) { - if (element == "subscribe" && ns == "urn:xmpp:mix:0") { - getPayloadInternal()->addSubscription(std::dynamic_pointer_cast<MIXSubscribe>(currentPayloadParser_->getPayload())); - } if (element == "x" && ns == "jabber:x:data") { getPayloadInternal()->setForm(std::dynamic_pointer_cast<Form>(currentPayloadParser_->getPayload())); } diff --git a/Swiften/Parser/PayloadParsers/MIXSubscribeParser.cpp b/Swiften/Parser/PayloadParsers/MIXSubscribeParser.cpp deleted file mode 100644 index 1500716..0000000 --- a/Swiften/Parser/PayloadParsers/MIXSubscribeParser.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2017 Tarun Gupta - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <Swiften/Parser/PayloadParsers/MIXSubscribeParser.h> - -#include <boost/optional.hpp> - -#include <Swiften/Parser/PayloadParserFactory.h> -#include <Swiften/Parser/PayloadParserFactoryCollection.h> - -using namespace Swift; - -MIXSubscribeParser::MIXSubscribeParser() : level_(0) { -} - -MIXSubscribeParser::~MIXSubscribeParser() { -} - -void MIXSubscribeParser::handleStartElement(const std::string&, const std::string&, const AttributeMap& attributes) { - if (level_ == 0) { - if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("node")) { - getPayloadInternal()->setNode(*attributeValue); - } - } - ++level_; -} - -void MIXSubscribeParser::handleEndElement(const std::string& , const std::string& ) { - --level_; -} - -void MIXSubscribeParser::handleCharacterData(const std::string& ) { -} diff --git a/Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.cpp b/Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.cpp new file mode 100644 index 0000000..d530e49 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.h> + +#include <boost/optional.hpp> + +#include <Swiften/Parser/PayloadParserFactory.h> +#include <Swiften/Parser/PayloadParsers/FormParser.h> + +namespace Swift { + +MIXUpdateSubscriptionParser::MIXUpdateSubscriptionParser() : level_(0) { +} + +MIXUpdateSubscriptionParser::~MIXUpdateSubscriptionParser() { +} + +void MIXUpdateSubscriptionParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { + if (level_ == 0) { + if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("jid")) { + if (boost::optional<JID> jid = JID::parse(*attributeValue)) { + getPayloadInternal()->setJID(*jid); + } + } + } + + if (level_ == 1) { + if (element == "subscribe" && ns == "urn:xmpp:mix:0") { + if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("node")) { + getPayloadInternal()->addSubscription(*attributeValue); + } + } + } + + ++level_; +} + +void MIXUpdateSubscriptionParser::handleEndElement(const std::string&, const std::string&) { + --level_; +} + +void MIXUpdateSubscriptionParser::handleCharacterData(const std::string&) { +} + +} diff --git a/Swiften/Parser/PayloadParsers/MIXSubscribeParser.h b/Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.h index 95d6d74..9975411 100644 --- a/Swiften/Parser/PayloadParsers/MIXSubscribeParser.h +++ b/Swiften/Parser/PayloadParsers/MIXUpdateSubscriptionParser.h @@ -15,17 +15,16 @@ #include <memory> #include <Swiften/Base/API.h> -#include <Swiften/Elements/MIXSubscribe.h> +#include <Swiften/Elements/MIXUpdateSubscription.h> #include <Swiften/Parser/GenericPayloadParser.h> namespace Swift { - class PayloadParserFactoryCollection; class PayloadParser; - class SWIFTEN_API MIXSubscribeParser : public GenericPayloadParser<MIXSubscribe> { + class SWIFTEN_API MIXUpdateSubscriptionParser : public GenericPayloadParser<MIXUpdateSubscription> { public: - MIXSubscribeParser(); - virtual ~MIXSubscribeParser() override; + MIXUpdateSubscriptionParser(); + virtual ~MIXUpdateSubscriptionParser(); virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) override; virtual void handleEndElement(const std::string& element, const std::string&) override; diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MIXJoinParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MIXJoinParserTest.cpp index 88d8fc1..0ad4589 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/MIXJoinParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/MIXJoinParserTest.cpp @@ -30,12 +30,11 @@ TEST(MIXJoinParserTest, XEP0369_Example22) { ASSERT_FALSE(payload->getJID()); ASSERT_FALSE(payload->getForm()); - const std::vector<MIXSubscribe::ref> items = payload->getSubscriptions(); - ASSERT_EQ(static_cast<size_t>(4), items.size()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:messages"), items[0]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:presence"), items[1]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:participants"), items[2]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:config"), items[3]->getNode()); + ASSERT_EQ(static_cast<size_t>(4), payload->getSubscriptions().size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:presence"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:participants"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:config"))); } TEST(MIXJoinParserTest, XEP0369_Example23) { @@ -56,12 +55,11 @@ TEST(MIXJoinParserTest, XEP0369_Example23) { ASSERT_FALSE(payload->getJID()); ASSERT_FALSE(payload->getForm()); - const std::vector<MIXSubscribe::ref> items = payload->getSubscriptions(); - ASSERT_EQ(static_cast<size_t>(4), items.size()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:messages"), items[0]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:presence"), items[1]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:participants"), items[2]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:config"), items[3]->getNode()); + ASSERT_EQ(static_cast<size_t>(4), payload->getSubscriptions().size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:presence"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:participants"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:config"))); } TEST(MIXJoinParserTest, XEP0369_Example24) { @@ -83,12 +81,11 @@ TEST(MIXJoinParserTest, XEP0369_Example24) { ASSERT_EQ(JID("123456#coven@mix.shakespeare.example"), *payload->getJID()); ASSERT_FALSE(payload->getForm()); - const std::vector<MIXSubscribe::ref> items = payload->getSubscriptions(); - ASSERT_EQ(static_cast<size_t>(4), items.size()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:messages"), items[0]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:presence"), items[1]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:participants"), items[2]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:config"), items[3]->getNode()); + ASSERT_EQ(static_cast<size_t>(4), payload->getSubscriptions().size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:presence"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:participants"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:config"))); } TEST(MIXJoinParserTest, XEP0369_Example29) { @@ -112,10 +109,10 @@ TEST(MIXJoinParserTest, XEP0369_Example29) { ASSERT_FALSE(payload->getChannel()); ASSERT_FALSE(payload->getJID()); - const std::vector<MIXSubscribe::ref> items = payload->getSubscriptions(); - ASSERT_EQ(static_cast<size_t>(2), items.size()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:messages"), items[0]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:presence"), items[1]->getNode()); + + ASSERT_EQ(static_cast<size_t>(2), payload->getSubscriptions().size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:presence"))); ASSERT_TRUE(payload->getForm()); ASSERT_EQ(Form::Type::SubmitType, payload->getForm()->getType()); @@ -156,10 +153,9 @@ TEST(MIXJoinParserTest, XEP0369_Example30) { ASSERT_TRUE(payload->getJID()); ASSERT_EQ(JID("hag66@shakespeare.example"), *payload->getJID()); - const std::vector<MIXSubscribe::ref> items = payload->getSubscriptions(); - ASSERT_EQ(static_cast<size_t>(2), items.size()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:messages"), items[0]->getNode()); - ASSERT_EQ(std::string("urn:xmpp:mix:nodes:presence"), items[1]->getNode()); + ASSERT_EQ(static_cast<size_t>(2), payload->getSubscriptions().size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:presence"))); ASSERT_TRUE(payload->getForm()); ASSERT_EQ(Form::Type::ResultType, payload->getForm()->getType()); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MIXUpdateSubscriptionParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MIXUpdateSubscriptionParserTest.cpp new file mode 100644 index 0000000..985a34b --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/MIXUpdateSubscriptionParserTest.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <gtest/gtest.h> + +#include <Swiften/Elements/MIXUpdateSubscription.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> + +using namespace Swift; + +TEST(MIXUpdateSubscriptionParserTest, XEP0369_Example28) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<update-subscription xmlns=\"urn:xmpp:mix:0\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "</update-subscription>" + )); + + auto payload = parser.getPayload<MIXUpdateSubscription>(); + ASSERT_TRUE(payload); + + ASSERT_FALSE(payload->getJID()); + + auto items = payload->getSubscriptions(); + ASSERT_EQ(static_cast<size_t>(1), items.size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); +} + +TEST(MIXUpdateSubscriptionParserTest, XEP0369_Example28_WithJID) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<update-subscription xmlns=\"urn:xmpp:mix:0\" jid=\"hag66@shakespeare.example\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "</update-subscription>" + )); + + auto payload = parser.getPayload<MIXUpdateSubscription>(); + ASSERT_TRUE(payload); + + ASSERT_TRUE(payload->getJID()); + ASSERT_EQ(JID("hag66@shakespeare.example"), *payload->getJID()); + + auto items = payload->getSubscriptions(); + ASSERT_EQ(static_cast<size_t>(1), items.size()); + ASSERT_TRUE(payload->hasSubscription(std::string("urn:xmpp:mix:nodes:messages"))); +} diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index bfc7850..30c61a7 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -75,8 +75,8 @@ sources = [ "PayloadParsers/MIXPayloadParser.cpp", "PayloadParsers/MIXLeaveParser.cpp", "PayloadParsers/MIXJoinParser.cpp", - "PayloadParsers/MIXSubscribeParser.cpp", "PayloadParsers/MIXUserPreferenceParser.cpp", + "PayloadParsers/MIXUpdateSubscriptionParser.cpp", "PayloadParsers/MUCUserPayloadParser.cpp", "PayloadParsers/MUCAdminPayloadParser.cpp", "PayloadParsers/MUCOwnerPayloadParser.cpp", |