Permalink
Browse files

Run a request for dsChecksumValid lazily when you call the dsChecksum…

…Valid method
  • Loading branch information...
jcoyne committed Jan 27, 2012
1 parent 97e2f6c commit 92b05c602dba657064f9c2cdad55879877dbfbcd
Showing with 79 additions and 11 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 History.textile
  3. +14 −7 lib/rubydora/datastream.rb
  4. +2 −0 lib/rubydora/rest_api_client.rb
  5. +59 −4 spec/lib/datastream_spec.rb
View
@@ -43,3 +43,4 @@ Gemfile.lock
#
# For vim:
*.swp
+*.swo
View
@@ -1,3 +1,6 @@
+h3. 0.5.2
+* Force checksum query when you call dsChecksumValid, unless it's already in the profile.
+
h3. 0.5.1
* symbolize keys that are passed as config options
View
@@ -43,7 +43,7 @@ def #{attribute.to_s}
}
def dsChecksumValid
- profile['dsChecksumValid']
+ profile(:validateChecksum=>true)['dsChecksumValid']
end
end
@@ -140,20 +140,27 @@ def content_will_change!
end
# Retrieve the datastream profile as a hash (and cache it)
+ # @param opts [Hash] :validateChecksum if you want fedora to validate the checksum
# @return [Hash] see Fedora #getDatastream documentation for keys
- def profile profile_xml = nil
- @profile ||= begin
+ def profile opts= {}
+ if @profile && !(opts[:validateChecksum] && !@profile.has_key?('dsChecksumValid'))
+ ## Force a recheck of the profile if they've passed :validateChecksum and we don't have dsChecksumValid
+ return @profile
+ end
+ @profile = begin
options = { :pid => pid, :dsid => dsid }
+ options.merge!(opts)
options[:asOfDateTime] = asOfDateTime if asOfDateTime
options[:validateChecksum] = true if repository.config[:validateChecksum]
- profile_xml ||= repository.datastream(options)
- self.profile_xml_to_hash(profile_xml)
-
+ self.profile_xml_to_hash(repository.datastream(options))
rescue
{}
end
end
- alias_method :profile=, :profile
+
+ def profile= profile_xml
+ @profile = self.profile_xml_to_hash(profile_xml)
+ end
def profile_xml_to_hash profile_xml
profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"' unless profile_xml =~ /xmlns=/
@@ -181,6 +181,8 @@ def object_xml options = {}
# @param [Hash] options
# @option options [String] :pid
# @option options [String] :dsid
+ # @option options [String] :asOfDateTime
+ # @option options [String] :validateChecksum
# @return [String]
def datastream options = {}
pid = options.delete(:pid)
@@ -2,7 +2,7 @@
describe Rubydora::Datastream do
before do
- @mock_repository = mock(Rubydora::Repository, :config=>{:validateChecksum=>true})
+ @mock_repository = mock(Rubydora::Repository, :config=>{})
@mock_object = mock(Rubydora::DigitalObject)
@mock_object.stub(:repository => @mock_repository, :pid => 'pid')
end
@@ -47,15 +47,15 @@
@datastream = Rubydora::Datastream.new @mock_object, 'dsid'
end
it "should be nil when it hasn't been set" do
- @mock_repository.should_receive(:datastream).any_number_of_times.and_return <<-XML
+ @mock_repository.should_receive(:datastream).with(hash_including(:validateChecksum => true)).and_return <<-XML
<datastreamProfile>
</datastreamProfile>
XML
@datastream.dsChecksumValid.should be_nil
end
it "should be true when it's returned as true" do
- @mock_repository.should_receive(:datastream).any_number_of_times.and_return <<-XML
+ @mock_repository.should_receive(:datastream).with(hash_including(:validateChecksum => true)).and_return <<-XML
<datastreamProfile>
<dsChecksumValid>true</dsChecksumValid>
</datastreamProfile>
@@ -64,7 +64,7 @@
end
it "should be false when it's returned as false" do
- @mock_repository.should_receive(:datastream).any_number_of_times.and_return <<-XML
+ @mock_repository.should_receive(:datastream).with(hash_including(:validateChecksum => true)).and_return <<-XML
<datastreamProfile>
<dsChecksumValid>false</dsChecksumValid>
</datastreamProfile>
@@ -373,7 +373,62 @@
end
end
+ describe "profile=" do
+ before(:each) do
+ @datastream = Rubydora::Datastream.new @mock_object, 'dsid'
+ end
+ it "should set the profile" do
+ prof = <<-XML
+ <datastreamProfile>
+ <dsChecksumValid>true</dsChecksumValid>
+ </datastreamProfile>
+ XML
+ @datastream.profile = prof
+ @datastream.profile.should == {'dsChecksumValid' =>true}
+ end
+ end
+
describe "profile" do
+ before(:each) do
+ @datastream = Rubydora::Datastream.new @mock_object, 'dsid'
+ end
+ it "should accept a validateChecksum argument" do
+ @mock_repository.should_receive(:datastream).with(hash_including(:validateChecksum => true)).and_return <<-XML
+ <datastreamProfile>
+ <dsChecksumValid>true</dsChecksumValid>
+ </datastreamProfile>
+ XML
+ @datastream.profile(:validateChecksum=>true).should == {'dsChecksumValid' =>true}
+ end
+ describe "once it has a profile" do
+ it "should use the profile from cache" do
+ @mock_repository.should_receive(:datastream).once.and_return <<-XML
+ <datastreamProfile>
+ <dsChecksumValid>true</dsChecksumValid>
+ </datastreamProfile>
+ XML
+ @datastream.profile().should == {'dsChecksumValid' =>true}
+ #second time should not trigger the mock, which demonstrates that the profile is coming from cache.
+ @datastream.profile().should == {'dsChecksumValid' =>true}
+ end
+ it "should re-fetch and replace the profile when validateChecksum is passed in, and there is no dsChecksumValid in the existing profile" do
+ @mock_repository.should_receive(:datastream).once.and_return <<-XML
+ <datastreamProfile>
+ <dsLabel>The description of the content</dsLabel>
+ </datastreamProfile>
+ XML
+ @mock_repository.should_receive(:datastream).with(hash_including(:validateChecksum => true)).once.and_return <<-XML
+ <datastreamProfile>
+ <dsLabel>The description of the content</dsLabel>
+ <dsChecksumValid>true</dsChecksumValid>
+ </datastreamProfile>
+ XML
+ @datastream.profile().should == {"dsLabel"=>"The description of the content"}
+ @datastream.profile(:validateChecksum=>true).should == {"dsLabel"=>"The description of the content", 'dsChecksumValid' =>true}
+ ## Third time should not trigger a mock, which demonstrates that the profile is coming from cache.
+ @datastream.profile(:validateChecksum=>true)
+ end
+ end
end

0 comments on commit 92b05c6

Please sign in to comment.