1
|
|
|
/* eslint-env mocha */ |
2
|
|
|
/* eslint-disable no-unused-expressions */ |
3
|
|
|
|
4
|
|
|
var Chai = require('chai') |
5
|
|
|
var expect = Chai.expect |
6
|
|
|
|
7
|
|
|
var Validator = require('../../../../lib/Schema/Validator').Validator |
8
|
|
|
var Severity = Validator.Severity |
9
|
|
|
var TriggerType = require('../../../../lib/Schema/TriggerType').TriggerType |
10
|
|
|
|
11
|
|
|
var worstViolation = function (violations) { |
12
|
|
|
return violations.reduce(function (carrier, violation) { |
13
|
|
|
return ((carrier && carrier.weight) || 0) >= violation.weight ? carrier : violation |
14
|
|
|
}, null) |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
describe('Unit', function () { |
18
|
|
|
describe('/Schema', function () { |
19
|
|
|
describe('/Validator.js', function () { |
20
|
|
|
describe('.Validator', function () { |
21
|
|
|
describe('.state()', function () { |
22
|
|
|
it('doesn\'t produce any violations for valid definition', function () { |
23
|
|
|
var input = { |
24
|
|
|
transition: function () {}, |
25
|
|
|
abort: function () {}, |
26
|
|
|
entrypoint: true, |
27
|
|
|
terminal: false, |
28
|
|
|
timeout: null |
29
|
|
|
} |
30
|
|
|
var violations = Validator.state(input) |
31
|
|
|
expect(violations.violations).to.be.empty |
|
|
|
|
32
|
|
|
}) |
33
|
|
|
|
34
|
|
|
it('reports illegal value', function () { |
35
|
|
|
var violations = Validator.state(false) |
36
|
|
|
expect(violations.severity).to.eq(Severity.Fatal) |
37
|
|
|
}) |
38
|
|
|
|
39
|
|
|
it('reports missing $.transition handler', function () { |
40
|
|
|
var violations = Validator.state({}) |
41
|
|
|
violations = violations.violations['$.transition'] |
42
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
43
|
|
|
}) |
44
|
|
|
|
45
|
|
|
it('reports missing $.abort handler', function () { |
46
|
|
|
var violations = Validator.state({}) |
47
|
|
|
violations = violations.violations['$.abort'] |
48
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
49
|
|
|
}) |
50
|
|
|
}) |
51
|
|
|
|
52
|
|
|
describe('.scenario()', function () { |
53
|
|
|
var scenario |
54
|
|
|
|
55
|
|
|
beforeEach(function () { |
56
|
|
|
scenario = { |
57
|
|
|
id: 'callback', |
58
|
|
|
version: '0.1.2', |
59
|
|
|
environment: 'environment', |
60
|
|
|
states: { |
61
|
|
|
entrypoint: { |
62
|
|
|
transition: function () {}, |
63
|
|
|
abort: function () {}, |
64
|
|
|
entrypoint: true, |
65
|
|
|
terminal: false |
66
|
|
|
}, |
67
|
|
|
terminal: { |
68
|
|
|
transition: function () {}, |
69
|
|
|
abort: function () {}, |
70
|
|
|
entrypoint: false, |
71
|
|
|
terminal: true |
72
|
|
|
} |
73
|
|
|
}, |
74
|
|
|
onError: function () {}, |
75
|
|
|
onTermination: function () {}, |
76
|
|
|
trigger: TriggerType.Http, |
77
|
|
|
deserializer: function () {} |
78
|
|
|
} |
79
|
|
|
}) |
80
|
|
|
|
81
|
|
|
it('doesn\'t report anything on reference scenario', function () { |
82
|
|
|
var violations = Validator.scenario(scenario) |
83
|
|
|
expect(violations.severity).to.eq(Severity.None) |
84
|
|
|
expect(violations.violations).to.be.empty |
|
|
|
|
85
|
|
|
}) |
86
|
|
|
|
87
|
|
|
it('reports illegal input', function () { |
88
|
|
|
var violations = Validator.scenario(false) |
89
|
|
|
expect(violations.severity).to.eq(Severity.Fatal) |
90
|
|
|
}) |
91
|
|
|
|
92
|
|
|
it('reports missing $.onTermination handler', function () { |
93
|
|
|
delete scenario.onTermination |
94
|
|
|
var violations = Validator.scenario(scenario) |
95
|
|
|
violations = violations.violations['$.onTermination'] |
96
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
97
|
|
|
}) |
98
|
|
|
|
99
|
|
|
it('reports missing $.onError handler', function () { |
100
|
|
|
delete scenario.onError |
101
|
|
|
var violations = Validator.scenario(scenario) |
102
|
|
|
violations = violations.violations['$.onError'] |
103
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
104
|
|
|
}) |
105
|
|
|
|
106
|
|
|
it('reports missing $.trigger', function () { |
107
|
|
|
delete scenario.trigger |
108
|
|
|
var violations = Validator.scenario(scenario) |
109
|
|
|
violations = violations.violations['$.trigger'] |
110
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
111
|
|
|
var violation = worstViolation(violations) |
112
|
|
|
expect(violation).to.have.property('severity').eq(Severity.Fatal) |
113
|
|
|
}) |
114
|
|
|
|
115
|
|
|
it('reports invalid $.trigger', function () { |
116
|
|
|
scenario.trigger = 'gRPC' |
117
|
|
|
var violations = Validator.scenario(scenario) |
118
|
|
|
violations = violations.violations['$.trigger'] |
119
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
120
|
|
|
var violation = worstViolation(violations) |
121
|
|
|
expect(violation).to.have.property('severity').eq(Severity.Fatal) |
122
|
|
|
}) |
123
|
|
|
|
124
|
|
|
it('reports missing $.deserializer', function () { |
125
|
|
|
delete scenario.deserializer |
126
|
|
|
var violations = Validator.scenario(scenario) |
127
|
|
|
violations = violations.violations['$.deserializer'] |
128
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
129
|
|
|
var violation = worstViolation(violations) |
130
|
|
|
expect(violation).to.have.property('severity').eq(Severity.Minor) |
131
|
|
|
}) |
132
|
|
|
|
133
|
|
|
it('reports invalid $.deserializer', function () { |
134
|
|
|
scenario.deserializer = 200 |
135
|
|
|
var violations = Validator.scenario(scenario) |
136
|
|
|
violations = violations.violations['$.deserializer'] |
137
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
138
|
|
|
var violation = worstViolation(violations) |
139
|
|
|
expect(violation).to.have.property('severity').eq(Severity.Fatal) |
140
|
|
|
}) |
141
|
|
|
|
142
|
|
|
it('reports illegal states input', function () { |
143
|
|
|
scenario.states = false |
144
|
|
|
var violations = Validator.scenario(scenario).violations['$.states'] |
145
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
146
|
|
|
var violation = worstViolation(violations) |
147
|
|
|
expect(violation).to.have.property('severity').eq(Severity.Fatal) |
148
|
|
|
}) |
149
|
|
|
|
150
|
|
|
it('reports multiple entrypoint states', function () { |
151
|
|
|
scenario.states.terminal.entrypoint = true |
152
|
|
|
var violations = Validator.scenario(scenario).violations['$.states'] |
153
|
|
|
expect(violations).to.have.lengthOf(1) |
154
|
|
|
expect(violations[0]).to.have.property('severity').eq(Severity.Fatal) |
155
|
|
|
}) |
156
|
|
|
|
157
|
|
|
it('reports zero entrypoint states', function () { |
158
|
|
|
scenario.states.entrypoint.entrypoint = false |
159
|
|
|
var violations = Validator.scenario(scenario).violations['$.states'] |
160
|
|
|
expect(violations).to.have.lengthOf(1) |
161
|
|
|
expect(violations[0]).to.have.property('severity').eq(Severity.Fatal) |
162
|
|
|
}) |
163
|
|
|
|
164
|
|
|
it('reports missing terminal states', function () { |
165
|
|
|
scenario.states.terminal.terminal = false |
166
|
|
|
var violations = Validator.scenario(scenario).violations['$.states'] |
167
|
|
|
expect(violations).to.have.lengthOf(1) |
168
|
|
|
expect(violations[0]).to.have.property('severity').eq(Severity.Fatal) |
169
|
|
|
}) |
170
|
|
|
|
171
|
|
|
it('incorporates invalid state violations', function () { |
172
|
|
|
scenario.states.entrypoint = null |
173
|
|
|
var violations = Validator.scenario(scenario) |
174
|
|
|
violations = violations.violations['$.states.entrypoint'] |
175
|
|
|
expect(violations).not.to.be.empty |
|
|
|
|
176
|
|
|
}) |
177
|
|
|
}) |
178
|
|
|
}) |
179
|
|
|
}) |
180
|
|
|
}) |
181
|
|
|
}) |
182
|
|
|
|