summaryrefslogtreecommitdiffstats
path: root/test/afb-test/tests/signal-composer_BasicAPITest.lua
blob: 61ef1672474b4677625c3829fb8c24b5c28db335 (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
--[[
   Copyright (C) 2018 "IoT.bzh"
   Author Clément Malléjac <clementmallejac@gmail.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


   NOTE: strict mode: every global variables should be prefixed by '_'
--]]

local testPrefix ="signal_composer_BasicAPITest_";

--[[
################################################################################
########################## Subscribe/Unsubscribe ###############################
################################################################################
]]

---------------------------
-- Successful verb calls --
---------------------------

-- This tests the 'subscribe' verb of the signal-composer API
_AFT.testVerbStatusSuccess(testPrefix.."subscribe","signal-composer","subscribe",{ signal = "longitude"}, nil,
	function()
		_AFT.callVerb("signal-composer","unsubscribe",{ signal = "longitude"})
	end);

-- This tests the 'unsubscribe' verb of the signal-composer API
_AFT.testVerbStatusSuccess(testPrefix.."unsubscribe","signal-composer","unsubscribe",{ signal = "heading"},
	function()
		_AFT.callVerb("signal-composer","subscribe",{ signal = "heading"})
	end, nil);

-- This tests the 'unsubscribe' verb of the signal-composer API when we are not actually subscribed to a signal
_AFT.testVerbStatusSuccess(testPrefix.."doubleUnsubscribe","signal-composer","unsubscribe",{ signal = "latitude"},
	function()
		_AFT.callVerb("signal-composer","unsubscribe",{ signal = "latitude"})
	end, nil);

-- This tests the 'unsubscribe' verb of the signal-composer API when unsubscribing from a non-existing signal
_AFT.testVerbStatusSuccess(testPrefix.."unsubscribeNonExistingSignal","signal-composer","unsubscribe",{ signal = "notASignal"});

-----------------------
-- Failed verb calls --
-----------------------

-- This tests the 'subscribe' verb of the signal-composer API, with a non existing signal, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."subscribeNonExistingSignal","signal-composer","subscribe",{ signal = "notASignal"});

--[[
################################################################################
################################ List/Get ######################################
################################################################################
]]

---------------------------
-- Successful verb calls --
---------------------------

-- This tests the 'list' verb of the signal-composer API
_AFT.testVerbStatusSuccess(testPrefix.."list","signal-composer","list", {});

-- This tests the 'get' verb of the signal-composer API, without any options, this should return the last value for that signal
_AFT.testVerbStatusSuccess(testPrefix.."getNoFilter","signal-composer","get",{signal= "fuel_level"});

-- This tests the 'get' verb of the signal-composer API, with the 'average' option, this should return the average value over the last X seconds
_AFT.testVerbStatusSuccess(testPrefix.."getFilterAvg","signal-composer","get",{signal= "odometer", options= {average= 10}});

-- This tests the 'get' verb of the signal-composer API, with the 'minimum' option, this should return the minimum value over the last X seconds
_AFT.testVerbStatusSuccess(testPrefix.."getFilterMin","signal-composer","get",{signal= "latitude", options= {minimum= 10}});

-- This tests the 'get' verb of the signal-composer API, with the 'maximum' option, this should return the maximum value over the last X seconds
_AFT.testVerbStatusSuccess(testPrefix.."getFilterMax","signal-composer","get",{signal= "vehicle_speed", options= {maximum= 10}});

-- This tests the 'get' verb of the signal-composer API, with the 'last' option, this should return the last value over the last X seconds
_AFT.testVerbStatusSuccess(testPrefix.."getFilterMax","signal-composer","get",{signal= "fuel_level", options= {last= 10}});

-----------------------
-- Failed verb calls --
-----------------------

-- This tests the 'get' verb of the signal-composer API, with an invalid option name, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."getFilterInvalid","signal-composer","get",{signal= "latitude", options= {notValid= 10}});

-- This tests the 'get' verb of the signal-composer API, with a non existing signal, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."getNoFilterInvalidSignal","signal-composer","get",{signal= "notAValidSignal"});

-- This tests the 'get' verb of the signal-composer API, with an invalid option value, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."getFilterOptionInvalidValue","signal-composer","get",{signal= "odometer", options= {average= -1}});

-- This tests the 'get' verb of the signal-composer API, with an invalid parameter name, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."getFilterInvalidFirstArgument","signal-composer","get",{notValidAtAll= "vehicule_speed", options= {average= 10}});

-- This tests the 'get' verb of the signal-composer API, with an invalid second parameter name, it should reply with an error
_AFT.testVerbStatusError(testPrefix.."getFilterInvalidSecondArgument","signal-composer","get",{signal= "vehicule_speed", notValidAtAll= {average= 10}});

--[[
################################################################################
########################### Adding new sources #################################
################################################################################
]]

---------------------------
-- Successful verb calls --
---------------------------

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing the path of a json containing sources
	 then making a get, a subscribe, and an unsubscribe looking for any misbehaviour from sources added with the verb ]]
_AFT.describe(testPrefix.."addSourcesByFile", function()
	_AFT.assertVerbStatusSuccess("signal-composer", "addObjects", {file = _AFT.bindingRootDir.."/var/source.json"})
end);

-- This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table
_AFT.testVerbStatusSuccess(testPrefix.."addSourcesDirect","signal-composer","addObjects",
	{
		sources = {
			uid = "DummySource2",
			api = "dummy2",
			info = "Another dummy source to be used as test source",
		}
	}
);

-----------------------
-- Failed verb calls --
-----------------------

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table.
	This one has invalid doesn't specified the required api field which should fails the assertion]]
_AFT.testVerbStatusError(testPrefix.."addSourcesDirect_InvalidSource","signal-composer","addObjects",
	{
		sources = {
			{
				uid = "invalidSource",
				info = "An invalid source without Api specified",
			}
		}
	}
);

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table.
	This one has a wrong retention field ]]
_AFT.testVerbStatusError(testPrefix.."addSourcesDirect_MissingField","signal-composer","addObjects",
	{
		sources= {
			{
				api = "invalidSource3",
				info = "An invalid source without UID specified",
				retention = "Invalid retention"
			}
		}
	}
);

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing the path of a json containing sources
	This one has invalid values for most of its field, the binding should not be able to add it ]]
_AFT.testVerbStatusError(testPrefix.."addSourcesByFile_InvalidSources","signal-composer","addObjects",{file = _AFT.bindingRootDir.."/var/sources_invalid.json"});

--[[
################################################################################
########################### Adding new signals #################################
################################################################################
]]

---------------------------
-- Successful verb calls --
---------------------------

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing the path of a json containing signals
	 then making a get, a subscribe, and an unsubscribe looking for any misbehaviour from signals added with the verb ]]
		_AFT.describe(testPrefix.."addSignalsByFile", function()
			_AFT.assertVerbStatusSuccess("signal-composer","addObjects",{file = _AFT.bindingRootDir.."/var/sig_test.json"})
			_AFT.assertVerbStatusSuccess("signal-composer","get",{signal= "vehicle_speedTest1", options= {average=10}});
			_AFT.assertVerbStatusSuccess("signal-composer","subscribe",{ signal = "vehicle_speedTest1"});
			    _AFT.assertVerbStatusSuccess("signal-composer","unsubscribe",{ signal = "vehicle_speedTest1"});
		end);

_AFT.setAfter(testPrefix.."addSignalsByFile", function()
	_AFT.callVerb("signal-composer","unsubscribe",{ signal = "vehicle_speedTest1"})
end)

-- This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table
_AFT.testVerbStatusSuccess(testPrefix.."addSignalsDirect","signal-composer","addObjects",
{
	signals=  {
		{
			uid=  "vehicle_speedTest3",
			event=  "txc-binding/vehicle_speed",
			retention=  30,
			unit=  "km/h",
			getSignalsArgs=  {
				event=  "vehicle_speed"
			},
			onReceived=  {
				action=  "lua://convert#_Unit_Converter",
				args=  {
					from=  "km/h",
					to=  "mi/h"
				}
			}
		},
		{
			uid=  "engine_speedTest4",
			event=  "txc-binding/engine_speed",
			retention=  30,
			unit=  "rpm",
			getSignalsArgs=  {
				event=  "engine_speed"
			}
		}
	}
}
);

-----------------------
-- Failed verb calls --
-----------------------

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table.
	This one has invalid values for most of its field, the binding should not be able to add it ]]
_AFT.testVerbStatusError(testPrefix.."addSignalsDirect_InvalidSignal","signal-composer","addObjects",
	{
		signals=  {
			{
				uid=  "invalidSignal",
				event=  "txc-binding/invalidSignal",
				retention=  -1,
				unit=  "invalidSignal",
				getSignalsArgs=  {
					event=  "invalidSignal"
				},
				onReceived=  {
					action=  "lua://convert#_Unit_Converter",
					args=  {
						from=  "km/h",
						to=  "mi/h"
					}
				}
			}
		}
	}
);

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing directly the json object as a lua table.
	This one is missing the mandatory 'uid' field, the binding should not be able to add it ]]
_AFT.testVerbStatusError(testPrefix.."addSignalsDirect_MissingField","signal-composer","addObjects",
{
		signals=  {
			{
				event=  "txc-binding/invalidSignal2",
				retention=  30,
				unit=  "km/h",
				getSignalsArgs=  {
					event=  "vehicle_speed"
				},
				onReceived=  {
					action=  "lua://convert#_Unit_Converter",
					args=  {
						from=  "km/h",
						to=  "mi/h"
					}
				}
			}
		}
	}
);

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing the path of a json containing signals
	This one has invalid values for most of its field, the binding should not be able to add it ]]
_AFT.testVerbStatusError(testPrefix.."addSignalsByFile_InvalidSignal","signal-composer","addObjects",{file = _AFT.bindingRootDir.."/var/sig_testInvalid.json"});

--[[ This tests the 'addObjects' verb of the signal-composer API, this is by passing the path of a json containing signals
	This one is missing the mandatory 'uid' field, the binding should not be able to add it ]]
_AFT.testVerbStatusError(testPrefix.."addSignalsByFile_Missingfield","signal-composer","addObjects",{file = _AFT.bindingRootDir.."/var/sig_incomplete.json"});

_AFT.exitAtEnd();