From 7ce0760cf53202ecb210dd692a78782183956d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Mon, 8 Aug 2022 12:11:08 +0100 Subject: [PATCH] Refactored the full import calls --- app/services/imports/core_import_service.rb | 22 --------- lib/tasks/full_import.rake | 25 ++++++---- spec/lib/tasks/full_import_spec.rb | 51 +++++++++++---------- 3 files changed, 43 insertions(+), 55 deletions(-) delete mode 100644 app/services/imports/core_import_service.rb diff --git a/app/services/imports/core_import_service.rb b/app/services/imports/core_import_service.rb deleted file mode 100644 index 77ba07519..000000000 --- a/app/services/imports/core_import_service.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Imports - class CoreImportService < ImportService - - def start_import - - end - - private - - ARCHIVE_FOLDERS = { - organisation: "institution", - scheme: "mgmtgroups", - scheme_location: "schemes", - user: "user", - data_protection_confirmation: "dataprotect", - organisation_rent_periods: "rent-period", - case_log: "logs" - }.freeze - - - end -end diff --git a/lib/tasks/full_import.rake b/lib/tasks/full_import.rake index 6ab8bcece..49c1348ce 100644 --- a/lib/tasks/full_import.rake +++ b/lib/tasks/full_import.rake @@ -1,17 +1,26 @@ +Import = Struct.new("Import", :import_class, :import_method, :folder) + namespace :core do desc "Import all data XMLs from legacy CORE" task :full_import, %i[path] => :environment do |_task, args| path = args[:path] - raise "Usage: rake core:full_import['path/to/xml_files']" if path.blank? + raise "Usage: rake core:full_import['path/to/main_folder']" if path.blank? storage_service = StorageService.new(PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) - Imports::OrganisationImportService.new(storage_service).create_organisations(path) - Imports::SchemeImportService.new(storage_service).create_schemes(path) - Imports::SchemeLocationImportService.new(storage_service).create_scheme_locations(path) - Imports::UserImportService.new(storage_service).create_users(path) - Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) - Imports::OrganisationRentPeriodImportService.new(storage_service).create_organisation_rent_periods(path) - Imports::CaseLogsImportService.new(storage_service).create_logs(path) + import_list = [ + Import.new(Imports::OrganisationImportService, :create_organisations, "institution"), + Import.new(Imports::SchemeImportService, :create_schemes, "mgmtgroups"), + Import.new(Imports::SchemeLocationImportService, :create_scheme_locations, "schemes"), + Import.new(Imports::UserImportService, :create_users, "user"), + Import.new(Imports::DataProtectionConfirmationImportService, :create_data_protection_confirmations, "dataprotect"), + Import.new(Imports::OrganisationRentPeriodImportService, :create_organisation_rent_periods, "rent-period"), + Import.new(Imports::CaseLogsImportService, :create_logs, "logs"), + ] + + import_list.each do |import| + folder_path = File.join(path, import.folder, "") + import.import_class.new(storage_service).send(import.import_method, folder_path) + end end end diff --git a/spec/lib/tasks/full_import_spec.rb b/spec/lib/tasks/full_import_spec.rb index 6b022663b..1ceed5fcb 100644 --- a/spec/lib/tasks/full_import_spec.rb +++ b/spec/lib/tasks/full_import_spec.rb @@ -20,38 +20,39 @@ describe "rake core:full_import", type: :task do end context "when starting a full import" do + let(:fixture_path) { "spec/fixtures/imports" } + let(:case_logs_service) { instance_double(Imports::CaseLogsImportService) } + let(:rent_period_service) { instance_double(Imports::OrganisationRentPeriodImportService) } + let(:data_protection_service) { instance_double(Imports::DataProtectionConfirmationImportService) } + let(:user_service) { instance_double(Imports::UserImportService) } + let(:location_service) { instance_double(Imports::SchemeLocationImportService) } + let(:scheme_service) { instance_double(Imports::SchemeImportService) } + let(:organisation_service) { instance_double(Imports::OrganisationImportService) } + + before do + allow(Imports::OrganisationImportService).to receive(:new).and_return(organisation_service) + allow(Imports::SchemeImportService).to receive(:new).and_return(scheme_service) + allow(Imports::SchemeLocationImportService).to receive(:new).and_return(location_service) + allow(Imports::UserImportService).to receive(:new).and_return(user_service) + allow(Imports::DataProtectionConfirmationImportService).to receive(:new).and_return(data_protection_service) + allow(Imports::OrganisationRentPeriodImportService).to receive(:new).and_return(rent_period_service) + allow(Imports::CaseLogsImportService).to receive(:new).and_return(case_logs_service) + end + it "raises an exception if no parameters are provided" do expect { task.invoke }.to raise_error(/Usage/) end context "with all folders being present" - let(:organisation_service) { instance_double(Imports::OrganisationImportService) } - let(:scheme_service) { instance_double(Imports::SchemeImportService) } - let(:location_service) { instance_double(Imports::SchemeLocationImportService) } - let(:user_service) { instance_double(Imports::UserImportService) } - let(:data_protection_service) { instance_double(Imports::DataProtectionConfirmationImportService) } - let(:rent_period_service) { instance_double(Imports::OrganisationRentPeriodImportService) } - let(:case_logs_service) { instance_double(Imports::CaseLogsImportService) } - let(:fixture_path) { "spec/fixtures/imports" } - - before do - allow(Imports::OrganisationImportService).to receive(:new).and_return(organisation_service) - allow(Imports::SchemeImportService).to receive(:new).and_return(scheme_service) - allow(Imports::SchemeLocationImportService).to receive(:new).and_return(location_service) - allow(Imports::UserImportService).to receive(:new).and_return(user_service) - allow(Imports::DataProtectionConfirmationImportService).to receive(:new).and_return(data_protection_service) - allow(Imports::OrganisationRentPeriodImportService).to receive(:new).and_return(rent_period_service) - allow(Imports::CaseLogsImportService).to receive(:new).and_return(case_logs_service) - end it "calls every import method" do - expect(organisation_service).to receive(:create_organisations).with(fixture_path) - expect(scheme_service).to receive(:create_schemes).with(fixture_path) - expect(location_service).to receive(:create_scheme_locations).with(fixture_path) - expect(user_service).to receive(:create_users).with(fixture_path) - expect(data_protection_service).to receive(:create_data_protection_confirmations).with(fixture_path) - expect(rent_period_service).to receive(:create_organisation_rent_periods).with(fixture_path) - expect(case_logs_service).to receive(:create_logs).with(fixture_path) + expect(organisation_service).to receive(:create_organisations).with("#{fixture_path}/institution/") + expect(scheme_service).to receive(:create_schemes).with("#{fixture_path}/mgmtgroups/") + expect(location_service).to receive(:create_scheme_locations).with("#{fixture_path}/schemes/") + expect(user_service).to receive(:create_users).with("#{fixture_path}/user/") + expect(data_protection_service).to receive(:create_data_protection_confirmations).with("#{fixture_path}/dataprotect/") + expect(rent_period_service).to receive(:create_organisation_rent_periods).with("#{fixture_path}/rent-period/") + expect(case_logs_service).to receive(:create_logs).with("#{fixture_path}/logs/") task.invoke(fixture_path) end