From 7e0473a6509a17cd22206e62600c91cf3fee211a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Drga?= Date: Fri, 4 Oct 2024 14:08:13 +0200 Subject: [PATCH] Separated the delete logic from data extraction result job, modified tests --- .../project_data_extraction_delete_job.rb | 21 +++++++++ .../project_data_extraction_result_job.rb | 14 +----- ...project_data_extraction_delete_job_spec.rb | 47 +++++++++++++++++++ ...project_data_extraction_result_job_spec.rb | 6 +-- 4 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 app/jobs/metais/project_data_extraction_delete_job.rb create mode 100644 spec/jobs/metais/project_data_extraction_delete_job_spec.rb diff --git a/app/jobs/metais/project_data_extraction_delete_job.rb b/app/jobs/metais/project_data_extraction_delete_job.rb new file mode 100644 index 0000000..3461df8 --- /dev/null +++ b/app/jobs/metais/project_data_extraction_delete_job.rb @@ -0,0 +1,21 @@ +require 'net/http' +require 'uri' + +class Metais::ProjectDataExtractionDeleteJob < ApplicationJob + queue_as :metais_data_extraction + + def perform(project_uuid) + url = "#{ENV.fetch('API_URL')}/projects/#{project_uuid}" + uri = URI(url) + + req = Net::HTTP::Delete.new(uri) + res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request(req) + end + + unless res.is_a?(Net::HTTPSuccess) + error_message = "Failed to delete project: #{res.code}, body: #{res.body}" + raise RuntimeError, error_message + end + end +end diff --git a/app/jobs/metais/project_data_extraction_result_job.rb b/app/jobs/metais/project_data_extraction_result_job.rb index d1dec96..a8f8455 100644 --- a/app/jobs/metais/project_data_extraction_result_job.rb +++ b/app/jobs/metais/project_data_extraction_result_job.rb @@ -21,7 +21,7 @@ def perform(project_uuid, location_header) update_project_origin(project_origin, result) process_harmonogram(result['harmonogram'], project_origin) - send_delete_request(URI(url)) + Metais::ProjectDataExtractionDeleteJob.perform_later(project_uuid) end private @@ -105,16 +105,4 @@ def parse_event_date(start_date) rescue ArgumentError => e raise RuntimeError, e end - - def send_delete_request(uri) - req = Net::HTTP::Delete.new(uri) - res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| - http.request(req) - end - - unless res.is_a?(Net::HTTPSuccess) - error_message = "Failed to delete project: #{res.code}, body: #{res.body}" - raise RuntimeError, error_message - end - end end diff --git a/spec/jobs/metais/project_data_extraction_delete_job_spec.rb b/spec/jobs/metais/project_data_extraction_delete_job_spec.rb new file mode 100644 index 0000000..d3e8f81 --- /dev/null +++ b/spec/jobs/metais/project_data_extraction_delete_job_spec.rb @@ -0,0 +1,47 @@ +require 'rails_helper' +require 'net/http' + +RSpec.describe Metais::ProjectDataExtractionDeleteJob, type: :job do + include ActiveJob::TestHelper + + let(:project_uuid) { 'sample-uuid' } + let(:api_url) { 'http://example.com/api' } + let(:url) { "#{api_url}/projects/#{project_uuid}" } + let(:uri) { URI(url) } + let(:response) { instance_double('Net::HTTPResponse') } + + before do + allow(ENV).to receive(:fetch).with('API_URL').and_return(api_url) + allow(Net::HTTP).to receive(:start).and_return(response) + end + + describe '#perform' do + context 'when the delete request is successful' do + before do + allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(true) + end + + it 'sends a delete request' do + expect { + described_class.perform_now(project_uuid) + }.not_to raise_error + + expect(Net::HTTP).to have_received(:start).with(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') + end + end + + context 'when the delete request fails' do + before do + allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(false) + allow(response).to receive(:code).and_return('500') + allow(response).to receive(:body).and_return('Internal Server Error') + end + + it 'raises a RuntimeError with the correct message' do + expect { + described_class.perform_now(project_uuid) + }.to raise_error(RuntimeError, /Failed to delete project: 500, body: Internal Server Error/) + end + end + end +end diff --git a/spec/jobs/metais/project_data_extraction_result_job_spec.rb b/spec/jobs/metais/project_data_extraction_result_job_spec.rb index f6b9c37..5b83902 100644 --- a/spec/jobs/metais/project_data_extraction_result_job_spec.rb +++ b/spec/jobs/metais/project_data_extraction_result_job_spec.rb @@ -67,16 +67,12 @@ allow(Metais::ProjectEventType).to receive(:find_by).with(name: 'Predpoklad').and_return(event_type) allow(Metais::ProjectEvent).to receive(:find_or_initialize_by).and_return(double('Metais::ProjectEvent', save!: true)) - - allow(Net::HTTP).to receive(:start).and_return(double('Net::HTTPResponse', is_a?: true, code: '200')) end it 'successfully processes the result and sends a delete request' do expect { described_class.perform_now(project_uuid, location_header) - }.not_to have_enqueued_job - - expect(Net::HTTP).to have_received(:start) + }.to have_enqueued_job(Metais::ProjectDataExtractionDeleteJob).with(project_uuid) end end