1
|
|
|
/* global VoxEngine */ |
2
|
|
|
|
3
|
|
|
var Slf4j = require('@ama-team/voxengine-sdk').Logger.Slf4j |
4
|
|
|
var Barricade = require('./Barricade').Barricade |
5
|
|
|
var Run = require('../Execution').Run |
6
|
|
|
var Binder = require('../Adapter/Binder').Binder |
7
|
|
|
var Printer = require('./Printer').Printer |
8
|
|
|
var OperationStatus = require('../Schema').OperationStatus |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Main library class that provides public API. |
12
|
|
|
* |
13
|
|
|
* @param {TFrameworkOptions} [options] |
14
|
|
|
* |
15
|
|
|
* @class |
16
|
|
|
*/ |
17
|
|
|
function Framework (options) { |
18
|
|
|
var self = this |
19
|
|
|
options = options || {} |
20
|
|
|
options.behavior = options.behavior || {terminate: true} |
21
|
|
|
var logger = Slf4j.factory(options.logger, 'ama-team.vsf.framework') |
22
|
|
|
var printer = new Printer(options.logger) |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Runs provided scenario. |
26
|
|
|
* |
27
|
|
|
* @param {TScenarioInput} scenario |
28
|
|
|
* |
29
|
|
|
* @return {TRunResult} |
30
|
|
|
*/ |
31
|
|
|
this.run = function (scenario) { |
32
|
|
|
return self.execute(self.prepare(scenario)) |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Executes prepared Run |
37
|
|
|
* |
38
|
|
|
* @param {Run} run |
39
|
|
|
* |
40
|
|
|
* @return {TRunResult} |
41
|
|
|
*/ |
42
|
|
|
this.execute = function (run) { |
43
|
|
|
printer.scenario(run.getScenario()) |
44
|
|
|
run.initialize() |
45
|
|
|
Binder.bind(run) |
46
|
|
|
return run |
47
|
|
|
.getCompletion() |
48
|
|
|
.then(printer.result, function (reason) { |
49
|
|
|
logger.error('Unexpected error during execution:', reason) |
50
|
|
|
return { |
51
|
|
|
status: OperationStatus.Tripped, |
52
|
|
|
error: reason, |
53
|
|
|
stages: {} |
54
|
|
|
} |
55
|
|
|
}) |
56
|
|
|
.then(function (result) { |
57
|
|
|
if (options.behavior.terminate) { |
58
|
|
|
logger.debug('Shutting down VoxEngine') |
59
|
|
|
VoxEngine.terminate() |
60
|
|
|
} |
61
|
|
|
return result |
62
|
|
|
}) |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates run from scenario input. |
67
|
|
|
* |
68
|
|
|
* @param {TScenarioInput} scenario |
69
|
|
|
* |
70
|
|
|
* @return {Run} |
71
|
|
|
*/ |
72
|
|
|
this.prepare = function (scenario) { |
73
|
|
|
try { |
74
|
|
|
var barricade = new Barricade({logger: options.logger, printer: printer}) |
75
|
|
|
var normalized = barricade.scenario(scenario) |
76
|
|
|
var settings = { |
77
|
|
|
state: scenario.state || {}, |
78
|
|
|
arguments: scenario.arguments || {}, |
79
|
|
|
container: scenario.container || {}, |
80
|
|
|
logger: options.logger |
81
|
|
|
} |
82
|
|
|
return new Run(normalized, normalized.deserializer, settings) |
83
|
|
|
} catch (e) { |
84
|
|
|
logger.error('Failed to create run, most probably due to invalid scenario') |
85
|
|
|
throw e |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
module.exports = { |
91
|
|
|
Framework: Framework |
92
|
|
|
} |
93
|
|
|
|