Skip to content

Commit

Permalink
more powerful ActiveFedora::FixityService
Browse files Browse the repository at this point in the history
The fedora fixity service also reports the expected message
digest and file size. It makes sense to allow these to be accessed.

Actually cache the http response. Comment erroneously implied
it was cached before, but it wasn't. #check still forces a new
check, as it did before this commit, but verified? is a more
clear method name for return value, and will not bust response
cache.

Cleaned up some erroneous comments.
  • Loading branch information
jrochkind committed May 4, 2017
1 parent 6118ae3 commit b78c788
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
41 changes: 36 additions & 5 deletions lib/active_fedora/fixity_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,57 @@ module ActiveFedora
class FixityService
extend ActiveSupport::Concern

attr_accessor :target, :response
attr_accessor :target

# @param [String, RDF::URI] target url for a Fedora resource
# @param [String, RDF::URI] target url for a Fedora resource.
def initialize(target)
raise ArgumentError, 'You must provide a uri' unless target
@target = target.to_s
end

# Executes a fixity check on Fedora and saves the Faraday::Response.
def response
@response ||= fixity_response_from_fedora
end

# For backwards compat, check always insists on doing a new request.
# you might want verified? instead which uses a cached request.
# @return true or false
def check
@response = fixity_response_from_fedora
@response = nil
verified?
end

# Executes a fixity check on Fedora
# @return true or false
def verified?
status.include?(success)
end

# An array of 1 or more literals reported by Fedora.
# See 'success' for which one indicates fixity check is good.
def status
fixity_graph.query(predicate: premis_status_predicate).map(&:object) +
fixity_graph.query(predicate: fedora_status_predicate).map(&:object)
end

# the currently calculated checksum, as a string URI, like
# "urn:sha1:09a848b79f86f3a4f3f301b8baafde455d6f8e0e"
def expected_message_digest
fixity_graph.query(predicate: ::RDF::Vocab::PREMIS.hasMessageDigest).first.try(:object).try(:to_s)
end

# integer, as reported by fedora. bytes maybe?
def expected_size
fixity_graph.query(predicate: ::RDF::Vocab::PREMIS.hasSize).first.try(:object).try(:to_s).try(:to_i)
end

# Fedora response as an ::RDF::Graph object. Public API, so consumers
# can do with it what they will, especially if future fedora versions
# add more things to it.
def response_graph
fixity_graph
end

private

def premis_status_predicate
Expand All @@ -46,7 +77,7 @@ def fixity_response_from_fedora
end

def fixity_graph
::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(response.body)
@fixity_graph ||= ::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(response.body)
end

# See https://jira.duraspace.org/browse/FCREPO-1247
Expand Down
68 changes: 64 additions & 4 deletions spec/unit/fixity_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@
let(:service) { described_class.new(uri) }
let(:uri) { RDF::URI("http://path/to/resource") }

let(:passing_fedora44_response_body) {
<<-EOF
@prefix premis: <http://www.loc.gov/premis/rdf/v1#> .
<http://127.0.0.1:8080/rest/dev/0k/22/5b/04/0k225b04p/files/9f296a1f-10e7-44a3-83eb-4811d611edc6/fcr:versions/version1> premis:hasFixity <http://127.0.0.1:8080/rest/dev/0k/22/5b/04/0k225b04p/files/9f296a1f-10e7-44a3-83eb-4811d611edc6/fcr:versions/version1#fixity/1493843767961> .
<http://127.0.0.1:8080/rest/dev/0k/22/5b/04/0k225b04p/files/9f296a1f-10e7-44a3-83eb-4811d611edc6/fcr:versions/version1#fixity/1493843767961> a premis:Fixity , premis:EventOutcomeDetail ;
premis:hasEventOutcome "SUCCESS"^^<http://www.w3.org/2001/XMLSchema#string> ;
premis:hasMessageDigest <urn:sha1:b995eef5262dd1c74f0ed9c96be1f404394d45dc> ;
premis:hasSize "103945"^^<http://www.w3.org/2001/XMLSchema#long> .
EOF
}

let(:failing_fedora44_response_body) {
<<-EOF
@prefix premis: <http://www.loc.gov/premis/rdf/v1#> .
<http://127.0.0.1:8080/rest/dev/ks/65/hc/20/ks65hc20t/files/e316b4b5-4627-44f8-9fdb-d2016e0e7380/fcr:versions/version3> premis:hasFixity <http://127.0.0.1:8080/rest/dev/ks/65/hc/20/ks65hc20t/files/e316b4b5-4627-44f8-9fdb-d2016e0e7380/fcr:versions/version3#fixity/1493844791463> .
<http://127.0.0.1:8080/rest/dev/ks/65/hc/20/ks65hc20t/files/e316b4b5-4627-44f8-9fdb-d2016e0e7380/fcr:versions/version3#fixity/1493844791463> a premis:Fixity , premis:EventOutcomeDetail ;
premis:hasEventOutcome "BAD_CHECKSUM"^^<http://www.w3.org/2001/XMLSchema#string> , "BAD_SIZE"^^<http://www.w3.org/2001/XMLSchema#string> ;
premis:hasMessageDigest <urn:sha1:1a89571e25dd372563a10740a883e93f8af2d146> ;
premis:hasSize "1878582"^^<http://www.w3.org/2001/XMLSchema#long> .
EOF
}

describe "the instance" do
subject { described_class.new(uri) }
it { is_expected.to respond_to(:response) }
Expand All @@ -22,21 +48,21 @@
end
end

describe "#check" do
describe "#verified?" do
before { allow(service).to receive(:fixity_response_from_fedora).and_return(response) }
subject { service.check }
subject { service.verified? }

context "with Fedora version >= 4.4.0" do
context "with a passing result" do
let(:response) do
instance_double("Response", body: '<subject> <http://www.loc.gov/premis/rdf/v1#hasEventOutcome> "SUCCESS"^^<http://www.w3.org/2001/XMLSchema#string> .')
instance_double("Response", body: passing_fedora44_response_body)
end
it { is_expected.to be true }
end

context "with a failing result" do
let(:response) do
instance_double("Response", body: '<subject> <http://www.loc.gov/premis/rdf/v1#hasEventOutcome> "BAD_CHECKSUM"^^<http://www.w3.org/2001/XMLSchema#string> .')
instance_double("Response", body: failing_fedora44_response_body)
end
it { is_expected.to be false }
end
Expand Down Expand Up @@ -65,4 +91,38 @@
it { is_expected.to be false }
end
end

describe "expected_message_digest" do
before { allow(service).to receive(:fixity_response_from_fedora).and_return(response) }
subject { service.expected_message_digest }
context "with success response" do
let(:response) do
instance_double("Response", body: passing_fedora44_response_body)
end
it { is_expected.to match(/urn:sha1:[a-f0-9]+/) }
end
context "with failure response" do
let(:response) do
instance_double("Response", body: failing_fedora44_response_body)
end
it { is_expected.to match(/urn:sha1:[a-f0-9]+/) }
end
end

describe "expected_size" do
before { allow(service).to receive(:fixity_response_from_fedora).and_return(response) }
subject { service.expected_size }
context "with success response" do
let(:response) do
instance_double("Response", body: passing_fedora44_response_body)
end
it { is_expected.to be_kind_of Numeric }
end
context "with failure response" do
let(:response) do
instance_double("Response", body: failing_fedora44_response_body)
end
it { is_expected.to be_kind_of Numeric }
end
end
end

0 comments on commit b78c788

Please sign in to comment.