From 121ac1676034f10454190638c2590014dff3941c Mon Sep 17 00:00:00 2001
From: Romain Forlot <romain.forlot@iot.bzh>
Date: Thu, 26 Jul 2018 11:21:54 +0200
Subject: Some fixes

Fix: No events defined in the mock apis
Fix: new_api function has changed in binder

Using the dynapi new_api function returned 0 in case of success
and -1 on failure. New function since v3 returned NULL in case of
failure or the api struct in case of success.

Fix: wrong install_prefix variable used in CMake config file
Fix: wrong variable used

Typo after a copy/paste from event group handling.

- Wrong use of table.insert
- Wrong variable name used
- only pass the data table, which is embed in another data
table...
- Fix aftereach and beforeeach function's call

Change-Id: I9523ae50c170a3cdb9e5c3bc2b9e923c30f6ba39
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
---
 conf.d/cmake/config.cmake       |  4 +--
 conf.d/controller/lua.d/aft.lua | 26 +++++++++---------
 src/mapis.c                     | 60 ++++++++++++++++++++---------------------
 3 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake
index 38e2ce7..c68cfcb 100644
--- a/conf.d/cmake/config.cmake
+++ b/conf.d/cmake/config.cmake
@@ -126,8 +126,8 @@ list(APPEND link_libraries afb-helpers)
 # CACHE STRING "Compilation flags for RELEASE build type.")
 
 set(CONTROL_SUPPORT_LUA 1)
-add_definitions(-DCONTROL_PLUGIN_PATH="./var:${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib/plugins:${CMAKE_BINARY_DIR}/package/lib/plugins:${CMAKE_BINARY_DIR}/package/var")
-add_definitions(-DCONTROL_CONFIG_PATH="./etc:${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/etc:${CMAKE_BINARY_DIR}/package/etc")
+add_definitions(-DCONTROL_PLUGIN_PATH="./var:${INSTALL_PREFIX}/${PROJECT_NAME}/lib/plugins:${CMAKE_BINARY_DIR}/package/lib/plugins:${CMAKE_BINARY_DIR}/package/var")
+add_definitions(-DCONTROL_CONFIG_PATH="./etc:${INSTALL_PREFIX}/${PROJECT_NAME}/etc:${CMAKE_BINARY_DIR}/package/etc")
 add_definitions(-DCTL_PLUGIN_MAGIC=1286576532)
 add_definitions(-DUSE_API_DYN=1 -DAFB_BINDING_VERSION=3 -DAFB_BINDING_WANT_DYNAPI)
 
diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua
index b229e39..4161183 100644
--- a/conf.d/controller/lua.d/aft.lua
+++ b/conf.d/controller/lua.d/aft.lua
@@ -96,7 +96,7 @@ end
 function _AFT.registerData(dict, eventData)
 	if dict.data and type(dict.data) == 'table' then
 		if _AFT.event_history == true then
-			table.insert(dict.data, eventData, 1)
+			table.insert(dict.data, 1, eventData)
 		else
 			dict.data[1] = eventData
 		end
@@ -111,9 +111,9 @@ function _AFT.triggerEvtCallback(eventName)
 		if _AFT.monitored_events[eventName].data ~= nil then
 			local data_n = table_size(_AFT.monitored_events[eventName].data)
 			if _AFT.event_history == true then
-				_AFT.monitored_events[eventName].cb(v.name, _AFT.monitored_events[eventName].data[data_n], _AFT.monitored_events[eventName].data)
+				_AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n], _AFT.monitored_events[eventName].data)
 			else
-				_AFT.monitored_events[eventName].cb(v.name, _AFT.monitored_events[eventName].data[data_n])
+				_AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n])
 			end
 		end
 	end
@@ -132,7 +132,7 @@ function _AFT.bindingEventHandler(eventObj, uid)
 		eventListeners = eventObj.data.result
 		-- Remove from event to hold the bare event data and be able to assert it
 		eventObj.data.result = nil
-		data = eventObj.data
+		data = eventObj.data.data
 	end
 
 	if type(_AFT.monitored_events[eventName]) == 'table' then
@@ -274,7 +274,7 @@ function _AFT.assertEvtNotReceived(eventName, timeout)
 
 	_AFT.assertIsTrue(count == 0, "Event '".. eventName .."' received but it shouldn't")
 
-	_AFT.triggerEvtCallback(event.name)
+	_AFT.triggerEvtCallback(eventName)
 end
 
 function _AFT.assertEvtReceived(eventName, timeout)
@@ -285,7 +285,7 @@ function _AFT.assertEvtReceived(eventName, timeout)
 
 	_AFT.assertIsTrue(count > 0, "No event '".. eventName .."' received")
 
-	_AFT.triggerEvtCallback(event.name)
+	_AFT.triggerEvtCallback(eventName)
 end
 
 function _AFT.testEvtNotReceived(testName, eventName, timeout, setUp, tearDown)
@@ -371,8 +371,6 @@ function _AFT.testVerbError(testName, api, verb, args, cb, setUp, tearDown)
 end
 
 function _AFT.describe(testName, testFunction, setUp, tearDown)
-	if _AFT.beforeEach then local b = _AFT.beforeEach() end
-	if _AFT.afterEach then local a = _AFT.afterEach() end
 	local aTest = {}
 
 	if type(testFunction) == 'function' then
@@ -382,12 +380,12 @@ function _AFT.describe(testName, testFunction, setUp, tearDown)
 		os.exit(1)
 	end
 	function aTest:setUp()
+		if _AFT.beforeEach then _AFT.beforeEach() end
 		if type(setUp) == 'function' then setUp() end
-		if b then b() end
 	end
 	function aTest:tearDown()
-		if a then a() end
 		if type(tearDown) == 'function' then tearDown() end
+		if _AFT.afterEach then _AFT.afterEach() end
 	end
 
 	table.insert(_AFT.tests_list, {testName, aTest})
@@ -569,10 +567,10 @@ local function call_tests()
 	local failures="Failures : "..tostring(lu.LuaUnit.result.testCount-lu.LuaUnit.result.passedCount)
 
 	local evtHandle = AFB:evtmake(_AFT.context, 'results')
-	if type(evtHandle) == "userdata" then
-		AFB:subscribe(_AFT.context,evtHandle)
-		AFB:evtpush(_AFT.context,evtHandle,{info = success.." "..failures})
-	end
+	--if type(evtHandle) == "userdata" then
+	--	AFB:subscribe(_AFT.context,evtHandle)
+	--	AFB:evtpush(_AFT.context,evtHandle,{info = success.." "..failures})
+	--end
 end
 
 function _launch_test(context, args)
diff --git a/src/mapis.c b/src/mapis.c
index 0c2a4aa..ba524a0 100644
--- a/src/mapis.c
+++ b/src/mapis.c
@@ -45,43 +45,45 @@ static int LoadOneMapi(void *data, AFB_ApiT apiHandle) {
 		return -1;
 	}
 
-	// Add actions to the section to be able to respond to defined events.
-	for(idx = 0; ctrlConfig->sections[idx].key != NULL; ++idx) {
-		if(! strcasecmp(ctrlConfig->sections[idx].key, "events")) {
-			savedActions = ctrlConfig->sections[idx].actions;
-			break;
+	if(mapisHandle->eventsJ) {
+		// Add actions to the section to be able to respond to defined events.
+		for(idx = 0; ctrlConfig->sections[idx].key != NULL; ++idx) {
+			if(! strcasecmp(ctrlConfig->sections[idx].key, "events")) {
+				savedActions = ctrlConfig->sections[idx].actions;
+				break;
+			}
 		}
-	}
-	newActions = ActionConfig(apiHandle, mapisHandle->eventsJ, 0);
+		newActions = ActionConfig(apiHandle, mapisHandle->eventsJ, 0);
 
-	if(savedActions) {
-		while(savedActions[savedCount].uid != NULL)
-			savedCount++;
-	}
+		if(savedActions) {
+			while(savedActions[savedCount].uid != NULL)
+				savedCount++;
+		}
+
+		while(newActions[count].uid != NULL)
+			count++;
 
-	while(newActions[count].uid != NULL)
-		count++;
+		int total = savedCount + count;
+		count = 0;
+		savedCount = 0;
+		CtlActionT * mergedActions = calloc(total + 1, sizeof(CtlActionT));
 
-	int total = savedCount + count;
-	count = 0;
-	savedCount = 0;
-	CtlActionT * mergedActions = calloc(total + 1, sizeof(CtlActionT));
+		if(savedActions) {
+			while(savedActions[count].uid != NULL) {
+				mergedActions[count] = savedActions[count];
+				count++;
+			}
+		}
 
-	if(savedActions) {
-		while(savedActions[count].uid != NULL) {
-			mergedActions[count] = savedActions[count];
+		while(newActions[savedCount].uid != NULL && count <= total) {
+			mergedActions[count] = newActions[savedCount];
 			count++;
+			savedCount++;
 		}
-	}
 
-	while(newActions[savedCount].uid != NULL && count <= total) {
-		mergedActions[count] = newActions[savedCount];
-		count++;
-		savedCount++;
+		ctrlConfig->sections[idx].actions = mergedActions;
 	}
 
-	ctrlConfig->sections[idx].actions = mergedActions;
-
 	// declare an event event manager for this API;
 	afb_api_on_event(apiHandle, CtrlDispatchApiEvent);
 
@@ -112,10 +114,8 @@ static void OneMapiConfig(void *data, json_object *mapiJ) {
 		json_object_object_del(mapiJ, "events");
 		mapisHandle->mapiJ = mapiJ;
 
-		if (afb_api_new_api(mapisHandle->mainApiHandle, uid, info, 1, LoadOneMapi, (void*)mapisHandle)) {
+		if (!afb_api_new_api(mapisHandle->mainApiHandle, uid, info, 1, LoadOneMapi, (void*)mapisHandle))
 			AFB_ApiError(mapisHandle->mainApiHandle, "Error creating new api: %s", uid);
-			return;
-		}
 	}
 }
 
-- 
cgit