Skip to content

Commit

Permalink
FI-2035: refactor resource_is_valid? and check for non-json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dehall committed Aug 4, 2023
1 parent 01409cd commit 4c6c70f
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions lib/inferno/dsl/fhir_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,14 @@ def exclude_message(&block)
def resource_is_valid?(resource, profile_url, runnable)
profile_url ||= FHIR::Definitions.resource_definition(resource.resourceType).url

begin
validation_response = validate(resource, profile_url)
rescue StandardError => e
runnable.add_message('error', e.message)
raise Inferno::Exceptions::ErrorInValidatorException, "Unable to connect to validator at #{url}."
end
outcome, http_status = validate(resource, profile_url, runnable)

# The validator should return OperationOutcome both on successful validation and on error,
# so parse the OO before checking the HTTP status code.
outcome = FHIR::OperationOutcome.new(JSON.parse(validation_response.body))

message_hashes = outcome.issue&.map { |issue| message_hash_from_issue(issue, resource) } || []

message_hashes.concat(additional_validation_messages(resource, profile_url))

filter_messages(message_hashes)
message_hashes = message_hashes_from_outcome(outcome, resource, profile_url)

message_hashes
.each { |message_hash| runnable.add_message(message_hash[:type], message_hash[:message]) }

unless validation_response.status == 200
unless http_status == 200
raise Inferno::Exceptions::ErrorInValidatorException,
'Error occurred in the validator. Review Messages tab or validator service logs for more information.'
end
Expand All @@ -156,6 +143,17 @@ def filter_messages(message_hashes)
message_hashes.reject! { |message| exclude_message.call(Entities::Message.new(message)) } if exclude_message
end

# @private
def message_hashes_from_outcome(outcome, resource, profile_url)
message_hashes = outcome.issue&.map { |issue| message_hash_from_issue(issue, resource) } || []

message_hashes.concat(additional_validation_messages(resource, profile_url))

filter_messages(message_hashes)

message_hashes
end

# @private
def message_hash_from_issue(issue, resource)
{
Expand Down Expand Up @@ -193,12 +191,32 @@ def issue_message(issue, resource)
#
# @param resource [FHIR::Model]
# @param profile_url [String]
# @return [Faraday::Response] the validation response
def validate(resource, profile_url)
Faraday.new(
url,
params: { profile: profile_url }
).post('validate', resource.source_contents)
# @return [[Array(FHIR::OperationOutcome, Number)] the validation response and HTTP status code
def validate(resource, profile_url, runnable)
begin
response = Faraday.new(
url,
params: { profile: profile_url }
).post('validate', resource.source_contents)
rescue StandardError => e
runnable.add_message('error', e.message)
raise Inferno::Exceptions::ErrorInValidatorException, "Unable to connect to validator at #{url}."
end
outcome = operation_outcome_from_validator_response(response.body, runnable)

[outcome, response.status]
end

# @private
def operation_outcome_from_validator_response(response, runnable)
if response.start_with? '{'
FHIR::OperationOutcome.new(JSON.parse(response))
else
runnable.add_message('error', "Validator Response: #{response}")
raise Inferno::Exceptions::ErrorInValidatorException,
'Validator response was an unexpected format. '\
'Review Messages tab or validator service logs for more information.'
end
end
end

Expand Down

0 comments on commit 4c6c70f

Please sign in to comment.