Module: ONCCertificationG10TestKit::BulkExportValidationTester

Includes:
AllResources, G10Options, ProfileSelector, USCoreTestKit::MustSupportTest
Defined in:
lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb

Constant Summary collapse

MAX_NUM_COLLECTED_LINES =
100
MIN_RESOURCE_COUNT =
2
PROFILES_TO_SKIP =

Remove us-core-observation-sexual-orientation from bulk data validation as directed by ASTP/ONC enforcement discretion issued on March 21, 2025: www.healthit.gov/topic/certification-ehrs/enforcement-discretion The enforcement discretion allies to US Core v6 and v7. US Core v5 also inlcude this profile. Since (g)(10) certification test kit does not cover US Core v5, adding profile url to this list for all versions is acceptable for now.

[
  'http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation-sexual-orientation',
  'http://hl7.org/fhir/us/core/StructureDefinition/us-core-simple-observation'
].freeze

Constants included from AllResources

AllResources::ALL_RESOURCES, AllResources::V5_ALL_RESOURCES, AllResources::V6_ALL_RESOURCES, AllResources::V7_ALL_RESOURCES

Constants included from G10Options

G10Options::BULK_DATA_1, G10Options::BULK_DATA_1_REQUIREMENT, G10Options::BULK_DATA_2, G10Options::BULK_DATA_2_REQUIREMENT, G10Options::SMART_1, G10Options::SMART_1_REQUIREMENT, G10Options::SMART_2, G10Options::SMART_2_2, G10Options::SMART_2_2_REQUIREMENT, G10Options::SMART_2_REQUIREMENT, G10Options::US_CORE_3, G10Options::US_CORE_3_REQUIREMENT, G10Options::US_CORE_4, G10Options::US_CORE_4_REQUIREMENT, G10Options::US_CORE_5, G10Options::US_CORE_5_REQUIREMENT, G10Options::US_CORE_6, G10Options::US_CORE_6_REQUIREMENT, G10Options::US_CORE_7, G10Options::US_CORE_7_REQUIREMENT, G10Options::US_CORE_VERSION_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AllResources

#all_required_resources

Methods included from G10Options

#us_core_7_and_above?, #us_core_version, #using_us_core_3?, #using_us_core_4?, #using_us_core_5?, #using_us_core_6?, #using_us_core_7?, #versioned_us_core_module

Methods included from ProfileSelector

#extract_profile, #location_use_base_fhir?, #observation_contains_code?, #resource_contains_category?, #select_profile

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



11
12
13
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 11

def 
  @metadata
end

Instance Method Details

#bearer_tokenObject



47
48
49
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 47

def bearer_token
  bulk_smart_auth_info.access_token
end

#build_headers(use_token) ⇒ Object



51
52
53
54
55
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 51

def build_headers(use_token)
  headers = { accept: 'application/fhir+ndjson' }
  headers.merge!({ authorization: "Bearer #{bearer_token}" }) if use_token == 'true'
  headers
end

#check_file_request(url) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



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
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
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 140

def check_file_request(url) # rubocop:disable Metrics/CyclomaticComplexity
  line_count = 0
  resources = Hash.new { |h, k| h[k] = [] }

  process_line = proc do |line|
    next unless lines_to_validate.blank? ||
                line_count < lines_to_validate.to_i ||
                (resource_type == 'Patient' && patient_ids_seen.length < MIN_RESOURCE_COUNT)

    line_count += 1

    begin
      resource = FHIR.from_contents(line)
    rescue StandardError
      skip "Server response at line \"#{line_count}\" is not a processable FHIR resource."
    end

    if resource.resourceType != resource_type
      assert false, "Resource type \"#{resource.resourceType}\" at line \"#{line_count}\" does not match type " \
                    "defined in output \"#{resource_type}\""
    end

    profile_urls = determine_profile(resource)
    profile_urls.each do |profile_url|
      resources[profile_url] << resource

      scratch[:patient_ids_seen] = patient_ids_seen | [resource.id] if resource_type == 'Patient'

      profile_with_version = versioned_profile_url(profile_url)
      unless resource_is_valid?(resource:, profile_url: profile_with_version)
        if first_error.key?(:line_number)
          @invalid_resource_count += 1
        else
          @invalid_resource_count = 1
          first_error[:line_number] = line_count
          first_error[:messages] = messages.dup
        end
      end
    end
  end

  process_headers = proc { |response|
    value = (response[:headers].find { |header| header.name.downcase == 'content-type' })&.value
    unless value&.start_with?('application/fhir+ndjson')
      skip "Content type must have 'application/fhir+ndjson' but found '#{value}'"
    end
  }

  stream_ndjson(url, build_headers(requires_access_token), process_line, process_headers)
  resources_from_all_files.merge!(resources) do |_key, all_resources, file_resources|
    all_resources | file_resources
  end
  line_count
end

#determine_profile(resource) ⇒ Object



107
108
109
110
111
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 107

def determine_profile(resource)
  return [] if resource.resourceType == 'Device' && !predefined_device_type?(resource)

  select_profile(resource)
end

#first_errorObject



39
40
41
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 39

def first_error
  @first_error ||= {}
end

#metadata_listObject



27
28
29
30
31
32
33
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 27

def 
  @metadata_list ||=
    versioned_us_core_module::USCoreTestSuite
      .
      .select { || .resource == resource_type }
      .reject { || PROFILES_TO_SKIP.include? .profile_url }
end

#patient_ids_seenObject



43
44
45
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 43

def patient_ids_seen
  scratch[:patient_ids_seen] ||= []
end

#perform_bulk_export_validationObject



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
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 208

def perform_bulk_export_validation
  skip_if status_output.blank?, 'Could not verify this functionality when Bulk Status Output is not provided'
  skip_if (requires_access_token == 'true' && bearer_token.blank?),
          'Could not verify this functionality when Bearer Token is required and not provided'

  assert_valid_json(status_output)
  file_list = JSON.parse(status_output).select { |file| file['type'] == resource_type }
  if file_list.empty?
    message = "No #{resource_type} resource file item returned by server."
    omit_if (all_required_resources.exclude? resource_type), "#{message} #{resource_type} resources are optional."
    skip message
  end

  @resources_from_all_files = {}
  resource_count = 0

  file_list.each do |file|
    resource_count += check_file_request(file['url'])
  end

  process_validation_errors(resource_count)

  validate_conformance(resources_from_all_files)

  pass "Successfully validated #{resource_count} #{resource_type} resource(s)."
end

#predefined_device_type?(resource) ⇒ Boolean

rubocop:disable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 95

def predefined_device_type?(resource) # rubocop:disable Metrics/CyclomaticComplexity
  return true if bulk_device_types_in_group.blank?

  expected = Set.new(bulk_device_types_in_group.split(',').map(&:strip))

  actual = resource&.type&.coding&.filter_map do |coding|
    coding.code if coding.system.nil? || coding.system == 'http://snomed.info/sct'
  end

  expected.intersect?(actual)
end

#process_validation_errors(resource_count) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 195

def process_validation_errors(resource_count)
  return if @invalid_resource_count.nil? || @invalid_resource_count.zero?

  first_error_message = "The line number for the first failed resource is #{first_error[:line_number]}."

  messages.clear
  messages.concat(first_error[:messages])

  assert false,
         "#{@invalid_resource_count} / #{resource_count} #{resource_type} resources failed profile validation. " \
         "#{first_error_message}"
end

#resources_from_all_filesObject



35
36
37
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 35

def resources_from_all_files
  @resources_from_all_files ||= {}
end

#stream_ndjson(endpoint, headers, process_chunk_line, process_response) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 57

def stream_ndjson(endpoint, headers, process_chunk_line, process_response) # rubocop:disable Metrics/CyclomaticComplexity
  hanging_chunk = ''

  process_body = proc { |chunk|
    hanging_chunk << chunk
     = hanging_chunk.lines

    hanging_chunk = .pop || ''

    .each do |elem|
      process_chunk_line.call(elem)
    end
  }

  stream(process_body, endpoint, headers:)

  max_redirect = 5

  while [301, 302, 303, 307].include?(response[:status]) &&
        request.response_header('location')&.value.present? &&
        max_redirect.positive?

    max_redirect -= 1

    redirect_url = request.response_header('location')&.value

    # handle relative redirects
    redirect_url = URI.parse(endpoint).merge(redirect_url).to_s unless redirect_url.start_with?('http')

    redirect_headers = headers.except(:authorization)

    stream(process_body, redirect_url, headers: redirect_headers)
  end

  process_chunk_line.call(hanging_chunk)
  process_response.call(response)
end

#validate_conformance(resources) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 113

def validate_conformance(resources)
  .each do |meta|
    next if resource_type == 'Location' && !us_core_7_and_above?

    skip_if resources[meta.profile_url].blank?,
            "No #{resource_type} resources found that conform to profile: #{meta.profile_url}."
    @metadata = meta
    @missing_elements = nil
    @missing_slices = nil
    @missing_extensions = nil
    begin
      perform_must_support_test(resources[meta.profile_url])
    rescue Inferno::Exceptions::PassException
      next
    rescue Inferno::Exceptions::SkipException => e
      e.message.concat " for `#{meta.profile_url}`"
      raise e
    end
  end
end

#versioned_profile_url(profile_url) ⇒ Object



134
135
136
137
138
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 134

def versioned_profile_url(profile_url)
  profile_version = .find { || .profile_url == profile_url }&.profile_version

  profile_version ? "#{profile_url}|#{profile_version}" : profile_url
end