Class: DaVinciPASTestKit::Jobs::SendPASSubscriptionNotification

Inherits:
Object
  • Object
show all
Includes:
ClientURLs, Sidekiq::Job, SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils
Defined in:
lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientURLs

#fhir_base_url, #fhir_subscription_url, #inquire_url, #registration_url, #session_fhir_base_url, #session_fhir_subscription_url, #session_inquire_url, #session_submit_url, #submit_url, #token_url, #udap_discovery_url

Methods included from BaseURLs

#base_url, #resume_fail_url, #resume_pass_url, #resume_skip_url

Instance Attribute Details

#ig_versionObject (readonly)

Returns the value of attribute ig_version.



19
20
21
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 19

def ig_version
  @ig_version
end

Instance Method Details

#authorization_headerObject



84
85
86
87
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 84

def authorization_header
  @authorization_header ||=
    @notification_bearer_token.present? ? { 'Authorization' => "Bearer #{@notification_bearer_token}" } : {}
end

#await_subscription_creationObject



101
102
103
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 101

def await_subscription_creation
  sleep 0.5 until subscription.present? || !test_still_waiting?
end

#content_type_headerObject



73
74
75
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 73

def content_type_header
  @content_type_header ||= { 'Content-Type' => actual_mime_type(subscription) }
end

#headersObject



60
61
62
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 60

def headers
  @headers ||= subscription_headers.merge(content_type_header).merge(authorization_header)
end

#perform(test_run_id, test_session_id, result_id, notification_bearer_token, notification_json, resume_token, notification_suite_id, ig_version = 'v2.0.1') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 23

def perform(test_run_id, test_session_id, result_id, notification_bearer_token, notification_json, resume_token,
            notification_suite_id, ig_version = 'v2.0.1')
  @test_run_id = test_run_id
  @test_session_id = test_session_id
  @result_id = result_id
  @notification_bearer_token = notification_bearer_token
  @notification_json = notification_json
  @resume_token = resume_token
  @notification_suite_id = notification_suite_id
  @ig_version = ig_version

  await_subscription_creation # NOTE: currently must exist - see PASClientPendedSubmitTest
  sleep 1
  return unless test_still_waiting?

  sleep rand(5..10)
  return unless test_still_waiting?

  send_event_notification
end

#persist_notification_request(response, tags) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 122

def persist_notification_request(response, tags)
  inferno_request_headers = headers.map { |name, value| { name:, value: } }
  inferno_response_headers = response.headers&.map { |name, value| { name:, value: } }
  requests_repo.create(
    verb: 'POST',
    url: response.env.url.to_s,
    direction: 'outgoing',
    status: response.status,
    request_body: response.env.request_body,
    response_body: response.env.response_body,
    test_session_id: @test_session_id,
    result_id: @result_id,
    request_headers: inferno_request_headers,
    response_headers: inferno_response_headers,
    tags:
  )
end

#requests_repoObject



44
45
46
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 44

def requests_repo
  @requests_repo ||= Inferno::Repositories::Requests.new
end

#rest_hook_connectionObject



64
65
66
67
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 64

def rest_hook_connection
  @rest_hook_connection ||= Faraday.new(url: subscription_notification_endpoint, request: { open_timeout: 30 },
                                        headers:)
end

#results_repoObject



48
49
50
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 48

def results_repo
  @results_repo ||= Inferno::Repositories::Results.new
end

#send_event_notificationObject



105
106
107
108
109
110
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 105

def send_event_notification
  event_json = derive_event_notification(@notification_json, subscription_full_url, subscription_topic,
                                         1).to_json
  response = send_notification(event_json)
  persist_notification_request(response, [REST_HOOK_EVENT_NOTIFICATION_TAG])
end

#send_notification(request_body) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 112

def send_notification(request_body)
  rest_hook_connection.post('', request_body)
rescue Faraday::Error => e
  # Warning: This is a hack. If there is an error with the request such that we never get a response, we have
  #          no clean way to persist that information for the Inferno test to check later. The solution here
  #          is to persist the request anyway with a status of nil, using the error message as response body
  Faraday::Response.new(response_body: e.message, url: rest_hook_connection.url_prefix.to_s,
                        request_body:, request_headers: headers)
end

#subscriptionObject



52
53
54
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 52

def subscription
  @subscription ||= find_subscription(@test_session_id)
end

#subscription_full_urlObject



93
94
95
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 93

def subscription_full_url
  @subscription_full_url ||= "#{fhir_subscription_url}/#{subscription.id}"
end

#subscription_headersObject



77
78
79
80
81
82
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 77

def subscription_headers
  @subscription_headers ||= subscription.channel&.header&.each_with_object({}) do |header, hash|
    header_name, header_value = header.split(': ', 2)
    hash[header_name] = header_value
  end || {}
end

#subscription_notification_endpointObject



56
57
58
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 56

def subscription_notification_endpoint
  subscription&.channel&.endpoint
end

#subscription_topicObject



89
90
91
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 89

def subscription_topic
  @subscription_topic ||= subscription&.criteria
end

#suite_idObject

provide a suite_id definition to use with the ClientURLs module



15
16
17
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 15

def suite_id
  @notification_suite_id
end

#test_still_waiting?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 97

def test_still_waiting?
  results_repo.find_waiting_result(test_run_id: @test_run_id)
end

#test_suite_connectionObject



69
70
71
# File 'lib/davinci_pas_test_kit/client/jobs/send_pas_subscription_notification.rb', line 69

def test_suite_connection
  @test_suite_connection ||= Faraday.new(base_url)
end