-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestscript_builder.rb
127 lines (101 loc) · 3.02 KB
/
testscript_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
require_relative 'workflow_builder'
class TestScriptBuilder
def scripts
@scripts ||= {}
end
def operation_counter
@operation_counter ||= 0
@operation_counter += 1
end
def assert_counter
@assert_counter ||= 0
@assert_counter += 1
end
def test_counter
@test_counter ||= 0
@test_counter += 1
end
def build(workflow)
script = scripts[workflow]
return script if script
script = build_from_workflow(workflow)
scripts[workflow] = script
script
end
def build_from_workflow(workflow)
@test_counter = nil
@assert_counter = nil
@operation_counter = nil
script = FHIR::TestScript.new
script.variable = create_variables(workflow)
script.fixture = create_fixtures(workflow)
script.setup = build_setup(workflow)
script.test = build_test(workflow)
script.teardown = build_teardown(workflow)
script
end
def create_variables(workflow)
workflow.variables.map do |var|
input = { name: var[0], sourceId: var[2] }
if !var[1].start_with?('$HEADER_')
input.merge!({ expression: var[1] })
else
var[1].slice!("$HEADER_")
input.merge!({ headerField: var[1] })
end
FHIR::TestScript::Variable.new(input)
end
end
def create_fixtures(workflow)
workflow.fixtures.map do |fixture|
reference = FHIR::Reference.new(reference: "fixtures/#{fixture}_reference")
FHIR::TestScript::Fixture.new(id: fixture, resource: reference, autocreate: false, autodelete: false)
end
end
def build_setup(workflow)
return unless !workflow.setup.empty?
actions = workflow.setup.map do |action|
if action.class == WorkflowBuilder::Operation
FHIR::TestScript::Setup::Action.new(operation: build_operation(action))
else
FHIR::TestScript::Setup::Action.new(assert: build_assert(action))
end
end
FHIR::TestScript::Setup.new({action: actions})
end
def build_operation(operation)
FHIR::TestScript::Setup::Action::Operation.new({
label: "Operation_#{operation_counter}",
params: operation.params,
#method: operation.method,
type: FHIR::Coding.new(system: "http://terminology.hl7.org/CodeSystem/testscript-operation-codes", code: operation.method),
sourceId: operation.sourceId,
resource: operation.resource,
responseId: operation.responseId,
encodeRequestUrl: false
})
end
def build_assert(assertion)
FHIR::TestScript::Setup::Action::Assert.new(label: "Assert_#{assert_counter}")
end
def build_test(workflow)
return unless workflow.test
workflow.test.map do |test|
actions = test.map do |action|
if action.class == WorkflowBuilder::Operation
FHIR::TestScript::Test::Action.new(operation: build_operation(action))
else
FHIR::TestScript::Test::Action.new(assert: build_assert(action))
end
end
FHIR::TestScript::Test.new(name: "Test_#{test_counter}", action: actions)
end
end
def build_teardown(workflow)
return unless workflow.teardown
actions = workflow.teardown.map do |action|
FHIR::TestScript::Teardown::Action.new(operation: build_operation(action))
end
FHIR::TestScript::Teardown.new(action: actions)
end
end