From 48ac937df79f0e12d872cfe9574c8e96d31915e5 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Apr 2022 12:09:25 +0100 Subject: [PATCH] Add import --- ...organisation_rent_period_import_service.rb | 22 ++++++++++ lib/tasks/data_import.rake | 2 + spec/lib/tasks/data_import_spec.rb | 18 ++++++++ ...isation_rent_period_import_service_spec.rb | 41 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 app/services/imports/organisation_rent_period_import_service.rb create mode 100644 spec/services/imports/organisation_rent_period_import_service_spec.rb diff --git a/app/services/imports/organisation_rent_period_import_service.rb b/app/services/imports/organisation_rent_period_import_service.rb new file mode 100644 index 000000000..74b20b830 --- /dev/null +++ b/app/services/imports/organisation_rent_period_import_service.rb @@ -0,0 +1,22 @@ +module Imports + class OrganisationRentPeriodImportService < ImportService + def create_organisation_rent_periods(folder) + import_from(folder, :create_organisation_rent_period) + end + + private + + def create_organisation_rent_period(xml_document) + organisation = Organisation.find_by(old_org_id: record_field_value(xml_document, "institution")) + + OrganisationRentPeriod.create!( + organisation:, + rent_period: Integer(record_field_value(xml_document, "period")), + ) + end + + def record_field_value(xml_document, field) + field_value(xml_document, "rent-period", field) + end + end +end diff --git a/lib/tasks/data_import.rake b/lib/tasks/data_import.rake index eb4554083..ad15b766e 100644 --- a/lib/tasks/data_import.rake +++ b/lib/tasks/data_import.rake @@ -16,6 +16,8 @@ namespace :core do Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) when "organisation-las" Imports::OrganisationLaImportService.new(storage_service).create_organisation_las(path) + when "organisation-rent-periods" + Imports::OrganisationRentPeriodImportService.new(storage_service).create_organisation_rent_periods(path) else raise "Type #{type} is not supported by data_import" end diff --git a/spec/lib/tasks/data_import_spec.rb b/spec/lib/tasks/data_import_spec.rb index eae18ff33..4e225bfe3 100644 --- a/spec/lib/tasks/data_import_spec.rb +++ b/spec/lib/tasks/data_import_spec.rb @@ -90,6 +90,24 @@ describe "rake core:data_import", type: :task do end end + context "when importing organisation rent period data" do + let(:type) { "organisation-rent-periods" } + let(:import_service) { instance_double(Imports::OrganisationRentPeriodImportService) } + let(:fixture_path) { "spec/fixtures/softwire_imports/organisation_rent_periods" } + + before do + allow(Imports::OrganisationRentPeriodImportService).to receive(:new).and_return(import_service) + end + + it "creates an organisation la from the given XML file" do + expect(StorageService).to receive(:new).with(paas_config_service, instance_name) + expect(Imports::OrganisationRentPeriodImportService).to receive(:new).with(storage_service) + expect(import_service).to receive(:create_organisation_rent_periods).with(fixture_path) + + task.invoke(type, fixture_path) + end + end + it "raises an exception if no parameters are provided" do expect { task.invoke }.to raise_error(/Usage/) end diff --git a/spec/services/imports/organisation_rent_period_import_service_spec.rb b/spec/services/imports/organisation_rent_period_import_service_spec.rb new file mode 100644 index 000000000..a0ee2e6fe --- /dev/null +++ b/spec/services/imports/organisation_rent_period_import_service_spec.rb @@ -0,0 +1,41 @@ +require "rails_helper" + +RSpec.describe Imports::OrganisationRentPeriodImportService do + let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_rent_periods" } + let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" } + let(:old_id) { "ebd22326d33e389e9f1bfd546979d2c05f9e68d6" } + let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") } + let(:storage_service) { instance_double(StorageService) } + + context "when importing data protection confirmations" do + subject(:import_service) { described_class.new(storage_service) } + + before do + allow(storage_service) + .to receive(:list_files) + .and_return(["organisation_rent_period_directory/#{old_id}.xml"]) + allow(storage_service) + .to receive(:get_file_io) + .with("organisation_rent_period_directory/#{old_id}.xml") + .and_return(import_file) + end + + context "when the organisation in the import file doesn't exist in the system" do + it "does not create an organisation rent period record" do + expect { import_service.create_organisation_rent_periods("organisation_rent_period_directory") } + .to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/) + end + end + + context "when the organisation does exist" do + before do + FactoryBot.create(:organisation, old_org_id:) + end + + it "successfully create an organisation rent period record with the expected data" do + import_service.create_organisation_rent_periods("organisation_rent_period_directory") + expect(Organisation.find_by(old_org_id:).organisation_rent_periods.pluck("rent_period")).to eq([1]) + end + end + end +end