Module: ONCCertificationG10TestKit::BulkExportValidationTester

Includes:
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
OMIT_KLASS =
['Medication', 'Location', 'QuestionnaireResponse', 'PractitionerRole'].freeze
PROFILES_TO_SKIP =
[
  'http://hl7.org/fhir/us/core/StructureDefinition/us-core-simple-observation'
].freeze

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_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_VERSION_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from G10Options

#us_core_version, #using_us_core_3?, #using_us_core_5?, #using_us_core_6?, #versioned_us_core_module

Methods included from ProfileSelector

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

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 9

def 
  @metadata
end

Instance Method Details

#build_headers(use_token) ⇒ Object



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

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



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
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 127

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



94
95
96
97
98
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 94

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

  select_profile(resource)
end

#first_errorObject



30
31
32
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 30

def first_error
  @first_error ||= {}
end

#metadata_listObject



18
19
20
21
22
23
24
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 18

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

#patient_ids_seenObject



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

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

#perform_bulk_export_validationObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 195

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 (OMIT_KLASS.include? 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)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 82

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 & actual).any?
end

#process_validation_errors(resource_count) ⇒ Object



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 182

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



26
27
28
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 26

def resources_from_all_files
  @resources_from_all_files ||= {}
end

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

rubocop:disable Metrics/CyclomaticComplexity



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 44

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

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

    hanging_chunk = .pop || String.new

    .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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 100

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

    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



121
122
123
124
125
# File 'lib/onc_certification_g10_test_kit/bulk_export_validation_tester.rb', line 121

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

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