Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ def post_schema(schema, force)
should_send = do_server_want_schema(api_map[:meta][:schemaFileHash])

if should_send || force
client = ForestAdminAgent::Http::ForestAdminApiRequester.new
client.post('/forest/apimaps', api_map.to_json)
ForestAdminAgent::Facades::Container.logger.log('Info', 'schema was updated, sending new version')
send_schema_to_server(api_map)
else
@container.resolve(:logger)
ForestAdminAgent::Facades::Container.logger.log('Info', 'Schema was not updated since last run')
Expand Down Expand Up @@ -241,6 +239,25 @@ def log_schema_skip
@logger.log('Info',
"[ForestAdmin] Running in #{environment} mode")
end

def send_schema_to_server(api_map)
ForestAdminAgent::Facades::Container.logger.log('Info', 'schema was updated, sending new version')
client = ForestAdminAgent::Http::ForestAdminApiRequester.new
client.post('/forest/apimaps', api_map.to_json)
rescue Faraday::Error => e
status = e.response[:status] if e.response
if status
ForestAdminAgent::Facades::Container.logger.log(
'Error',
"Failed to send schema: invalid request (HTTP #{status})"
)
else
ForestAdminAgent::Facades::Container.logger.log(
'Error',
'Failed to send schema: cannot reach ForestAdmin server'
)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with high complexity (count = 6): send_schema_to_server [qlty:function-complexity]

end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,79 @@ module Builder
expect(result).to eq(instance)
end
end

describe 'send_schema_to_server' do
let(:instance) { described_class.instance }
let(:logger) { instance_double(Services::LoggerService) }
let(:client) { instance_double(ForestAdminAgent::Http::ForestAdminApiRequester) }
let(:api_map) { { meta: { schemaFileHash: 'abc123' }, collections: [] } }

before do
allow(ForestAdminAgent::Facades::Container).to receive(:logger).and_return(logger)
allow(ForestAdminAgent::Http::ForestAdminApiRequester).to receive(:new).and_return(client)
allow(logger).to receive(:log)
end

it 'logs success message and posts schema when successful' do
allow(client).to receive(:post).with('/forest/apimaps', api_map.to_json)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version')
expect(client).to have_received(:post).with('/forest/apimaps', api_map.to_json)
end

context 'when error occurs with HTTP status' do
it 'logs error with status 400' do
error = Faraday::ClientError.new('Bad Request', { status: 400 })
allow(client).to receive(:post).and_raise(error)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version')
expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 400)')
end

it 'logs error with status 500' do
error = Faraday::ServerError.new('Internal Server Error', { status: 500 })
allow(client).to receive(:post).and_raise(error)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 500)')
end

it 'logs error with status 502' do
error = Faraday::ServerError.new('Bad Gateway', { status: 502 })
allow(client).to receive(:post).and_raise(error)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 502)')
end
end

context 'when error occurs without HTTP status (unreachable)' do
it 'logs connection failure message' do
error = Faraday::ConnectionFailed.new('Failed to open TCP connection')
allow(client).to receive(:post).and_raise(error)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version')
expect(logger).to have_received(:log).with('Error', 'Failed to send schema: cannot reach ForestAdmin server')
end

it 'logs timeout error message' do
error = Faraday::TimeoutError.new('execution expired')
allow(client).to receive(:post).and_raise(error)

instance.send(:send_schema_to_server, api_map)

expect(logger).to have_received(:log).with('Error', 'Failed to send schema: cannot reach ForestAdmin server')
end
end
end
end
end
end
Expand Down
Loading