Skip to content

Commit

Permalink
content directory is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Ingulfsen committed Nov 15, 2017
1 parent 5a0fffe commit e23fbf1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
40 changes: 36 additions & 4 deletions lib/moab/storage_object_validator.rb
@@ -1,12 +1,14 @@
require 'moab'
require 'set'

module Moab
# Given a druid path, are the contents actually a well-formed Moab?
# Shameless green: repetitious code included.
class StorageObjectValidator

EXPECTED_DATA_SUB_DIRS = ["content", "metadata"].freeze
IMPLICIT_DIRS = ['.', '..'].freeze # unlike Find.find, Dir.entries returns these
METADATA_DIR = 'metadata'.freeze
EXPECTED_DATA_SUB_DIRS = ["content", METADATA_DIR].freeze
IMPLICIT_DIRS = ['.', '..', '.keep'].freeze # unlike Find.find, Dir.entries returns the current/parent dirs
DATA_DIR_NAME = "data".freeze
EXPECTED_VERSION_SUB_DIRS = [DATA_DIR_NAME, "manifests"].freeze
MANIFEST_INVENTORY_PATH = 'manifests/manifestInventory.xml'.freeze
Expand Down Expand Up @@ -63,6 +65,7 @@ def version_directories

def check_correctly_named_version_dirs
errors = []
errors << result_hash(MISSING_DIR, 'no versions exist') unless version_directories.count > 0
version_directories.each do |version_dir|
errors << result_hash(VERSION_DIR_BAD_FORMAT) unless version_dir =~ /^[v]\d{4}$/
end
Expand All @@ -89,20 +92,49 @@ def check_correctly_formed_moabs
version_path = "#{storage_obj_path}/#{version_dir}"
version_sub_dirs = sub_dirs(version_path)
before_result_size = errors.size
errors.concat check_sub_dirs(version_sub_dirs, version_dir, EXPECTED_VERSION_SUB_DIRS)
errors.concat check_version_sub_dirs(version_sub_dirs, version_dir)
after_result_size = errors.size
# run the following checks if this version dir passes check_sub_dirs, even if some prior version dirs didn't
if before_result_size == after_result_size
data_dir_path = "#{version_path}/#{DATA_DIR_NAME}"
data_sub_dirs = sub_dirs(data_dir_path)
errors.concat check_sub_dirs(data_sub_dirs, version_dir, EXPECTED_DATA_SUB_DIRS)
errors.concat check_data_sub_dirs(data_sub_dirs, version_dir)
errors.concat check_required_manifest_files(version_path, version_dir)
end
end

errors
end

def check_version_sub_dirs(version_sub_dirs, version)
errors = []
version_sub_dir_count = version_sub_dirs.size
if version_sub_dir_count == EXPECTED_VERSION_SUB_DIRS.size
errors.concat expected_dirs(version_sub_dirs, version, EXPECTED_VERSION_SUB_DIRS)
elsif version_sub_dir_count > EXPECTED_VERSION_SUB_DIRS.size
errors.concat found_unexpected(version_sub_dirs, version, EXPECTED_VERSION_SUB_DIRS)
elsif version_sub_dir_count < EXPECTED_VERSION_SUB_DIRS.size
errors.concat missing_dir(version_sub_dirs, version, EXPECTED_VERSION_SUB_DIRS)
end
errors
end

def check_data_sub_dirs(data_sub_dirs, version)
errors = []
if data_sub_dirs.size > EXPECTED_DATA_SUB_DIRS.size
errors.concat found_unexpected(data_sub_dirs, version, EXPECTED_DATA_SUB_DIRS)
else
errors.concat missing_dir(data_sub_dirs, version, [METADATA_DIR]) unless data_sub_dirs.include?(METADATA_DIR)
errors.concat found_unexpected(data_sub_dirs, version, EXPECTED_DATA_SUB_DIRS) unless subset?(data_sub_dirs,
EXPECTED_DATA_SUB_DIRS)
end
errors
end

def subset?(first_array, second_array)
first_array.to_set.subset?(second_array.to_set)
end

def check_sub_dirs(sub_dirs, version, required_sub_dirs)
errors = []
sub_dir_count = sub_dirs.size
Expand Down
4 changes: 2 additions & 2 deletions spec/unit_tests/moab/storage_object_validator_spec.rb
Expand Up @@ -50,9 +50,9 @@
# v0005
expect(verification_array[5]).to eq(Moab::StorageObjectValidator::NO_XML_FILES =>"Version: v0005 Missing all required metadata files")
end
it 'has missing content directory' do
it 'has missing metadata directory' do
# v0006
expect(verification_array[6]).to eq(Moab::StorageObjectValidator::MISSING_DIR =>"Missing directory: [\"content\", \"metadata\"] Version: v0006")
expect(verification_array[6]).to eq(Moab::StorageObjectValidator::MISSING_DIR =>"Missing directory: [\"metadata\"] Version: v0006")
end
it 'has no required items' do
# v0006
Expand Down
9 changes: 7 additions & 2 deletions spec/unit_tests/stanford/storage_object_validator_spec.rb
Expand Up @@ -8,14 +8,19 @@
let(:verification_array) { storage_obj_validator1.validation_errors }

it 'returns errors from calling the superclass validation_errors' do
expect(verification_array.count).to eq(2)
erroneous_druid = 'xx000xx0000'
erroneous_druid_path = 'spec/fixtures/bad_root01/bad_moab_storage_trunk/xx/000/xx/0000/xx000xx0000'
erroneous_object = Moab::StorageObject.new(erroneous_druid, erroneous_druid_path)
erroneous_object_validator = described_class.new(erroneous_object)
error_list = erroneous_object_validator.validation_errors
expect(error_list.count).to eq(9)
end
it 'returns validation error codes when there is file path for druid to directory validation' do
invalid_druid_path = 'spec/fixtures/bad_root01/bad_moab_storage_trunk/dd/000/dd/0000/dd000dd0000'
storage_obj = Moab::StorageObject.new('dd000dd0000', invalid_druid_path)
storage_obj_validator = described_class.new(storage_obj)
verification_array = storage_obj_validator.validation_errors
expect(verification_array).to include(Moab::StorageObjectValidator::VERSION_DIR_BAD_FORMAT => "Version directory name not in 'v00xx' format")
expect(verification_array).to include(Moab::StorageObjectValidator::MISSING_DIR => "Missing directory: no versions exist")
end
end

Expand Down

0 comments on commit e23fbf1

Please sign in to comment.