From d7c2ff5490aab074aa236c8b6002cf336af3570a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Tue, 2 Aug 2022 12:00:08 +0100 Subject: [PATCH] Implement storage folder check --- app/services/imports/core_import_service.rb | 22 +++++++ app/services/storage_service.rb | 5 ++ spec/services/storage_service_spec.rb | 67 ++++++++++++++++----- 3 files changed, 79 insertions(+), 15 deletions(-) create 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 new file mode 100644 index 000000000..77ba07519 --- /dev/null +++ b/app/services/imports/core_import_service.rb @@ -0,0 +1,22 @@ +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/app/services/storage_service.rb b/app/services/storage_service.rb index 1e9e374ce..766af69fa 100644 --- a/app/services/storage_service.rb +++ b/app/services/storage_service.rb @@ -13,6 +13,11 @@ class StorageService .flat_map { |response| response.contents.map(&:key) } end + def folder_present?(folder) + response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1) + response.key_count == 1 + end + def get_file_io(file_name) @client.get_object(bucket: @configuration.bucket_name, key: file_name) .body diff --git a/spec/services/storage_service_spec.rb b/spec/services/storage_service_spec.rb index 4e79228f6..d19d51ed8 100644 --- a/spec/services/storage_service_spec.rb +++ b/spec/services/storage_service_spec.rb @@ -99,7 +99,7 @@ RSpec.describe StorageService do end end - context "when we create a storage service and list files" do + context "when we create a storage service" do subject(:storage_service) { described_class.new(PaasConfigurationService.new, instance_name) } let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) } @@ -110,22 +110,59 @@ RSpec.describe StorageService do allow(Aws::S3::Client).to receive(:new).and_return(s3_client_stub) end - it "returns a list with all present file names in a given folder" do - expected_filenames = %w[my_folder/my_file1.xml my_folder/my_file2.xml] - s3_client_stub.stub_responses(:list_objects_v2, { - contents: [ - { - key: expected_filenames[0], - }, - { - key: expected_filenames[1], - }, - ], - }) + context "and we list files based on a prefix" do + let(:expected_filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] } + + before do + s3_client_stub.stub_responses(:list_objects_v2, { + contents: [ + { + key: expected_filenames[0], + }, + { + key: expected_filenames[1], + }, + ], + }) + end + + it "returns a list with all present file names in a given folder" do + filenames = storage_service.list_files("my_folder") + expect(filenames).to eq(expected_filenames) + end + end - filenames = storage_service.list_files("my_folder") + context "and we check for an existing folder" do + before do + expected_filenames = %w[my_folder/my_file1.xml] + s3_client_stub.stub_responses(:list_objects_v2, { + key_count: 1, + contents: [ + { + key: expected_filenames[0], + }, + ], + }) + end + + it "returns true" do + response = storage_service.folder_present?("my_folder") + expect(response).to be_truthy + end + end - expect(filenames).to eq(expected_filenames) + context "and we check for a folder that does not exists" do + before do + s3_client_stub.stub_responses(:list_objects_v2, { + key_count: 0, + contents: [], + }) + end + + it "returns false" do + response = storage_service.folder_present?("my_folder") + expect(response).to be_falsey + end end end end