From 71accda257c880d576b8b55f1e62d6d7178bab25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be> Date: Mon, 14 Sep 2009 22:16:43 +0200 Subject: Added Get/SetPrivateStorageRequest. diff --git a/Swiften/Elements/PrivateStorage.h b/Swiften/Elements/PrivateStorage.h index 66259a3..93f1cfe 100644 --- a/Swiften/Elements/PrivateStorage.h +++ b/Swiften/Elements/PrivateStorage.h @@ -7,7 +7,7 @@ namespace Swift { class PrivateStorage : public Payload { public: - PrivateStorage() { + PrivateStorage(boost::shared_ptr<Payload> payload = boost::shared_ptr<Payload>()) : payload(payload) { } boost::shared_ptr<Payload> getPayload() const { diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp index 4235c6f..90aa295 100644 --- a/Swiften/Queries/Request.cpp +++ b/Swiften/Queries/Request.cpp @@ -7,7 +7,11 @@ namespace Swift { Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router) : router_(router), type_(type), receiver_(receiver), payload_(payload), sent_(false) { } +Request::Request(IQ::Type type, const JID& receiver, IQRouter* router) : router_(router), type_(type), receiver_(receiver), sent_(false) { +} + void Request::send() { + assert(payload_); assert(!sent_); sent_ = true; diff --git a/Swiften/Queries/Request.h b/Swiften/Queries/Request.h index 1c972dc..8f7a1d1 100644 --- a/Swiften/Queries/Request.h +++ b/Swiften/Queries/Request.h @@ -20,10 +20,18 @@ namespace Swift { const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router); + Request( + IQ::Type type, + const JID& receiver, + IQRouter* router); void send(); protected: + virtual void setPayload(boost::shared_ptr<Payload> p) { + payload_ = p; + } + virtual void handleResponse(boost::shared_ptr<Payload>, boost::optional<Error>) = 0; private: diff --git a/Swiften/Queries/Requests/GetPrivateStorageRequest.h b/Swiften/Queries/Requests/GetPrivateStorageRequest.h new file mode 100644 index 0000000..c5f8aef --- /dev/null +++ b/Swiften/Queries/Requests/GetPrivateStorageRequest.h @@ -0,0 +1,30 @@ +#pragma once + +#include <boost/signals.hpp> +#include <boost/shared_ptr.hpp> + +#include "Swiften/Queries/Request.h" +#include "Swiften/Elements/PrivateStorage.h" +#include "Swiften/Elements/Error.h" + +namespace Swift { + template<typename PAYLOAD_TYPE> + class GetPrivateStorageRequest : public Request { + public: + GetPrivateStorageRequest(IQRouter* router) : Request(IQ::Get, JID(), boost::shared_ptr<PrivateStorage>(new PrivateStorage(boost::shared_ptr<Payload>(new PAYLOAD_TYPE()))), router) { + } + + virtual void handleResponse(boost::shared_ptr<Payload> payload, boost::optional<Error> error) { + boost::shared_ptr<PrivateStorage> storage = boost::dynamic_pointer_cast<PrivateStorage>(payload); + if (storage) { + onResponse(boost::dynamic_pointer_cast<PAYLOAD_TYPE>(storage->getPayload()), error); + } + else { + onResponse(boost::shared_ptr<PAYLOAD_TYPE>(), error); + } + } + + public: + boost::signal<void (boost::shared_ptr<PAYLOAD_TYPE>, const boost::optional<Error>&)> onResponse; + }; +} diff --git a/Swiften/Queries/Requests/SetPrivateStorageRequest.h b/Swiften/Queries/Requests/SetPrivateStorageRequest.h new file mode 100644 index 0000000..63ac8dc --- /dev/null +++ b/Swiften/Queries/Requests/SetPrivateStorageRequest.h @@ -0,0 +1,24 @@ +#pragma once + +#include <boost/signals.hpp> +#include <boost/shared_ptr.hpp> + +#include "Swiften/Queries/Request.h" +#include "Swiften/Elements/PrivateStorage.h" +#include "Swiften/Elements/Error.h" + +namespace Swift { + template<typename PAYLOAD_TYPE> + class SetPrivateStorageRequest : public Request { + public: + SetPrivateStorageRequest(boost::shared_ptr<PAYLOAD_TYPE> payload, IQRouter* router) : Request(IQ::Set, JID(), boost::shared_ptr<PrivateStorage>(new PrivateStorage(payload)), router) { + } + + virtual void handleResponse(boost::shared_ptr<Payload> payload, boost::optional<Error> error) { + onResponse(error); + } + + public: + boost::signal<void (const boost::optional<Error>&)> onResponse; + }; +} diff --git a/Swiften/Queries/Requests/UnitTest/GetPrivateStorageRequestTest.cpp b/Swiften/Queries/Requests/UnitTest/GetPrivateStorageRequestTest.cpp new file mode 100644 index 0000000..14e04cf --- /dev/null +++ b/Swiften/Queries/Requests/UnitTest/GetPrivateStorageRequestTest.cpp @@ -0,0 +1,106 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/shared_ptr.hpp> +#include <boost/bind.hpp> + +#include "Swiften/Queries/Requests/GetPrivateStorageRequest.h" +#include "Swiften/Queries/IQRouter.h" +#include "Swiften/Queries/DummyIQChannel.h" +#include "Swiften/Elements/Payload.h" + +using namespace Swift; + +class GetPrivateStorageRequestTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(GetPrivateStorageRequestTest); + CPPUNIT_TEST(testSend); + CPPUNIT_TEST(testHandleResponse); + CPPUNIT_TEST(testHandleResponse_Error); + CPPUNIT_TEST_SUITE_END(); + + public: + class MyPayload : public Payload { + public: + MyPayload(const String& text = "") : text(text) {} + String text; + }; + + public: + GetPrivateStorageRequestTest() {} + + void setUp() { + channel = new DummyIQChannel(); + router = new IQRouter(channel); + } + + void tearDown() { + delete router; + delete channel; + } + + void testSend() { + GetPrivateStorageRequest<MyPayload> request(router); + request.send(); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->iqs_.size())); + CPPUNIT_ASSERT_EQUAL(JID(), channel->iqs_[0]->getTo()); + CPPUNIT_ASSERT_EQUAL(IQ::Get, channel->iqs_[0]->getType()); + boost::shared_ptr<PrivateStorage> storage = channel->iqs_[0]->getPayload<PrivateStorage>(); + CPPUNIT_ASSERT(storage); + boost::shared_ptr<MyPayload> payload = boost::dynamic_pointer_cast<MyPayload>(storage->getPayload()); + CPPUNIT_ASSERT(payload); + } + + void testHandleResponse() { + GetPrivateStorageRequest<MyPayload> testling(router); + testling.onResponse.connect(boost::bind(&GetPrivateStorageRequestTest::handleResponse, this, _1, _2)); + testling.send(); + channel->onIQReceived(createResponse("test-id", "foo")); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(responses.size())); + CPPUNIT_ASSERT_EQUAL(String("foo"), boost::dynamic_pointer_cast<MyPayload>(responses[0])->text); + } + + void testHandleResponse_Error() { + GetPrivateStorageRequest<MyPayload> testling(router); + testling.onResponse.connect(boost::bind(&GetPrivateStorageRequestTest::handleResponse, this, _1, _2)); + testling.send(); + channel->onIQReceived(createError("test-id")); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(responses.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(errors.size())); + } + + private: + void handleResponse(boost::shared_ptr<Payload> p, const boost::optional<Error>& e) { + if (e) { + errors.push_back(*e); + } + else { + responses.push_back(p); + } + } + + boost::shared_ptr<IQ> createResponse(const String& id, const String& text) { + boost::shared_ptr<IQ> iq(new IQ(IQ::Result)); + boost::shared_ptr<PrivateStorage> storage(new PrivateStorage()); + storage->setPayload(boost::shared_ptr<Payload>(new MyPayload(text))); + iq->addPayload(storage); + iq->setID(id); + return iq; + } + + boost::shared_ptr<IQ> createError(const String& id) { + boost::shared_ptr<IQ> iq(new IQ(IQ::Error)); + iq->setID(id); + return iq; + } + + private: + IQRouter* router; + DummyIQChannel* channel; + std::vector< Error > errors; + std::vector< boost::shared_ptr<Payload> > responses; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(GetPrivateStorageRequestTest); diff --git a/Swiften/SConscript b/Swiften/SConscript index fa10fb1..12fbe15 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -143,6 +143,7 @@ env.Append(UNITTEST_SOURCES = [ File("Parser/UnitTest/StreamFeaturesParserTest.cpp"), File("Parser/UnitTest/XMLParserTest.cpp"), File("Parser/UnitTest/XMPPParserTest.cpp"), + File("Queries/Requests/UnitTest/GetPrivateStorageRequestTest.cpp"), File("Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp"), File("Queries/UnitTest/IQRouterTest.cpp"), File("Queries/UnitTest/RequestTest.cpp"), -- cgit v0.10.2-6-g49f6