summaryrefslogtreecommitdiff
path: root/tests/test_stream_xep_0325.py
blob: 4b3250fcc8198a7bf9397b3899bf0aad4ff9408a (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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
"""
    SleekXMPP: The Sleek XMPP Library
    Implementation of xeps for Internet of Things
    http://wiki.xmpp.org/web/Tech_pages/IoT_systems
    Copyright (C) 2013 Sustainable Innovation, Joachim.lindborg@sust.se, bjorn.westrom@consoden.se
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import sys
import datetime
import time
import threading

from sleekxmpp.test import *
from sleekxmpp.xmlstream import ElementBase
from sleekxmpp.plugins.xep_0325.device import Device


class TestStreamControl(SleekTest):

    """
    Test using the XEP-0325 plugin.
    """
    def setUp(self):
        pass

    def _time_now(self):
        return datetime.datetime.now().replace(microsecond=0).isoformat();

    def tearDown(self):
        self.stream_close()

    def testRequestSetOk(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325'])

        myDevice = Device("Device22");
        myDevice._add_control_field(name="Temperature", typename="int", value="15");

        self.xmpp['xep_0325'].register_node(nodeId="Device22", device=myDevice, commTimeout=0.5);

        self.recv("""
            <iq type='set'
                from='master@clayster.com/amr'
                to='device@clayster.com'
                id='1'>
                <set xmlns='urn:xmpp:iot:control'>
                    <int name="Temperature" value="17"/>
                </set>
            </iq>
        """)

        self.send("""
            <iq type='result'
                from='device@clayster.com'
                to='master@clayster.com/amr'
                id='1'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode="OK" />
            </iq>            
            """)

        self.assertEqual(myDevice._get_field_value("Temperature"), "17");

    def testRequestSetMulti(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325'])

        myDevice = Device("Device22");
        myDevice._add_control_field(name="Temperature", typename="int", value="15");
        myDevice._add_control_field(name="Startup", typename="date", value="2013-01-03");

        myDevice2 = Device("Device23");
        myDevice2._add_control_field(name="Temperature", typename="int", value="19");
        myDevice2._add_control_field(name="Startup", typename="date", value="2013-01-09");

        self.xmpp['xep_0325'].register_node(nodeId="Device22", device=myDevice, commTimeout=0.5);
        self.xmpp['xep_0325'].register_node(nodeId="Device23", device=myDevice2, commTimeout=0.5);

        self.recv("""
            <iq type='set'
                from='master@clayster.com/amr'
                to='device@clayster.com'
                id='1'>
                <set xmlns='urn:xmpp:iot:control'>
                    <node nodeId='Device22' />
                    <int name="Temperature" value="17"/>
                </set>
            </iq>
        """)

        self.send("""
            <iq type='result'
                from='device@clayster.com'
                to='master@clayster.com/amr'
                id='1'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode="OK" />
            </iq>            
            """)

        self.assertEqual(myDevice._get_field_value("Temperature"), "17");
        self.assertEqual(myDevice2._get_field_value("Temperature"), "19");

        self.recv("""
            <iq type='set'
                from='master@clayster.com/amr'
                to='device@clayster.com'
                id='2'>
                <set xmlns='urn:xmpp:iot:control'>
                    <node nodeId='Device23' />
                    <node nodeId='Device22' />
                    <date name="Startup" value="2013-02-01"/>
                    <int name="Temperature" value="20"/>
                </set>
            </iq>
        """)

        self.send("""
            <iq type='result'
                from='device@clayster.com'
                to='master@clayster.com/amr'
                id='2'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode="OK" />
            </iq>            
            """)

        self.assertEqual(myDevice._get_field_value("Temperature"), "20");
        self.assertEqual(myDevice2._get_field_value("Temperature"), "20");
        self.assertEqual(myDevice._get_field_value("Startup"), "2013-02-01");
        self.assertEqual(myDevice2._get_field_value("Startup"), "2013-02-01");

    def testRequestSetFail(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325'])

        myDevice = Device("Device23");
        myDevice._add_control_field(name="Temperature", typename="int", value="15");

        self.xmpp['xep_0325'].register_node(nodeId="Device23", device=myDevice, commTimeout=0.5);

        self.recv("""
            <iq type='set'
                from='master@clayster.com/amr'
                to='device@clayster.com'
                id='9'>
                <set xmlns='urn:xmpp:iot:control'>
                    <int name="Voltage" value="17"/>
                </set>
            </iq>
        """)

        self.send("""
            <iq type='error'
                from='device@clayster.com'
                to='master@clayster.com/amr'
                id='9'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode='NotFound'>
                    <parameter name='Voltage' />
                    <error var='Output'>Invalid field Voltage</error>
                </setResponse>
            </iq>            
            """)

        self.assertEqual(myDevice._get_field_value("Temperature"), "15");
        self.assertFalse(myDevice.has_control_field("Voltage", "int"));

    def testDirectSetOk(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325'])

        myDevice = Device("Device22");
        myDevice._add_control_field(name="Temperature", typename="int", value="15");

        self.xmpp['xep_0325'].register_node(nodeId="Device22", device=myDevice, commTimeout=0.5);

        self.recv("""
            <message 
                from='master@clayster.com/amr'
                to='device@clayster.com'>
                <set xmlns='urn:xmpp:iot:control'>
                    <int name="Temperature" value="17"/>
                </set>
            </message>
        """)

        time.sleep(.5)

        self.assertEqual(myDevice._get_field_value("Temperature"), "17");

    def testDirectSetFail(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325'])

        myDevice = Device("Device22");
        myDevice._add_control_field(name="Temperature", typename="int", value="15");

        self.xmpp['xep_0325'].register_node(nodeId="Device22", device=myDevice, commTimeout=0.5);

        self.recv("""
            <message 
                from='master@clayster.com/amr'
                to='device@clayster.com'>
                <set xmlns='urn:xmpp:iot:control'>
                    <int name="Voltage" value="17"/>
                </set>
            </message>
        """)

        time.sleep(.5)

        self.assertEqual(myDevice._get_field_value("Temperature"), "15");
        self.assertFalse(myDevice.has_control_field("Voltage", "int"));


    def testRequestSetOkAPI(self):

        self.stream_start(mode='client',
                          plugins=['xep_0030',
                                   'xep_0325'])

        results = []; 

        def my_callback(from_jid, result, nodeIds=None, fields=None, error_msg=None):
            results.append(result);

        fields = []
        fields.append(("Temperature", "double", "20.5"))
        fields.append(("TemperatureAlarmSetting", "string", "High"))

        self.xmpp['xep_0325'].set_request(from_jid="tester@localhost", to_jid="you@google.com", fields=fields, nodeIds=['Device33', 'Device22'], callback=my_callback);

        self.send("""
            <iq type='set'
                from='tester@localhost'
                to='you@google.com'
                id='1'>
                <set xmlns='urn:xmpp:iot:control'>
                    <node nodeId='Device33' />
                    <node nodeId='Device22' />
                    <double name="Temperature" value="20.5" />
                    <string name="TemperatureAlarmSetting" value="High" />
                </set>
            </iq>
            """)

        self.recv("""
            <iq type='result'
                from='you@google.com'
                to='tester@localhost'
                id='1'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode="OK" />
            </iq>               
            """)

        time.sleep(.5)

        self.assertEqual(results, ["OK"]);

    def testRequestSetErrorAPI(self):

        self.stream_start(mode='client',
                          plugins=['xep_0030',
                                   'xep_0325'])

        results = []; 

        def my_callback(from_jid, result, nodeIds=None, fields=None, error_msg=None):
            results.append(result);

        fields = []
        fields.append(("Temperature", "double", "20.5"))
        fields.append(("TemperatureAlarmSetting", "string", "High"))

        self.xmpp['xep_0325'].set_request(from_jid="tester@localhost", to_jid="you@google.com", fields=fields, nodeIds=['Device33', 'Device22'], callback=my_callback);

        self.send("""
            <iq type='set'
                from='tester@localhost'
                to='you@google.com'
                id='1'>
                <set xmlns='urn:xmpp:iot:control'>
                    <node nodeId='Device33' />
                    <node nodeId='Device22' />
                    <double name="Temperature" value="20.5" />
                    <string name="TemperatureAlarmSetting" value="High" />
                </set>
            </iq>
            """)

        self.recv("""
            <iq type='error'
                from='you@google.com'
                to='tester@localhost'
                id='1'>
                <setResponse xmlns='urn:xmpp:iot:control' responseCode="OtherError" >
                    <error var='Temperature'>Sensor error</error>
                </setResponse>
            </iq>               
            """)

        time.sleep(.5)

        self.assertEqual(results, ["OtherError"]);

    def testServiceDiscoveryClient(self):
        self.stream_start(mode='client',
                          plugins=['xep_0030',
                                   'xep_0325']);

        self.recv("""
        <iq type='get'
                from='master@clayster.com/amr'
                to='tester@localhost'
                id='disco1'>
            <query xmlns='http://jabber.org/protocol/disco#info'/>
        </iq>
        """)

        self.send("""
        <iq type='result'
            to='master@clayster.com/amr'
            id='disco1'>
            <query xmlns='http://jabber.org/protocol/disco#info'>
                <identity category='client' type='bot'/>
                <feature var='urn:xmpp:iot:control'/>
            </query>  
        </iq>            
        """)

    def testServiceDiscoveryComponent(self):
        self.stream_start(mode='component',
                          plugins=['xep_0030',
                                   'xep_0325']);

        self.recv("""
        <iq type='get'
                from='master@clayster.com/amr'
                to='tester@localhost'
                id='disco1'>
            <query xmlns='http://jabber.org/protocol/disco#info'/>
        </iq>
        """)

        self.send("""
        <iq type='result'
            from='tester@localhost'
            to='master@clayster.com/amr'
            id='disco1'>
            <query xmlns='http://jabber.org/protocol/disco#info'>
                <identity category='component' type='generic'/>
                <feature var='urn:xmpp:iot:control'/>
            </query>  
        </iq>            
        """)


suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamControl)