Class: TestScriptRunnable

Inherits:
Object
  • Object
show all
Includes:
Assertion, MessageHandler, Operation, TestReportHandler
Defined in:
lib/testscript_engine/testscript_runnable.rb

Constant Summary

Constants included from Assertion

Assertion::ASSERT_TYPES, Assertion::ASSERT_TYPES_MATCHER, Assertion::CODE_MAP

Constants included from Operation

Operation::INTERACTION_NEEDS_PAYLOAD, Operation::OPERATION_NEEDS_RESOURCE_TYPE

Instance Attribute Summary collapse

Attributes included from MessageHandler

#debug_mode, #modify_report

Instance Method Summary collapse

Methods included from TestReportHandler

#builder_template, #cascade_skips, #error, #fail, #finalize_report, #fresh_builder, #fresh_testreport, #pass, #report_builder, #skip, #testreport, #warning

Methods included from Assertion

#compare, #content_type, #determine_assert_type, #determine_expected_value, #direction, #evaluate, #evaluate_path, #expression, #extract_xpath_value, #fail_message, #get_request, #get_resource, #get_response, #header_field, #minimum_id, #navigation_links, #pass_message, #path, #request_header, #request_method, #request_url, #resource, #response, #response_code, #response_header, #validate_profile_id

Methods included from Operation

#build_request, #create_operation, #delete_operation, #execute, #get_format, #get_headers, #get_id, #get_interaction, #get_path, #get_payload, #get_resource_type, #get_vid, #operation_to_interaction

Methods included from MessageHandler

#begin_symbol, #cascade_skips, #decrease_space, #error, #fail, #finish_symbol, #increase_space, #info, #load_scripts, #logger_formatter_with_spacing, #logger_formatters_with_spacing, #make_runnables, #messages, #newline, #outcome_symbol, #pass, #print_action_header, #print_out, #skip, #space, #unit_of_space, #warning

Constructor Details

#initialize(script) ⇒ TestScriptRunnable

Returns a new instance of TestScriptRunnable.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/testscript_engine/testscript_runnable.rb', line 39

def initialize(script)
  raise ArgumentError.new(messages(:bad_script)) unless script.is_a?(FHIR::TestScript)
  raise ArgumentError.new(messages(:invalid_script_input)) unless script.valid?

  @script = script
  load_fixtures
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/testscript_engine/testscript_runnable.rb', line 13

def client
  @client
end

#replyObject

Returns the value of attribute reply.



13
14
15
# File 'lib/testscript_engine/testscript_runnable.rb', line 13

def reply
  @reply
end

#scriptObject

Returns the value of attribute script.



13
14
15
# File 'lib/testscript_engine/testscript_runnable.rb', line 13

def script
  @script
end

Instance Method Details

#abort_test(actions_to_skip) ⇒ Object



157
158
159
# File 'lib/testscript_engine/testscript_runnable.rb', line 157

def abort_test(actions_to_skip)
  cascade_skips(:abort_test, actions_to_skip, 'setup', actions_to_skip.length)
end

#autocreateObject



31
32
33
# File 'lib/testscript_engine/testscript_runnable.rb', line 31

def autocreate
  @autocreate ||= []
end

#autodelete_idsObject



35
36
37
# File 'lib/testscript_engine/testscript_runnable.rb', line 35

def autodelete_ids
  @autodelete_ids ||= []
end

#cascade_skips_with_message(actions, current_action) ⇒ Object



152
153
154
155
# File 'lib/testscript_engine/testscript_runnable.rb', line 152

def cascade_skips_with_message(actions, current_action)
  actions_to_skip = actions.slice(current_action, actions.length)
  cascade_skips(:skip_on_fail, actions_to_skip, actions_to_skip.length)
end

#evaluate_expression(expression, resource) ⇒ Object



252
253
254
255
256
# File 'lib/testscript_engine/testscript_runnable.rb', line 252

def evaluate_expression(expression, resource)
  return unless expression and resource

  return FHIRPath.evaluate(expression, resource.to_hash)
end

#evaluate_variable(var) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/testscript_engine/testscript_runnable.rb', line 241

def evaluate_variable var
  if var.expression
    evaluate_expression(var.expression, find_resource(var.sourceId))
  elsif var.path
    evaluate_path(var.path, find_resource(var.sourceId))
  elsif var.headerField
    headers = response_map[var.sourceId]&.[](:headers)
    headers&.find { |h, v| h == var.headerField || h == var.headerField.downcase }&.last
  end || var.defaultValue
end

#find_resource(id) ⇒ Object



224
225
226
# File 'lib/testscript_engine/testscript_runnable.rb', line 224

def find_resource id
  fixtures[id] || response_map[id]&.[](:body)
end

#fixturesObject



19
20
21
# File 'lib/testscript_engine/testscript_runnable.rb', line 19

def fixtures
  @fixtures ||= {}
end

#get_resource_from_ref(reference) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/testscript_engine/testscript_runnable.rb', line 175

def get_resource_from_ref(reference)
  return warning(:bad_reference) unless reference.is_a?(FHIR::Reference)

  ref = reference.reference
  return warning(:no_reference) unless ref
  return warning(:unsupported_ref, ref) if ref.start_with? 'http'

  if ref.start_with? '#'
    contained = script.contained.find { |r| r.id == ref[1..] }
    return contained || warning(:no_contained_resource, ref)
  end

  begin
    fixture_path = script.url.split('/')[0...-1].join('/') + '/fixtures'
    filepath = File.expand_path(ref, File.absolute_path(fixture_path))
    file = File.open(filepath, 'r:UTF-8', &:read)
    file.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
    resource = FHIR.from_contents(file)
    info(:loaded_static_fixture, ref, script.id)
    return resource
  rescue => e
    warning(:resource_extraction, ref, e.message)
  end
end

#handle_actions(actions, end_on_fail) ⇒ Object



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
# File 'lib/testscript_engine/testscript_runnable.rb', line 103

def handle_actions(actions, end_on_fail)
  @modify_report = true
  if @ended
    abort_test(actions)
    @modify_report = false
    return
  end
  current_action = 0

  begin
    actions.each do |action|
      current_action += 1
      if action.operation
        execute(action.operation)
      elsif action.respond_to?(:assert)
        begin
          evaluate(action.assert)
        rescue AssertionException => ae
          if ae.outcome == :skip
            skip(:eval_assert_result, ae.details)
          elsif ae.outcome == :fail
            next warning(:eval_assert_result, ae.details) if action.assert.warningOnly
            if end_on_fail
              @ended = true
              fail(:eval_assert_result, ae.details)
              cascade_skips_with_message(actions, current_action) unless current_action == actions.length
              @modify_report = false
              return
            else
              fail(:eval_assert_result, ae.details)
            end
          end
        end
      end
    end
  rescue OperationException => oe
    error(oe.details)
    if end_on_fail
      @ended = true
      cascade_skips_with_message(actions, current_action) unless current_action == actions.length
    end
  rescue => e
    error(:uncaught_error, e.message)
    cascade_skips_with_message(actions, current_action) unless current_action == actions.length
  end

  @modify_report = false
end

#id_mapObject



15
16
17
# File 'lib/testscript_engine/testscript_runnable.rb', line 15

def id_map
  @id_map ||= {}
end

#load_fixturesObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/testscript_engine/testscript_runnable.rb', line 161

def load_fixtures
  script.fixture.each do |fixture|
    next warning(:no_static_fixture_id) unless fixture.id
    next warning(:no_static_fixture_resource) unless fixture.resource

    resource = get_resource_from_ref(fixture.resource)
    next warning(:bad_static_fixture_reference) unless resource

    fixtures[fixture.id] = resource
    autocreate << fixture.id if fixture.autocreate
    autodelete_ids << fixture.id if fixture.autodelete
  end
end

#postprocessingObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/testscript_engine/testscript_runnable.rb', line 86

def postprocessing
  return info(:no_postprocess) if autocreate.empty?

  autodelete_ids.each do |fixture_id|
    begin
      client.send(*build_request((delete_operation(fixture_id))))
    rescue => e
      error(:uncaught_error, e.message)
    end
  end

  @ended = nil
  @id_map = {}
  @request_map = {}
  @response_map = {}
end

#preprocessObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/testscript_engine/testscript_runnable.rb', line 61

def preprocess
  return info(:no_preprocess) if autocreate.empty?
  autocreate.each do |fixture|
    begin
      client.send(*build_request((create_operation(fixture))))
    rescue => e
      error(:uncaught_error, e.message)
    end
  end
end

#replace_variables(placeholder) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/testscript_engine/testscript_runnable.rb', line 228

def replace_variables placeholder
  return placeholder unless placeholder&.include? '${'
  replaced = placeholder.clone

  script.variable.each do |var|
    next unless replaced.include? "${#{var.name}}"
    replacement = evaluate_variable(var)
    replaced.gsub!("${#{var.name}}", replacement) if replacement
  end

  return replaced
end

#request_mapObject



23
24
25
# File 'lib/testscript_engine/testscript_runnable.rb', line 23

def request_map
  @request_map ||= {}
end

#response_mapObject



27
28
29
# File 'lib/testscript_engine/testscript_runnable.rb', line 27

def response_map
  @response_map ||= {}
end

#run(client) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/testscript_engine/testscript_runnable.rb', line 47

def run(client)
  @client = client

  fresh_testreport

  preprocess
  setup
  test
  teardown
  postprocessing

  finalize_report
end

#setupObject



72
73
74
75
# File 'lib/testscript_engine/testscript_runnable.rb', line 72

def setup
  return info(:no_setup) unless script.setup
  handle_actions(script.setup.action, true)
end

#storage(op) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/testscript_engine/testscript_runnable.rb', line 200

def storage(op)
  @reply = client.reply
  reply.nil? ? return : client.reply = nil

  request_map[op.requestId] = reply.request if op.requestId
  response_map[op.responseId] = reply.response if op.responseId

  (reply.resource = FHIR.from_contents(reply.response&.[](:body).to_s)) rescue {}
  (reply.response[:body] = reply.resource)
  response_map[op.responseId][:body] = reply.resource if reply.resource and response_map[op.responseId]

  if op.targetId and (reply.request[:method] == :delete) and [200, 201, 204].include?(reply.response[:code])
    id_map.delete(op.targetId) and return
  end

  dynamic_id = reply.resource&.id || begin
    reply.response&.[](:headers)&.[]('location')&.remove(reply.request[:url].to_s)&.split('/')&.[](2)
  end

  id_map[op.responseId] = dynamic_id if op.responseId and dynamic_id
  id_map[op.sourceId] = dynamic_id if op.sourceId and dynamic_id
  return
end

#teardownObject



81
82
83
84
# File 'lib/testscript_engine/testscript_runnable.rb', line 81

def teardown
  return info(:no_teardown) unless script.teardown
  handle_actions(script.teardown.action, false)
end

#testObject



77
78
79
# File 'lib/testscript_engine/testscript_runnable.rb', line 77

def test
  script.test.each { |test| handle_actions(test.action, false) }
end