Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API endpoint for handling legacy metadata
This can be used by accessionWF robots so they don't need to make Fedora calls
- Loading branch information
Showing
7 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
# Operations on the legacy Fedora 3 metadata. | ||
# This is used by the accessionWF | ||
class LegacyMetadataService | ||
# If the updated value is newer than then createDate of the datastream, then update it. | ||
def self.update_datastream_if_newer(datastream:, updated:, content:) | ||
datastream.content = content if !datastream.createDate || updated > datastream.createDate | ||
return if !datastream.createDate || updated > datastream.createDate | ||
|
||
Honeybadger.notify("Found #{datasteam.pid}/#{datastream.dsid} that had a create " \ | ||
"date (#{datastream.createDate}) after the file was modified (#{updated}). " \ | ||
'Doing an experiment to see if this ever happens.') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Update the legacy (datastream) metadata' do | ||
let(:work) { instance_double(Dor::Item, pid: 'druid:bc123df4567', datastreams: datastreams, save!: nil) } | ||
let(:create_date) { Time.zone.parse('2019-08-09T19:18:15Z') } | ||
let(:descMetadata) { instance_double(Dor::DescMetadataDS, createDate: create_date) } | ||
let(:rightsMetadata) { instance_double(Dor::RightsMetadataDS, createDate: create_date) } | ||
let(:technicalMetadata) { instance_double(Dor::TechnicalMetadataDS, createDate: create_date) } | ||
let(:contentMetadata) { instance_double(Dor::ContentMetadataDS, createDate: create_date) } | ||
|
||
let(:datastreams) do | ||
{ | ||
'descMetadata' => descMetadata, | ||
'rightsMetadata' => rightsMetadata, | ||
'technicalMetadata' => technicalMetadata, | ||
'contentMetadata' => contentMetadata | ||
} | ||
end | ||
|
||
context 'when update is successful' do | ||
before do | ||
allow(Dor).to receive(:find).and_return(work) | ||
allow(LegacyMetadataService).to receive(:update_datastream_if_newer) | ||
end | ||
|
||
let(:data) do | ||
<<~JSON | ||
{ | ||
"descriptive": { | ||
"updated": "2019-11-08T15:15:43Z", | ||
"content": "<descMetadata></descMetadata>" | ||
}, | ||
"rights": { | ||
"updated": "2019-11-08T15:15:43Z", | ||
"content": "<rightsMetadata></rightsMetadata>" | ||
} | ||
} | ||
JSON | ||
end | ||
|
||
it 'updates the object datastreams' do | ||
patch "/v1/objects/#{work.pid}/metadata/legacy", | ||
params: data, | ||
headers: { 'Authorization' => "Bearer #{jwt}", 'CONTENT_TYPE' => 'application/json' } | ||
expect(response).to have_http_status(:no_content) | ||
|
||
expect(LegacyMetadataService).to have_received(:update_datastream_if_newer) | ||
.with(datastream: descMetadata, | ||
updated: Time.zone.parse('2019-11-08T15:15:43Z'), | ||
content: '<descMetadata></descMetadata>') | ||
|
||
expect(LegacyMetadataService).to have_received(:update_datastream_if_newer) | ||
.with(datastream: rightsMetadata, | ||
updated: Time.zone.parse('2019-11-08T15:15:43Z'), | ||
content: '<rightsMetadata></rightsMetadata>') | ||
|
||
expect(work).to have_received(:save!) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe LegacyMetadataService do | ||
describe '.update_datastream_if_newer' do | ||
subject(:update) { described_class.update_datastream_if_newer(datastream: datastream, updated: updated, content: content) } | ||
|
||
let(:updated) { Time.zone.parse('2019-08-09T19:18:15Z') } | ||
let(:content) { '<descMetadata><foo/></descMetadata>' } | ||
let(:datastream) { instance_double(Dor::DescMetadataDS, createDate: create_date, :content= => nil) } | ||
|
||
context 'with a new datastream' do | ||
let(:create_date) { nil } | ||
|
||
it 'updates the content' do | ||
update | ||
expect(datastream).to have_received(:content=).with(content) | ||
end | ||
end | ||
|
||
context 'with a datastream that is older than the content' do | ||
let(:create_date) { Time.zone.parse('2019-07-09T19:18:15Z') } | ||
|
||
it 'updates the content' do | ||
update | ||
expect(datastream).to have_received(:content=).with(content) | ||
end | ||
end | ||
end | ||
end |