Class: DaVinciCRDTestKit::V221::ClientLocationAddressPropagationTest

Inherits:
Inferno::Test
  • Object
show all
Includes:
TaggedRequestLoadHelper
Defined in:
lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb

Instance Method Summary collapse

Methods included from TaggedRequestLoadHelper

#crd_interaction_group, #hook_name, #load_interaction_group_requests, #load_requests_for_cross_hook_analysis

Instance Method Details

#add_location_to_hash(location, location_hash) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 129

def add_location_to_hash(location, location_hash)
  return unless location.is_a?(FHIR::Location) && location.id.present?

  relative_reference = "#{location.resourceType}/#{location.id}"
  return if location_hash.key?(relative_reference)

  location_hash[relative_reference] = location
end

#build_fetched_locations_hashObject



121
122
123
124
125
126
127
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 121

def build_fetched_locations_hash
  load_tagged_requests(PARENT_LOCATION_FETCH_TAG, DATA_FETCH_TAG).each_with_object({}) do |request, location_hash|
    add_location_to_hash(FHIR.from_contents(request.response_body), location_hash)
  rescue JSON::ParserError
    next
  end
end

#build_prefetched_locations_hashObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 92

def build_prefetched_locations_hash
  loaded_requests.each_with_object({}) do |request, location_hash|
    request_body = JSON.parse(request.request_body)
    next unless request_body.is_a?(Hash)

    prefetched_location_bundle = extract_prefetched_location_bundle(request_body)
    next unless prefetched_location_bundle.present?

    prefetched_location_bundle.entry.each do |entry|
      add_location_to_hash(entry.resource, location_hash)
    end
  rescue JSON::ParserError
    next
  end
end

#check_location_address_propagation(location, root_id = location.id) ⇒ Object

checks that the location has an address if a parent also has an address



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 39

def check_location_address_propagation(location, root_id = location.id)
  return if location.address.present?
  return unless location.partOf.present? && location.partOf.reference.present?

  parent = prefetched_location_hash[location.partOf.reference]
  parent = fetched_location_hash[location.partOf.reference] unless parent.present?
  if parent.present?
    check_location_conformance(parent, root_id)
    if parent.address.present?
      add_message('error', "Address missing on prefetched 'Location/#{root_id}': " \
                           "parent 'Location/#{parent.id}' has an address.")
    else
      check_location_address_propagation(parent, root_id)
    end
  else
    add_message('error', "Unable to check address propagation prefetched 'Location/#{root_id}': " \
                         "`partOf` reference '#{location.partOf.reference}' could not be fetched.")
  end

  nil
end

#check_location_conformance(location, root_id) ⇒ Object

skip profile check if in the prefetch hash (done elsewhere) or already checked



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 62

def check_location_conformance(location, root_id)
  key = "Location/#{location.id}"
  return if prefetched_location_hash.key?(key) || fetched_locations_checked_for_conformance.key?(key)

  fetched_locations_checked_for_conformance[key] = location
  validator_response_details = []
  return if resource_is_valid?(resource: location, profile_url: 'http://hl7.org/fhir/us/davinci-crd/StructureDefinition/profile-location|2.2.1',
                               add_messages_to_runnable: false, validator_response_details:)

  message_prefix = "Parent of prefetched 'Location/#{root_id}'"
  add_message('error', "Location/#{location.id}: #{message_prefix} does not conform to the CRD Location profile.")
  validator_response_details.each do |issue|
    next if issue.filtered

    add_message(issue.severity, "(#{message_prefix}) #{issue.message}")
  end
end

#extract_prefetched_location_bundle(request_body) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 108

def extract_prefetched_location_bundle(request_body)
  locations_data = request_body.dig('prefetch', 'locations') || request_body.dig('prefetch', 'locs')
  locations = FHIR.from_contents(locations_data.to_json) if locations_data.present?
  return locations if locations.is_a?(FHIR::Bundle)
  return nil unless locations.is_a?(FHIR::Location)

  FHIR::Bundle.new({ entry: [FHIR::Bundle::Entry.new({ resource: locations })] })
end

#fetched_location_hashObject



117
118
119
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 117

def fetched_location_hash
  @fetched_location_hash ||= build_fetched_locations_hash
end

#fetched_locations_checked_for_conformanceObject



80
81
82
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 80

def fetched_locations_checked_for_conformance
  @fetched_locations_checked_for_conformance ||= {}
end

#loaded_requestsObject



84
85
86
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 84

def loaded_requests
  @loaded_requests ||= load_requests_for_cross_hook_analysis
end

#prefetched_location_hashObject



88
89
90
# File 'lib/davinci_crd_test_kit/client/v2.2.1/cross_hook/client_location_address_propagation_test.rb', line 88

def prefetched_location_hash
  @prefetched_location_hash ||= build_prefetched_locations_hash
end