diff --git a/.rubocop.yml b/.rubocop.yml index f8ad16c32..e251a0b97 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -32,7 +32,6 @@ Layout/SpaceAroundEqualsInParameterDefault: Metrics/BlockLength: Exclude: - - 'app/services/preserved_object_handler.rb' # FIXME check_existence shameless green - '**/*.rake' - 'spec/**/*' @@ -48,8 +47,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 25 - Exclude: - - 'app/services/preserved_object_handler.rb' Naming/FileName: Exclude: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 11e6f6fe7..66a607995 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-12-03 10:39:19 -0800 using RuboCop version 0.73.0. +# on 2019-12-17 13:40:54 -0500 using RuboCop version 0.77.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -41,9 +41,9 @@ Lint/AmbiguousBlockAssociation: - 'spec/services/preserved_object_handler_update_version_spec.rb' - 'spec/services/shared_examples_preserved_object_handler.rb' -# Offense count: 21 +# Offense count: 25 Metrics/AbcSize: - Max: 69 + Max: 75 # Offense count: 2 # Configuration parameters: CountBlocks. @@ -53,15 +53,20 @@ Metrics/BlockNesting: # Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 262 + Max: 280 # Offense count: 3 Metrics/CyclomaticComplexity: - Max: 11 + Max: 12 + +# Offense count: 2 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/MethodLength: + Max: 37 # Offense count: 3 Metrics/PerceivedComplexity: - Max: 15 + Max: 14 # Offense count: 1 Naming/AccessorMethodName: @@ -92,6 +97,20 @@ Style/Documentation: - 'app/models/application_record.rb' - 'config/application.rb' +# Offense count: 69 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: annotated, template, unannotated +Style/FormatStringToken: + Exclude: + - 'app/models/druid_version_zip.rb' + - 'app/services/audit_results.rb' + - 'config/routes.rb' + - 'spec/factories/moab_storage_root.rb' + - 'spec/factories/preserved_object.rb' + - 'spec/factories/zip_endpoints.rb' + - 'spec/factories/zip_parts.rb' + - 'spec/jobs/zipmaker_job_spec.rb' + # Offense count: 1 # Configuration parameters: MinBodyLength. Style/GuardClause: diff --git a/app/lib/audit/catalog_to_moab.rb b/app/lib/audit/catalog_to_moab.rb index 332d042d8..4f1c78da1 100644 --- a/app/lib/audit/catalog_to_moab.rb +++ b/app/lib/audit/catalog_to_moab.rb @@ -27,18 +27,13 @@ def check_catalog_version end unless online_moab_found? - transaction_ok = ActiveRecordUtils.with_transaction_and_rescue(results) do - update_status('online_moab_not_found') - complete_moab.save! - end - results.remove_db_updated_results unless transaction_ok + handle_missing_moab + + # Check if it moved to another location + MoabMovedHandler.new(complete_moab, results).check_and_handle_moved_moab - results.add_result(AuditResults::MOAB_NOT_FOUND, - db_created_at: complete_moab.created_at.iso8601, - db_updated_at: complete_moab.updated_at.iso8601) return results.report_results(logger) end - return results.report_results(logger) unless can_validate_current_comp_moab_status? compare_version_and_take_action @@ -50,11 +45,6 @@ def storage_location complete_moab.moab_storage_root.storage_location end - def online_moab_found? - return true if moab - false - end - # compare the catalog version to the actual Moab; update the catalog version if the Moab is newer # report results (and return them) def compare_version_and_take_action diff --git a/app/services/audit_results.rb b/app/services/audit_results.rb index 5bb5d6c56..3b681bc01 100644 --- a/app/services/audit_results.rb +++ b/app/services/audit_results.rb @@ -23,6 +23,7 @@ class AuditResults DB_OBJ_ALREADY_EXISTS = :db_obj_already_exists DB_OBJ_DOES_NOT_EXIST = :db_obj_does_not_exist CM_STATUS_CHANGED = :cm_status_changed + CM_STORAGE_ROOT_CHANGED = :cm_storage_root_changed UNEXPECTED_VERSION = :unexpected_version INVALID_MOAB = :invalid_moab CM_PO_VERSION_MISMATCH = :cm_po_version_mismatch @@ -53,6 +54,7 @@ class AuditResults DB_OBJ_ALREADY_EXISTS => "%{addl} db object already exists", DB_OBJ_DOES_NOT_EXIST => "%{addl} db object does not exist", CM_STATUS_CHANGED => "CompleteMoab status changed from %{old_status} to %{new_status}", + CM_STORAGE_ROOT_CHANGED => "CompleteMoab storage root changed from %{old_storage_root} to %{new_storage_root}", UNEXPECTED_VERSION => "actual version (%{actual_version}) has unexpected relationship to %{db_obj_name} db version (%{db_obj_version}); ERROR!", INVALID_MOAB => "Invalid Moab, validation errors: %{addl}", CM_PO_VERSION_MISMATCH => "CompleteMoab online Moab version %{cm_version} does not match PreservedObject current_version %{po_version}", @@ -99,6 +101,7 @@ class AuditResults ].freeze HONEYBADGER_REPORT_CODES = [ + CM_STORAGE_ROOT_CHANGED, MOAB_FILE_CHECKSUM_MISMATCH, ZIP_PART_NOT_FOUND, ZIP_PART_CHECKSUM_MISMATCH, @@ -109,14 +112,15 @@ class AuditResults DB_UPDATED_CODES = [ CREATED_NEW_OBJECT, - CM_STATUS_CHANGED + CM_STATUS_CHANGED, + CM_STORAGE_ROOT_CHANGED ].freeze def self.logger_severity_level(result_code) case result_code when DB_OBJ_DOES_NOT_EXIST, ZIP_PARTS_NOT_CREATED, ZIP_PARTS_NOT_ALL_REPLICATED Logger::WARN - when VERSION_MATCHES, ACTUAL_VERS_GT_DB_OBJ, CREATED_NEW_OBJECT, CM_STATUS_CHANGED, MOAB_CHECKSUM_VALID + when VERSION_MATCHES, ACTUAL_VERS_GT_DB_OBJ, CREATED_NEW_OBJECT, CM_STATUS_CHANGED, CM_STORAGE_ROOT_CHANGED, MOAB_CHECKSUM_VALID Logger::INFO else Logger::ERROR diff --git a/app/services/moab_moved_handler.rb b/app/services/moab_moved_handler.rb new file mode 100644 index 000000000..5fa640fd7 --- /dev/null +++ b/app/services/moab_moved_handler.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +## +# Service for handling a Moab that has moved +class MoabMovedHandler + attr_reader :complete_moab, :results + + def initialize(complete_moab, results) + @complete_moab = complete_moab + @results = results + end + + def check_and_handle_moved_moab + update_moab_storage_root if moved_moab.exist? + end + + private + + def moved_moab + @moved_moab ||= Moab::StorageServices.find_storage_object(complete_moab.preserved_object.druid) + end + + def validation_errors? + object_validator = Stanford::StorageObjectValidator.new(moved_moab) + object_validator.validation_errors(Settings.moab.allow_content_subdirs).any? + end + + def storage_root + @storage_root ||= MoabStorageRoot.find_by!(storage_location: File.join(moved_moab.storage_root.to_path, Settings.moab.storage_trunk)) + end + + def update_status_ok + complete_moab.status = 'ok' + return unless complete_moab.status_changed? + + results.add_result( + AuditResults::CM_STATUS_CHANGED, old_status: complete_moab.status_was, new_status: complete_moab.status + ) + end + + def update_storage_root + old_storage_root = complete_moab.moab_storage_root + complete_moab.moab_storage_root = storage_root + results.add_result( + AuditResults::CM_STORAGE_ROOT_CHANGED, old_storage_root: old_storage_root.storage_location, + new_storage_root: complete_moab.moab_storage_root.storage_location + ) + end + + def update_moab_storage_root + version = nil + transaction_ok = ActiveRecordUtils.with_transaction_and_rescue(results) do + version = complete_moab.version + end + results.remove_db_updated_results unless transaction_ok + # Ensures Moab at the current location matches the catalog in all ways (e.g. version, full validation) except for disk location + return if moved_moab.current_version_id != version + return if validation_errors? + + # Update the disk location in the catalog + transaction_ok = ActiveRecordUtils.with_transaction_and_rescue(results) do + update_storage_root + update_status_ok + complete_moab.save! + end + results.remove_db_updated_results unless transaction_ok + end +end diff --git a/app/services/moab_validation_handler.rb b/app/services/moab_validation_handler.rb index f2bcef808..5657499db 100644 --- a/app/services/moab_validation_handler.rb +++ b/app/services/moab_validation_handler.rb @@ -17,6 +17,10 @@ def moab @moab ||= Moab::StorageObject.new(druid, object_dir) end + def online_moab_found? + moab&.exist? + end + def can_validate_checksums? false end @@ -90,4 +94,16 @@ def set_status_as_seen_on_disk(found_expected_version) update_status('validity_unknown') end end + + def handle_missing_moab + transaction_ok = ActiveRecordUtils.with_transaction_and_rescue(results) do + results.add_result(AuditResults::MOAB_NOT_FOUND, + db_created_at: complete_moab.created_at.iso8601, + db_updated_at: complete_moab.updated_at.iso8601) + + update_status('online_moab_not_found') + complete_moab.save! + end + results.remove_db_updated_results unless transaction_ok + end end diff --git a/app/services/preserved_object_handler.rb b/app/services/preserved_object_handler.rb index 88dcc1b01..c4edc4dd8 100644 --- a/app/services/preserved_object_handler.rb +++ b/app/services/preserved_object_handler.rb @@ -71,45 +71,74 @@ def check_existence if invalid? results.add_result(AuditResults::INVALID_ARGUMENTS, errors.full_messages) - elsif PreservedObject.exists?(druid: druid) - Rails.logger.debug "check_existence #{druid} called" - transaction_ok = with_active_record_transaction_and_rescue do - raise_rollback_if_cm_po_version_mismatch - - return results.report_results unless can_validate_current_comp_moab_status? - - if incoming_version == comp_moab.version - set_status_as_seen_on_disk(true) unless comp_moab.status == 'ok' - results.add_result(AuditResults::VERSION_MATCHES, 'CompleteMoab') - elsif incoming_version > comp_moab.version - set_status_as_seen_on_disk(true) unless comp_moab.status == 'ok' - results.add_result(AuditResults::ACTUAL_VERS_GT_DB_OBJ, db_obj_name: 'CompleteMoab', db_obj_version: comp_moab.version) - if moab_validation_errors.empty? - comp_moab.upd_audstamps_version_size(ran_moab_validation?, incoming_version, incoming_size) - pres_object.current_version = incoming_version - pres_object.save! - else - update_status('invalid_moab') - end - else # incoming_version < comp_moab.version - set_status_as_seen_on_disk(false) - results.add_result(AuditResults::ACTUAL_VERS_LT_DB_OBJ, db_obj_name: 'CompleteMoab', db_obj_version: comp_moab.version) + return results.report_results + end + + return results.report_results unless check_preserved_object_db_obj_exists && check_moab_db_obj_exists + + unless online_moab_found? + handle_missing_moab + + # Check if it moved to another location + MoabMovedHandler.new(comp_moab, results).check_and_handle_moved_moab + return results.report_results + end + + Rails.logger.debug "check_existence #{druid} called" + + transaction_ok = with_active_record_transaction_and_rescue do + raise_rollback_if_cm_po_version_mismatch + + return results.report_results unless can_validate_current_comp_moab_status? + + if incoming_version == comp_moab.version + # Error thrown if comp_moab not found for this storage root and caught by with_active_record_transaction_and_rescue + set_status_as_seen_on_disk(true) unless comp_moab.status == 'ok' + results.add_result(AuditResults::VERSION_MATCHES, 'CompleteMoab') + elsif incoming_version > comp_moab.version + set_status_as_seen_on_disk(true) unless comp_moab.status == 'ok' + results.add_result(AuditResults::ACTUAL_VERS_GT_DB_OBJ, db_obj_name: 'CompleteMoab', db_obj_version: comp_moab.version) + if moab_validation_errors.empty? + comp_moab.upd_audstamps_version_size(ran_moab_validation?, incoming_version, incoming_size) + pres_object.current_version = incoming_version + pres_object.save! + else + update_status('invalid_moab') end - comp_moab.update_audit_timestamps(ran_moab_validation?, true) - comp_moab.save! - end - results.remove_db_updated_results unless transaction_ok - else - results.add_result(AuditResults::DB_OBJ_DOES_NOT_EXIST, 'PreservedObject') - if moab_validation_errors.empty? - create_db_objects('validity_unknown') - else - create_db_objects('invalid_moab') + else # incoming_version < comp_moab.version + set_status_as_seen_on_disk(false) + results.add_result(AuditResults::ACTUAL_VERS_LT_DB_OBJ, db_obj_name: 'CompleteMoab', db_obj_version: comp_moab.version) end + comp_moab.update_audit_timestamps(ran_moab_validation?, true) + comp_moab.save! end + results.remove_db_updated_results unless transaction_ok results.report_results end + def check_preserved_object_db_obj_exists + with_active_record_transaction_and_rescue do + # If it doesn't exist, error will be caught and DB_OBJ_DOES_NOT_EXIST added to results + pres_object + return true + end + if moab_validation_errors.empty? + create_db_objects('validity_unknown') + else + create_db_objects('invalid_moab') + end + false + end + + def check_moab_db_obj_exists + with_active_record_transaction_and_rescue do + # If it doesn't exist, error will be caught and DB_OBJ_DOES_NOT_EXIST added to results + comp_moab + return true + end + false + end + def confirm_version results.check_name = 'confirm_version' if invalid? diff --git a/spec/lib/audit/catalog_to_moab_spec.rb b/spec/lib/audit/catalog_to_moab_spec.rb index 430da6776..ba828a59a 100644 --- a/spec/lib/audit/catalog_to_moab_spec.rb +++ b/spec/lib/audit/catalog_to_moab_spec.rb @@ -9,6 +9,7 @@ let(:c2m) { described_class.new(comp_moab) } let(:mock_sov) { instance_double(Stanford::StorageObjectValidator) } let(:po) { create(:preserved_object_fixture, druid: druid) } + let(:ms_root) { MoabStorageRoot.find_by!(storage_location: storage_location) } let(:comp_moab) do MoabStorageRoot.find_by!(storage_location: storage_location).complete_moabs.find_by!(preserved_object: po) end @@ -48,7 +49,7 @@ end it 'gets the current version on disk from the Moab::StorageObject' do - moab = instance_double(Moab::StorageObject, object_pathname: object_dir) + moab = instance_double(Moab::StorageObject, object_pathname: object_dir, exist?: true) allow(Moab::StorageObject).to receive(:new).with(druid, String).and_return(moab) expect(moab).to receive(:current_version_id).and_return(3) c2m.check_catalog_version @@ -71,12 +72,18 @@ end it 'calls online_moab_found?' do - expect(c2m).to receive(:online_moab_found?) + expect(c2m).to receive(:online_moab_found?).and_return(true) c2m.check_catalog_version end - context 'moab is nil (exists in catalog but not online)' do - before { allow(Moab::StorageObject).to receive(:new).with(druid, String).and_return(nil) } + context 'moab is nil (exists in catalog but not online at all)' do + let(:moab) { instance_double(Moab::StorageObject) } + + before do + allow(Moab::StorageObject).to receive(:new).with(druid, String).and_return(nil) + allow(Moab::StorageServices).to receive(:find_storage_object).with(druid).and_return(moab) + allow(moab).to receive(:exist?).and_return(false) + end it 'adds a MOAB_NOT_FOUND result' do c2m.instance_variable_set(:@results, results_double) @@ -118,6 +125,39 @@ end end + context 'moab is nil (exists in catalog but online in a different location)' do + let(:wrong_ms_root) { MoabStorageRoot.find_by!(storage_location: 'spec/fixtures/storage_root02/sdr2objects') } + + before do + comp_moab.ok! + comp_moab.moab_storage_root = wrong_ms_root + comp_moab.save! + c2m.instance_variable_set(:@results, results_double) + end + + it 'adds a MOAB_NOT_FOUND and CM_STORAGE_ROOT result' do + expect(c2m.results).to receive(:add_result).with( + AuditResults::MOAB_NOT_FOUND, db_created_at: anything, db_updated_at: anything + ) + expect(c2m.results).to receive(:add_result).with( + AuditResults::CM_STATUS_CHANGED, old_status: "ok", new_status: "online_moab_not_found" + ) + expect(c2m.results).to receive(:add_result).with( + AuditResults::CM_STORAGE_ROOT_CHANGED, old_storage_root: 'spec/fixtures/storage_root02/sdr2objects', + new_storage_root: 'spec/fixtures/storage_root01/sdr2objects' + ) + expect(c2m.results).to receive(:add_result).with( + AuditResults::CM_STATUS_CHANGED, old_status: "online_moab_not_found", new_status: "ok" + ) + c2m.check_catalog_version + end + + it 'changes the storage root' do + c2m.check_catalog_version + expect(comp_moab.moab_storage_root).to eq ms_root + end + end + context 'complete_moab version != current_version of preserved_object' do before do comp_moab.version = 666 @@ -210,7 +250,7 @@ context 'catalog version < moab version' do before do - moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir) + moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir, exist?: true) allow(Moab::StorageObject).to receive(:new).with(druid, instance_of(String)).and_return(moab) allow(moab).to receive(:current_version_id).and_return(4) end @@ -302,7 +342,7 @@ context 'catalog version > moab version' do before do - moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir, current_version_id: 2) + moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir, current_version_id: 2, exist?: true) allow(Moab::StorageObject).to receive(:new).with(druid, instance_of(String)).and_return(moab) end @@ -445,7 +485,7 @@ # use the same setup as 'catalog version > moab version', since we know that should # lead to an update_status('unexpected_version_on_storage') call before do - moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir) + moab = instance_double(Moab::StorageObject, size: 666, object_pathname: object_dir, exist?: true) allow(Moab::StorageObject).to receive(:new).with(druid, instance_of(String)).and_return(moab) allow(moab).to receive(:current_version_id).and_return(2) end diff --git a/spec/lib/audit/moab_to_catalog_spec.rb b/spec/lib/audit/moab_to_catalog_spec.rb index f889f6120..783b85f65 100644 --- a/spec/lib/audit/moab_to_catalog_spec.rb +++ b/spec/lib/audit/moab_to_catalog_spec.rb @@ -40,7 +40,7 @@ describe '.check_existence_for_druid' do let(:druid) { 'bz514sm9647' } let(:results) do - [{ db_obj_does_not_exist: "PreservedObject db object does not exist" }, + [{ db_obj_does_not_exist: "# db object does not exist" }, { created_new_object: "added object to db as it did not exist" }] end diff --git a/spec/services/moab_moved_handler_spec.rb b/spec/services/moab_moved_handler_spec.rb new file mode 100644 index 000000000..96411deea --- /dev/null +++ b/spec/services/moab_moved_handler_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'services/shared_examples_preserved_object_handler' + +RSpec.describe MoabMovedHandler do + let(:druid) { 'bj102hs9687' } + let(:preserved_obj) { create(:preserved_object, druid: druid) } + let(:ms_root) { MoabStorageRoot.find_by!(storage_location: 'spec/fixtures/storage_root01/sdr2objects') } + let(:wrong_ms_root) { MoabStorageRoot.find_by!(storage_location: 'spec/fixtures/storage_root02/sdr2objects') } + let(:complete_moab) { create(:complete_moab, version: 3, status: 'ok', preserved_object: preserved_obj, moab_storage_root: ms_root) } + let(:results) { AuditResults.new(druid, nil, ms_root) } + let(:handler) { described_class.new(complete_moab, results) } + + before do + allow(WorkflowReporter).to receive(:report_error) + allow(WorkflowReporter).to receive(:report_completed) + end + + describe '#check_and_handle_moved_moab' do + context 'CompleteMoab path does not match moab on disk' do + before do + complete_moab.moab_storage_root = wrong_ms_root + complete_moab.status = 'online_moab_not_found' + complete_moab.save! + end + + it 'returns expected results' do + handler.check_and_handle_moved_moab + expect(results.report_results).to include(a_hash_including(AuditResults::CM_STORAGE_ROOT_CHANGED)) + expect(results.report_results).to include(a_hash_including(AuditResults::CM_STATUS_CHANGED => + 'CompleteMoab status changed from online_moab_not_found to ok')) + end + + it 'changes the storage root' do + expect(complete_moab.moab_storage_root).to eq wrong_ms_root + handler.check_and_handle_moved_moab + + complete_moab.reload + expect(complete_moab.moab_storage_root).to eq ms_root + end + end + + context 'Moab on disk not found' do + let(:druid) { 'bj102hs9688' } + + it 'returns no results' do + handler.check_and_handle_moved_moab + expect(results.report_results).to be_empty + end + + it 'does not change the storage root' do + expect(complete_moab.moab_storage_root).to eq ms_root + handler.check_and_handle_moved_moab + expect(complete_moab.moab_storage_root).to eq ms_root + end + end + + context 'Moab on disk found but version mismatch' do + before do + complete_moab.moab_storage_root = wrong_ms_root + complete_moab.version = 4 + complete_moab.save! + end + + it 'returns no results' do + handler.check_and_handle_moved_moab + expect(results.report_results).to be_empty + end + + it 'does not change the storage root' do + expect(complete_moab.moab_storage_root).to eq wrong_ms_root + handler.check_and_handle_moved_moab + expect(complete_moab.moab_storage_root).to eq wrong_ms_root + end + end + + context 'Moab on disk found but invalid' do + before do + complete_moab.moab_storage_root = wrong_ms_root + complete_moab.save! + mock_validator = instance_double(Stanford::StorageObjectValidator) + allow(mock_validator).to receive(:validation_errors).and_return(['An error']) + allow(Stanford::StorageObjectValidator).to receive(:new).and_return(mock_validator) + end + + it 'returns no results' do + handler.check_and_handle_moved_moab + expect(results.report_results).to be_empty + end + + it 'does not change the storage root' do + expect(complete_moab.moab_storage_root).to eq wrong_ms_root + handler.check_and_handle_moved_moab + expect(complete_moab.moab_storage_root).to eq wrong_ms_root + end + end + end +end diff --git a/spec/services/preserved_object_handler_check_exist_spec.rb b/spec/services/preserved_object_handler_check_exist_spec.rb index e7d9e9ee5..9d47a0034 100644 --- a/spec/services/preserved_object_handler_check_exist_spec.rb +++ b/spec/services/preserved_object_handler_check_exist_spec.rb @@ -4,8 +4,6 @@ require 'services/shared_examples_preserved_object_handler' RSpec.describe PreservedObjectHandler do - before { allow(WorkflowReporter).to receive(:report_error) } - let(:druid) { 'ab123cd4567' } let(:incoming_version) { 6 } let(:incoming_size) { 9876 } @@ -15,6 +13,8 @@ let(:db_update_failed_prefix) { "db update failed" } let(:po_handler) { described_class.new(druid, incoming_version, incoming_size, ms_root) } + before { allow(WorkflowReporter).to receive(:report_error) } + describe '#check_existence' do it_behaves_like 'attributes validated', :check_existence @@ -394,13 +394,45 @@ po_handler.check_existence expect(Rails.logger).to have_received(:debug).with("check_existence #{druid} called") end + + context 'CompleteMoab path does not match moab on disk' do + let(:druid) { 'bj102hs9687' } + let(:wrong_ms_root) { MoabStorageRoot.find_by!(storage_location: 'spec/fixtures/storage_root02/sdr2objects') } + let(:cm) { po.complete_moabs.find_by!(moab_storage_root: ms_root) } + let(:po_handler) { described_class.new(druid, incoming_version, incoming_size, wrong_ms_root) } + + before do + allow(WorkflowReporter).to receive(:report_completed) + cm.moab_storage_root = wrong_ms_root + cm.version = 3 + cm.save! + end + + it 'returns expected results' do + results = po_handler.check_existence + expect(results).to include(a_hash_including(AuditResults::CM_STATUS_CHANGED => + 'CompleteMoab status changed from ok to online_moab_not_found')) + expect(results).to include(a_hash_including(AuditResults::MOAB_NOT_FOUND)) + expect(results).to include(a_hash_including(AuditResults::CM_STORAGE_ROOT_CHANGED)) + expect(results).to include(a_hash_including(AuditResults::CM_STATUS_CHANGED => + 'CompleteMoab status changed from online_moab_not_found to ok')) + end + + it 'changes the storage root' do + po_handler.check_existence + cm.reload + expect(cm.moab_storage_root).to eq ms_root + end + end end context 'object not in db' do - let(:exp_po_not_exist_msg) { "PreservedObject db object does not exist" } + let(:exp_po_not_exist_msg) { "# db object does not exist" } let(:exp_obj_created_msg) { "added object to db as it did not exist" } context 'presume validity and test other common behavior' do + let(:druid) { 'bp628nk4868' } + before { allow(po_handler).to receive(:moab_validation_errors).and_return([]) } # FIXME: if requirements change to a single message for "object does not exist" and "created object"