Class: TestScriptEngine

Inherits:
Object
  • Object
show all
Includes:
MessageHandler
Defined in:
lib/testscript_engine.rb

Instance Attribute Summary collapse

Attributes included from MessageHandler

#debug_mode, #modify_report

Instance Method Summary collapse

Methods included from MessageHandler

#begin_symbol, #cascade_skips, #decrease_space, #error, #fail, #finish_symbol, #increase_space, #info, #load_fixtures, #logger_formatter_with_spacing, #logger_formatters_with_spacing, #messages, #newline, #outcome_symbol, #pass, #postprocessing, #preprocess, #print_action_header, #print_out, #run, #setup, #skip, #space, #teardown, #test, #unit_of_space, #warning

Constructor Details

#initialize(endpoint, testscript_path, testreport_path) ⇒ TestScriptEngine

Returns a new instance of TestScriptEngine.



33
34
35
36
37
38
# File 'lib/testscript_engine.rb', line 33

def initialize(endpoint, testscript_path, testreport_path)
  self.endpoint = endpoint
  self.testscript_path = testscript_path
  self.testreport_path = testreport_path
  self.debug_mode = true
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/testscript_engine.rb', line 10

def endpoint
  @endpoint
end

#testreport_pathObject

Returns the value of attribute testreport_path.



10
11
12
# File 'lib/testscript_engine.rb', line 10

def testreport_path
  @testreport_path
end

#testscript_pathObject

Returns the value of attribute testscript_path.



10
11
12
# File 'lib/testscript_engine.rb', line 10

def testscript_path
  @testscript_path
end

Instance Method Details

#clientObject



24
25
26
27
28
29
30
31
# File 'lib/testscript_engine.rb', line 24

def client
  @client ||= begin
    info(:begin_initialize_client)
    client = FHIR::Client.new(endpoint || 'localhost:3000')
    info(:finish_initialize_client)
    client
  end
end

#execute_runnables(runnable_id = nil) ⇒ Object

TODO: Clean-up, possibly modularize into a pretty_print type method



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/testscript_engine.rb', line 98

def execute_runnables runnable_id = nil
  if runnable_id
    if runnables[runnable_id]
      reports[runnable_id] = runnables[runnable_id].run(client)
    else
      error(:unable_to_locate_runnable, runnable_id)
    end
  else
    runnables.each do |id, runnable|
      reports[id] = runnable.run(client)
    end
  end
end

#load_scriptsObject



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

def load_scripts
if File.file?(testscript_path)
	on_deck = [testscript_path]
elsif File.directory?(testscript_path)
	on_deck = Dir.glob (["#{testscript_path}/**/*.{json}", "#{testscript_path}/**/*.{xml}"])
end
  on_deck.each do |resource|
    next if resource.include? "/fixtures/"

    begin
      script = FHIR.from_contents File.read(resource)
      if valid_testscript? script
        script.url = resource
        if scripts[script.id]
          info(:overwrite_existing_script, script.id)
        else
          info(:loaded_script, script.id)
        end
        scripts[script.id] = script
      else
        info(:invalid_script, resource)
      end
    rescue
      info(:bad_serialized_script, resource)
    end
  end
end

#make_runnables(script = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/testscript_engine.rb', line 79

def make_runnables script = nil
  begin
    if valid_testscript? script
      runnables[script.id] = TestScriptRunnable.new script
      info(:created_runnable, script.id)
    else
      scripts.each do |_, script|
        runnables[script.id] = TestScriptRunnable.new script
        info(:created_runnable, script.id)
      end
    end
  rescue => e
    error(:unable_to_create_runnable, script.id)
  end
end

#new_client(url) ⇒ Object



117
118
119
120
# File 'lib/testscript_engine.rb', line 117

def new_client(url)
	@client = nil
	@endpoint = url
end

#reportsObject



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

def reports
  @reports ||= {}
end

#runnablesObject



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

def runnables
  @runnables ||= {}
end

#scriptsObject



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

def scripts
  @scripts ||= {}
end

#valid_testscript?(script) ⇒ Boolean

TODO: Tie-in stronger validation. Possibly, Inferno validator.

Returns:

  • (Boolean)


41
42
43
# File 'lib/testscript_engine.rb', line 41

def valid_testscript? script
  return (script.is_a? FHIR::TestScript) && script.valid?
end

#verify_runnable(runnable_id) ⇒ Object



112
113
114
115
# File 'lib/testscript_engine.rb', line 112

def verify_runnable(runnable_id)
	return true unless runnables[runnable_id].nil?
	false
end

#write_reports(path = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/testscript_engine.rb', line 124

def write_reports path = nil
  report_directory = path || testreport_path
  FileUtils.mkdir_p report_directory

  reports.each do |_, report|
    report_name = report.name.downcase.split(' ')[1...].join('_')
    report_name = report.name.downcase.split('_')[0...].join('_') if report_name == ""
    File.open("#{report_directory}/#{report_name}.json", 'w') do |f|
      f.write(report.to_json)
    end
  end
end