Skip to content

Commit

Permalink
Merge pull request #4843 from sul-dlss/repo-object-find
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Apr 11, 2024
2 parents 4ab87f3 + eaafe24 commit 376c355
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
27 changes: 24 additions & 3 deletions app/services/cocina_object_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,34 @@ def find(druid)
# TODO: After migration, remove the nil-checks
cocina = RepositoryObject.find_by(external_identifier: druid)&.head_version&.to_cocina_with_metadata

cocina || ar_find(druid).to_cocina_with_metadata
if cocina
return cocina unless Settings.enabled_features.repository_object_test

legacy = ar_find(druid).to_cocina_with_metadata
return cocina if legacy == cocina

Honeybadger.notify('Comparison of RepositoryObject with legacy object failed.', context: { druid: })
return legacy
end

ar_find(druid).to_cocina_with_metadata
end

def find_by_source_id(source_id)
# TODO: Nil check can be removed after migrating to RepositoryObject
ar_cocina_object = RepositoryObject.find_by(source_id:)&.head_version ||
Dro.find_by_source_id(source_id) ||
cocina = RepositoryObject.find_by(source_id:)&.head_version&.to_cocina_with_metadata

if cocina
return cocina unless Settings.enabled_features.repository_object_test

legacy = (Dro.find_by_source_id(source_id) || Collection.find_by_source_id(source_id)).to_cocina_with_metadata
return cocina if legacy == cocina

Honeybadger.notify('Comparison of RepositoryObject with legacy object failed.', context: { source_id: })
return legacy
end

ar_cocina_object = Dro.find_by_source_id(source_id) ||
Collection.find_by_source_id(source_id)

raise CocinaObjectNotFoundError unless ar_cocina_object
Expand Down
77 changes: 73 additions & 4 deletions spec/services/cocina_object_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@
expect(store.find(repo_object.external_identifier)).to be_instance_of(Cocina::Models::DROWithMetadata)
end
end

context 'when enabled_features.repository_object_test is enabled' do
let(:ar_cocina_object) { create(:ar_dro) }
let(:repo_object) { create(:repository_object, external_identifier: ar_cocina_object.external_identifier) }

before do
allow(Settings.enabled_features).to receive(:repository_object_test).and_return true
allow(Honeybadger).to receive(:notify)
repo_object.head_version.update!(version_attributes)
end

context "when objects don't match" do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(build(:dro, id: repo_object.external_identifier)) }

it 'returns Cocina::Models::DRO and reports a diff' do
expect(store.find(repo_object.external_identifier)).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(Honeybadger).to have_received(:notify)
.with('Comparison of RepositoryObject with legacy object failed.',
context: { druid: ar_cocina_object.external_identifier })
end
end

context 'when objects match' do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(ar_cocina_object.to_cocina) }

it "doesn't report a diff" do
expect(store.find(repo_object.external_identifier)).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(Honeybadger).not_to have_received(:notify)
end
end
end
end

describe '#version' do
Expand Down Expand Up @@ -125,38 +156,76 @@
end

describe '#find_by_source_id' do
subject(:find_by_source) { store.find_by_source_id(source_id) }

let(:source_id) { ar_cocina_object.identification['sourceId'] }

context 'when object is not found in datastore' do
let(:source_id) { 'sul:abc123' }

it 'raises' do
expect { store.find('sul:abc123') }.to raise_error(CocinaObjectStore::CocinaObjectNotFoundError)
expect { find_by_source }.to raise_error(CocinaObjectStore::CocinaObjectNotFoundError)
end
end

context 'when object is a DRO' do
let(:ar_cocina_object) { create(:ar_dro) }

it 'returns Cocina::Models::DROWithMetadata' do
expect(store.find_by_source_id(ar_cocina_object.identification['sourceId'])).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(find_by_source).to be_instance_of(Cocina::Models::DROWithMetadata)
end
end

context 'when object is a Collection' do
let(:ar_cocina_object) { create(:ar_collection) }

it 'returns Cocina::Models::Collection' do
expect(store.find_by_source_id(ar_cocina_object.identification['sourceId'])).to be_instance_of(Cocina::Models::CollectionWithMetadata)
expect(find_by_source).to be_instance_of(Cocina::Models::CollectionWithMetadata)
end
end

context 'when object is a RepositoryObject' do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(build(:dro, id: repo_object.external_identifier)) }
let(:repo_object) { create(:repository_object) }
let(:source_id) { repo_object.head_version.identification['sourceId'] }

before do
repo_object.head_version.update!(version_attributes)
end

it 'returns Cocina::Models::DRO' do
expect(store.find_by_source_id(repo_object.head_version.identification['sourceId'])).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(find_by_source).to be_instance_of(Cocina::Models::DROWithMetadata)
end
end

context 'when enabled_features.repository_object_test is enabled' do
let(:ar_cocina_object) { create(:ar_dro) }
let(:repo_object) { create(:repository_object, external_identifier: ar_cocina_object.external_identifier) }

before do
allow(Settings.enabled_features).to receive(:repository_object_test).and_return true
allow(Honeybadger).to receive(:notify)
repo_object.head_version.update!(version_attributes)
end

context "when objects don't match" do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(build(:dro, id: repo_object.external_identifier, source_id:)) }

it 'returns Cocina::Models::DRO and reports a diff' do
expect(find_by_source).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(Honeybadger).to have_received(:notify)
.with('Comparison of RepositoryObject with legacy object failed.',
context: { source_id: })
end
end

context 'when objects match' do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(ar_cocina_object.to_cocina) }

it "doesn't report a diff" do
expect(find_by_source).to be_instance_of(Cocina::Models::DROWithMetadata)
expect(Honeybadger).not_to have_received(:notify)
end
end
end
end
Expand Down

0 comments on commit 376c355

Please sign in to comment.