Skip to content

Commit

Permalink
Merge pull request #51 from OpenGeoMetadata/validate-dct-ref
Browse files Browse the repository at this point in the history
validate dct_references as parseable serialized json string
  • Loading branch information
Darren Hardy authored Aug 5, 2016
2 parents 68a14dc + b2d6e6e commit 58d1317
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/geo_combine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def to_html
end
end

# Require custom exceptions
require 'geo_combine/exceptions'

# Require translation mixins
require 'geo_combine/formats'
require 'geo_combine/subjects'
Expand Down
6 changes: 6 additions & 0 deletions lib/geo_combine/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GeoCombine
module Exceptions
class InvalidDCTReferences < StandardError
end
end
end
16 changes: 15 additions & 1 deletion lib/geo_combine/geoblacklight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ def to_json
# @return [Boolean]
def valid?
@schema ||= JSON.parse(open("https://raw.githubusercontent.com/geoblacklight/geoblacklight/#{GEOBLACKLIGHT_VERSION}/schema/geoblacklight-schema.json").read)
JSON::Validator.validate!(@schema, to_json, fragment: '#/properties/layer')
JSON::Validator.validate!(@schema, to_json, fragment: '#/properties/layer') && dct_references_validate!
end

##
# Validate dct_references_s
# @return [Boolean]
def dct_references_validate!
return true unless metadata.key?('dct_references_s')
begin
ref = JSON.parse(metadata['dct_references_s'])
raise GeoCombine::Exceptions::InvalidDCTReferences, 'dct_references must be parsed to a Hash' unless ref.is_a?(Hash)
true
rescue JSON::ParserError => e
raise e, "Invalid JSON in dct_references_s: #{e.message}"
end
end

private
Expand Down
46 changes: 44 additions & 2 deletions spec/lib/geo_combine/geoblacklight_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe GeoCombine::Geoblacklight do
include XmlDocs
include JsonDocs
include GeoCombine::Exceptions
let(:full_geobl) { GeoCombine::Geoblacklight.new(full_geoblacklight) }
let(:basic_geobl) { GeoCombine::Geoblacklight.new(basic_geoblacklight) }
describe '#initialize' do
Expand Down Expand Up @@ -31,9 +32,9 @@
expect(valid_json?(basic_geobl.to_json)).to be_truthy
end
end
let(:enhanced_geobl) { GeoCombine::Geoblacklight.new(basic_geoblacklight, 'layer_geom_type_s' => 'esriGeometryPolygon') }
before { enhanced_geobl.enhance_metadata }
describe '#enhance_metadata' do
let(:enhanced_geobl) { GeoCombine::Geoblacklight.new(basic_geoblacklight, 'layer_geom_type_s' => 'esriGeometryPolygon') }
before { enhanced_geobl.enhance_metadata }
it 'calls enhancement methods to validate document' do
expect(enhanced_geobl.valid?).to be true
end
Expand All @@ -54,5 +55,46 @@
it 'an invalid document' do
expect { basic_geobl.valid? }.to raise_error JSON::Schema::ValidationError
end
it 'calls the dct_references_s validator' do
expect(enhanced_geobl).to receive(:dct_references_validate!)
enhanced_geobl.valid?
end
end
describe '#dct_references_validate!' do
context 'with valid document' do
it 'is valid' do
expect(full_geobl.dct_references_validate!).to be true
end
end
context 'with invalid document' do
let(:unparseable_json) do
<<-JSON
{
\"http://schema.org/url\":\"http://example.com/abc123\",,
\"http://schema.org/downloadUrl\":\"http://example.com/abc123/data.zip\"
}
JSON
end
let(:bad_ref) do
GeoCombine::Geoblacklight.new(
basic_geoblacklight, 'dct_references_s' => unparseable_json, 'layer_geom_type_s' => 'esriGeometryPolygon'
)
end
let(:not_hash) do
GeoCombine::Geoblacklight.new(
basic_geoblacklight, 'dct_references_s' => '[{}]', 'layer_geom_type_s' => 'esriGeometryPolygon'
)
end
before do
bad_ref.enhance_metadata
not_hash.enhance_metadata
end
it 'unparseable json' do
expect { bad_ref.dct_references_validate! }.to raise_error JSON::ParserError
end
it 'not a hash' do
expect { not_hash.dct_references_validate! }.to raise_error GeoCombine::Exceptions::InvalidDCTReferences
end
end
end
end

0 comments on commit 58d1317

Please sign in to comment.