Skip to content

Commit

Permalink
Merge pull request #219 from ministryofjustice/extend-health-check
Browse files Browse the repository at this point in the history
Extend the healthchecks
  • Loading branch information
colinbruce authored Jul 27, 2016
2 parents b052f7b + 08ee57b commit a780ec2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 20 deletions.
20 changes: 18 additions & 2 deletions app/services/health_status/health_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ module HealthStatus
class HealthCheck
def initialize
check_submission_api!
downstream_api_ok?
end

def as_json(_options = {})
def as_json(_options = {}) # rubocop:disable Metrics/MethodLength
{
submission_api: {
description: 'Submission API',
ok: @submission_api_available
},
ok: @submission_api_available
downstream_api_ok: {
description: 'Downstream API',
ok: @downstream_api_ok
},
ok: @submission_api_available && @downstream_api_ok
}
end

Expand All @@ -24,5 +29,16 @@ def check_submission_api!
service = SubmitApplication.new(Settings.submission.url, Settings.submission.token)
@submission_api_available = service.available?
end

def downstream_api_ok?
@downstream_api_ok = staff_app_health_check_result
end

def staff_app_health_check_result
json = JSON.parse(RestClient.get("#{Settings.submission.url}/healthcheck.json"))
json['ok']
rescue
false
end
end
end
89 changes: 71 additions & 18 deletions spec/services/health_status/health_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,94 @@
subject(:service) { described_class.new }

let(:submit_application) { double(available?: submit_application_available?) }
let(:downstream_api) { double(available?: downstream_api_ok?) }
let(:downstream_api_ok?) { true }
let(:json_returned) { "{\"database\":{\"description\":\"Postgres database\",\"ok\":true},\"ok\":#{downstream_api_ok?}}" }

before do
stub_request(:get, "http://submit.to.this/healthcheck.json").
to_return(status: downstream_api_ok? ? 200 : 500, body: json_returned, headers: {})
allow(SubmitApplication).to receive(:new).and_return(submit_application)
end

describe '#as_json' do
subject { service.as_json }

context 'when the submission api is available' do
let(:submit_application_available?) { true }
context 'when there are no issues with the downstream health check' do
context 'when the submission api is available' do
let(:submit_application_available?) { true }

it 'returns hash with all services ok' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
it 'returns hash with all services ok' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
ok: true
},
downstream_api_ok: {
description: 'Downstream API',
ok: true
},
ok: true
},
ok: true
)
)
end
end

context 'when the submission api is not available' do
let(:submit_application_available?) { false }

it 'returns hash with services unavailable' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
ok: false
},
downstream_api_ok: {
description: 'Downstream API',
ok: true
},
ok: false
)
end
end
end
context 'when there are issues with the downstream health check' do
let(:downstream_api_ok?) { false }

context 'when the submission api is not available' do
let(:submit_application_available?) { false }
context 'when the submission api is available' do
let(:submit_application_available?) { true }

it 'returns hash with services unavailable' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
it 'returns hash with all services ok' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
ok: true
},
downstream_api_ok: {
description: 'Downstream API',
ok: false
},
ok: false
},
ok: false
)
)
end
end

context 'when the submission api is not available' do
let(:submit_application_available?) { false }

it 'returns hash with services unavailable' do
is_expected.to eql(
submission_api: {
description: 'Submission API',
ok: false
},
downstream_api_ok: {
description: 'Downstream API',
ok: false
},
ok: false
)
end
end
end
end

Expand Down

0 comments on commit a780ec2

Please sign in to comment.