|
42 | 42 | expect(service.prefix_id(id)).to eq('ext-store/es/si/-e/xt/essi-ext-store-spec') |
43 | 43 | end |
44 | 44 | end |
| 45 | + |
| 46 | + let(:file_set) { FactoryBot.build(:file_set) } |
| 47 | + let(:external_id) { "s3_id" } |
| 48 | + let(:external_location) { "s3://#{external_id}" } |
| 49 | + describe "#external?" do |
| 50 | + context "when stored in S3" do |
| 51 | + before { allow(file_set).to receive(:content_location).and_return(external_location) } |
| 52 | + it "returns true" do |
| 53 | + expect(service.external?(file_set)).to eq true |
| 54 | + end |
| 55 | + end |
| 56 | + context "when stored in Fedora" do |
| 57 | + it "returns false" do |
| 58 | + expect(service.external?(file_set)).to eq false |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe "#external_id" do |
| 64 | + context "when stored in S3" do |
| 65 | + before { allow(file_set).to receive(:content_location).and_return(external_location) } |
| 66 | + it "returns the S3 internal id" do |
| 67 | + expect(service.external_id(file_set)).to eq external_id |
| 68 | + end |
| 69 | + end |
| 70 | + context "when stored in Fedora" do |
| 71 | + it "returns nil" do |
| 72 | + expect(file_set.external_id).to be_nil |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe "#find_or_retrieve" do |
| 78 | + shared_examples "find_or_retrieve examples" do |argument_filepath| |
| 79 | + context "when file is stored in S3" do |
| 80 | + let(:output_filepath) { 'filepath_from_s3' } |
| 81 | + before do |
| 82 | + allow(file_set).to receive(:content_location).and_return("s3://server/external_id") |
| 83 | + allow(service).to receive(:get).and_return(double(body: nil)) |
| 84 | + allow(Hyrax::WorkingDirectory).to receive(:copy_stream_to_working_directory).and_return(output_filepath) |
| 85 | + end |
| 86 | + it "retrieves external file content" do |
| 87 | + expect(service).to receive(:get) |
| 88 | + service.find_or_retrieve(file_set, filepath: argument_filepath) |
| 89 | + end |
| 90 | + it "copies stream to working directory" do |
| 91 | + expect(Hyrax::WorkingDirectory).to receive(:copy_stream_to_working_directory) |
| 92 | + service.find_or_retrieve(file_set, filepath: argument_filepath) |
| 93 | + end |
| 94 | + it "returns filepath" do |
| 95 | + expect(service.find_or_retrieve(file_set, filepath: argument_filepath)).to eq output_filepath |
| 96 | + end |
| 97 | + end |
| 98 | + context "when file is stored in Fedora" do |
| 99 | + it "logs warning" do |
| 100 | + expect(Rails.logger).to receive(:warn) |
| 101 | + service.find_or_retrieve(file_set, filepath: argument_filepath) |
| 102 | + end |
| 103 | + it "returns nil" do |
| 104 | + expect(service.find_or_retrieve(file_set, filepath: argument_filepath)).to be_nil |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + context "when filepath provided" do |
| 109 | + let(:argument_filepath) { '/tmp/existing_file.txt' } |
| 110 | + context "when file present" do |
| 111 | + before { allow(File).to receive(:exist?).with(argument_filepath).and_return(true) } |
| 112 | + it "returns the filepath" do |
| 113 | + expect(service.find_or_retrieve(file_set, filepath: argument_filepath)).to eq argument_filepath |
| 114 | + end |
| 115 | + end |
| 116 | + context "when file absent" do |
| 117 | + include_examples "find_or_retrieve examples", '/tmp/existing_file.txt' |
| 118 | + end |
| 119 | + end |
| 120 | + context "when filepath not provided" do |
| 121 | + include_examples "find_or_retrieve examples", nil |
| 122 | + end |
| 123 | + end |
45 | 124 | end |
0 commit comments