summaryrefslogtreecommitdiffstats
blob: 71c6cf0d125885c5343d0c8d54abab253d55d35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#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:
		void setUp() {
			channel = new DummyIQChannel();
			router = new IQRouter(channel);
		}

		void tearDown() {
			delete router;
			delete channel;
		}

		void testSend() {
			GetPrivateStorageRequest<MyPayload>::ref request = GetPrivateStorageRequest<MyPayload>::create(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>::ref testling = GetPrivateStorageRequest<MyPayload>::create(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>::ref testling = GetPrivateStorageRequest<MyPayload>::create(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, ErrorPayload::ref 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< ErrorPayload > errors;
		std::vector< boost::shared_ptr<Payload> > responses;
};

CPPUNIT_TEST_SUITE_REGISTRATION(GetPrivateStorageRequestTest);