Skip to content

Commit

Permalink
Refactor tests and WorkflowErrorsReporter class
Browse files Browse the repository at this point in the history
  • Loading branch information
SaravShah committed Jan 11, 2018
1 parent 5b3f9e2 commit 7a13d5b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Metrics/AbcSize:
Exclude:
- 'app/services/preserved_object_handler.rb' # methods still readable; shameless green
- 'lib/audit/moab_to_catalog.rb' # .xxx_for_all_storage_roots is decidedly readable
- 'app/services/preserved_object_handler_results.rb' # method still readable

Metrics/BlockLength:
Exclude:
Expand All @@ -74,6 +75,7 @@ Metrics/BlockNesting:
Metrics/ClassLength:
Exclude:
- 'app/services/preserved_object_handler.rb'
- 'app/services/preserved_object_handler_results.rb'

Metrics/CyclomaticComplexity:
Exclude:
Expand Down
29 changes: 28 additions & 1 deletion app/services/preserved_object_handler_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class PreservedObjectHandlerResults
PC_PO_VERSION_MISMATCH => "PreservedCopy online moab version %{pc_version} does not match PreservedObject current_version %{po_version}"
}.freeze

REPORT_ERROR_LIST = [
ARG_VERSION_LESS_THAN_DB_OBJECT,
DB_UPDATE_FAILED,
OBJECT_ALREADY_EXISTS,
OBJECT_DOES_NOT_EXIST,
UNEXPECTED_VERSION,
PC_PO_VERSION_MISMATCH
].freeze

DB_UPDATED_CODES = [
UPDATED_DB_OBJECT,
UPDATED_DB_OBJECT_TIMESTAMP_ONLY,
Expand Down Expand Up @@ -90,15 +99,33 @@ def result_hash(code, msg_args=nil)
# result1 = {response_code => msg}
# result2 = {response_code => msg}
def report_results
WorkflowErrorsReporter.update_workflow(druid, result_array)
combined_error_messages = []
result_array.each do |r|
severity = self.class.logger_severity_level(r.keys.first)
msg = r.values.first
Rails.logger.log(severity, msg)
if r.key?(PreservedObjectHandlerResults::INVALID_MOAB)
WorkflowErrorsReporter.update_workflow(druid, 'moab-valid', r.values.first)
elsif REPORT_ERROR_LIST.include?(r.keys.first)
combined_error_messages << r
end
end
stack_trace = caller(1..1).first[/.+?(?=:in)/]
display_message(combined_error_messages, stack_trace)
result_array
end

def display_message(combined_error_messages, stack_trace)
value_array = []
combined_error_messages.each do |x|
x.each_value do |val|
value_array << val
end
end
value_array << stack_trace
WorkflowErrorsReporter.update_workflow(druid, 'pc-process', value_array.join(" || "))
end

private

def result_code_msg(code, addl=nil)
Expand Down
18 changes: 6 additions & 12 deletions app/services/workflow_errors_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
# out a shorter error message, that only includes the moab validation error.
class WorkflowErrorsReporter

def self.update_workflow(druid, error_message)
error_message.select { |error| request(druid, 'moab-valid', moab_error(error)) if error.key?(PreservedObjectHandlerResults::INVALID_MOAB) }
end

def self.request(druid, process_name, error_message)
def self.update_workflow(druid, process_name, error_message)
return unless conn
handler = proc do |exception, attempt_number, total_delay|
Rails.logger.warn("Handler saw a #{exception.class}; retry attempt #{attempt_number}; #{total_delay} seconds have passed.")
Expand All @@ -21,11 +17,12 @@ def self.request(druid, process_name, error_message)
end
end
rescue *workflow_service_exceptions_to_catch => e
raise Faraday::Error, e
raise Faraday::Error, e.backtrace
end

private_class_method def self.conn
if Settings.workflow_services.url.present?
@connection ||= Faraday.new(url: Settings.workflow_services.url) do |c|
if Settings.workflow_services_url.present?
@connection ||= Faraday.new(url: Settings.workflow_services_url) do |c|
c.use Faraday::Response::RaiseError
c.use Faraday::Adapter::NetHttp
end
Expand All @@ -34,12 +31,9 @@ def self.request(druid, process_name, error_message)
end
@connection
end

private_class_method def self.workflow_service_exceptions_to_catch
[Faraday::Error]
end

private_class_method def self.moab_error(error)
/^.*\)(.*)$/.match(error[PreservedObjectHandlerResults::INVALID_MOAB]).captures.first
end

end
3 changes: 1 addition & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ preservation_policies:
provlog:
enable: false

workflow_services:
url: ''
workflow_services_url: ''
38 changes: 21 additions & 17 deletions spec/services/workflow_errors_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
require 'rails_helper'

RSpec.describe WorkflowErrorsReporter do
describe '.update_workflow(druid, error_message)' do
let(:body) { "<process name='moab-valid' status='error' errorMessage=' Invalid moab, validation errors: [\"Should contain only sequential version directories. Current directories: [\\\"v0001\\\", \\\"v0002\\\", \\\"v0009\\\"]\"]'/>" }
let(:put) { 'https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid' }
let(:headers) { { 'Content-Type' => 'application/xml' } }

describe '.update_workflow' do
let(:error_msg) do
[{ 10 => "PreservedObjectHandler(jj925bx9565, 9, 6570668, <Endpoint:...>) PreservedObject db object does not exist" },
{ 13 => "PreservedObjectHandler(jj925bx9565, 9, 6570668, <Endpoint:...>) Invalid moab, validation errors: [\"Should contain only sequential version directories. Current directories: [\\\"v0001\\\", \\\"v0002\\\", \\\"v0009\\\"]\"]" }]
{ 13 => "PreservedObjectHandler(jj925bx9565, 9, 6570668, <Endpoint:...>) Invalid moab, validation error...ential version directories. Current directories: [\\\"v0001\\\", \\\"v0002\\\", \\\"v0009\\\"]\"]" }
end

let(:body) { "<process name='moab-valid' status='error' errorMessage='#{error_msg}'/>" }

before do
Settings.workflow_services.url = 'https://sul-lyberservices-test.stanford.edu/workflow/'
stub_request(:put, 'https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid')
Settings.workflow_services_url = 'https://sul-lyberservices-test.stanford.edu/workflow/'
stub_request(:put, put)
.with(body: body,
headers: { 'Content-Type' => 'application/xml' })
headers: headers)
.to_return(status: 204, body: "", headers: {})
end
it 'updates preservationWF with invalid moab errors' do
expect(described_class.update_workflow('jj925bx9565', error_msg)).to eq [error_msg[1]]
expect(described_class.update_workflow('jj925bx9565', 'moab-valid', error_msg)).to be_an_instance_of(Faraday::Response)
end
end
describe '.request' do

it 'rescues for Faraday::Error' do
allow(described_class).to receive(:request).and_raise(Faraday::Error)
expect { described_class.request('jj925bx9565', 'moab-valid', 'Foo() error') }.to raise_error(Faraday::Error)
allow(described_class).to receive(:update_workflow).and_raise(Faraday::Error)
expect { described_class.update_workflow('jj925bx9565', 'moab-valid', 'Foo() error') }.to raise_error(Faraday::Error)
end
end

describe '.update_workflow with invalid workflow_services.url' do
let(:body) { "<process name='moab-valid' status='error' errorMessage='[{13=>\"Foo() Invalid Moab\"}]'/>" }

before do
stub_request(:put, "https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid")
.with(body: "<process name='moab-valid' status='error' errorMessage=' Invalid Moab'/>",
headers: { 'Content-Type' => 'application/xml' })
stub_request(:put, put)
.with(body: body,
headers: headers)
.to_return(status: 200, body: "", headers: {})
end
it 'returns Rails warning' do
Settings.workflow_services.url = ''
Settings.workflow_services_url = ''
expect(Rails.logger).to receive(:warn).with('no workflow hookup - assume you are in test or dev environment')
described_class.update_workflow('jj925bx9565', [{ 13 => "Foo() Invalid Moab" }])
described_class.update_workflow('jj925bx9565', 'moab-valid', [{ 13 => "Foo() Invalid Moab" }])
end
end
end

0 comments on commit 7a13d5b

Please sign in to comment.