From c6aab79af992ade840b42d0803e926211a92743d Mon Sep 17 00:00:00 2001 From: Wendel Fabian Chinsamy Date: Thu, 24 Oct 2024 17:19:45 +0200 Subject: [PATCH 1/4] do not send events to the event busy unless datacite_crossref --- app/models/related_identifier.rb | 20 +++++++++++--------- spec/models/related_identifier_spec.rb | 26 +++++++++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/models/related_identifier.rb b/app/models/related_identifier.rb index 5b1650a8..32ade414 100644 --- a/app/models/related_identifier.rb +++ b/app/models/related_identifier.rb @@ -1,5 +1,6 @@ class RelatedIdentifier < Base LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/".freeze + DATACITE_CROSSREF = "datacite_crossref" include Helpable include Cacheable @@ -58,23 +59,24 @@ def push_data(result, _options = {}) def self.push_item(item) attributes = item.fetch("attributes", {}) doi = attributes.fetch("doi", nil) + return nil unless doi.present? && cached_doi_ra(doi) == "DataCite" pid = normalize_doi(doi) - related_doi_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers", - nil)).select do |r| + + related_doi_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers", nil)).select do |r| r["relatedIdentifierType"] == "DOI" end + registration_agencies = {} push_items = Array.wrap(related_doi_identifiers).reduce([]) do |ssum, iitem| - related_identifier = iitem.fetch("relatedIdentifier", - nil).to_s.strip.downcase + related_identifier = iitem.fetch("relatedIdentifier", nil).to_s.strip.downcase obj_id = normalize_doi(related_identifier) prefix = validate_prefix(related_identifier) + unless registration_agencies[prefix] - registration_agencies[prefix] = - cached_doi_ra(related_identifier) + registration_agencies[prefix] = cached_doi_ra(related_identifier) end if registration_agencies[prefix].nil? @@ -87,7 +89,7 @@ def self.push_item(item) source_token = ENV["DATACITE_RELATED_SOURCE_TOKEN"] obj = cached_datacite_response(obj_id) elsif registration_agencies[prefix] == "Crossref" - source_id = "datacite_crossref" + source_id = DATACITE_CROSSREF source_token = ENV["DATACITE_CROSSREF_SOURCE_TOKEN"] obj = cached_crossref_response(obj_id) elsif registration_agencies[prefix].present? @@ -112,7 +114,6 @@ def self.push_item(item) "subj" => subj, "obj" => obj } end - ssum end @@ -158,7 +159,8 @@ def self.push_item(item) end # send to Event Data Bus - if ENV["EVENTDATA_TOKEN"].present? + # we only send datacite_crossref events to the bus + if ENV["EVENTDATA_TOKEN"].present? && iiitem['source_id'] == DATACITE_CROSSREF iiitem = set_event_for_bus(iiitem) host = ENV["EVENTDATA_URL"] diff --git a/spec/models/related_identifier_spec.rb b/spec/models/related_identifier_spec.rb index ff9990d8..8af01029 100644 --- a/spec/models/related_identifier_spec.rb +++ b/spec/models/related_identifier_spec.rb @@ -104,9 +104,27 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).twice + expect(Maremma).to have_received(:post).once expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") + end + + it "does not push the event to the event data bus when source_id is not datacite_crossref" do + related_identifier = RelatedIdentifier.new + allow(ENV).to receive(:[]).with("DATACITE_CROSSREF_SOURCE_TOKEN").and_return("fake-token") + allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") + allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier") + allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite") + allow(RelatedIdentifier).to receive(:cached_doi_ra).with("https://doi.org/10.5678/related").and_return("Crossref") + allow(RelatedIdentifier).to receive(:cached_doi_ra).with("https://doi.org/10.1234/example").and_return("DataCite") + allow(RelatedIdentifier).to receive(:cached_crossref_response).and_return({}) + allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({}) + allow(related_identifier).to receive(:set_event_for_bus).and_return({}) + allow(Rails.logger).to receive(:info) + + expect(RelatedIdentifier.push_item(item)).to eq(1) + expect(Maremma).to have_received(:post).twice + expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") end end @@ -135,10 +153,9 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).twice + expect(Maremma).to have_received(:post).once expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.") - expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.") end end @@ -169,10 +186,9 @@ allow(Rails.logger).to receive(:error) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).twice + expect(Maremma).to have_received(:post).once expect(Rails.logger).to have_received(:error).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error: An error occurred during the put request.") - expect(Rails.logger).to have_received(:error).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error:") end it "Does not sent an event to Event Data Bus." do From 5daff4b0cc540bb4c9da0ab0f57b191fe86927a6 Mon Sep 17 00:00:00 2001 From: Wendel Fabian Chinsamy Date: Fri, 25 Oct 2024 09:40:27 +0200 Subject: [PATCH 2/4] refactor specs in order to make expectations more precise --- spec/models/related_identifier_spec.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spec/models/related_identifier_spec.rb b/spec/models/related_identifier_spec.rb index 8af01029..619acfc6 100644 --- a/spec/models/related_identifier_spec.rb +++ b/spec/models/related_identifier_spec.rb @@ -104,7 +104,7 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).once + expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") end @@ -123,7 +123,8 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).twice + expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once + expect(Maremma).to have_received(:post).with("https://fake.eventdataurl.com/events", anything).once expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") end @@ -153,7 +154,7 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).once + expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.") end @@ -186,7 +187,7 @@ allow(Rails.logger).to receive(:error) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).once + expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once expect(Rails.logger).to have_received(:error).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error: An error occurred during the put request.") end @@ -203,7 +204,7 @@ allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) - expect(Maremma).to have_received(:post).once + expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related was not sent to Event Data Bus.") end From 59ec46b73ac269dbc5bb26c62500a09c5867fc9c Mon Sep 17 00:00:00 2001 From: Wendel Fabian Chinsamy Date: Mon, 28 Oct 2024 09:41:00 +0200 Subject: [PATCH 3/4] rename test to match expectation --- spec/models/related_identifier_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/related_identifier_spec.rb b/spec/models/related_identifier_spec.rb index 619acfc6..bf328fdb 100644 --- a/spec/models/related_identifier_spec.rb +++ b/spec/models/related_identifier_spec.rb @@ -109,7 +109,7 @@ expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") end - it "does not push the event to the event data bus when source_id is not datacite_crossref" do + it "does push the event to the event data bus when source_id is not datacite_crossref" do related_identifier = RelatedIdentifier.new allow(ENV).to receive(:[]).with("DATACITE_CROSSREF_SOURCE_TOKEN").and_return("fake-token") allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") From 9c728b743b8a1e98720ae55711232b2547b8648e Mon Sep 17 00:00:00 2001 From: Mike Bennett <93522067+digitaldogsbody@users.noreply.github.com> Date: Mon, 28 Oct 2024 07:46:32 +0000 Subject: [PATCH 4/4] Fix test name slightly more --- spec/models/related_identifier_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/related_identifier_spec.rb b/spec/models/related_identifier_spec.rb index bf328fdb..1e776954 100644 --- a/spec/models/related_identifier_spec.rb +++ b/spec/models/related_identifier_spec.rb @@ -109,7 +109,7 @@ expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.") end - it "does push the event to the event data bus when source_id is not datacite_crossref" do + it "does push the event to the event data bus when source_id is datacite_crossref" do related_identifier = RelatedIdentifier.new allow(ENV).to receive(:[]).with("DATACITE_CROSSREF_SOURCE_TOKEN").and_return("fake-token") allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi")