Class: DaVinciPASTestKit::Jobs::SendSubscriptionHandshake

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

Instance Method Summary collapse

Instance Method Details

#authorization_headerObject



91
92
93
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 91

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

#await_subscription_creationObject



99
100
101
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 99

def await_subscription_creation
  sleep 0.5 until subscription.present?
end

#content_type_headerObject



74
75
76
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 74

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

#default_handshake_parameters_base_jsonObject



46
47
48
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 46

def default_handshake_parameters_base_json
  '{ "parameter": [ { "name": "subscription", "valueReference": { "reference": "replace_with_subscription_ref" } }, { "name": "topic", "valueCanonical": "replace_with_topic_canonical" }, { "name": "status", "valueCode": "requested" }, { "name": "type", "valueCode": "handshake" }, { "name": "events-since-subscription-start", "valueString": "0" } ], "resourceType": "Parameters" }' # rubocop:disable Layout/LineLength
end

#default_notification_base_jsonObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 35

def default_notification_base_json
  FHIR::Bundle.new(
    timestamp: Time.now.utc.iso8601,
    type: 'history',
    entry: [
      FHIR::Bundle::Entry.new(fullUrl: "urn:uuid:#{SecureRandom.uuid}",
                              resource: FHIR.from_contents(default_handshake_parameters_base_json))
    ]
  ).to_json
end

#headersObject



62
63
64
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 62

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

#perform(test_run_id, test_session_id, result_id, subscription_id, subscription_url, client_endpoint, bearer_token, notification_json, test_run_identifier, test_suite_base_url) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 14

def perform(test_run_id, test_session_id, result_id, subscription_id, subscription_url, client_endpoint,
            bearer_token, notification_json, test_run_identifier, test_suite_base_url)
  @test_run_id = test_run_id
  @test_session_id = test_session_id
  @result_id = result_id
  @subscription_id = subscription_id
  @subscription_url = subscription_url
  @client_endpoint = client_endpoint
  @bearer_token = bearer_token
  @notification_json = notification_json.present? ? notification_json : default_notification_base_json
  @test_run_identifier = test_run_identifier
  @test_suite_base_url = test_suite_base_url

  await_subscription_creation
  sleep 1
  return unless test_still_waiting?

  send_handshake_notification
  test_suite_connection.get(RESUME_PASS_PATH.delete_prefix('/'), { token: @test_run_identifier })
end

#persist_notification_request(response, tags) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 120

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



50
51
52
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 50

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

#rest_hook_connectionObject



66
67
68
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 66

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

#results_repoObject



54
55
56
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 54

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

#send_handshake_notificationObject



103
104
105
106
107
108
109
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 103

def send_handshake_notification
  handshake_json = derive_handshake_notification(@notification_json, @subscription_url,
                                                 subscription_topic).to_json
  response = send_notification(handshake_json)
  persist_notification_request(response, [REST_HOOK_HANDSHAKE_NOTIFICATION_TAG])
  response
end

#send_notification(request_body) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 111

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)
end

#subscriptionObject



58
59
60
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 58

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

#subscription_headersObject



78
79
80
81
82
83
84
85
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 78

def subscription_headers
  return {} unless subscription.present?

  @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_topicObject



87
88
89
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 87

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

#test_still_waiting?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 95

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

#test_suite_connectionObject



70
71
72
# File 'lib/davinci_pas_test_kit/jobs/send_subscription_handshake.rb', line 70

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