diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2017-06-26 11:08:07 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2017-07-12 09:06:44 (GMT) |
commit | 8f4d198c4d3aa55a5215061e84572d5faf4e4117 (patch) | |
tree | 95a6b0b29c10d9ab24ba16b1058640510b98370d /Swiften | |
parent | e5111d0125ad107e12150e0cb5447209189c5338 (diff) | |
download | swift-8f4d198c4d3aa55a5215061e84572d5faf4e4117.zip swift-8f4d198c4d3aa55a5215061e84572d5faf4e4117.tar.bz2 |
Add MIXSetNick Element, its Parser and Serializer
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Added tests for MIXSetNick Parser and Serializer based on examples in XEP 0369, which passes.
Tested on Ubuntu 16.04 LTS.
Change-Id: I23081637f2aecf9fc5abc63147d01bfd4d2bb2f1
Diffstat (limited to 'Swiften')
12 files changed, 205 insertions, 0 deletions
diff --git a/Swiften/Elements/MIXSetNick.h b/Swiften/Elements/MIXSetNick.h new file mode 100644 index 0000000..94c5a3b --- /dev/null +++ b/Swiften/Elements/MIXSetNick.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <Swiften/Base/API.h> +#include <Swiften/Elements/Payload.h> + +namespace Swift { + class SWIFTEN_API MIXSetNick : public Payload { + public: + using ref = std::shared_ptr<MIXSetNick>; + + public: + + MIXSetNick() {} + + const std::string& getNick() const { + return nick_; + } + + void setNick(const std::string& nick) { + nick_ = nick; + } + + private: + std::string nick_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 17fb478..939e1af 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -53,6 +53,7 @@ #include <Swiften/Parser/PayloadParsers/MAMResultParser.h> #include <Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXCreateParser.h> +#include <Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXDestroyParser.h> #include <Swiften/Parser/PayloadParsers/MIXJoinParserFactory.h> #include <Swiften/Parser/PayloadParsers/MIXPayloadParserFactory.h> @@ -138,6 +139,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(std::make_shared<ClientStateParserFactory>()); factories_.push_back(std::make_shared<MIXParticipantParserFactory>()); factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXDestroyParser> >("destroy", "urn:xmpp:mix:0")); + 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<MIXUserPreferenceParser> >("user-preference", "urn:xmpp:mix:0")); factories_.push_back(std::make_shared<MIXPayloadParserFactory>()); diff --git a/Swiften/Parser/PayloadParsers/MIXSetNickParser.cpp b/Swiften/Parser/PayloadParsers/MIXSetNickParser.cpp new file mode 100644 index 0000000..34fec70 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXSetNickParser.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Parser/PayloadParsers/MIXSetNickParser.h> + +namespace Swift { + +void MIXSetNickParser::handleTree(ParserElement::ref root) { + for (const auto& child : root->getAllChildren()) { + if (child->getName() == "nick" && child->getNamespace() == root->getNamespace()) { + getPayloadInternal()->setNick(child->getText()); + } + } +} + +} diff --git a/Swiften/Parser/PayloadParsers/MIXSetNickParser.h b/Swiften/Parser/PayloadParsers/MIXSetNickParser.h new file mode 100644 index 0000000..85a5ba9 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXSetNickParser.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Base/Override.h> +#include <Swiften/Elements/MIXSetNick.h> +#include <Swiften/Parser/GenericPayloadTreeParser.h> + +namespace Swift { + class SWIFTEN_API MIXSetNickParser : public GenericPayloadTreeParser<MIXSetNick> { + public: + MIXSetNickParser() {} + virtual void handleTree(ParserElement::ref root) SWIFTEN_OVERRIDE; + }; +} diff --git a/Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h b/Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h new file mode 100644 index 0000000..4b11bb5 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Base/Override.h> +#include <Swiften/Parser/GenericPayloadParserFactory.h> +#include <Swiften/Parser/PayloadParsers/MIXSetNickParser.h> + +namespace Swift { + class SWIFTEN_API MIXSetNickParserFactory : public PayloadParserFactory { + public: + MIXSetNickParserFactory() { + } + + virtual bool canParse(const std::string& element, const std::string& ns, const AttributeMap&) const SWIFTEN_OVERRIDE { + return element == "setnick" && ns == "urn:xmpp:mix:0"; + } + + virtual PayloadParser* createPayloadParser() SWIFTEN_OVERRIDE { + return new MIXSetNickParser(); + } + }; +} diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MIXSetNickParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MIXSetNickParserTest.cpp new file mode 100644 index 0000000..84f8a5e --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/MIXSetNickParserTest.cpp @@ -0,0 +1,23 @@ +/* + * 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/MIXSetNick.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> + +using namespace Swift; + +TEST(MIXSetNickParserTest, WithNick) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<setnick xmlns=\"urn:xmpp:mix:0\"> <nick>thirdwitch</nick> </setnick>" + )); + + auto payload = parser.getPayload<MIXSetNick>(); + ASSERT_TRUE(payload); + ASSERT_EQ("thirdwitch", payload->getNick()); +} diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index 20dc3f4..d185052 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -68,6 +68,7 @@ sources = [ "PayloadParsers/VCardUpdateParser.cpp", "PayloadParsers/DelayParser.cpp", "PayloadParsers/MIXParticipantParser.cpp", + "PayloadParsers/MIXSetNickParser.cpp", "PayloadParsers/MIXDestroyParser.cpp", "PayloadParsers/MIXCreateParser.cpp", "PayloadParsers/MIXPayloadParser.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index 100f717..8aa3b1c 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -208,6 +208,7 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/ErrorSerializer.cpp", "Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp", "Serializer/PayloadSerializers/MIXParticipantSerializer.cpp", + "Serializer/PayloadSerializers/MIXSetNickSerializer.cpp", "Serializer/PayloadSerializers/MIXDestroySerializer.cpp", "Serializer/PayloadSerializers/MIXCreateSerializer.cpp", "Serializer/PayloadSerializers/MIXPayloadSerializer.cpp", @@ -440,6 +441,7 @@ if env["SCONS_STAGE"] == "build" : File("Parser/PayloadParsers/UnitTest/JingleParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MIXParticipantParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MIXPayloadParserTest.cpp"), + File("Parser/PayloadParsers/UnitTest/MIXSetNickParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MIXDestroyParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MIXLeaveParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MIXCreateParserTest.cpp"), @@ -525,6 +527,7 @@ if env["SCONS_STAGE"] == "build" : File("Serializer/PayloadSerializers/UnitTest/ReplaceSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MUCAdminPayloadSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MIXParticipantSerializerTest.cpp"), + File("Serializer/PayloadSerializers/UnitTest/MIXSetNickSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MIXDestroySerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MIXCreateSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MIXPayloadSerializerTest.cpp"), diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index 1a04435..7f76b31 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -50,6 +50,7 @@ #include <Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MIXCreateSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MIXPayloadSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MIXUserPreferenceSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MIXLeaveSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCAdminPayloadSerializer.h> @@ -164,6 +165,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new MIXParticipantSerializer()); serializers_.push_back(new MIXCreateSerializer()); serializers_.push_back(new MIXPayloadSerializer()); + serializers_.push_back(new MIXSetNickSerializer()); serializers_.push_back(new MIXUserPreferenceSerializer()); serializers_.push_back(new MIXLeaveSerializer()); serializers_.push_back(new MIXJoinSerializer()); diff --git a/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.cpp new file mode 100644 index 0000000..d111d8c --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.h> + +#include <memory> + +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLTextNode.h> + +namespace Swift { + +MIXSetNickSerializer::MIXSetNickSerializer() : GenericPayloadSerializer<MIXSetNick>() { +} + +std::string MIXSetNickSerializer::serializePayload(std::shared_ptr<MIXSetNick> payload) const { + XMLElement setnickElement("setnick", "urn:xmpp:mix:0"); + + auto nickElement = std::make_shared<XMLElement>("nick"); + nickElement->addNode(std::make_shared<XMLTextNode>(payload->getNick())); + setnickElement.addNode(nickElement); + + return setnickElement.serialize(); +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.h b/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.h new file mode 100644 index 0000000..a58443b --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXSetNickSerializer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Base/Override.h> +#include <Swiften/Elements/MIXSetNick.h> +#include <Swiften/Serializer/GenericPayloadSerializer.h> + +namespace Swift { + class SWIFTEN_API MIXSetNickSerializer : public GenericPayloadSerializer<MIXSetNick> { + public: + MIXSetNickSerializer(); + + virtual std::string serializePayload(std::shared_ptr<MIXSetNick> payload) const SWIFTEN_OVERRIDE ; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/MIXSetNickSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXSetNickSerializerTest.cpp new file mode 100644 index 0000000..ab29bc5 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXSetNickSerializerTest.cpp @@ -0,0 +1,22 @@ +/* + * 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/Serializer/PayloadSerializers/MIXSetNickSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> + +using namespace Swift; + +TEST(MIXSetNickSerializerTest, testSerializeNick) { + MIXSetNickSerializer testling; + + auto mix = std::shared_ptr<MIXSetNick>(new MIXSetNick()); + mix->setNick(std::string("thirdwitch")); + + std::string expectedResult = "<setnick xmlns=\"urn:xmpp:mix:0\"><nick>thirdwitch</nick></setnick>"; + ASSERT_EQ(expectedResult, testling.serialize(mix)); +} |