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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright (c) 2010-2013 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Serializer/PayloadSerializers/SearchPayloadSerializer.h>
using namespace Swift;
class SearchPayloadSerializerTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SearchPayloadSerializerTest);
CPPUNIT_TEST(testSerialize_Request);
CPPUNIT_TEST(testSerialize_Items);
CPPUNIT_TEST(testSerialize_DataForm);
CPPUNIT_TEST_SUITE_END();
public:
void testSerialize_Request() {
SearchPayloadSerializer testling;
SearchPayload::ref payload(new SearchPayload());
payload->setFirst("Juliet");
payload->setLast("Capulet");
CPPUNIT_ASSERT_EQUAL(std::string(
"<query xmlns=\"jabber:iq:search\">"
"<first>Juliet</first>"
"<last>Capulet</last>"
"</query>"
), testling.serialize(payload));
}
void testSerialize_Items() {
SearchPayloadSerializer testling;
SearchPayload::ref payload(new SearchPayload());
SearchPayload::Item item1;
item1.jid = JID("juliet@capulet.com");
item1.first = "Juliet";
item1.last = "Capulet";
item1.nick = "JuliC";
item1.email = "juliet@shakespeare.lit";
payload->addItem(item1);
SearchPayload::Item item2;
item2.jid = JID("tybalt@shakespeare.lit");
item2.first = "Tybalt";
item2.last = "Capulet";
item2.nick = "ty";
item2.email = "tybalt@shakespeare.lit";
payload->addItem(item2);
CPPUNIT_ASSERT_EQUAL(std::string(
"<query xmlns=\"jabber:iq:search\">"
"<item jid=\"juliet@capulet.com\">"
"<first>Juliet</first>"
"<last>Capulet</last>"
"<nick>JuliC</nick>"
"<email>juliet@shakespeare.lit</email>"
"</item>"
"<item jid=\"tybalt@shakespeare.lit\">"
"<first>Tybalt</first>"
"<last>Capulet</last>"
"<nick>ty</nick>"
"<email>tybalt@shakespeare.lit</email>"
"</item>"
"</query>"
), testling.serialize(payload));
}
void testSerialize_DataForm() {
SearchPayloadSerializer testling;
SearchPayload::ref payload(new SearchPayload());
boost::shared_ptr<Form> form(new Form(Form::ResultType));
FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:iq:search");
field->setName("FORM_TYPE");
form->addField(field);
// reported fields
field = boost::make_shared<FormField>(FormField::TextSingleType);
field->setName("first");
field->setLabel("Given Name");
form->addReportedField(field);
field = boost::make_shared<FormField>(FormField::TextSingleType);
field->setName("last");
field->setLabel("Family Name");
form->addReportedField(field);
field = boost::make_shared<FormField>(FormField::JIDSingleType);
field->setName("jid");
field->setLabel("Jabber ID");
form->addReportedField(field);
field = boost::make_shared<FormField>(FormField::ListSingleType);
field->setName("x-gender");
field->setLabel("Gender");
form->addReportedField(field);
Form::FormItem firstItem;
field = boost::make_shared<FormField>(FormField::TextSingleType, "Benvolio");
field->setName("first");
firstItem.push_back(field);
field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague");
field->setName("last");
firstItem.push_back(field);
field = boost::make_shared<FormField>(FormField::TextSingleType, "benvolio@montague.net");
field->setName("jid");
firstItem.push_back(field);
field = boost::make_shared<FormField>(FormField::ListSingleType, "male");
field->setName("x-gender");
firstItem.push_back(field);
Form::FormItem secondItem;
field = boost::make_shared<FormField>(FormField::TextSingleType, "Romeo");
field->setName("first");
secondItem.push_back(field);
field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague");
field->setName("last");
secondItem.push_back(field);
field = boost::make_shared<FormField>(FormField::TextSingleType, "romeo@montague.net");
field->setName("jid");
secondItem.push_back(field);
field = boost::make_shared<FormField>(FormField::ListSingleType, "male");
field->setName("x-gender");
secondItem.push_back(field);
form->addItem(firstItem);
form->addItem(secondItem);
payload->setForm(form);
CPPUNIT_ASSERT_EQUAL(std::string(
"<query xmlns=\"jabber:iq:search\">"
"<x type=\"result\" xmlns=\"jabber:x:data\">"
"<field type=\"hidden\" var=\"FORM_TYPE\">"
"<value>jabber:iq:search</value>"
"</field>"
"<reported>"
"<field label=\"Given Name\" type=\"text-single\" var=\"first\"/>"
"<field label=\"Family Name\" type=\"text-single\" var=\"last\"/>"
"<field label=\"Jabber ID\" type=\"jid-single\" var=\"jid\"/>"
"<field label=\"Gender\" type=\"list-single\" var=\"x-gender\"/>"
"</reported>"
"<item>"
"<field var=\"first\"><value>Benvolio</value></field>"
"<field var=\"last\"><value>Montague</value></field>"
"<field var=\"jid\"><value>benvolio@montague.net</value></field>"
"<field var=\"x-gender\"><value>male</value></field>"
"</item>"
"<item>"
"<field var=\"first\"><value>Romeo</value></field>"
"<field var=\"last\"><value>Montague</value></field>"
"<field var=\"jid\"><value>romeo@montague.net</value></field>"
"<field var=\"x-gender\"><value>male</value></field>"
"</item>"
"</x>"
"</query>"), testling.serialize(payload));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(SearchPayloadSerializerTest);
|