Class: SubscriptionsTestKit::Jobs::SendSubscriptionNotifications

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils
Defined in:
lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb

Constant Summary

Constants included from SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils

SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils::ALLOWED_MIME_TYPES, SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils::DEFAULT_MIME_TYPE

Instance Method Summary collapse

Methods included from SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils

#actual_mime_type, #client_access_token_input, #derive_event_notification, #derive_handshake_notification, #derive_status_bundle, #determine_event_count, #determine_subscription_status_code, #find_subscription, #notification_bundle_input, #operation_outcome

Instance Method Details

#authorization_headerObject



73
74
75
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 73

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

#await_subcription_creationObject



81
82
83
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 81

def await_subcription_creation
  sleep 0.5 until subscription.present?
end

#content_type_headerObject



62
63
64
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 62

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

#headersObject



50
51
52
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 50

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
34
35
36
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.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
  @test_run_identifier = test_run_identifier
  @test_suite_base_url = test_suite_base_url

  await_subcription_creation
  sleep 1
  return unless test_still_waiting?

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

  send_event_notification
end

#persist_notification_request(response, tags) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 111

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



38
39
40
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 38

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

#rest_hook_connectionObject



54
55
56
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 54

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

#results_repoObject



42
43
44
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 42

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

#resume_inferno_testObject



98
99
100
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 98

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

#send_event_notificationObject



92
93
94
95
96
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 92

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

#send_handshake_notificationObject



85
86
87
88
89
90
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 85

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

#send_notification(request_body) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 102

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



46
47
48
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 46

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

#subscription_headersObject



66
67
68
69
70
71
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 66

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

#test_still_waiting?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 77

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

#test_suite_connectionObject



58
59
60
# File 'lib/subscriptions_test_kit/jobs/send_subscription_notifications.rb', line 58

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