Module: SubscriptionsTestKit::NotificationConformanceVerification

Instance Method Summary collapse

Instance Method Details

#check_bundle_entry_reference(bundle_entries, reference) ⇒ Object



150
151
152
153
154
155
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 150

def check_bundle_entry_reference(bundle_entries, reference)
  referenced_entry = bundle_entries.find do |entry|
    reference.include?("#{entry.resource.resourceType}/#{entry.resource.id}")
  end
  referenced_entry.present?
end

#check_entry_request_and_response(entry, entry_num) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 19

def check_entry_request_and_response(entry, entry_num)
  if entry.request.blank?
    add_message('error', %(
      The `entry.request` field is mandatory for history Bundles, but was not included in entry #{entry_num}))
  end
  return unless entry.response.blank?

  add_message('error', %(
    The `entry.response` field is mandatory for history Bundles, but was not included in entry #{entry_num}))
end

#check_history_bundle_request_response(bundle, subscription_status_entry, subscription_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 30

def check_history_bundle_request_response(bundle, subscription_status_entry, subscription_id)
  check_entry_request_and_response(subscription_status_entry, 1)

  unless subscription_status_entry.request.present? &&
         (
           subscription_id ?
             subscription_status_entry.request.url.end_with?("Subscription/#{subscription_id}/$status") :
             subscription_status_entry.request.url.match?(%r{Subscription/[^/]+/\$status\z})
         )

    add_message('error',
                'The SubscriptionStatus `request` SHALL be filled out to match a request to the $status operation')
  end

  bundle.entry.drop(1).each_with_index do |entry, index|
    check_entry_request_and_response(entry, index + 2)
  end
end

#check_notification_event_additional_context(additional_context_list, bundle_entries) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 171

def check_notification_event_additional_context(additional_context_list, bundle_entries)
  return if additional_context_list.empty?

  additional_context_list.each do |additional_context|
    next if check_bundle_entry_reference(bundle_entries, additional_context.valueReference.reference)

    add_message('error', %(
        The Notification Bundle does not include a resource entry for the reference found in
        SubscriptionStatus.notificationEvent.additional-context with id
        #{additional_context.valueReference.reference}))
  end
end

#check_notification_event_focus(focus_elem, bundle_entries) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 157

def check_notification_event_focus(focus_elem, bundle_entries)
  if focus_elem.blank?
    add_message('error', %(
        When the content type is `full-resource`, notification bundles SHALL include references to
        the appropriate focus resources in the SubscriptionStatus.notificationEvent.focus element))
  else
    unless check_bundle_entry_reference(bundle_entries, focus_elem.valueReference.reference)
      add_message('error', %(
        The Notification Bundle does not include a resource entry for the reference found in
        SubscriptionStatus.notificationEvent.focus with id #{focus_elem.valueReference.reference}))
    end
  end
end

#empty_event_notification_verification(notification_bundle) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 236

def empty_event_notification_verification(notification_bundle)
  assert_valid_json(notification_bundle)
  bundle = FHIR.from_contents(notification_bundle)
  assert bundle.present?, 'Not a FHIR resource'

  subscription_status = bundle.entry[0].resource

  parameter_topic = find_elem(subscription_status.parameter, 'topic')
  if parameter_topic.present?
    add_message('warning',
                'Parameters.parameter:topic.value[x]: This value SHOULD NOT be present when using empty payloads')
  end

  notification_events = find_all_elems(subscription_status.parameter, 'notification-event')
  bundle_entries = bundle.entry.drop(1)

  empty_notification_verification(bundle_entries, notification_events)
end

#empty_notification_event_references(notification_events) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 99

def empty_notification_event_references(notification_events)
  empty_req_check = true
  notification_events.each do |notification_event|
    empty_req_check = notification_event.part.none? do |part|
      part.name == 'focus' || part.name == 'additional-context'
    end
    break unless empty_req_check
  end
  empty_req_check
end

#empty_notification_verification(bundle_entries, notification_events) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 110

def empty_notification_verification(bundle_entries, notification_events)
  unless bundle_entries.empty?
    add_message('error', %(
    When the content type is empty, notification bundles SHALL not contain Bundle.entry elements other than
    the SubscriptionStatus for the notification.))
  end

  if notification_events.empty?
    add_message('error', %(
      Events are required for empty notifications, but the SubscriptionStatus does not contain event-notifications
    ))
    return
  end

  empty_req_check = empty_notification_event_references(notification_events)
  return if empty_req_check

  add_message('error', %(
        When populating the SubscriptionStatus.notificationEvent structure for a notification with an empty
        payload, a server SHALL NOT include references to resources))
end

#find_all_elems(resource_array, param_name) ⇒ Object



7
8
9
10
11
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 7

def find_all_elems(resource_array, param_name)
  resource_array.select do |param|
    param.name == param_name
  end
end

#find_elem(resource_array, param_name) ⇒ Object



13
14
15
16
17
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 13

def find_elem(resource_array, param_name)
  resource_array.find do |param|
    param.name == param_name
  end
end

#full_resource_event_notification_verification(notification_bundle, criteria_resource_type) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 255

def full_resource_event_notification_verification(notification_bundle, criteria_resource_type)
  assert_valid_json(notification_bundle)
  bundle = FHIR.from_contents(notification_bundle)
  assert bundle.present?, 'Not a FHIR resource'

  subscription_status = bundle.entry[0].resource

  parameter_topic = find_elem(subscription_status.parameter, 'topic')
  if parameter_topic.blank?
    add_message('warning', %(
          Parameters.parameter:topic.value[x]: This value SHOULD be present when using full-resource payloads))
  end

  notification_events = find_all_elems(subscription_status.parameter, 'notification-event')
  bundle_entries = bundle.entry.drop(1)

  if criteria_resource_type.present?
    full_resource_notification_criteria_resource_check(bundle_entries, criteria_resource_type)
  end

  if notification_events.empty?
    add_message('error', %(
      The notification event parameter must be present in `full-resource` notification bundles.))
  else
    full_resource_notification_event_parameter_verification(notification_events, bundle_entries)
  end

  verify_full_resource_notification_bundle_entries(bundle_entries)
end

#full_resource_notification_criteria_resource_check(bundle_entries, criteria_resource_type) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 216

def full_resource_notification_criteria_resource_check(bundle_entries, criteria_resource_type)
  relevant_resource = bundle_entries.any? do |entry|
    entry.resource.resourceType == criteria_resource_type || entry.request.url.include?(criteria_resource_type)
  end
  return if relevant_resource

  add_message('error', %(
      The notification bundle of type `full-resource` must include at least one #{criteria_resource_type}
      resource in the entry.resource element.))
end

#full_resource_notification_event_parameter_verification(notification_events, bundle_entries) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 184

def full_resource_notification_event_parameter_verification(notification_events, bundle_entries)
  notification_events.each do |notification_event|
    focus_elem = find_elem(notification_event.part, 'focus')
    additional_context_list = find_all_elems(notification_event.part, 'additional-context')

    check_notification_event_focus(focus_elem, bundle_entries)
    check_notification_event_additional_context(additional_context_list, bundle_entries)
  end
end

#id_only_event_notification_verification(notification_bundle, criteria_resource_type) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 285

def id_only_event_notification_verification(notification_bundle, criteria_resource_type)
  assert_valid_json(notification_bundle)
  bundle = FHIR.from_contents(notification_bundle)
  assert bundle.present?, 'Not a FHIR resource'

  subscription_status = bundle.entry[0].resource

  parameter_topic = find_elem(subscription_status.parameter, 'topic')
  add_message('info', %(
    Parameters.parameter:topic.value[x] is #{'not ' if parameter_topic.blank?}populated in `id-only` Notification.
    This value MAY be present when using id-only payloads))

  notification_events = find_all_elems(subscription_status.parameter, 'notification-event')
  bundle_entries = bundle.entry.drop(1)

  if notification_events.empty?
    add_message('error', %(
        The notification event parameter must be present in `id-only` notification bundles.))
  else
    id_only_notification_event_parameter_verification(notification_events, criteria_resource_type)
  end

  verify_id_only_notification_bundle_entries(bundle_entries)
end

#id_only_notification_event_parameter_verification(notification_events, criteria_resource_type) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 194

def id_only_notification_event_parameter_verification(notification_events, criteria_resource_type)
  notification_events.each do |notification_event|
    focus_elem = find_elem(notification_event.part, 'focus')

    if focus_elem.blank?
      add_message('error', %(
          When the content type is `id-only`, notification bundles SHALL include references to
          the appropriate focus resources in the SubscriptionStatus.notificationEvent.focus element))
      break
    else
      break unless criteria_resource_type.present?

      unless focus_elem.valueReference.reference.include?(criteria_resource_type)
        add_message('error', %(
          The SubscriptionStatus.notificationEvent.focus should include a reference to a #{criteria_resource_type}
          resource, the resource type the Subscription is focused on))
        break
      end
    end
  end
end

#no_error_verification(message) ⇒ Object



3
4
5
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 3

def no_error_verification(message)
  assert messages.none? { |msg| msg[:type] == 'error' }, message
end

#notification_verification(notification_bundle, notification_type, subscription_id: nil, status: nil) ⇒ Object



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
94
95
96
97
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 59

def notification_verification(notification_bundle, notification_type, subscription_id: nil, status: nil)
  assert_valid_json(notification_bundle)
  bundle = FHIR.from_contents(notification_bundle)
  assert bundle.present?, 'Not a FHIR resource'
  assert_resource_type(:bundle, resource: bundle)
  unless bundle.type == 'history'
    add_message('error', "Notification should be a history Bundle, instead was #{bundle.type}")
  end

  if bundle.entry.empty?
    add_message('error', 'Notification Bundle is empty.')
    return
  end
  subscription_status_entry = bundle.entry[0]

  check_history_bundle_request_response(bundle, subscription_status_entry, subscription_id)

  subscription_status = subscription_status_entry.resource
  parameters_verification(subscription_status)

  return unless subscription_status.respond_to?(:parameter)

  subscription_type = find_elem(subscription_status.parameter, 'type')

  unless subscription_type.valueCode == notification_type
    add_message('error', %(
        The Subscription resource should have it's `type` set to '#{notification_type}', was
        '#{subscription_type.valueCode}'))
  end

  return unless status.present?

  subscription_param_status = find_elem(subscription_status.parameter, 'status')
  return if subscription_param_status.valueCode == status

  add_message('error', %(
          The Subscription resource should have it's `status` set to '#{status}', was
          '#{subscription_param_status.valueCode}'))
end

#parameters_verification(subscription_status) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 49

def parameters_verification(subscription_status)
  resource_type = subscription_status.resourceType
  if resource_type == 'Parameters'
    resource_is_valid?(resource: subscription_status, profile_url: 'http://hl7.org/fhir/uv/subscriptions-backport/StructureDefinition/backport-subscription-status-r4')
  else
    add_message('error',
                "Unexpected resource type: Expected `Parameters`. Got `#{resource_type}`")
  end
end

#subscription_criteria(subscription) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 227

def subscription_criteria(subscription)
  return unless subscription['_criteria']

  criteria_extension = subscription['_criteria']['extension'].find do |ext|
    ext['url'].ends_with?('/backport-filter-criteria')
  end
  criteria_extension['valueString'].split('?').first
end

#verify_full_resource_notification_bundle_entries(bundle_entries) ⇒ Object



144
145
146
147
148
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 144

def verify_full_resource_notification_bundle_entries(bundle_entries)
  bundle_entries.each do |entry|
    resource_is_valid?(resource: entry.resource) if entry.resource.present?
  end
end

#verify_id_only_notification_bundle_entries(bundle_entries) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/subscriptions_test_kit/common/notification_conformance_verification.rb', line 132

def verify_id_only_notification_bundle_entries(bundle_entries)
  bundle_entries.each do |entry|
    if entry.resource.present?
      add_message('error', 'Each Bundle.entry for id-only notification SHALL not contain the `resource` field')
    end
    if entry.fullUrl.blank?
      add_message('error', %(
        Each Bundle.entry for id-only notification SHALL contain a relevant resource URL in the fullUrl))
    end
  end
end