require 'spec_helper'
describe ActiveFedora::RelsExtDatastream do
describe "short_predicate" do
it 'should parse' do
ActiveFedora::RelsExtDatastream.short_predicate('http://www.openarchives.org/OAI/2.0/itemID').should == :oai_item_id
end
end
before(:all) do
@pid = "test:sample_pid"
@sample_xml = Nokogiri::XML::Document.parse(@sample_xml_string)
end
before(:each) do
mock_inner = double('inner object')
@mock_repo = double('repository')
@mock_repo.stub(:datastream_dissemination=>'My Content', :config=>{})
mock_inner.stub(:repository).and_return(@mock_repo)
mock_inner.stub(:pid).and_return(@pid)
@test_ds = ActiveFedora::RelsExtDatastream.new(mock_inner, "RELS-EXT")
@test_ds.stub(:profile).and_return({})
end
its(:metadata?) { should be_true}
its(:controlGroup) { should == "X"}
describe "#mimeType" do
it 'should use the application/rdf+xml mime type' do
@test_ds.mimeType.should == 'application/rdf+xml'
end
end
describe "#changed?" do
it "should be false when no changes have been made" do
subject.changed?.should == false
end
it "should be true when the model has changes" do
subject.model = double(:relationships_are_dirty=>true)
subject.changed?.should == true
end
end
describe '#serialize!' do
it "should generate new rdf/xml as the datastream content if the object has been changed" do
graph = RDF::Graph.new
subject = RDF::URI.new "info:fedora/test:sample_pid"
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:is_member_of), RDF::URI.new('demo:10'))
@test_ds.stub(:new? => true, :relationships_are_dirty? =>true, :relationships => graph, :model => double(:relationships_are_dirty= => true))
@test_ds.serialize!
EquivalentXml.equivalent?(@test_ds.content, "\n \n \n ").should be_true
end
end
describe '#to_rels_ext' do
before(:all) do
@sample_rels_ext_xml = <<-EOS
EOS
@pid = "test:sample_pid"
end
it 'should serialize the relationships array to Fedora RELS-EXT rdf/xml' do
graph = RDF::Graph.new
subject = RDF::URI.new "info:fedora/test:sample_pid"
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:is_member_of), RDF::URI.new('info:fedora/demo:10'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:is_part_of), RDF::URI.new('info:fedora/demo:11'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_part), RDF::URI.new('info:fedora/demo:12'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_model), RDF::URI.new("info:fedora/afmodel:OtherModel"))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_model), RDF::URI.new("info:fedora/afmodel:SampleModel"))
@test_ds.should_receive(:model).and_return(double("model", :relationships=>graph, :relationships_are_dirty= => true))
EquivalentXml.equivalent?(@test_ds.to_rels_ext(), @sample_rels_ext_xml).should be_true
end
it 'should use mapped namespace prefixes when given' do
graph = RDF::Graph.new
subject = RDF::URI.new "info:fedora/test:sample_pid"
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:is_member_of), RDF::URI.new('info:fedora/demo:10'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:is_part_of), RDF::URI.new('info:fedora/demo:11'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_part), RDF::URI.new('info:fedora/demo:12'))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_model), RDF::URI.new("info:fedora/afmodel:OtherModel"))
graph.insert RDF::Statement.new(subject, ActiveFedora::Predicates.find_graph_predicate(:has_model), RDF::URI.new("info:fedora/afmodel:SampleModel"))
@test_ds.stub(:model).and_return(double("model", :relationships=>graph, :relationships_are_dirty= => true))
rels = @test_ds.to_rels_ext()
EquivalentXml.equivalent?(rels, @sample_rels_ext_xml).should be_true
rels.should_not =~ /fedora:isMemberOf/
rels.should_not =~ /fedora-model:hasModel/
rels.should =~ /ns\d:isMemberOf/
rels.should =~ /ns\d:hasModel/
ActiveFedora::Predicates.predicate_config[:predicate_namespaces] = {:"fedora-model"=>"info:fedora/fedora-system:def/model#", :fedora=>"info:fedora/fedora-system:def/relations-external#"}
rels = @test_ds.to_rels_ext()
EquivalentXml.equivalent?(rels, @sample_rels_ext_xml).should be_true
rels.should =~ /fedora:isMemberOf/
rels.should =~ /fedora-model:hasModel/
rels.should_not =~ /ns\d:isMemberOf/
rels.should_not =~ /ns\d:hasModel/
ActiveFedora::Predicates.predicate_config[:predicate_namespaces] = nil
end
end
describe "#from_xml" do
before(:all) do
@test_obj = ActiveFedora::Base.new
@test_obj.add_relationship(:is_member_of, "info:fedora/demo:10")
@test_obj.add_relationship(:is_part_of, "info:fedora/demo:11")
@test_obj.add_relationship(:conforms_to, "AnInterface", true)
@test_obj.save
end
after(:all) do
@test_obj.delete
end
it "should handle un-mapped predicates gracefully" do
@test_obj.add_relationship("foo", "info:fedora/foo:bar")
@test_obj.save
@test_obj.relationships.size.should == 5
@test_obj.ids_for_outbound("foo").should == ["foo:bar"]
end
it "should automatically map un-mapped predicates" do
xml = <<-EOS
EOS
model = ActiveFedora::Base.new
new_ds = ActiveFedora::RelsExtDatastream.new
new_ds.model = model
lambda { ActiveFedora::RelsExtDatastream.from_xml(xml, new_ds) }.should_not raise_exception
new_ds.to_rels_ext.should =~ /missing:hasOtherRelationship/
end
it "should handle un-mapped literals" do
xml = "
oai:hull.ac.uk:hull:2708
"
model = ActiveFedora::Base.new
new_ds = ActiveFedora::RelsExtDatastream.new
new_ds.model = model
ActiveFedora::RelsExtDatastream.from_xml(xml, new_ds)
new_ext = new_ds.to_rels_ext()
new_ext.should match "oai:hull.ac.uk:hull:2708"
end
end
describe "#short_predicate" do
before(:all) do
@original_mapping = ActiveFedora::Predicates.predicate_config[:predicate_mapping]
end
after(:all) do
ActiveFedora::Predicates.predicate_config[:predicate_mapping] = @original_mapping
end
it "should find predicates regardless of order loaded or shared namespace prefixes" do
ActiveFedora::Predicates.predicate_config[:predicate_mapping] = {
"http://example.org/"=>{:ceo => 'Manager'},
"http://example.org/zoo/wolves/"=>{:alpha => 'Manager'},
"http://example.org/zoo/"=>{:keeper => 'Manager'}
}
ActiveFedora::RelsExtDatastream.short_predicate("http://example.org/zoo/Manager").should == :keeper
ActiveFedora::RelsExtDatastream.short_predicate("http://example.org/zoo/wolves/Manager").should == :alpha
ActiveFedora::RelsExtDatastream.short_predicate("http://example.org/Manager").should == :ceo
end
end
end