Skip to content

Commit

Permalink
Merge pull request #1274 from sul-dlss/t1229-404
Browse files Browse the repository at this point in the history
Returns 409 from content_diff when missing druid(s).
  • Loading branch information
justinlittman committed Dec 16, 2019
2 parents d87a8ac + fe46d9d commit ff4cc9f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
34 changes: 26 additions & 8 deletions app/controllers/objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ def checksums
return
end

checksum_list, missing_druids = generate_checksum_list

if missing_druids.any?
render(plain: "409 conflict - storage object(s) not found for #{missing_druids.join(', ')}", status: :conflict)
return
end

respond_to do |format|
format.json do
render json: json_checksum_list
render json: checksum_list.to_json
end
format.csv do
render plain: csv_checksum_list
render plain: to_csv_checksum_list(checksum_list)
end
format.any { render status: :not_acceptable, plain: 'Format not acceptable' }
end
Expand Down Expand Up @@ -104,15 +111,26 @@ def returned_druid(druid)
return_bare_druids? ? druid.to_s : "druid:#{druid}"
end

def json_checksum_list
normalized_druids.map { |druid| { returned_druid(druid) => content_files_checksums(druid) } }.to_json
def generate_checksum_list
checksum_list = []
missing_druids = []
normalized_druids.each do |druid|
begin
checksum_list << { returned_druid(druid) => content_files_checksums(druid) }
rescue Moab::ObjectNotFoundException
missing_druids << druid
end
end
[checksum_list, missing_druids]
end

def csv_checksum_list
def to_csv_checksum_list(checksum_list)
CSV.generate do |csv|
normalized_druids.each do |druid|
content_files_checksums(druid).each do |checksum|
csv << [returned_druid(druid), checksum[:filename], checksum[:md5], checksum[:sha1], checksum[:sha256], checksum[:filesize]]
checksum_list.each do |druid_checksum|
druid_checksum.each do |druid, checksums|
checksums.each do |checksum|
csv << [druid, checksum[:filename], checksum[:md5], checksum[:sha1], checksum[:sha256], checksum[:filesize]]
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/objects_controller_checksums_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@
end

context 'when object not found' do
it 'returns a 404 response code' do
it 'returns a 409 response code' do
post checksums_objects_url, params: { druids: [prefixed_druid, 'druid:xx123yy9999'], format: :json }
expect(response).to have_http_status(:not_found)
expect(response).to have_http_status(:conflict)
end

it 'body has additional information from the exception if available' do
post checksums_objects_url, params: { druids: [prefixed_druid, 'druid:xx123yy9999'], format: :json }
expect(response.body).to eq '404 Not Found: No storage object found for xx123yy9999'
expect(response.body).to eq '409 conflict - storage object(s) not found for xx123yy9999'
end
end

Expand Down

0 comments on commit ff4cc9f

Please sign in to comment.