diff --git a/app/models/mediaflux/http/asset_metadata_request.rb b/app/models/mediaflux/http/asset_metadata_request.rb index a4d3f4c3..18a5cd73 100644 --- a/app/models/mediaflux/http/asset_metadata_request.rb +++ b/app/models/mediaflux/http/asset_metadata_request.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true module Mediaflux module Http + # Get metadata about an asset in mediaflux + # @example + # metadata_request = Mediaflux::Http::AssetMetadataRequest.new( + # session_token: current_user.mediaflux_session, id: mediaflux_id).metadata class AssetMetadataRequest < Request attr_reader :id diff --git a/app/models/project.rb b/app/models/project.rb index 61d5a27f..d5a27086 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -96,10 +96,13 @@ def self.data_user_projects(user) Project.where("(metadata_json @> ? :: jsonb) OR (metadata_json @> ? :: jsonb)", query_ro, query_rw) end + # If the project hasn't yet been created in mediaflux, create it. + # If it already exists, update it. + # @return [String] the mediaflux id of the project def save_in_mediaflux(session_id:) if mediaflux_id.nil? - self.mediaflux_id = ProjectMediaflux.create!(project: self, session_id: session_id) - save! + ProjectMediaflux.create!(project: self, session_id: session_id) + self.reload Rails.logger.debug "Project #{id} has been created in MediaFlux (asset id #{mediaflux_id})" else ProjectMediaflux.update(project: self, session_id: session_id) diff --git a/app/models/project_mediaflux.rb b/app/models/project_mediaflux.rb index 4a449205..bbd8ba5a 100644 --- a/app/models/project_mediaflux.rb +++ b/app/models/project_mediaflux.rb @@ -50,9 +50,33 @@ def self.create!(project:, session_id:, xml_namespace: nil) raise(StandardError,"An error has occured during project creation, not related to namespace creation or collection creation") end end + project.mediaflux_id = id + project.save! + self.create_accumulators(mediaflux_project_id: id, session_id: session_id) id end + # Create accumulators for all newly created mediaflux projects + # + # @param mediaflux_project_id [] the id of the project that needs accumulators + # @param session_id [] the session id for the user who is currently authenticated to MediaFlux + def self.create_accumulators(mediaflux_project_id:, session_id:) + accum_count = Mediaflux::Http::CreateCollectionAccumulatorRequest.new( + session_token: session_id, + name: "accum-count", + collection: mediaflux_project_id, + type: "collection.asset.count" + ) + accum_count.resolve + accum_size = Mediaflux::Http::CreateCollectionAccumulatorRequest.new( + session_token: session_id, + name: "accum-size", + collection: mediaflux_project_id, + type: "content.all.size" + ) + accum_size.resolve + end + def self.update(project:, session_id:) tigerdata_values = project_values(project: project) Mediaflux::Http::AssetUpdateRequest.new(session_token: session_id, id: project.mediaflux_id, tigerdata_values: tigerdata_values).resolve diff --git a/app/services/test_project_generator.rb b/app/services/test_project_generator.rb index 1db423f6..13e4cd7a 100644 --- a/app/services/test_project_generator.rb +++ b/app/services/test_project_generator.rb @@ -12,11 +12,7 @@ def initialize(user:, number:, project_prefix:) def generate project = create_project session_id = user.mediaflux_session - id = project.save_in_mediaflux(session_id:) - accum_count = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(session_token: session_id, name: "accum-count", collection: id, type: "collection.asset.count") - accum_count.resolve - accum_size = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(session_token: session_id, name: "accum-size", collection: id, type: "content.all.size") - accum_size.resolve + project.save_in_mediaflux(session_id:) project.save! project end diff --git a/spec/models/mediaflux/http/asset_metadata_request_spec.rb b/spec/models/mediaflux/http/asset_metadata_request_spec.rb index 97ec098d..b45b73d8 100644 --- a/spec/models/mediaflux/http/asset_metadata_request_spec.rb +++ b/spec/models/mediaflux/http/asset_metadata_request_spec.rb @@ -75,8 +75,8 @@ expect(metadata[:collection]).to be_truthy expect(metadata[:path].include?(valid_project.metadata_json["project_directory"])).to be_truthy expect(metadata[:type]).to eq("application/arc-asset-collection") - expect(metadata[:size]).to eq("") # accumulators are not added to project when created - expect(metadata[:total_file_count]).to eq("") # accumulators are not added to project when created + expect(metadata[:size]).to eq("0 bytes") + expect(metadata[:total_file_count]).to eq("0") expect(metadata[:quota_allocation]).to eq("") # quotas are not added to project when created expect(metadata[:project_id]).to eq("10.34770/tbd") expect(metadata[:project_directory]).to eq(valid_project.project_directory) diff --git a/spec/models/project_mediaflux_spec.rb b/spec/models/project_mediaflux_spec.rb index 536bbd67..936795e8 100644 --- a/spec/models/project_mediaflux_spec.rb +++ b/spec/models/project_mediaflux_spec.rb @@ -36,6 +36,20 @@ expect(namespace_metadata[:description]).to match(/Namespace for/) end + describe "accumulators", connect_to_mediaflux: true do + it "adds accumulators when it creates a project in mediaflux" do + project = FactoryBot.create(:project_with_doi) + described_class.create!(project: project, session_id: current_user.mediaflux_session) + # project.save_in_mediaflux(session_id: current_user.mediaflux_session) + metadata = Mediaflux::Http::AssetMetadataRequest.new( + session_token: current_user.mediaflux_session, + id: project.mediaflux_id + ).metadata + expect(metadata[:accumulators]).not_to be_empty + expect(metadata[:accumulators].size).to eq 2 + end + end + context "when the name is already taken" do it "raises an error" do # Make the project once diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 4868ba8b..c1a23b77 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -168,14 +168,13 @@ end end - describe "#save_in_mediaflux" do - let(:project) { FactoryBot.create(:project) } + describe "#save_in_mediaflux", connect_to_mediaflux: true do + let(:user) { FactoryBot.create(:user) } + let(:project) { FactoryBot.create(:project_with_doi) } it "calls ProjectMediaflux to create the project and save the id" do - allow(ProjectMediaflux).to receive(:create!).with(project: project, session_id: "111222333").and_return(27) - project.save_in_mediaflux(session_id: "111222333") - expect(ProjectMediaflux).to have_received(:create!) - expect(project.mediaflux_id).to eq(27) - expect(project).not_to be_changed # the method saves the id to the database + expect(project.mediaflux_id).to be nil + project.save_in_mediaflux(session_id: user.mediaflux_session) + expect(project.mediaflux_id).not_to be nil end end diff --git a/spec/system/project_show_spec.rb b/spec/system/project_show_spec.rb index 10391ddd..4a505b21 100644 --- a/spec/system/project_show_spec.rb +++ b/spec/system/project_show_spec.rb @@ -142,7 +142,7 @@ click_on("Review Contents") expect(page).to have_content("Project Contents") expect(page).to have_content("File Count") - expect(find(:css, "#file_count").text).to eq "16" + expect(find(:css, "#file_count").text).to eq "1616" # Be able to return to the dashboard expect(page).to have_selector(:link_or_button, "Return to Dashboard")