Module: AUCoreTestKit::ReferenceResolutionTest

Extended by:
Forwardable
Includes:
AssertHelpers, FHIRResourceNavigation
Included in:
AUCoreV100::AllergyIntoleranceReferenceResolutionTest, AUCoreV100::BloodpressureReferenceResolutionTest, AUCoreV100::BodyheightReferenceResolutionTest, AUCoreV100::BodytempReferenceResolutionTest, AUCoreV100::BodyweightReferenceResolutionTest, AUCoreV100::ConditionReferenceResolutionTest, AUCoreV100::DiagnosticresultPathReferenceResolutionTest, AUCoreV100::DiagnosticresultReferenceResolutionTest, AUCoreV100::EncounterReferenceResolutionTest, AUCoreV100::HeartrateReferenceResolutionTest, AUCoreV100::ImmunizationReferenceResolutionTest, AUCoreV100::LocationReferenceResolutionTest, AUCoreV100::MedicationRequestReferenceResolutionTest, AUCoreV100::PractitionerRoleReferenceResolutionTest, AUCoreV100::ProcedureReferenceResolutionTest, AUCoreV100::ResprateReferenceResolutionTest, AUCoreV100::SmokingstatusReferenceResolutionTest, AUCoreV100::WaistcircumReferenceResolutionTest, AUCoreV110_PREVIEW::AllergyIntoleranceReferenceResolutionTest, AUCoreV110_PREVIEW::BloodpressureReferenceResolutionTest, AUCoreV110_PREVIEW::BodyheightReferenceResolutionTest, AUCoreV110_PREVIEW::BodytempReferenceResolutionTest, AUCoreV110_PREVIEW::BodyweightReferenceResolutionTest, AUCoreV110_PREVIEW::ConditionReferenceResolutionTest, AUCoreV110_PREVIEW::DiagnosticresultPathReferenceResolutionTest, AUCoreV110_PREVIEW::DiagnosticresultReferenceResolutionTest, AUCoreV110_PREVIEW::EncounterReferenceResolutionTest, AUCoreV110_PREVIEW::HeartrateReferenceResolutionTest, AUCoreV110_PREVIEW::ImmunizationReferenceResolutionTest, AUCoreV110_PREVIEW::LocationReferenceResolutionTest, AUCoreV110_PREVIEW::MedicationRequestReferenceResolutionTest, AUCoreV110_PREVIEW::MedicationStatementReferenceResolutionTest, AUCoreV110_PREVIEW::PractitionerRoleReferenceResolutionTest, AUCoreV110_PREVIEW::ProcedureReferenceResolutionTest, AUCoreV110_PREVIEW::RelatedPersonReferenceResolutionTest, AUCoreV110_PREVIEW::ResprateReferenceResolutionTest, AUCoreV110_PREVIEW::SmokingstatusReferenceResolutionTest, AUCoreV110_PREVIEW::WaistcircumReferenceResolutionTest, AUCoreV200_DRAFT::AllergyIntoleranceReferenceResolutionTest, AUCoreV200_DRAFT::BloodpressureReferenceResolutionTest, AUCoreV200_DRAFT::BodyheightReferenceResolutionTest, AUCoreV200_DRAFT::BodytempReferenceResolutionTest, AUCoreV200_DRAFT::BodyweightReferenceResolutionTest, AUCoreV200_DRAFT::ConditionReferenceResolutionTest, AUCoreV200_DRAFT::DiagnosticresultPathReferenceResolutionTest, AUCoreV200_DRAFT::DiagnosticresultReferenceResolutionTest, AUCoreV200_DRAFT::EncounterReferenceResolutionTest, AUCoreV200_DRAFT::HeartrateReferenceResolutionTest, AUCoreV200_DRAFT::ImmunizationReferenceResolutionTest, AUCoreV200_DRAFT::LocationReferenceResolutionTest, AUCoreV200_DRAFT::MedicationRequestReferenceResolutionTest, AUCoreV200_DRAFT::MedicationStatementReferenceResolutionTest, AUCoreV200_DRAFT::PractitionerRoleReferenceResolutionTest, AUCoreV200_DRAFT::ProcedureReferenceResolutionTest, AUCoreV200_DRAFT::RelatedPersonReferenceResolutionTest, AUCoreV200_DRAFT::ResprateReferenceResolutionTest, AUCoreV200_DRAFT::SmokingstatusReferenceResolutionTest, AUCoreV200_DRAFT::WaistcircumReferenceResolutionTest
Defined in:
lib/au_core_test_kit/reference_resolution_test.rb

Constant Summary

Constants included from FHIRResourceNavigation

FHIRResourceNavigation::DAR_EXTENSION_URL

Instance Method Summary collapse

Methods included from AssertHelpers

#act_if_with_message, #act_with_message, #conditional_skip_with_msg, #skip_with_msg

Methods included from FHIRResourceNavigation

#find_a_value_at, #find_slice_via_discriminator, #get_extension_url, #get_next_value, #get_value_from_extension, #is_extension?, #resolve_path, #verify_slice_by_values

Instance Method Details

#is_reference_resolved?(reference, target_profile) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 48

def is_reference_resolved?(reference, target_profile)
  resolved_references.any? do |item|
    item[:reference] == reference.reference &&
      (
        target_profile.blank? || item[:profiles].include?(target_profile)
      )
  end
end

#must_support_referencesObject



66
67
68
69
70
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 66

def must_support_references
  .must_supports[:elements].select do |element_definition|
    element_definition[:types]&.include?('Reference')
  end
end

#must_support_references_with_target_profileObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 72

def must_support_references_with_target_profile
  # mapping array of target_profiles to array of {path, target_profile} pair
  must_support_references.map do |element_definition|
    (element_definition[:target_profiles] || ['']).map do |target_profile|
      {
        path: element_definition[:path],
        target_profile:
      }
    end
  end.flatten
end

#no_resources_skip_messageObject



61
62
63
64
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 61

def no_resources_skip_message
  "No #{resource_type} resources appear to be available. " \
  'Please use patients with more information.'
end

#perform_reference_resolution_test(resources) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 14

def perform_reference_resolution_test(resources)
  conditional_skip_with_msg resources.blank?, no_resources_skip_message

  pass if unresolved_references(resources).empty?

  skip_with_msg "Could not resolve and validate any Must Support references for #{unresolved_references_strings.join(', ')}"
end

#record_resolved_reference(reference, target_profile) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 32

def record_resolved_reference(reference, target_profile)
  saved_reference = resolved_references.find { |item| item[:reference] == reference.reference }

  if saved_reference.present?
    saved_reference[:profiles] << target_profile if target_profile.present? && !saved_reference[:profiles].include?(target_profile)
  else
    saved_reference = {
      reference: reference.reference,
      profiles: []
    }

    saved_reference[:profiles] << target_profile if target_profile.present?
    resolved_references.add(saved_reference)
  end
end

#resolved_referencesObject



57
58
59
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 57

def resolved_references
  scratch[:resolved_references] ||= Set.new
end

#resource_is_valid_with_target_profile?(resource, target_profile) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 169

def resource_is_valid_with_target_profile?(resource, target_profile)
  return true if target_profile.blank?

  # Only need to know if the resource is valid.
  # Calling resource_is_valid? causes validation errors to be logged.
  validator = find_validator(:default)

  target_profile_with_version = target_profile == "http://hl7.org.au/fhir/StructureDefinition/au-specimen" ? target_profile : "#{target_profile}|#{.profile_version}"

  validator_response = validator.validate(resource, target_profile_with_version)
  outcome = validator.operation_outcome_from_hl7_wrapped_response(validator_response)

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

  message_hashes.concat(validator.additional_validation_messages(resource, target_profile_with_version))

  validator.filter_messages(message_hashes)

  message_hashes.none? { |message_hash| message_hash[:type] == 'error' }
end

#unresolved_references(resources = []) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 84

def unresolved_references(resources = [])
  @unresolved_references ||=
    must_support_references_with_target_profile.select do |reference_path_profile_pair|
      path = reference_path_profile_pair[:path]
      target_profile = reference_path_profile_pair[:target_profile]

      found_one_reference = false

      resolve_one_reference = resources.any? do |resource|
        value_found = resolve_path(resource, path)
        next if value_found.empty?

        found_one_reference = true

        value_found.any? do |reference|
          validate_reference_resolution(resource, reference, target_profile)
        end
      end

      found_one_reference && !resolve_one_reference
    end

  if .must_supports[:choices].present?
    @unresolved_references.delete_if do |reference|
      choice_profiles = .must_supports[:choices].find do |choice|
        choice[:target_profiles]&.include?(reference[:target_profile])
      end

      choice_profiles.present? &&
        choice_profiles[:target_profiles]&.any? do |profile|
          @unresolved_references.none? do |element|
            element[:target_profile] == profile
          end
        end
    end
  end

  @unresolved_references
end

#unresolved_references_stringsObject



22
23
24
25
26
27
28
29
30
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 22

def unresolved_references_strings
  unresolved_reference_hash =
    unresolved_references.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |missing, hash|
      hash[missing[:path]] << missing[:target_profile]
    end
  unresolved_reference_hash.map do |path, profiles|
    "#{path} element: Reference#{"(#{profiles.join('|')})" unless profiles.first.empty?}"
  end
end

#validate_reference_resolution(resource, reference, target_profile) ⇒ Object



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/au_core_test_kit/reference_resolution_test.rb', line 124

def validate_reference_resolution(resource, reference, target_profile)
  return true if is_reference_resolved?(reference, target_profile)

  if reference.contained?
    # if reference_id is blank it is referring to itself, so we know it exists
    return true if reference.reference_id.blank?

    return resource.contained.any? do |contained_resource|
             contained_resource&.id == reference.reference_id &&
             resource_is_valid_with_target_profile?(contained_resource, target_profile)
           end
  end

  reference_type = reference.resource_type
  reference_id = reference.reference_id

  resolved_resource =
    begin
      if reference.relative?
        begin
          reference.resource_class
        rescue NameError
          return false
        end

        fhir_read(reference_type, reference_id)&.resource
      elsif reference.base_uri.chomp('/') == fhir_client.instance_variable_get(:@base_service_url).chomp('/')
        fhir_read(reference_type, reference_id)&.resource
      else
        get(reference.reference)&.resource
      end
    rescue StandardError => e
      Inferno::Application['logger'].error("Unable to resolve reference #{reference.reference}")
      Inferno::Application['logger'].error(e.full_message)
      return false
    end

  return false unless resolved_resource&.resourceType == reference_type && resolved_resource&.id == reference_id

  return false unless resource_is_valid_with_target_profile?(resolved_resource, target_profile)

  record_resolved_reference(reference, target_profile)
  true
end