Skip to content

Commit

Permalink
FI-2236: support arbitrary fields on hl7 validator cliContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dehall committed Dec 4, 2023
1 parent 549a7a2 commit 5070e44
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lib/inferno/dsl/fhir_resource_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,30 @@ def url(validator_url = nil)
@url
end

# Set the IGs that the validator will need to load
# Example: ["hl7.fhir.us.core#4.0.0"]
# @param igs [Array<String>]
def igs(validator_igs = nil)
@igs = validator_igs if validator_igs
# @private
def cli_context
@cli_context ||= {
sv: '4.0.1',
igs: [],
doNative: false,
extensions: ['any']
}
end

@igs
# @private
def method_missing(method_name, *args)
# Interpret any other method as setting a field on cliContext.
# Follow the same format as `url` here:
# only set the value if one is provided.
# args will be an empty array if no value is provided.
cli_context[method_name] = args[0] unless args.empty?

cli_context[method_name]
end

# @private
def respond_to_missing?(_method_name, _include_private = false)
true
end

# @private
Expand Down Expand Up @@ -193,13 +210,7 @@ def issue_message(issue, resource)
def wrap_resource_for_hl7_wrapper(resource, profile_url)
wrapped_resource = {
cliContext: {
# TODO: these should be configurable as well
sv: '4.0.1',
# displayWarnings: true, # -display-issues-are-warnings
# txServer: nil, # -tx n/a
igs: @igs || [],
# NOTE: this profile must be part of a loaded IG,
# otherwise the response is an HTTP 500 with no content
**cli_context,
profiles: [profile_url]
},
filesToValidate: [
Expand Down
61 changes: 61 additions & 0 deletions spec/inferno/dsl/fhir_resource_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
cliContext: {
sv: '4.0.1',
igs: [],
doNative: false,
extensions: ['any'],
profiles: [profile_url]
},
filesToValidate: [
Expand Down Expand Up @@ -170,6 +172,65 @@
end
end

describe '.method_missing' do
it 'applies the correct settings to cli_context' do
v1 = Inferno::DSL::FHIRResourceValidation::Validator.new do
url validation_url
txServer nil
end

v2 = Inferno::DSL::FHIRResourceValidation::Validator.new do
url validation_url
displayWarnings true
end

expect(v1.cli_context.fetch(:txServer, :missing)).to eq(nil)
expect(v1.cli_context.fetch(:displayWarnings, :missing)).to eq(:missing)
expect(v1.txServer).to eq(nil)

expect(v2.cli_context.fetch(:txServer, :missing)).to eq(:missing)
expect(v2.cli_context[:displayWarnings]).to eq(true)
expect(v2.displayWarnings).to eq(true)
end

it 'uses the right cli_context when submitting the validation request' do
v3 = Inferno::DSL::FHIRResourceValidation::Validator.new do
url 'http://example.com'
txServer nil
displayWarnings true
igs ['hl7.fhir.us.core#1.0.1']
end

expected_request_body = {
cliContext: {
sv: '4.0.1',
igs: ['hl7.fhir.us.core#1.0.1'],
doNative: false,
extensions: ['any'],
txServer: nil,
displayWarnings: true,
profiles: [profile_url]
},
filesToValidate: [
{
fileName: 'Patient/.json',
fileContent: resource.to_json,
fileType: 'json'
}
],
sessionId: nil
}.to_json

stub_request(:post, 'http://example.com/validate')
.with(body: expected_request_body)
.to_return(status: 200, body: '{}')

expect(v3.validate(resource, profile_url)).to eq('{}')
# if the request body doesn't match the stub,
# validate will throw an exception
end
end

describe '.find_validator' do
it 'finds the correct validator based on suite options' do
suite = OptionsSuite::Suite
Expand Down

0 comments on commit 5070e44

Please sign in to comment.