Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Add more specific response.success to target specifically for fedora …
Browse files Browse the repository at this point in the history
…and specifically for adding a monitor to the service
  • Loading branch information
aprilrieger committed Mar 29, 2024
1 parent ca66d8c commit 56f391e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
10 changes: 7 additions & 3 deletions app/models/fcrepo_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ def self.reset!
end

def ping
ActiveFedora::Fedora.instance.connection.head(
if ActiveFedora::Fedora.instance.connection.head(
ActiveFedora::Fedora.instance.connection.connection.http.url_prefix.to_s
).response.success?
rescue StandardError
false
"Fedora is OK"
else
"Fedora is Down"
end
rescue StandardError => e
"Error checking Fedora status: #{e.message}"
end

# Remove the Fedora resource for this endpoint, then destroy the record
Expand Down
37 changes: 24 additions & 13 deletions spec/models/fcrepo_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@

RSpec.describe FcrepoEndpoint do
let(:base_path) { 'foobar' }
subject { described_class.new base_path: base_path }

describe '.options' do
subject { described_class.new base_path: base_path }

it 'uses the configured application settings' do
expect(subject.options[:base_path]).to eq base_path
end
end

describe '#ping' do
let(:success_response) { double(response: double(success?: true)) }
let(:url_prefix) { ActiveFedora::Fedora.instance.connection.connection.http.url_prefix.to_s }

context 'when Fedora is accessible' do
let(:success_response) { double(response: double(success?: true)) }

it 'returns "Fedora is OK"' do
allow(ActiveFedora::Fedora.instance.connection).to receive(:head).with(url_prefix).and_return(success_response)
expect(subject.ping).to eq("Fedora is OK")
end
end

context 'when Fedora is down' do
let(:failure_response) { double(response: double(success?: false)) }

it 'checks if the service is up' do
allow(ActiveFedora::Fedora.instance.connection).to receive(:head).with(
ActiveFedora::Fedora.instance.connection.connection.http.url_prefix.to_s
).and_return(success_response)
expect(subject.ping).to be_truthy
it 'returns "Fedora is Down"' do
allow(ActiveFedora::Fedora.instance.connection).to receive(:head).with(url_prefix).and_return(failure_response)
expect(subject.ping).to eq("Fedora is Down")
end
end

it 'is false if the service is down' do
allow(ActiveFedora::Fedora.instance.connection).to receive(:head).with(
ActiveFedora::Fedora.instance.connection.connection.http.url_prefix.to_s
).and_raise(RuntimeError)
expect(subject.ping).to be_falsey
context 'when an error occurs' do
it 'returns a custom error message with the error details' do
error_message = "Network error"
allow(ActiveFedora::Fedora.instance.connection).to receive(:head).and_raise(StandardError.new(error_message))
expect(subject.ping).to eq("Error checking Fedora status: #{error_message}")
end
end
end
end

0 comments on commit 56f391e

Please sign in to comment.