diff --git a/app/models/fcrepo_endpoint.rb b/app/models/fcrepo_endpoint.rb index 6b9031dc..704c18f3 100644 --- a/app/models/fcrepo_endpoint.rb +++ b/app/models/fcrepo_endpoint.rb @@ -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 + "Error checking Fedora status: 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 diff --git a/spec/models/fcrepo_endpoint_spec.rb b/spec/models/fcrepo_endpoint_spec.rb index f20e1f7c..1061ece1 100644 --- a/spec/models/fcrepo_endpoint_spec.rb +++ b/spec/models/fcrepo_endpoint_spec.rb @@ -1,31 +1,43 @@ # frozen_string_literal: true RSpec.describe FcrepoEndpoint do + subject { described_class.new base_path: base_path } + let(:base_path) { 'foobar' } 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("Error checking Fedora status: 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