Class: Crucible::Tests::BaseTestScript
- Inherits:
-
BaseTest
- Object
- BaseTest
- Crucible::Tests::BaseTestScript
show all
- Defined in:
- lib/tests/testscripts/base_testscript.rb
Constant Summary
collapse
- FORMAT_MAP =
{
'json' => FHIR::Formats::ResourceFormat::RESOURCE_JSON,
'xml' => FHIR::Formats::ResourceFormat::RESOURCE_XML
}
- CODE_MAP =
{
'okay' => 200,
'created' => 201,
'noContent' => 204,
'notModified' => 304,
'bad' => 400,
'forbidden' => 403,
'notFound' => 404,
'methodNotAllowed' => 405,
'conflict' => 409,
'gone' => 410,
'preconditionFailed' => 412,
'unprocessable' => 422
}
- OPERATOR_MAP =
{
'equals' => :equals,
'notEquals' => :notEquals,
'in' => :in,
'notIn' => :notIn,
'greaterThan' => :greaterThan,
'lessThan' => :lessThan,
'empty' => :empty,
'notEmpty' => :notEmpty,
'contains' => :contains,
'notContains' => :notContains,
}
Constants inherited
from BaseTest
Crucible::Tests::BaseTest::BASE_SPEC_LINK, Crucible::Tests::BaseTest::JSON_FIELDS, Crucible::Tests::BaseTest::METADATA_FIELDS, Crucible::Tests::BaseTest::REST_SPEC_LINK, Crucible::Tests::BaseTest::STATUS
Instance Attribute Summary
Attributes inherited from BaseTest
#category, #tags, #tests_subset
Instance Method Summary
collapse
Methods inherited from BaseTest
#details, #execute, #execute_test_method, #execute_test_methods, #multiserver, #requires_authorization, #warning
Methods included from Assertions
#assert, #assert_bundle_entry_count, #assert_bundle_response, #assert_bundle_transactions_okay, #assert_equal, #assert_etag_present, #assert_last_modified_present, #assert_minimum, #assert_navigation_links, #assert_operator, #assert_resource_content_type, #assert_resource_type, #assert_response_bad, #assert_response_code, #assert_response_created, #assert_response_gone, #assert_response_not_found, #assert_response_ok, #assert_valid_content_location_present, #assert_valid_profile, #assert_valid_resource_content_type_present, #assertion_negated, #skip
Constructor Details
#initialize(testscript, client, client2 = nil) ⇒ BaseTestScript
Returns a new instance of BaseTestScript.
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/tests/testscripts/base_testscript.rb', line 38
def initialize(testscript, client, client2=nil)
super(client, client2)
@category = {id: 'testscript', title: 'TestScript'}
@id_map = {}
@response_map = {}
@warnings = []
@autocreate = []
@autodelete = []
@testscript = testscript
@preprocessed_vars = {}
define_tests
load_fixtures
end
|
Instance Method Details
#author ⇒ Object
52
53
54
|
# File 'lib/tests/testscripts/base_testscript.rb', line 52
def author
@testscript.name
end
|
#call_assertion(method, warned, *params) ⇒ Object
340
341
342
343
344
345
346
|
# File 'lib/tests/testscripts/base_testscript.rb', line 340
def call_assertion(method, warned, *params)
if warned
warning { self.method(method).call(*params) }
else
self.method(method).call(*params)
end
end
|
97
98
99
100
101
102
103
|
# File 'lib/tests/testscripts/base_testscript.rb', line 97
def collect_metadata(methods_only=false)
@metadata_only = true
result = execute
result = result.values.first if methods_only
@metadata_only = false
result
end
|
#debug_prefix ⇒ Object
72
73
74
|
# File 'lib/tests/testscripts/base_testscript.rb', line 72
def debug_prefix
"[TESTSCRIPT]:\t"
end
|
#define_tests ⇒ Object
80
81
82
83
84
85
|
# File 'lib/tests/testscripts/base_testscript.rb', line 80
def define_tests
@testscript.test.each do |test|
test_method = "#{test.xmlId} #{test.name} test".downcase.tr(' ', '_').to_sym
define_singleton_method test_method, -> { process_test(test) }
end
end
|
#description ⇒ Object
56
57
58
|
# File 'lib/tests/testscripts/base_testscript.rb', line 56
def description
@testscript.description
end
|
#execute_operation(operation) ⇒ Object
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
|
# File 'lib/tests/testscripts/base_testscript.rb', line 168
def execute_operation(operation)
return if @client.nil?
= Hash[operation..all.map{|u| [u.field, u.value]}] format = FHIR::Formats::ResourceFormat::RESOURCE_XML
format = FORMAT_MAP[operation.contentType] unless operation.contentType.nil?
format = FORMAT_MAP[operation.accept] unless operation.accept.nil?
operationCode = 'empty'
operationCode = operation.fhirType.code unless operation.fhirType.nil?
case operationCode
when 'read'
if !operation.targetId.nil?
@last_response = @client.read @fixtures[operation.targetId].class, @id_map[operation.targetId]
else
resource_type = replace_variables(operation.resource)
resource_id = replace_variables(operation.params)
@last_response = @client.read "FHIR::#{resource_type}".constantize, id_from_path(resource_id)
end
when 'vread'
raise 'vread not implemented'
when 'search'
if operation.url.nil?
params = (operation)
@last_response = @client.search "FHIR::#{operation.resource}".constantize, {search: {parameters: params}}, format
else
url = replace_variables(operation.url)
@last_response = @client.search "FHIR::#{operation.resource}".constantize, url: url end
when 'history'
target_id = @id_map[operation.targetId]
fixture = @fixtures[operation.targetId]
@last_response = @client.resource_instance_history(fixture.class,target_id)
when 'create'
@last_response = @client.base_create(@fixtures[operation.sourceId], , format)
@id_map[operation.sourceId] = @last_response.id
when 'update'
target_id = nil
if !operation.targetId.nil?
target_id = @id_map[operation.targetId]
elsif !operation.params.nil?
target_id = id_from_path(replace_variables(operation.params))
end
raise "No target specified for update" if target_id.nil?
fixture = @fixtures[operation.sourceId]
fixture.xmlId = replace_variables(target_id) if fixture.xmlId.nil?
@last_response = @client.update fixture, replace_variables(target_id), format
when 'transaction'
raise 'transaction not implemented'
when 'conformance'
raise 'conformance not implemented'
when 'delete'
if operation.targetId.nil?
params = replace_variables(operation.params)
@last_response = @client.destroy "FHIR::#{operation.resource}".constantize, nil, params: params
else
@last_response = @client.destroy @fixtures[operation.targetId].class, @id_map[operation.targetId]
@id_map.delete(operation.targetId)
end
when '$expand'
raise '$expand not supported'
when '$validate'
raise '$validate not supported'
when '$validate-code'
raise 'could not find params for $validate-code' unless operation.params
params = nil
raise 'could not find any parameters for $validate-code' unless params
raise 'could not find system for $validate-code' unless params['system']
raise 'could not find code for $validate-code' unless params['code']
options = {
:operation => {
:method => 'GET',
:parameters => {
'code' => { type: 'Code', value: params['code'] },
'identifier' => { type: 'Uri', value: params['system'] }
}
}
}
@last_response = @client.value_set_code_validation(options)
when 'empty'
if !operation.params.nil? && !operation.resource.nil?
resource = "FHIR::#{operation.resource}".constantize
@last_response = @client.read resource, nil, FORMAT_MAP[operation.accept], nil, params: replace_variables(operation.params)
end
else
raise "Undefined operation for #{@testscript.name}-#{title}: #{operation.fhirType}"
end
handle_response(operation)
end
|
382
383
384
385
386
387
388
389
390
391
392
|
# File 'lib/tests/testscripts/base_testscript.rb', line 382
def (operation)
parameters = {}
return parameters if operation.params.nil?
params = operation.params
params = operation.params[1..-1] if operation.params.length > 0 && operation.params[0] == "?"
params.split("&").each do |param|
key, value = param.split("=")
parameters[key.to_sym] = replace_variables(value)
end unless operation.params.blank?
parameters
end
|
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
# File 'lib/tests/testscripts/base_testscript.rb', line 402
def (resource_xml, resource_xpath)
resource_xpath = resource_xpath.split("/").map{|s| if s.starts_with?("fhir:") || s.length == 0 then s else "fhir:#{s}" end}.join("/")
resource_xpath = "#{resource_xpath}/@value" unless resource_xpath.ends_with? "/@value"
resource_xpath = "//#{resource_xpath}"
resource_doc = Nokogiri::XML(resource_xml)
resource_doc.root.add_namespace_definition('fhir', 'http://hl7.org/fhir')
resource_element = resource_doc.xpath(resource_xpath)
resource_element.first.try(:value)
end
|
#generate_mock_data(type, length) ⇒ Object
472
473
474
475
476
477
|
# File 'lib/tests/testscripts/base_testscript.rb', line 472
def generate_mock_data(type, length)
choices = []
choices << ('a'..'z') << ('A'..'Z') if type.downcase.include?('c') choices << (0..9) if type.downcase.include?('d') (choices * length).map(&:to_a).flatten.shuffle[0,length].join end
|
#get_reference(reference) ⇒ Object
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
# File 'lib/tests/testscripts/base_testscript.rb', line 423
def get_reference(reference)
resource = nil
if reference.start_with?('#')
contained_id = reference[1..-1]
resource = @testscript.contained.select{|r| r.xmlId == contained_id}.first
else
root = File.expand_path '.', File.dirname(File.absolute_path(__FILE__))
return nil unless File.exist? "#{root}/xml#{reference}"
file = File.open("#{root}/xml#{reference}", "r:UTF-8", &:read)
file.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
file = preprocess(file) if file.include?('${')
if reference.split('.').last == "json"
resourceType = JSON.parse(file)["resourceType"]
resource = "FHIR::#{resourceType}".constantize.from_fhir_json(file)
else
resourceType = Nokogiri::XML(file).children.find{|x| x.class == Nokogiri::XML::Element}.name
fhirType = "FHIR::#{resourceType}".constantize
if fhirType.respond_to? :from_xml
resource = fhirType.from_xml(file)
else
puts "Unable to load reference: Method from_xml undefined on FHIR::#{resourceType}"
end
end
end
resource
end
|
#handle_assertion(assertion) ⇒ Object
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/tests/testscripts/base_testscript.rb', line 273
def handle_assertion(assertion)
operator = :equals
operator = OPERATOR_MAP[assertion.operator] unless assertion.operator.nil?
warningOnly = false
warningOnly = assertion.warningOnly unless assertion.warningOnly.nil?
case
when !assertion.contentType.nil?
call_assertion(:assert_resource_content_type, warningOnly, @last_response, assertion.contentType)
when !assertion..nil?
call_assertion(:assert_operator, warningOnly, operator, replace_variables(assertion.value), @last_response.response[:headers][assertion..downcase], "Header field #{assertion.}")
when !assertion.minimumId.nil?
call_assertion(:assert_minimum, warningOnly, @last_response, @fixtures[assertion.minimumId])
when !assertion.navigationLinks.nil?
call_assertion(:assert_navigation_links, warningOnly, @last_response.resource)
when !assertion.path.nil?
actual_value = nil
resource_xml = nil
if assertion.sourceId.nil?
resource_xml = @last_response.try(:resource).try(:to_xml) || @last_response.body
else
resource_xml = @fixtures[assertion.sourceId].try(:to_xml)
resource_xml = @response_map[assertion.sourceId].try(:resource).try(:to_xml) || @response_map[assertion.soureId].body if resource_xml.nil?
end
actual_value = (resource_xml, assertion.path)
expected_value = replace_variables(assertion.value)
unless assertion.compareToSourceId.nil?
resource_xml = @fixtures[assertion.compareToSourceId].try(:to_xml)
resource_xml = @response_map[assertion.compareToSourceId].try(:resource).try(:to_xml) || @response_map[assertion.compareToSourceId].body if resource_xml.nil?
expected_value = (resource_xml, assertion.path)
end
call_assertion(:assert_operator, warningOnly, operator, expected_value, actual_value)
when !assertion.resource.nil?
call_assertion(:assert_resource_type, warningOnly, @last_response, "FHIR::#{assertion.resource}".constantize)
when !assertion.responseCode.nil?
call_assertion(:assert_operator, warningOnly, operator, assertion.responseCode, @last_response.response[:code], "Response Code #{assertion.responseCode}")
when !assertion.response.nil?
call_assertion(:assert_response_code, warningOnly, @last_response, CODE_MAP[assertion.response])
when !assertion.validateProfileId.nil?
profile_uri = @testscript.profile.first{|p| p.xmlId = assertion.validateProfileId}.reference
reply = @client.validate(@last_response.resource,{profile_uri: profile_uri})
call_assertion(:assert_valid_profile, warningOnly, reply.response, @last_response.resource.class)
else
raise "Unknown Assertion"
end
end
|
#handle_response(operation) ⇒ Object
394
395
396
397
398
399
400
|
# File 'lib/tests/testscripts/base_testscript.rb', line 394
def handle_response(operation)
if !operation.responseId.blank? && operation.fhirType.code != 'delete'
log "Overwriting response #{operation.responseId}..." if @response_map.keys.include?(operation.responseId)
log "Storing response #{operation.responseId}..."
@response_map[operation.responseId] = @last_response
end
end
|
#id ⇒ Object
60
61
62
|
# File 'lib/tests/testscripts/base_testscript.rb', line 60
def id
@testscript.xmlId
end
|
#id_from_path(path) ⇒ Object
419
420
421
|
# File 'lib/tests/testscripts/base_testscript.rb', line 419
def id_from_path(path)
path[1..-1]
end
|
#load_fixtures ⇒ Object
87
88
89
90
91
92
93
94
95
|
# File 'lib/tests/testscripts/base_testscript.rb', line 87
def load_fixtures
@fixtures = {}
@testscript.fixture.each do |fixture|
@fixtures[fixture.xmlId] = get_reference(fixture.resource.reference)
@fixtures[fixture.xmlId].xmlId = nil unless @fixtures[fixture.xmlId].nil? @autocreate << fixture.xmlId if fixture.autocreate
@autodelete << fixture.xmlId if fixture.autodelete
end
end
|
#log(message) ⇒ Object
76
77
78
|
# File 'lib/tests/testscripts/base_testscript.rb', line 76
def log(message)
puts "#{debug_prefix}#{message}"
end
|
163
164
165
166
|
# File 'lib/tests/testscripts/base_testscript.rb', line 163
def perform_action(action)
execute_operation action.operation unless action.operation.nil?
handle_assertion action.assert unless action.assert.nil?
end
|
#preprocess(input) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/tests/testscripts/base_testscript.rb', line 452
def preprocess(input)
output = input;
input.scan(/\${(\w+)}/).each do |match|
if @preprocessed_vars.key?(match[0])
output.sub!("${#{match[0]}}", @preprocessed_vars[match[0]])
else
code_matches = /^([a-zA-Z]+)(\d+)$/.match(match[0])
continue unless code_matches.size == 3
mock_data = generate_mock_data(code_matches[1], code_matches[2].to_i)
output.sub!("${#{match[0]}}", mock_data)
@preprocessed_vars[match[0]] = mock_data
end
end
output
end
|
#process_test(test) ⇒ Object
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
|
# File 'lib/tests/testscripts/base_testscript.rb', line 105
def process_test(test)
result = TestResult.new("TS_#{test.xmlId}", test.name, STATUS[:pass], '','')
@last_response = nil @warnings = [] begin
test.action.each do |action|
perform_action action
end unless @setup_failed || @metadata_only
rescue AssertionException => e
result.update(STATUS[:fail], e.message, e.data)
rescue => e
result.update(STATUS[:error], "Fatal Error: #{e.message}", e.backtrace.join("\n"))
end
result.update(STATUS[:skip], "Skipped because setup failed.", "-") if @setup_failed
result.warnings = @warnings unless @warnings.empty?
result.requires = []
result.validates = []
unless test.metadata.nil?
test.metadata.capability.each do |capability|
conf = get_reference(capability.conformance.reference)
conf.rest.each do |rest|
interactions = rest.resource.map{|resource| { resource: resource.fhirType, methods: resource.interaction.map(&:code)}}
result.requires.concat(interactions) if capability.required
result.validates.concat(interactions) if capability.fhirValidated
end
end
result.links = test.metadata.capability.map(&:link).flatten.uniq
end
result
end
|
#replace_variables(input) ⇒ Object
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# File 'lib/tests/testscripts/base_testscript.rb', line 348
def replace_variables(input)
return nil if input.nil?
@testscript.variable.each do |var|
if !var..nil?
variable_source_response = @response_map[var.sourceId]
variable_value = variable_source_response.response[:headers][var.]
input.gsub!("${" + var.name + "}", variable_value)
elsif !var.path.nil?
resource_xml = nil
variable_source_response = @response_map[var.sourceId]
unless variable_source_response.nil?
resource_xml = variable_source_response.try(:resource).try(:to_xml) || variable_source_response.body
else
resource_xml = @fixtures[var.sourceId].to_xml
end
= (resource_xml, var.path)
input.gsub!("${" + var.name + "}", ) unless .nil?
end if input.include? "${" + var.name + "}"
end
if input.include? '${'
puts "An unknown variable was unable to be replaced: #{input}!"
warning { assert !input.include?('${'), "An unknown variable was unable to be substituted: #{input}" }
end
input
end
|
#setup ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/tests/testscripts/base_testscript.rb', line 136
def setup
return if @testscript.setup.blank? && @autocreate.empty?
@setup_failed = false
begin
@autocreate.each do |fixture_id|
@last_response = @client.create @fixtures[fixture_id]
@id_map[fixture_id] = @last_response.id
end unless @client.nil?
@testscript.setup.action.each do |action|
perform_action action
end unless @testscript.setup.blank?
rescue AssertionException
@setup_failed = true
end
end
|
#teardown ⇒ Object
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/tests/testscripts/base_testscript.rb', line 152
def teardown
return if @testscript.teardown.blank? && @autodelete.empty?
@testscript.teardown.action.each do |action|
execute_operation action.operation unless action.operation.nil?
end unless @testscript.teardown.blank?
@autodelete.each do |fixture_id|
@last_response = @client.destroy @fixtures[fixture_id].class, @id_map[fixture_id]
@id_map.delete(fixture_id)
end unless @client.nil?
end
|
#tests ⇒ Object
68
69
70
|
# File 'lib/tests/testscripts/base_testscript.rb', line 68
def tests
@testscript.test.map { |test| "#{test.xmlId} #{test.name} test".downcase.tr(' ', '_').to_sym }
end
|
#title ⇒ Object
64
65
66
|
# File 'lib/tests/testscripts/base_testscript.rb', line 64
def title
"TS-#{id}"
end
|