summaryrefslogtreecommitdiff
path: root/plugins/testplugin/testplugin.cpp
blob: 6855ffbe56404047d5a545d7faaccfbb12b07411 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
	Copyright (C) 2012  Intel Corporation

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include <iostream>
#include <boost/assert.hpp>
#include <boost/lexical_cast.hpp>
#include <glib.h>
#include <sstream>
//#include <json-glib/json-glib.h>
#include <listplusplus.h>
#include <vehicleproperty.h>
#include <abstractpropertytype.h>
#include "debugout.h"
#include "timestamp.h"
#include "testplugin.h"
#include <jsonhelper.h>

#define __SMALLFILE__ std::string(__FILE__).substr(std::string(__FILE__).rfind("/")+1)
AbstractRoutingEngine *m_re;

#define TEST(success)	DebugOut(0) << "Testing " << ""#success"" << endl; g_assert((success));
const std::string TestProptertyName1 = "TestPropertyName1";
const std::string TestProptertyName2 = "TestPropertyName2";

//std::list<ObdPid*> Obd2Amb::supportedPidsList;

void testBooleanToStringFromString()
{
	BasicPropertyType<bool> boolean(true);
	std::string isTrue = boolean.toString();
	boolean.fromString(boolean.toString());
	std::string isTrue2 = boolean.toString();

	TEST(isTrue == isTrue2);
}

bool beginsWith(std::string a, std::string b)
{
	return (a.compare(0, b.length(), b) == 0);
}

/**
 * Tests Core's methods:
 * * setSupported
 * * supported
 * * sourcesForProperty
 * * getPropertyInfo
 */

bool TestPlugin::testCoreSetSupported()
{
	PropertyList supported(routingEngine->supported());
	TEST(contains(supported, TestProptertyName1) == false);

	//
	// CALL setSupported
	//

	// valid call
	routingEngine->updateSupported(m_supportedProperties, PropertyList(), this);

	TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == true);
	Zone::ZoneList zones(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).zones());
	TEST(contains(zones, Zone::LeftSide) == true);

	supported = routingEngine->supported();
	TEST(contains(supported, TestProptertyName1) == true);
	TEST(contains(supported, TestProptertyName2) == true);
	TEST(contains(supported, VehicleProperty::ClutchStatus) == false);

	std::vector<std::string> sources = routingEngine->sourcesForProperty(TestProptertyName1);
	TEST(contains(sources,uuid()) == true);

	TEST(routingEngine->getPropertyInfo(TestProptertyName2, uuid()).isValid() == true);
	zones = routingEngine->getPropertyInfo(TestProptertyName2, uuid()).zones();
	TEST(contains(zones, Zone::FrontSide) == true);

	return true;
}

/**
 * Tests Core's methods:
 * * unregisterSink
 * * registerSink
 * * sourcesForProperty
 * * subscribeToProperty - 2 versions
 * * unsubscribeToProperty
 */

bool TestPlugin::testSubscription()
{
	int oldSubscriptionsToSupportedCounter = subscriptionsToSupportedCounter;
	int oldSubscriptionsToUnsupportedCounter = subscriptionsToUnsupportedCounter;
	int oldUnsubscriptionsToSupportedCounter= unsubscriptionsToSupportedCounter;
	int oldUnsubscriptionsToUnsupportedCounter = unsubscriptionsToUnsupportedCounter;

	// ! subscription works without having sink registered ! so there is no need to test
	// routingEngine->registerSink(this) and routingEngine->unregisterSink(this)
	routingEngine->unregisterSink(this);
	routingEngine->registerSink(this);

	// VehicleProperty::ClutchStatus doesn't exist
	TEST(routingEngine->subscribeToProperty(VehicleProperty::ClutchStatus, uuid(), this) == false);
	TEST(oldSubscriptionsToSupportedCounter == subscriptionsToSupportedCounter);
	TEST(oldSubscriptionsToUnsupportedCounter == subscriptionsToUnsupportedCounter);

	// TestProptertyName1 exists in our testplugin
	TEST(routingEngine->subscribeToProperty(TestProptertyName1, uuid(), this) == true);
	TEST(++oldSubscriptionsToSupportedCounter == subscriptionsToSupportedCounter);
	TEST(oldSubscriptionsToUnsupportedCounter == subscriptionsToUnsupportedCounter);

	TEST(routingEngine->subscribeToProperty(TestProptertyName1, "", this) == false);
	TEST(oldSubscriptionsToSupportedCounter == subscriptionsToSupportedCounter);
	TEST(oldSubscriptionsToUnsupportedCounter == subscriptionsToUnsupportedCounter);

	// "other" source means no subscription callback in our plugin
	TEST(routingEngine->subscribeToProperty(TestProptertyName1, "other", this) == false);
	TEST(oldSubscriptionsToSupportedCounter == subscriptionsToSupportedCounter);
	TEST(oldSubscriptionsToUnsupportedCounter == subscriptionsToUnsupportedCounter);

	// we have subscribed TestProptertyName1 and not subscribed TestProptertyName2
	int oldSupportedPropertyChanges = supportedPropertyChanges;
	int oldPropertyChanges = propertyChanges;

	AbstractPropertyType* value = new BasicPropertyType<int>(TestProptertyName1, 22);
	value->priority = AbstractPropertyType::Instant;
	routingEngine->updateProperty(value, "");
	TEST(oldSupportedPropertyChanges == supportedPropertyChanges);
	TEST(oldPropertyChanges == propertyChanges);
	routingEngine->updateProperty(value, uuid());
	TEST(++oldSupportedPropertyChanges == supportedPropertyChanges);
	TEST(++oldPropertyChanges == propertyChanges);
	delete value;

	value = new BasicPropertyType<short>(TestProptertyName2, 255);
	value->priority = AbstractPropertyType::Instant;
	routingEngine->updateProperty(value, "");
	TEST(oldSupportedPropertyChanges == supportedPropertyChanges);
	TEST(oldPropertyChanges == propertyChanges);
	routingEngine->updateProperty(value, uuid());
	TEST(oldSupportedPropertyChanges == supportedPropertyChanges);
	TEST(oldPropertyChanges == propertyChanges);
	delete value;

	value = new BasicPropertyType<bool>(VehicleProperty::ClutchStatus, true);
	value->priority = AbstractPropertyType::Instant;
	routingEngine->updateSupported({VehicleProperty::ClutchStatus},PropertyList(), this);

	class TestSink : public AbstractSink
	{
	public:
		TestSink(AbstractRoutingEngine* engine, map<string, string> config) : AbstractSink(engine, config) {}
		virtual const string uuid() { return "other"; }
		virtual void propertyChanged(AbstractPropertyType *value){ ++propertyChanges; }
		virtual void supportedChanged(const PropertyList & supportedProperties){};

		int propertyChanges = 0;
	};

	//some other sink is subscribed, not this plug-in!
	TestSink anotherSink(routingEngine, {});
	TEST(routingEngine->subscribeToProperty(VehicleProperty::ClutchStatus, &anotherSink) == true);
	TEST(oldSubscriptionsToSupportedCounter == subscriptionsToSupportedCounter);
	TEST(++oldSubscriptionsToUnsupportedCounter == subscriptionsToUnsupportedCounter);
	routingEngine->updateProperty(value, uuid());// uuid() != "other" no sink notified
	TEST(anotherSink.propertyChanges == 1);// one update
	TEST(oldSupportedPropertyChanges == supportedPropertyChanges);// no updates
	TEST(oldPropertyChanges == propertyChanges);// no updates
	TEST(routingEngine->unsubscribeToProperty(VehicleProperty::ClutchStatus, &anotherSink) == true);
	routingEngine->updateSupported(PropertyList(),{VehicleProperty::ClutchStatus}, this);
	TEST(oldUnsubscriptionsToSupportedCounter == unsubscriptionsToSupportedCounter);
	TEST(++oldUnsubscriptionsToUnsupportedCounter == unsubscriptionsToUnsupportedCounter);


	/// test lamba subscription:

	TEST (routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed,[](AbstractPropertyType* value)
	{
		DebugOut(0)<<"lamba subscriptions work!"<<endl;
		TEST(value);
	}) > 0);

	/// change vehiclespeed to trigger subscribe above

	PropertyList s;
	s.push_back(VehicleProperty::VehicleSpeed);

	routingEngine->updateSupported(s, PropertyList(), this);
	VehicleProperty::VehicleSpeedType speed(10);
	speed.priority = AbstractPropertyType::Instant;

	routingEngine->updateProperty(&speed,uuid());
	routingEngine->updateSupported(PropertyList(), s, this);

	delete value;

	// unsubscription

	// VehicleProperty::ClutchStatus doesn't exist
	TEST(routingEngine->unsubscribeToProperty(VehicleProperty::ClutchStatus, this) == false);
	TEST(oldUnsubscriptionsToSupportedCounter == unsubscriptionsToSupportedCounter);
	TEST(oldUnsubscriptionsToUnsupportedCounter == unsubscriptionsToUnsupportedCounter);

	// TestProptertyName1 exists in our testplugin
	// subscribed 2x, lets try to unsubscribe 3x
	// we should get only one unsubscription callback
	TEST(routingEngine->unsubscribeToProperty(TestProptertyName1, this) == true);
	TEST(routingEngine->unsubscribeToProperty(TestProptertyName1, this) == false);
	TEST(routingEngine->unsubscribeToProperty(TestProptertyName1, this) == false);
	TEST(++oldUnsubscriptionsToSupportedCounter == unsubscriptionsToSupportedCounter);
	TEST(oldUnsubscriptionsToUnsupportedCounter == unsubscriptionsToUnsupportedCounter);

	// TestProptertyName2 not subscribed
	TEST(routingEngine->unsubscribeToProperty(TestProptertyName2, this) == false);;
	TEST(oldUnsubscriptionsToSupportedCounter == unsubscriptionsToSupportedCounter);
	TEST(oldUnsubscriptionsToUnsupportedCounter == unsubscriptionsToUnsupportedCounter);

	return true;
}

bool testListPlusPlus()
{
	std::map<std::string, int> theMap;
	theMap["1"] = 1;
	theMap["2"] = 2;

	TEST(amb::containsKey(theMap, "1"));
	TEST(!amb::containsKey(theMap, "bar"));

	std::vector<int> list;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);

	TEST(contains(list, 2));
	removeOne(&list, 2);
	TEST(!contains(list, 2));

	class Complex
	{
	public:
		Complex(int a, int b): foo(a), bar(b) {}
		int foo;
		int bar;
	};

	Complex complex1(1, 2);

	Complex complex2(2, 2);

	std::vector<Complex> complexList;
	complexList.push_back(complex1);
	complexList.push_back(complex2);

	TEST(contains(complexList, complex1, [](auto a, auto b) { return a.foo == b.foo && a.bar == b.bar; }));

	return true;
}

void testJsonHelper()
{

}

bool TestPlugin::testSetAndGet()
{
	bool replySuccess(false);
	int replyError(-1);
	std::string replySignalName("");

	// Invalid request test
	AsyncSetPropertyRequest requestInvalid;
	requestInvalid.timeout = 0;
	AsyncPropertyReply* reply = routingEngine->setProperty(requestInvalid);
	TEST(reply == nullptr);

	requestInvalid.property = "NotExists";
	requestInvalid.completed = [&](AsyncPropertyReply* reply)
	{
		replySuccess = reply->success;
		replyError = reply->error;
		delete reply;
	};

	reply = routingEngine->setProperty(requestInvalid);

	TEST(replySuccess == false);
	TEST(replyError == -1);

	AsyncSetPropertyRequest request;
	request.timeout = 0;
	request.property = TestProptertyName1;
	request.zoneFilter = Zone::LeftSide;
	request.value = VehicleProperty::getPropertyTypeForPropertyNameValue(request.property, "1");
	request.completed = requestInvalid.completed;

	reply = routingEngine->setProperty(request);
	delete request.value;
	request.value = nullptr;

	TEST(replySuccess == true);
	TEST(replyError == AsyncPropertyReply::NoError);

	return replySuccess;
}

bool TestPlugin::testCoreUpdateSupported()
{
	bool success = false;

	PropertyList toAdd;
	toAdd.push_back(VehicleProperty::ClutchStatus);

	routingEngine->updateSupported(toAdd, PropertyList(), this);

	PropertyList supported = routingEngine->supported();

	success = contains(supported,VehicleProperty::ClutchStatus);

	PropertyList toRemove = toAdd;

	routingEngine->updateSupported(PropertyList(),toRemove, this);

	supported = routingEngine->supported();

	success &= !contains(supported,VehicleProperty::ClutchStatus);

	return success;
}

void TestPlugin::setConfiguration(map<string, string> config)
{
	// 	//Config has been passed, let's start stuff up.
}

TestPlugin::TestPlugin(AbstractRoutingEngine *re, map<string, string> config)
	: AbstractSource(re, config),
	  m_supportedProperties({TestProptertyName1, TestProptertyName2}),
	  subscriptionsToSupportedCounter(0),
	  subscriptionsToUnsupportedCounter(0),
	  unsubscriptionsToSupportedCounter(0),
	  unsubscriptionsToUnsupportedCounter(0),
	  propertyChanges(0),
	  supportedPropertyChanges(0)
{

	DebugOut(0) << "Capabilities: " << endl;

	for(auto prop : VehicleProperty::capabilities())
	{
		DebugOut(0) << prop << endl;
	}

	DebugOut() << "Testing Core::setSupported... " << endl;
	testCoreSetSupported();

	DebugOut() << "Testing MapPropertyType... " << endl;
	MapPropertyType<BasicPropertyType<Door::Status>> propmap("Something");
	MapPropertyType<BasicPropertyType<Door::Status>> propmaptwo("SomethingElse");
	propmap.append("hi", Door::Ajar);
	GVariant *var = propmap.toVariant();
	propmaptwo.fromVariant(var);

	g_assert(propmaptwo.toString() == propmap.toString());

	///test fromString:
	propmap.fromString(propmaptwo.toString());

	DebugOut() << "Testing map string equivalency: " << propmaptwo.toString() << "==" << propmap.toString() << endl;

	g_assert(propmaptwo.toString() == propmap.toString());

	DebugOut() << "Testing ListPropertyType... " << endl;
	VehicleProperty::TripMetersType* tfirst = new VehicleProperty::TripMetersType();
	VehicleProperty::TripMetersType* tsecond = new VehicleProperty::TripMetersType();
	tfirst->append(0);
	tfirst->append(5);
	tfirst->append(10);
	tsecond->fromVariant(tfirst->toVariant());

	GVariant* testGVSVariant = g_variant_new("i", 9);
	TEST(GVS<int>::value(testGVSVariant) == 9);

	TEST (tfirst->toString() == tsecond->toString());

	testBooleanToStringFromString();

	TEST (testCoreUpdateSupported());

	testSubscription();

	testSetAndGet();

	TEST(testListPlusPlus());

	testJsonHelper();
}

TestPlugin::~TestPlugin()
{
	DebugOut() << "TestPlugin Destructor called!!!"<<endl;
}

PropertyList TestPlugin::supported()
{
	return m_supportedProperties;
}

int TestPlugin::supportedOperations()
{
	return Get | Set;
}

extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
{
	new TestPlugin(routingengine, config);
}
const string TestPlugin::uuid()
{
	return "f77af740-f1f8-11e1-aff1-0800200c9a66";
}
void TestPlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
{
	if(contains(m_supportedProperties, property))
		++subscriptionsToSupportedCounter;
	else
		++subscriptionsToUnsupportedCounter;
}


void TestPlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
{
	if(contains(m_supportedProperties, property))
		++unsubscriptionsToSupportedCounter;
	else
		++unsubscriptionsToUnsupportedCounter;
}


void TestPlugin::getPropertyAsync(AsyncPropertyReply *reply)
{

}

AsyncPropertyReply *TestPlugin::setProperty(AsyncSetPropertyRequest request )
{
	AsyncPropertyReply* reply = new AsyncPropertyReply (request);
	reply->success = true;
	reply->error = AsyncPropertyReply::NoError;
	if(reply->completed)
		reply->completed(reply);
	return reply;
}

PropertyInfo TestPlugin::getPropertyInfo(const VehicleProperty::Property &property)
{
	if(!contains(m_supportedProperties, property))
		return PropertyInfo::invalid();

	if(property == TestProptertyName1){
		return PropertyInfo(10, {Zone::LeftSide});
	}
	else if(property == TestProptertyName2){
		return PropertyInfo(30, {Zone::FrontSide});
	}
	else{
		return PropertyInfo(60, {Zone::None});
	}
}

void TestPlugin::propertyChanged(AbstractPropertyType *value)
{
	++propertyChanges;
	if(value && contains(m_supportedProperties, value->name))
		supportedPropertyChanges++;

}