Skip to content

Commit

Permalink
Updated the works controller to utilize DataCiteSerialization for XML…
Browse files Browse the repository at this point in the history
… validation (#1615)

Also capture expections during validation
  • Loading branch information
carolyncole authored Nov 27, 2023
1 parent aa0e173 commit 26410c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
16 changes: 8 additions & 8 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ def datacite
def datacite_validate
@errors = []
@work = Work.find(params[:id])
datacite_xml = Nokogiri::XML(@work.to_xml)
schema_location = Rails.root.join("config", "schema")
Dir.chdir(schema_location) do
xsd = Nokogiri::XML::Schema(File.read("datacite_4_4.xsd"))
xsd.validate(datacite_xml).each do |error|
@errors << error
end
end
datacite_serialization = work.resource.datacite_serialization
datacite_serialization.valid?
@errors = datacite_serialization.errors
rescue ArgumentError => error
argument_path = error.backtrace_locations.first.path
argument_file = argument_path.split("/").last
argument_name = argument_file.split(".").first
@errors << "#{argument_name.titleize}: #{error.message}"
end

def readme_select
Expand Down
6 changes: 5 additions & 1 deletion app/models/pdc_metadata/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ def other_titles

def to_xml
xml_declaration = '<?xml version="1.0"?>'
xml_body = PDCSerialization::Datacite.new_from_work_resource(self).to_xml
xml_body = datacite_serialization.to_xml
xml_declaration + "\n" + xml_body + "\n"
end

def datacite_serialization
@datacite_serialization ||= PDCSerialization::Datacite.new_from_work_resource(self)
end

class << self
# Creates a PDCMetadata::Resource from a JSONB postgres field
# This jsonb_hash can be created by running JSON.parse(pdc_metadata_resource.to_json)
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1549,5 +1549,28 @@
citation = DatasetCitation.new(creators, [work.resource.publication_year], work.resource.titles.first.title, work.resource.resource_type, work.resource.publisher, work.resource.doi)
expect(response.body).to eq(citation.bibtex)
end

it "validates the datacite xml" do
sign_in(work.created_by_user)
get :datacite_validate, params: { id: work.id }
expect(response).to be_successful
expect(assigns[:errors]).to eq([])
end
end

context "a work with invalid datacite xml" do
let(:work) do
work = FactoryBot.create :draft_work
work.resource.individual_contributors = [PDCMetadata::Creator.new_individual_contributor("Sally", "Smith", "", "", 0)]
work.save
work
end

it "validates the datacite xml" do
sign_in(work.created_by_user)
get :datacite_validate, params: { id: work.id }
expect(response).to be_successful
expect(assigns[:errors]).to eq(["Contributor: Type cannot be nil"])
end
end
end
2 changes: 1 addition & 1 deletion spec/system/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def value_for(label)
stub_s3
stub_ark
sign_in user
allow_any_instance_of(PDCMetadata::Resource).to receive(:to_xml).and_return(invalid_xml)
allow_any_instance_of(PDCSerialization::Datacite).to receive(:to_xml).and_return(invalid_xml)
end

it "Validates the record and prints any errors", js: true do
Expand Down

0 comments on commit 26410c5

Please sign in to comment.