-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblueprint_builder.rb
276 lines (229 loc) · 8.16 KB
/
blueprint_builder.rb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'SecureRandom'
require_relative 'interactions/interactions'
# This is a duplicate of the workflow_builder in the /testscript_generator
# folder. It is renamed as workflow is not a solid name for this class.
# The search param generator, and each generator added to the generators
# folder, ought to rely on the Blueprint and not the Workflow. However, until
# the conformance generator is added to the generators folder, the
# workflow_builder file has to stay in the /testscript_generator folder to
# support generating the conformance TestScripts.
class BlueprintBuilder
include Interactions
# All the operation types match up with HTTP methods, except for
# 'search-type'
def methods(op_code_type)
return 'search' if op_code_type == 'search-type'
return op_code_type
end
# Essentially a barebones representation of everything that goes into a
# TestScript. Besides the different 'phases' it tracks variables and fixturs.
class Workflow
attr_accessor :variables, :setup, :test, :teardown, :fixtures
def initialize
self.test = []
self.setup = []
self.teardown = []
self.variables = []
self.fixtures = []
end
end
# Instances of this Operation class are stored within the different 'phases'
# of the Blueprint (i.e. teardown, setup). Again, it's a very bare bones
# encapsulation of the same pertinent information you'd expect to find in
# a TestScript.
class Operation
attr_accessor :method, :sourceId, :params, :resource, :responseId
def initialize(input)
self.params = input[:params] || nil
self.method = input[:method] || nil
self.sourceId = input[:sourceId] || nil
self.resource = input[:resource] || nil
self.responseId = input[:responseId] || nil
end
# This allows comparisions between this custom Operation object
def eql?(input)
return false unless input.class == WorkflowBuilder::Operation
return false unless self.params == input.params &&
self.method == input.method &&
self.sourceId == input.sourceId &&
self.resource == input.resource &&
self.responseId == input.responseId
return true
end
end
# TODO: Figure out how to calculate which assertions are relevant based on
# a given operation
class Assertion
def initialize(input)
end
end
# Same information covered in the interactions_base.yml file.
class InteractionMeta
attr_accessor :send, :fetch, :modify, :getId, :dynamicReq, :staticReq, :getResource, :expression
def initialize(input)
self.send = input[:send] || nil # Does it send a body?
self.fetch = input[:fetch] || nil # Does it fetch a resource?
self.getId = input[:getId] || nil # Can you get the id of ~some~ resource with it?
self.modify = input[:modify] || nil # Does it modify the state of ~some~ resource on the server?
self.getResource = input[:getResource] || nil # Can you retrieve ~some~ resource with it? Here, the idea of what's guaranteed if also interesting. What if the search parameters don't find a single conformant resource?
self.staticReq = Array(input[:staticReq]) || [] # What needs to be statically loaded for the interaction? i.e. a Body
self.dynamicReq = Array(input[:dynamicReq]) || [] # What needs to be known from the server? I.e. an existing ID
end
end
def interactions_meta
@interactions_meta ||= {
'create' => InteractionMeta.new({
send: true,
getId: true,
modify: true,
staticReq: [:resource],
expression: '${RESOURCE_TYPE_1}.id'
}),
'read' => InteractionMeta.new({
fetch: true,
dynamicReq: [:id],
getResource: true
}),
'update' => InteractionMeta.new({
send: true,
modify: true,
dynamicReq: [:id, :resource]
}),
'delete' => InteractionMeta.new({
modify: true,
dynamicReq: [:id]
}),
'search-type' => InteractionMeta.new({
fetch: true,
getId: true,
dynamicReq: [:id],
getResource: true,
expression: 'Bundle.entry.resource.id'
})
}
end
def workflow
@workflow ||= Workflow.new
end
def variables
@variables ||= {}
end
def responseIds
@responseIds ||= {}
end
def build(setup: nil, test:, test_params: nil)
fresh_workflow
if setup_required?(test)
setup_methods = Array(setup || determine_setup_method(test))
setup_methods.each { |method| build_setup(method, test_params) }
end
build_test(test, test_params)
workflow
end
def build_setup(setup, test_params)
workflow.setup << Operation.new({
method: methods(setup),
params: determine_parameters(setup),
sourceId: determine_sourceId(setup),
resource: determine_resource(setup),
responseId: determine_responseId(setup)
})
build_variable(setup, test_params)
build_teardown(setup)
end
def fresh_workflow
@workflow = Workflow.new
@static_fixture_counter = 0
end
def setup_required?(test)
!interactions_meta[test].dynamicReq.empty?
end
def determine_setup_method(test)
interactions_meta[test].dynamicReq.each_with_object([]) do |req, array|
method = self.send("get_#{req.to_s}_method")
array.concat(determine_setup_method(method).concat([method])).uniq!
end
end
def get_id_method
@get_id_method ||= interactions_meta.find { |_, v| v.getId }.first
end
def get_resource_method
@get_resource_method ||= interactions_meta.find { |_, v| v.getResource }.first
end
def determine_parameters(method)
"/${#{variables[:id]}}" if interactions_meta[method].dynamicReq.include?(:id)
end
def determine_sourceId(method)
if interactions_meta[method].staticReq.include? :resource
@static_fixture_counter += 1
workflow.fixtures << "${EXAMPLE_RESOURCE_#{@static_fixture_counter}}"
workflow.fixtures.last
elsif interactions_meta[method].dynamicReq.include? :resource
responseIds[:resource]
end
end
def determine_resource(method)
return unless interactions_meta[method].dynamicReq.include? :id
"${RESOURCE_TYPE_#{@static_fixture_counter}}"
end
def determine_responseId(method)
meta = interactions_meta[method]
return unless meta.getResource || meta.getId
responseId = fresh_responseId
responseIds[:id] = responseId if meta.getId
responseIds[:resource] = responseId if meta.getResource
responseId
end
def fresh_responseId
SecureRandom.alphanumeric
end
def variable_required?(method)
!!interactions_meta[method].getId
end
def build_variable(method, param = nil)
return unless variable_required?(method)
variable = fresh_variable
workflow.variables << [variable, param.expression || '${RESOURCE_TYPE_1}.id', responseIds[:id]]
variables[:id] = variable
end
def fresh_variable
SecureRandom.alphanumeric
end
def build_teardown(method)
return unless teardown_required?(method)
teardown = determine_teardown_method(method)
workflow_teardown = Operation.new({
method: methods(teardown),
params: determine_parameters(teardown),
sourceId: determine_sourceId(teardown),
resource: determine_resource(teardown)
})
workflow.teardown << workflow_teardown unless workflow.teardown.any? do |teardown|
teardown.eql? workflow_teardown
end
end
def teardown_required?(method)
!!interactions_meta[method].modify unless method == 'delete'
end
def determine_teardown_method(method)
'delete' if interactions_meta[method].send
end
def build_test(test, test_params)
if test_params
params = "?" + test_params.code
params = params + "=${#{variables[:id]}}" if test_params.expression
else
params = determine_parameters(test)
end
responseId = determine_responseId(test) if interactions_meta[test].modify
workflow.test << [Operation.new({
method: methods(test),
resource: determine_resource(test),
sourceId: determine_sourceId(test),
responseId: responseId,
params: params
})]
build_variable(test) if teardown_required?(test)
build_teardown(test)
end
end