Merge pull request #69 from fredcadete/master
[profile/ivi/automotive-message-broker.git] / plugins / testplugin / testplugin.cpp
index 0354822..d854d5f 100644 (file)
 #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)  g_assert((success));
+#define TEST(success)  DebugOut(0) << "Testing " << ""#success"" << endl; g_assert((success));
 const std::string TestProptertyName1 = "TestPropertyName1";
 const std::string TestProptertyName2 = "TestPropertyName2";
 
@@ -45,17 +47,13 @@ void testBooleanToStringFromString()
        boolean.fromString(boolean.toString());
        std::string isTrue2 = boolean.toString();
 
-       g_assert(isTrue == isTrue2);
+       TEST(isTrue == isTrue2);
 }
 
 bool beginsWith(std::string a, std::string b)
 {
        return (a.compare(0, b.length(), b) == 0);
 }
-void TestPlugin::updateProperty(VehicleProperty::Property property,AbstractPropertyType* value)
-{
-
-}
 
 /**
  * Tests Core's methods:
@@ -68,46 +66,30 @@ void TestPlugin::updateProperty(VehicleProperty::Property property,AbstractPrope
 bool TestPlugin::testCoreSetSupported()
 {
        PropertyList supported(routingEngine->supported());
-       TEST(ListPlusPlus<VehicleProperty::Property>(&supported).contains(TestProptertyName1) == false);
-       std::list<std::string> sources(routingEngine->sourcesForProperty(TestProptertyName1));
-       TEST(ListPlusPlus<std::string>(&sources).contains(uuid()) == false);
-
-       // if we were registered in Core, it will ask us and we will return it valid, but we haven't registered yet
-       TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == false);
-
+       TEST(contains(supported, TestProptertyName1) == false);
 
        //
        // CALL setSupported
        //
-       routingEngine->setSupported(m_supportedProperties, nullptr);// invalid input
-       TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == false);
 
        // valid call
-       routingEngine->setSupported(m_supportedProperties, this);// we are register as source from now
+       routingEngine->updateSupported(m_supportedProperties, PropertyList(), this);
 
        TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == true);
        Zone::ZoneList zones(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).zones());
-       TEST(ListPlusPlus<Zone::Type>(&zones).contains(Zone::LeftSide) == true);
+       TEST(contains(zones, Zone::LeftSide) == true);
 
        supported = routingEngine->supported();
-       TEST(ListPlusPlus<VehicleProperty::Property>(&supported).contains(TestProptertyName1) == true);
-       TEST(ListPlusPlus<VehicleProperty::Property>(&supported).contains(TestProptertyName2) == true);
-       TEST(ListPlusPlus<VehicleProperty::Property>(&supported).contains(VehicleProperty::ClutchStatus) == false);
+       TEST(contains(supported, TestProptertyName1) == true);
+       TEST(contains(supported, TestProptertyName2) == true);
+       TEST(contains(supported, VehicleProperty::ClutchStatus) == false);
 
-       sources = routingEngine->sourcesForProperty(TestProptertyName1);
-       TEST(ListPlusPlus<std::string>(&sources).contains(uuid()) == true);
+       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(ListPlusPlus<Zone::Type>(&zones).contains(Zone::FrontSide) == true);
-
-       // we haven't put VehicleProperty::ClutchStatus to Core as supported,
-       // but our impl. returns valid Property for it if we will insert it into m_supportedProperties
-       m_supportedProperties.push_back(VehicleProperty::ClutchStatus);
-       TEST(routingEngine->getPropertyInfo(VehicleProperty::ClutchStatus, uuid()).isValid() == true);
-       zones = routingEngine->getPropertyInfo(VehicleProperty::ClutchStatus, uuid()).zones();
-       TEST(ListPlusPlus<Zone::Type>(&zones).contains(Zone::None) == true);
-       removeOne(&m_supportedProperties, VehicleProperty::ClutchStatus);
+       TEST(contains(zones, Zone::FrontSide) == true);
 
        return true;
 }
@@ -157,6 +139,7 @@ bool TestPlugin::testSubscription()
        int oldPropertyChanges = propertyChanges;
 
        AbstractPropertyType* value = new BasicPropertyType<int>(TestProptertyName1, 22);
+       value->priority = AbstractPropertyType::Instant;
        routingEngine->updateProperty(value, "");
        TEST(oldSupportedPropertyChanges == supportedPropertyChanges);
        TEST(oldPropertyChanges == propertyChanges);
@@ -166,6 +149,7 @@ bool TestPlugin::testSubscription()
        delete value;
 
        value = new BasicPropertyType<short>(TestProptertyName2, 255);
+       value->priority = AbstractPropertyType::Instant;
        routingEngine->updateProperty(value, "");
        TEST(oldSupportedPropertyChanges == supportedPropertyChanges);
        TEST(oldPropertyChanges == propertyChanges);
@@ -175,6 +159,7 @@ bool TestPlugin::testSubscription()
        delete value;
 
        value = new BasicPropertyType<bool>(VehicleProperty::ClutchStatus, true);
+       value->priority = AbstractPropertyType::Instant;
        routingEngine->updateSupported({VehicleProperty::ClutchStatus},PropertyList(), this);
 
        class TestSink : public AbstractSink
@@ -183,7 +168,7 @@ bool TestPlugin::testSubscription()
                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(PropertyList supportedProperties){};
+               virtual void supportedChanged(const PropertyList & supportedProperties){};
 
                int propertyChanges = 0;
        };
@@ -202,6 +187,27 @@ bool TestPlugin::testSubscription()
        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
@@ -228,6 +234,50 @@ bool TestPlugin::testSubscription()
        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);
@@ -275,11 +325,11 @@ bool TestPlugin::testCoreUpdateSupported()
        PropertyList toAdd;
        toAdd.push_back(VehicleProperty::ClutchStatus);
 
-       routingEngine->updateSupported(toAdd,PropertyList(), this);
+       routingEngine->updateSupported(toAdd, PropertyList(), this);
 
        PropertyList supported = routingEngine->supported();
 
-       success = ListPlusPlus<VehicleProperty::Property>(&supported).contains(VehicleProperty::ClutchStatus);
+       success = contains(supported,VehicleProperty::ClutchStatus);
 
        PropertyList toRemove = toAdd;
 
@@ -287,64 +337,79 @@ bool TestPlugin::testCoreUpdateSupported()
 
        supported = routingEngine->supported();
 
-       success &= !ListPlusPlus<VehicleProperty::Property>(&supported).contains(VehicleProperty::ClutchStatus);
+       success &= !contains(supported,VehicleProperty::ClutchStatus);
 
        return success;
 }
 
 void TestPlugin::setConfiguration(map<string, string> config)
 {
-//     //Config has been passed, let's start stuff up.
+       //      //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)
+         m_supportedProperties({TestProptertyName1, TestProptertyName2}),
+         subscriptionsToSupportedCounter(0),
+         subscriptionsToUnsupportedCounter(0),
+         unsubscriptionsToSupportedCounter(0),
+         unsubscriptionsToUnsupportedCounter(0),
+         propertyChanges(0),
+         supportedPropertyChanges(0)
 {
 
-  DebugOut() << "Testing Core::setSupported... " << endl;
-  testCoreSetSupported();
+       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();
+       gsize dictsize = g_variant_n_children(var);
+       //DebugOut() << var << endl;
+       propmaptwo.fromVariant(var);
 
-  DebugOut() << "Testing MapPropertyType... " << endl;
-  MapPropertyType<BasicPropertyType<Zone::Type>,BasicPropertyType<Door::Status>> propmap("something");
-  MapPropertyType<BasicPropertyType<Zone::Type>,BasicPropertyType<Door::Status>> propmaptwo("something");
-  propmap.append(Zone::RearLeft,Door::Ajar);
-  GVariant *var = propmap.toVariant();
-  gsize dictsize = g_variant_n_children(var);
-  //DebugOut() << var << endl;
-  propmaptwo.fromVariant(var);
+       g_assert(propmaptwo.toString() == propmap.toString());
 
-  g_assert(propmaptwo.toString() == propmap.toString());
+       ///test fromString:
+       propmap.fromString(propmaptwo.toString());
 
-  DebugOut() << "Testing ListPropertyType... " << endl;
-  VehicleProperty::TripMetersType* tfirst = new VehicleProperty::TripMetersType();
-  VehicleProperty::TripMetersType* tsecond = new VehicleProperty::TripMetersType();
-  BasicPropertyType<uint16_t> v1(0);
-  BasicPropertyType<uint16_t> v2(5);
-  BasicPropertyType<uint16_t> v3(10);
-  tfirst->append(&v1);
-  tfirst->append(&v2);
-  tfirst->append(&v3);
-  tsecond->fromVariant(tfirst->toVariant());
+       DebugOut() << "Testing map string equivalency: " << propmaptwo.toString() << "==" << propmap.toString() << endl;
 
-  g_assert (tfirst->toString() == tsecond->toString());
+       g_assert(propmaptwo.toString() == propmap.toString());
 
-  testBooleanToStringFromString();
+       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());
 
-  g_assert (testCoreUpdateSupported());
+       GVariant* testGVSVariant = g_variant_new("i", 9);
+       TEST(GVS<int>::value(testGVSVariant) == 9);
 
-  testSubscription();
+       TEST (tfirst->toString() == tsecond->toString());
 
-  testSetAndGet();
+       testBooleanToStringFromString();
 
-  DebugOut() << "Exiting..." << endl;
-  exit(-1);
+       TEST (testCoreUpdateSupported());
+
+       testSubscription();
+
+       testSetAndGet();
+
+       TEST(testListPlusPlus());
+
+       testJsonHelper();
 }
 
 TestPlugin::~TestPlugin()
@@ -362,10 +427,9 @@ int TestPlugin::supportedOperations()
        return Get | Set;
 }
 
-extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
+extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
 {
-       return new TestPlugin(routingengine, config);
-       
+       new TestPlugin(routingengine, config);
 }
 const string TestPlugin::uuid()
 {
@@ -373,7 +437,7 @@ const string TestPlugin::uuid()
 }
 void TestPlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
 {
-       if(ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+       if(contains(m_supportedProperties, property))
                ++subscriptionsToSupportedCounter;
        else
                ++subscriptionsToUnsupportedCounter;
@@ -382,7 +446,7 @@ void TestPlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
 
 void TestPlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
 {
-       if(ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+       if(contains(m_supportedProperties, property))
                ++unsubscriptionsToSupportedCounter;
        else
                ++unsubscriptionsToUnsupportedCounter;
@@ -391,7 +455,7 @@ void TestPlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property
 
 void TestPlugin::getPropertyAsync(AsyncPropertyReply *reply)
 {
-       
+
 }
 
 AsyncPropertyReply *TestPlugin::setProperty(AsyncSetPropertyRequest request )
@@ -404,9 +468,9 @@ AsyncPropertyReply *TestPlugin::setProperty(AsyncSetPropertyRequest request )
        return reply;
 }
 
-PropertyInfo TestPlugin::getPropertyInfo(VehicleProperty::Property property)
+PropertyInfo TestPlugin::getPropertyInfo(const VehicleProperty::Property &property)
 {
-       if(!ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+       if(!contains(m_supportedProperties, property))
                return PropertyInfo::invalid();
 
        if(property == TestProptertyName1){
@@ -423,7 +487,7 @@ PropertyInfo TestPlugin::getPropertyInfo(VehicleProperty::Property property)
 void TestPlugin::propertyChanged(AbstractPropertyType *value)
 {
        ++propertyChanges;
-       if(value && ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(value->name))
+       if(value && contains(m_supportedProperties, value->name))
                supportedPropertyChanges++;
 
 }