Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Prevent failure on reindexing when given APO or collection does not e…
Browse files Browse the repository at this point in the history
…xist.
  • Loading branch information
Tommy Ingulfsen committed Feb 16, 2016
1 parent 8b7a3f9 commit 3dff683
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
13 changes: 9 additions & 4 deletions lib/dor/models/identifiable.rb
Expand Up @@ -233,10 +233,15 @@ def solrize_related_obj_titles(solr_doc, relationships, title_hash, union_field_

# populate cache if necessary
unless title_hash.key?(rel_druid)
related_obj = Dor.find(rel_druid)
related_obj_title = get_related_obj_display_title(related_obj, rel_druid)
is_from_hydrus = (related_obj && related_obj.tags.include?('Project : Hydrus'))
title_hash[rel_druid] = {'related_obj_title' => related_obj_title, 'is_from_hydrus' => is_from_hydrus}
begin
related_obj = Dor.find(rel_druid)
related_obj_title = get_related_obj_display_title(related_obj, rel_druid)
is_from_hydrus = (related_obj && related_obj.tags.include?('Project : Hydrus'))
title_hash[rel_druid] = {'related_obj_title' => related_obj_title, 'is_from_hydrus' => is_from_hydrus}
rescue ActiveFedora::ObjectNotFoundError
# This may happen if the given APO or Collection does not exist (bad data)
title_hash[rel_druid] = {'related_obj_title' => rel_druid, 'is_from_hydrus' => false}
end
end

# cache should definitely be populated, so just use that to write solr field
Expand Down
41 changes: 27 additions & 14 deletions spec/dor/identifiable_spec.rb
Expand Up @@ -8,6 +8,18 @@ class IdentifiableItem < ActiveFedora::Base
before(:each) { stub_config }
after(:each) { unstub_config }

before(:all) do
@mock_rel_druid = 'druid:does_not_exist'
@mock_rels_ext_xml = %(<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<fedora-model:hasModel rdf:resource="info:fedora/testObject"/>
<hydra:isGovernedBy rdf:resource="info:fedora/#{@mock_rel_druid}"/>
<fedora:isMemberOfCollection rdf:resource="info:fedora/#{@mock_rel_druid}"/>
</rdf:Description>
</rdf:RDF>)
end

let(:item) do
item = instantiate_fixture('druid:ab123cd4567', IdentifiableItem)
allow(item).to receive(:new?).and_return(false)
Expand Down Expand Up @@ -192,29 +204,30 @@ class IdentifiableItem < ActiveFedora::Base

describe 'to_solr' do
it 'should generate collection and apo title fields' do
mock_apo_druid = 'druid:fg890hi1234'
mock_rels_ext_xml = %(<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<fedora-model:hasModel rdf:resource="info:fedora/testObject"/>
<hydra:isGovernedBy rdf:resource="info:fedora/#{mock_apo_druid}"/>
</rdf:Description>
</rdf:RDF>)

allow(item.datastreams['RELS-EXT']).to receive(:content).and_return(mock_rels_ext_xml)
allow(Dor).to receive(:find).with(mock_apo_druid).and_return(nil)

allow(item.datastreams['RELS-EXT']).to receive(:content).and_return(@mock_rels_ext_xml)
allow(Dor).to receive(:find).with(@mock_rel_druid).and_raise(ActiveFedora::ObjectNotFoundError)
doc = item.to_solr

['apo_title', 'nonhydrus_apo_title'].each do |field_name|
expect(doc[Solrizer.solr_name(field_name, :symbol)].first).to eq(mock_apo_druid)
expect(doc[Solrizer.solr_name(field_name, :stored_searchable)].first).to eq(mock_apo_druid)
expect(doc[Solrizer.solr_name(field_name, :symbol)].first).to eq(@mock_rel_druid)
expect(doc[Solrizer.solr_name(field_name, :stored_searchable)].first).to eq(@mock_rel_druid)
end
end
it 'should index metadata source' do
expect(item.to_solr).to match a_hash_including('metadata_source_ssi' => 'Symphony')
end
it 'should generate set collection and apo fields to the druid if the collection or apo does not exist' do
allow(item.datastreams['RELS-EXT']).to receive(:content).and_return(@mock_rels_ext_xml)
allow(Dor).to receive(:find).with(@mock_rel_druid).and_raise(ActiveFedora::ObjectNotFoundError)
doc = item.to_solr

['apo_title', 'collection_title'].each do |field_name|
expect(doc[Solrizer.solr_name(field_name, :symbol)].first).to eq(@mock_rel_druid)
expect(doc[Solrizer.solr_name(field_name, :stored_searchable)].first).to eq(@mock_rel_druid)
end
end
end

describe 'get_related_obj_display_title' do
it 'should return the dc:title if it is available' do
mock_apo_title = 'apo title'
Expand Down

0 comments on commit 3dff683

Please sign in to comment.