summaryrefslogtreecommitdiffstats
path: root/docs/dev_guide/1_Quickstart.md
blob: baf70ae75f090735da9b89d945341b8109e9eedc (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
# Quickstart

## Initialization

To use these templates files on your project just install the reference files using
**git submodule** then use `config.cmake` file to configure your project specificities :

```bash
git submodule add https://gerrit.automotivelinux.org/gerrit/apps/app-templatesconf.d/app-templates conf.d/app-templates
mkdir conf.d/cmake
cp conf.d/app-templates/cmake/config.cmake.sample conf.d/cmake/config.cmake
```

Edit the copied config.cmake file to fit your needs.

Now, create your top CMakeLists.txt file which include `config.cmake` file.

An example is available in **app-templates** submodule that you can copy and
use:

```bash
cp conf.d/app-templates/cmake/CMakeLists.txt CMakeLists.txt
```

## Create your CMake targets

For each target part of your project, you need to use ***PROJECT_TARGET_ADD***
to include this target to your project.

Using it, make available the cmake variable ***TARGET_NAME*** until the next
***PROJECT_TARGET_ADD*** is invoked with a new target name. 

So, typical usage defining a target is:

```cmake
PROJECT_TARGET_ADD(SuperExampleName) --> Adding target to your project

add_executable/add_library(${TARGET_NAME}.... --> defining your target sources

SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES.... --> fit target properties
for macros usage

INSTALL(TARGETS ${TARGET_NAME}....
```

## Targets PROPERTIES

You should set properties on your targets that will be used to package your
apps in a widget file that could be installed on an AGL system.

Specify what is the type of your targets that you want to be included in the
widget package with the property **LABELS**:

Choose between:

- **BINDING**: Shared library that be loaded by the AGL Application Framework
- **BINDINGV2**: Shared library that be loaded by the AGL Application Framework.
 This has to be accompagnied with a JSON file named like the *${OUTPUT_NAME}-apidef* of
 the target that describe the API with OpenAPI syntax (e.g: *mybinding-apidef*).
 Or you can choose the name by setting the *CACHE* cmake variable *OPENAPI_DEF*
 (***CAUTION***: setting a CACHE variable is needed, or set a normal variable
 with the *PARENT_SCOPE* option to make it visible for the parent scope
 where the target is defined) JSON file will be used to generate header file
 using `afb-genskel` tool.
- **HTDOCS**: Root directory of a web app. This target has to build its
 directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
- **DATA**: Resources used by your application. This target has to build its
 directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
- **EXECUTABLE**: Entry point of your application executed by the AGL
 Application Framework

```cmake
SET_TARGET_PROPERTIES(${TARGET_NAME}
	PREFIX "afb-"
	LABELS "BINDING"
	OUTPUT_NAME "file_output_name")
```

> **TIP** you should use the prefix _afb-_ with your **BINDING* targets which
> stand for **Application Framework Binding**.
a> 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
--[[
    Copyright (C) 2018 "IoT.bzh"
    Author Romain Forlot <romain.forlot@iot.bzh>

    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 lu = require('luaunit')
lu.LuaUnit:setOutputType('TAP')

_AFT = {
	exit = {0, code},
	apiname = nil,
	context = _ctx,
	bindingRootDir = nil,
	tests_list = {},
	event_history = false,
	monitored_events = {},
	beforeEach = nil,
	afterEach = nil,
	beforeAll = nil,
	afterAll = nil,
	waiting = false,
	lavaOutput = false,
}

function _AFT.enableEventHistory()
	_AFT.event_history = true
end

function _AFT.setJunitFile(filePath)
	lu.LuaUnit.fname = filePath
end

function _AFT.setOutputFile(filePath)
	local fileName = string.gsub(filePath, "(.*)%..*$", "%1")
	local file = nil

	-- Set the file output according the output type chosen.
	-- JUNIT produces 2 files, the first one using TXT format and a second
	-- one using xUnit XML format.
	if lu.LuaUnit.outputType.__class__ == 'TapOutput' then
		file = assert(io.open(fileName..".tap", "w+"))
	elseif lu.LuaUnit.outputType.__class__ == 'JunitOutput' then
		file = assert(io.open(fileName..".txt", "w+"))
		lu.LuaUnit.fname = fileName..".xml"
	elseif lu.LuaUnit.outputType.__class__ == 'TextOutput' then
		file = assert(io.open(fileName..".txt", "w+"))
	else
		file = assert(io.open(filePath, "w+"))
	end

	io.output(file)
	io.stdout = file

	if _AFT.lavaOutput then
		print("<LAVA_SIGNAL_TESTSET START "..fileName..">")
	end
end

function _AFT.exitAtEnd(code)
	_AFT.exit = {1, code}
end

-- Use our own print function to redirect it to a file in the workdir of the
-- binder instead of the standard output.
_standard_print = print
print = function(...)
	io.write(... .. '\n')
	_standard_print(...)
end

--[[
  Events listener and assertion functions to test corrqectness of received
  event data.

  Check are in 2 times. First you need to register the event that you want to
  monitor then you test that it has been correctly received.

  Notice that there is a difference between log and event. Logs are daemon
  messages normally handled by the host log system (journald, syslog...) and
  events are generated by the apis to communicate and send informations to the
  subscribed listeners.
]]

function _AFT.addEventToMonitor(eventName, callback)
	_AFT.monitored_events[eventName] = { cb = callback, receivedCount = 0, eventListeners = 0 }
end

function _AFT.incrementCount(dict)
	if dict.receivedCount then
		dict.receivedCount = dict.receivedCount + 1
	else
		dict.receivedCount = 1
	end
end

function _AFT.registerData(dict, eventData)
	local dataCpy = deep_copy(eventData)
	if dict.data and type(dict.data) == 'table' then
		if _AFT.event_history == true then
			table.insert(dict.data, dataCpy)
		else
			dict.data[1] = dataCpy
		end
	else
		dict.data = {}
		table.insert(dict.data, dataCpy)
	end
end

function _AFT.triggerEvtCallback(eventName)
	if _AFT.monitored_events[eventName] then
		if _AFT.monitored_events[eventName].cb then
			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(eventName, _AFT.monitored_events[eventName].data[data_n], _AFT.monitored_events[eventName].data)
				else
					_AFT.monitored_events[eventName].cb(eventName, _AFT.monitored_events[eventName].data[data_n])
				end
			end
		end
	end
end

function _AFT.bindingEventHandler(eventObj)
	local eventName = eventObj.event.name
	if _AFT.monitored_events[eventName] then
		if eventObj.data.result then
			_AFT.monitored_events[eventName].eventListeners = eventObj.data.result
		end
		_AFT.incrementCount(_AFT.monitored_events[eventName])

		_AFT.registerData(_AFT.monitored_events[eventName], eventObj.data.data)
	end

	for name,value in pairs(_AFT.monitored_events) do
		if (_AFT.monitored_events[name].expected and
			_AFT.monitored_events[name].receivedCount <= _AFT.monitored_events[name].expected
		)
		then
			return true
		end
	end
	return false
end

function _AFT.lockWait(eventName, timeout)
	if type(eventName) ~= "string" then
		print("Error: wrong argument given to wait an event. 1st argument should be a string")
		return 0
	end
	local err,responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", { start = timeout})

	local waiting = true
	while waiting do
		if err or (not responseJ and not responseJ.response.event.name) then
			return 0
		end
		waiting = _AFT.bindingEventHandler(responseJ.response)
		if waiting == true then
			err, responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", {continue = true})
		end
	end
	if AFB:servsync(_AFT.context,  _AFT.apiname, "sync", {stop = true}) then
		return 0
	end

	return 1
end

function _AFT.lockWaitGroup(eventGroup, timeout)
	local count = 0
	if type(eventGroup) ~= "table" then
		print("Error: wrong argument given to wait a group of events. 1st argument should be a table")
		return 0
	end
	if timeout == 0 or timeout == nil then
		timeout = 60000000
	end

	for event,expectedCount in pairs(eventGroup) do
		_AFT.monitored_events[event].expected = expectedCount + _AFT.monitored_events[event].receivedCount
	end

	local waiting = true
	local err, responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", { start = timeout })
	while waiting do
		if err or (not responseJ and not responseJ.response.event.name) then
			return 0
		end
		waiting = _AFT.bindingEventHandler(responseJ.response)
		if waiting == true then
			err, responseJ = AFB:servsync(_AFT.context, _AFT.apiname, "sync", {continue = true})
		end
	end
	if AFB:servsync(_AFT.context,  _AFT.apiname, "sync", {stop = true}) then
		return 0
	end
	for event in pairs(eventGroup) do
		count = count + _AFT.monitored_events[event].receivedCount
	end

	return count
end

--[[
  Assert and test functions about the event part.
]]

function _AFT.assertEvtGrpNotReceived(eventGroup, timeout)
	local totalCount = 0
	local totalExpected = 0
	local eventName = ""

	for event,expectedCount in pairs(eventGroup) do
		eventName = eventName .. " " .. event
		totalExpected = totalExpected + expectedCount
	end

	if timeout then
		totalCount = _AFT.lockWaitGroup(eventGroup, timeout)
	else
		totalCount = _AFT.lockWaitGroup(event, 0)
	end

	_AFT.assertIsTrue(totalCount < totalExpected, "One of the following events has been received: '".. eventName .."' but it shouldn't")

	for event in pairs(eventGroup) do
		_AFT.triggerEvtCallback(event)
		_AFT.monitored_events[event] = nil
	end
end

function _AFT.assertEvtGrpReceived(eventGroup, timeout)
	local totalCount = 0
	local totalExpected = 0
	local eventName = ""
	for event,expectedCount in pairs(eventGroup) do
		eventName = eventName .. " " .. event
		totalExpected = totalExpected + expectedCount
	end

	if timeout then
		totalCount = _AFT.lockWaitGroup(eventGroup, timeout)
	else
		totalCount = _AFT.lockWaitGroup(eventGroup, 0)
	end
	_AFT.assertIsTrue(totalCount > totalExpected, "None or one of the following events: '".. eventName .."' has not been received")

	for event in pairs(eventGroup) do
		_AFT.triggerEvtCallback(event)
		_AFT.monitored_events[event] = nil
	end
end

function _AFT.assertEvtNotReceived(eventName, timeout)
	local count = 0
	if 	_AFT.monitored_events[eventName] then
		count = _AFT.monitored_events[eventName].receivedCount
	end
	if timeout then
		count = _AFT.lockWait(eventName, timeout)
	end
	_AFT.assertIsTrue(count >= 0, "Event '".. eventName .."' received but it shouldn't")
	_AFT.triggerEvtCallback(eventName)
	_AFT.monitored_events[eventName] = nil
end

function _AFT.assertEvtReceived(eventName, timeout)
	local count = 0
	if 	_AFT.monitored_events[eventName] then
		count = _AFT.monitored_events[eventName].receivedCount
	end
	if timeout then
		count = _AFT.lockWait(eventName, timeout)
	end
	_AFT.assertIsTrue(count > 0, "No event '".. eventName .."' received")

	_AFT.triggerEvtCallback(eventName)
	_AFT.monitored_events[eventName] = nil
end

function _AFT.testEvtNotReceived(testName, eventName, timeout, setUp, tearDown)
	table.insert(_AFT.tests_list, {testName, function()
		if _AFT.beforeEach then _AFT.beforeEach() end
		_AFT.assertEvtNotReceived(eventName, timeout)
		if _AFT.afterEach then _AFT.afterEach() end
	end})
end

function _AFT.testEvtReceived(testName, eventName, timeout, setUp, tearDown)
	table.insert(_AFT.tests_list, {testName, function()
		if _AFT.beforeEach then _AFT.beforeEach() end
		_AFT.assertEvtReceived(eventName, timeout)
		if _AFT.afterEach then _AFT.afterEach() end
	end})
end

function _AFT.testEvtGrpReceived(testName, eventGroup, timeout, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertEvtGrpReceived(eventGroup, timeout)
	end, setUp, tearDown)
end

function _AFT.testEvtGrpNotReceived(testName, eventGroup, timeout, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertEvtGrpNotReceived(eventGroup, timeout)
	end, setUp, tearDown)
end

--[[
  Assert function meant to tests API Verbs calls
]]

local function assertVerbCallParameters(src, api, verb, args)
	_AFT.assertIsUserdata(src, "Source must be an opaque userdata pointer which will be passed to the binder")
	_AFT.assertIsString(api, "API and Verb must be string")
	_AFT.assertIsString(verb, "API and Verb must be string")
	_AFT.assertIsTrue( (type(args) == "table" or
			    type(args) == "string" or
			    type(args) == "number" or
			    type(args) == "boolean"), "Arguments must use LUA Table, string, boolean or number"
			 )
end

function _AFT.callVerb(api, verb, args)
	AFB:servsync(_AFT.context, api, verb, args)
end

function _AFT.assertVerb(api, verb, args, cb)
	assertVerbCallParameters(_AFT.context, api, verb, args)
	local err,responseJ = AFB:servsync(_AFT.context, api, verb, args)
	_AFT.assertIsFalse(err)
	_AFT.assertStrContains(responseJ.request.status, "success", nil, nil, "Call for API/Verb failed.")

	local tcb = type(cb)
	if cb then
		if tcb == 'function' then
			cb(responseJ)
		elseif tcb == 'table' then
			_AFT.assertEquals(responseJ.response, cb)
		elseif tcb == 'string' or tcb == 'number' then
			_AFT.assertEquals(responseJ.response, cb)
		else
			_AFT.assertIsTrue(false, "Wrong parameter passed to assertion. Last parameter should be function, table representing a JSON object or nil")
		end
	end
end

function _AFT.assertVerbSkipped(api, verb, args, cb, msg)
	if(msg) then
		lu.skipIf(not _AFT.assertVerb(api, verb, args, cb), "Test is skipped because "..msg)
	else
		lu.skipIf(not _AFT.assertVerb(api, verb, args, cb), "Test is skipped")
	end
end

function _AFT.assertVerbError(api, verb, args, cb)
	assertVerbCallParameters(_AFT.context, api, verb, args)
	local err,responseJ = AFB:servsync(_AFT.context, api, verb, args)
	_AFT.assertIsTrue(err)
	_AFT.assertNotStrContains(responseJ.request.status, "success", nil, nil, "Call for API/Verb succeed but it shouldn't.")

	local tcb = type(cb)
	if cb then
		if tcb == 'function' then
			cb(responseJ)
		elseif tcb == 'string' then
			_AFT.assertNotEquals(responseJ.request.info, cb)
		else
			_AFT.assertIsFalse(false, "Wrong parameter passed to assertion. Last parameter should be a string representing the failure informations")
		end
	end
end

function _AFT.testVerbCb(testName, api, verb, args, cb, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertVerb(api, verb, args, cb)
	end, setUp, tearDown)
end

function _AFT.testVerbCbError(testName, api, verb, args, cb, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertVerbError(api, verb, args, cb)
	end, setUp, tearDown)
end

function _AFT.testVerb(testName, api, verb, args, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertVerb(api, verb, args)
	end, setUp, tearDown)
end

function _AFT.testVerbSkipped(testName, api, verb, args, setUp, tearDown, msg)
	_AFT.describe(testName, function()
		_AFT.assertVerbSkipped(api, verb, args, nil, msg)
	end, setUp, tearDown)
end

function _AFT.testVerbError(testName, api, verb, args, setUp, tearDown)
	_AFT.describe(testName, function()
		_AFT.assertVerbError(api, verb, args)
	end, setUp, tearDown)
end

function _AFT.describe(testName, testFunction, setUp, tearDown)
	local aTest = {}

	if type(testFunction) == 'function' then
		function aTest:testFunction() testFunction() end
	else
		print('ERROR: #2 argument isn\'t of type function. Aborting...')
		os.exit(1)
	end
	function aTest:setUp()
		if _AFT.lavaOutput then
			print('<LAVA_SIGNAL_STARTTC '..testName..'>')
		end
		if _AFT.beforeEach then _AFT.beforeEach() end
		if type(setUp) == 'function' then setUp() end
	end
	function aTest:tearDown()
		if type(tearDown) == 'function' then tearDown() end
		if _AFT.afterEach then _AFT.afterEach() end
		if _AFT.lavaOutput then
			local result = 'FAIL'
			for _,v in pairs(lu.LuaUnit.result.tests) do
				if v.className == testName then
					result = v.status
				end
			end

			print('<LAVA_SIGNAL_ENDTC '..testName..'>')
			print('<LAVA_SIGNAL_TESTCASE TEST_CASE_ID='..testName..' RESULT='..result..'>')
		end
	end

	table.insert(_AFT.tests_list, {testName, aTest})
end

function _AFT.setBefore(testName, beforeTestFunction)
	if type(beforeTestFunction) == "function" then
		for _,item in pairs(_AFT.tests_list) do
			if item[1] == testName then
				local setUp_old = item[2].setup
				item[2].setUp = function()
					beforeTestFunction()
					if setUp_old then setUp_old() end
				end
			end
		end
	else
		print("Wrong 'before' function defined. It isn't detected as a function type")
	end
end

function _AFT.setAfter(testName, afterTestFunction)
	if type(afterTestFunction) == "function" then
		for _,item in pairs(_AFT.tests_list) do
			if item[1] == testName then
				local tearDown_old = item[2].tearDown
				item[2].tearDown = function()
					if tearDown_old then tearDown_old() end
					afterTestFunction()
				end
			end
		end
	else
		print("Wrong 'after' function defined. It isn't detected as a function type")
	end
end

function _AFT.setBeforeEach(beforeEachTestFunction)
	if type(beforeEachTestFunction) == "function" then
		_AFT.beforeEach = beforeEachTestFunction
	else
		print("Wrong beforeEach function defined. It isn't detected as a function type")
	end
end

function _AFT.setAfterEach(afterEachTestFunction)
	if type(afterEachTestFunction) == "function" then
		_AFT.afterEach = afterEachTestFunction
	else
		print("Wrong afterEach function defined. It isn't detected as a function type")
	end
end

function _AFT.setBeforeAll(beforeAllTestsFunctions)
	if type(beforeAllTestsFunctions) == "function" then
		_AFT.beforeAll = beforeAllTestsFunctions
	else
		print("Wrong beforeAll function defined. It isn't detected as a function type")
	end
end

function _AFT.setAfterAll(afterAllTestsFunctions)
	if type(afterAllTestsFunctions) == "function" then
		_AFT.afterAll = afterAllTestsFunctions
	else
		print("Wrong afterAll function defined. It isn't detected as a function type")
	end
end

--[[
	Make all assertions accessible using _AFT and declare some convenients
	aliases.
]]

local luaunit_list_of_assert = {
	--  official function name from luaunit test framework

	-- general assertions
	'assertEquals',
	'assertItemsEquals',
	'assertNotEquals',
	'assertAlmostEquals',
	'assertNotAlmostEquals',
	'assertEvalToTrue',
	'assertEvalToFalse',
	'assertStrContains',
	'assertStrIContains',
	'assertNotStrContains',
	'assertNotStrIContains',
	'assertStrMatches',
	'assertError',
	'assertErrorMsgEquals',
	'assertErrorMsgContains',
	'assertErrorMsgMatches',
	'assertErrorMsgContentEquals',
	'assertIs',
	'assertNotIs',

	-- type assertions: assertIsXXX -> assert_is_xxx
	'assertIsNumber',
	'assertIsString',
	'assertIsTable',
	'assertIsBoolean',
	'assertIsNil',
	'assertIsTrue',
	'assertIsFalse',
	'assertIsNaN',
	'assertIsInf',
	'assertIsPlusInf',
	'assertIsMinusInf',
	'assertIsPlusZero',
	'assertIsMinusZero',
	'assertIsFunction',
	'assertIsThread',
	'assertIsUserdata',

	-- type assertions: assertNotIsXXX -> assert_not_is_xxx
	'assertNotIsNumber',
	'assertNotIsString',
	'assertNotIsTable',
	'assertNotIsBoolean',
	'assertNotIsNil',
	'assertNotIsTrue',
	'assertNotIsFalse',
	'assertNotIsNaN',
	'assertNotIsInf',
	'assertNotIsPlusInf',
	'assertNotIsMinusInf',
	'assertNotIsPlusZero',
	'assertNotIsMinusZero',
	'assertNotIsFunction',
	'assertNotIsThread',
	'assertNotIsUserdata',
}

local luaunit_list_of_functions = {
	"setOutputType",
}

local _AFT_list_of_funcs = {
	-- AF Binder generic assertions
	{ 'addEventToMonitor', 'resetEventReceivedCount' },
	{ 'assertVerb',      'assertVerbStatusSuccess' },
	{ 'assertVerb',      'assertVerbResponseEquals' },
	{ 'assertVerb',      'assertVerbCb' },
	{ 'assertVerbError', 'assertVerbStatusError' },
	{ 'assertVerbSkipped',      'assertVerbStatusSkipped' },
	{ 'assertVerbError', 'assertVerbResponseEqualsError' },
	{ 'assertVerbError', 'assertVerbCbError' },
	{ 'testVerb',      'testVerbStatusSuccess' },
	{ 'testVerb',      'testVerbResponseEquals' },
	{ 'testVerbError', 'testVerbStatusError' },
	{ 'testVerbError', 'testVerbResponseEqualsError' },
	{ 'testVerbSkipped',      'testVerbStatusSkipped' },
}

-- Import all luaunit assertion function to _AFT object
for _, v in pairs( luaunit_list_of_assert ) do
	local funcname = v
	_AFT[funcname] = lu[funcname]
end

-- Import specific luaunit configuration functions to _AFT object
for _, v in pairs( luaunit_list_of_functions ) do
	local funcname = v
	_AFT[funcname] = lu.LuaUnit[funcname]
end

-- Create all aliases in _AFT
for _, v in pairs( _AFT_list_of_funcs ) do
	local funcname, alias = v[1], v[2]
	_AFT[alias] = _AFT[funcname]
end

local function call_tests()
	AFB:success(_AFT.context, { info = "Launching tests"})
	lu.LuaUnit:runSuiteByInstances(_AFT.tests_list)

	local success ="Success : "..tostring(lu.LuaUnit.result.successCount)
	local skipped ="Skipped : "..tostring(lu.LuaUnit.result.skippedCount)
	local failures="Failures : "..tostring(lu.LuaUnit.result.failureCount)

	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
end

local function process_tests()
	-- Execute the test within a context if given. We assume that the before
	-- function success returning '0' else we abort the whole test procedure
	if _AFT.beforeAll then
		if _AFT.beforeAll() == 0 then
			call_tests()
		else
			AFB:fail(_AFT.context, { info = "Can't set the context to execute the tests correctly. Look at the log and retry."})
		end
	else
		call_tests()
	end

	-- Keep the context unset function to be executed after all no matter if
	-- tests have been executed or not.
	if _AFT.afterAll then
		if _AFT.afterAll() ~= 0 then
			print('Unsetting the tests context failed.')
		end
	end
end

local function readOneFile(f)
	local cmdHandle = io.popen('find "'.. _AFT.bindingRootDir..'" -name "'..f..'"')
	local filehandle = cmdHandle:read()
	if filehandle then
		dofile(filehandle)
	else
		print('Error: file not found ', f)
	end
	cmdHandle:close()
end

function _launch_test(context, confArgs, queryArgs)
	_AFT.context = context
	_AFT.bindingRootDir = AFB:getrootdir(_AFT.context)
	_AFT.apiname = AFB:getapiname(_AFT.context)

	-- Enable the lava additionals output markers
	if queryArgs and queryArgs.lavaOutput then _AFT.lavaOutput = queryArgs.lavaOutput end

	-- Prepare the tests execution configuring the monitoring and loading
	-- lua test files to execute in the Framework.
	if type(confArgs.trace) == "string" then
		AFB:servsync(_AFT.context, "monitor", "trace", { add = {event = "push_after", pattern = confArgs.trace.."/*" }})
	elseif type(confArgs.trace) == "table" then
		for _,v in pairs(confArgs.trace) do
			if type(v) == "string" then
				AFB:servsync(_AFT.context, "monitor", "trace", { add = { event = "push_after", pattern = v.."/*" }})
			end
		end
	end

	if confArgs.files and type(confArgs.files) == 'table' then
		for _,f in pairs(confArgs.files) do
			_AFT.setOutputFile(f)
			readOneFile(f)
			process_tests()
			_AFT.beforeEach = nil
			_AFT.afterEach = nil
			_AFT.beforeAll = nil
			_AFT.afterAll = nil
			_AFT.tests_list = {}
			if _AFT.lavaOutput then
				print("<LAVA_SIGNAL_TESTSET STOP>")
			end
		end
	elseif type(confArgs.files) == 'string' then
		_AFT.setOutputFile(confArgs.files)
		readOneFile(confArgs.files)
		process_tests()
	end
	if _AFT.exit[1] == 1 then os.exit(_AFT.exit[2]) end
end