forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: api endpoint returns json-ld for the element with that URI
- Loading branch information
1 parent
d515a94
commit 422e0a6
Showing
8 changed files
with
231 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require_relative '../test/test_case' | ||
|
||
|
||
class ImadController < ApplicationController | ||
|
||
namespace '/dereference_resource' do | ||
|
||
get do | ||
raise error 405, "Method Not Allowd: This route must be provided via POST request with acronym, uri, output_format parameters" | ||
end | ||
|
||
def set_vars | ||
@@acronym = "TST" | ||
@@name = "Test Ontology" | ||
@@test_file = File.expand_path("../../test/data/ontology_files/BRO_v3.1.owl", __FILE__) | ||
@@file_params = { | ||
name: @@name, | ||
hasOntologyLanguage: "OWL", | ||
administeredBy: "tim", | ||
"file" => Rack::Test::UploadedFile.new(@@test_file, ""), | ||
released: DateTime.now.to_s, | ||
contact: [{name: "test_name", email: "[email protected]"}], | ||
URI: 'https://test.com/test', | ||
status: 'production', | ||
description: 'ontology description' | ||
} | ||
@@status_uploaded = "UPLOADED" | ||
@@status_rdf = "RDF" | ||
end | ||
|
||
def create_user | ||
username = "tim" | ||
test_user = User.new(username: username, email: "#{username}@example.org", password: "password") | ||
test_user.save if test_user.valid? | ||
@@user = test_user.valid? ? test_user : User.find(username).first | ||
end | ||
|
||
def create_onts | ||
ont = Ontology.new(acronym: @@acronym, name: @@name, administeredBy: [@@user]) | ||
end | ||
|
||
post do | ||
set_vars() | ||
create_user() | ||
create_onts() | ||
|
||
acronym = params[:acronym] | ||
uri = params[:uri] | ||
output_format = params[:output_format].presence || 'jsonld' | ||
acronym = URI.decode_www_form_component(acronym) | ||
uri = URI.decode_www_form_component(uri) | ||
unless valid_url?(acronym) && valid_url?(uri) | ||
raise error 500, "INVALID URLs" | ||
return | ||
end | ||
|
||
r = Resource.new(acronym, uri) | ||
case output_format | ||
when 'jsonld' | ||
content_type 'application/json' | ||
reply JSON.parse(r.to_json) | ||
when 'json' | ||
content_type 'application/json' | ||
reply JSON.parse(r.to_json) | ||
when 'xml' | ||
content_type 'application/xml' | ||
reply r.to_xml | ||
when 'turtle' | ||
content_type 'text/turtle' | ||
reply r.to_turtle | ||
when 'ntriples' | ||
content_type 'application/n-triples' | ||
reply r.to_ntriples | ||
else | ||
raise error 500, "Invalid output format" | ||
end | ||
|
||
end | ||
|
||
private | ||
|
||
def valid_url?(url) | ||
uri = URI.parse(url) | ||
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) | ||
rescue URI::InvalidURIError | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
test/controllers/test_dereference_resource_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
require_relative '../test_case' | ||
|
||
class TestImadController < TestCase | ||
|
||
def self.before_suite | ||
#LinkedData::TestCase.backend_4s_delete | ||
=begin | ||
data = %( | ||
@prefix ex: <http://example.org/> . | ||
@prefix rdf: <#{Goo.vocabulary(:rdf)}> . | ||
@prefix owl: <#{Goo.vocabulary(:owl)}> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
ex:TestSubject1 rdf:type owl:Ontology . | ||
ex:TestSubject1 ex:TestPredicate11 "TestObject11" . | ||
ex:TestSubject1 ex:TestPredicate12 ex:test . | ||
ex:TestSubject1 ex:TestPredicate13 1 . | ||
ex:TestSubject1 ex:TestPredicate14 true . | ||
ex:TestSubject1 ex:TestPredicate15 "1.9"^^xsd:float . | ||
ex:TestSubject2 ex:TestPredicate2 1.9 . | ||
) | ||
graph = "http://example.org/test_graph" | ||
Goo.sparql_data_client.execute_append_request(graph, data, "application/x-turtle") | ||
=end | ||
_set_vars | ||
_create_user | ||
_create_onts | ||
end | ||
|
||
|
||
def self._set_vars | ||
@@acronym = "TST" | ||
@@name = "Test Ontology" | ||
@@test_file = File.expand_path("../../data/ontology_files/BRO_v3.1.owl", __FILE__) | ||
@@file_params = { | ||
name: @@name, | ||
hasOntologyLanguage: "OWL", | ||
administeredBy: "tim", | ||
"file" => Rack::Test::UploadedFile.new(@@test_file, ""), | ||
released: DateTime.now.to_s, | ||
contact: [{name: "test_name", email: "[email protected]"}], | ||
URI: 'https://test.com/test', | ||
status: 'production', | ||
description: 'ontology description' | ||
} | ||
@@status_uploaded = "UPLOADED" | ||
@@status_rdf = "RDF" | ||
end | ||
|
||
def self._create_user | ||
username = "tim" | ||
test_user = User.new(username: username, email: "#{username}@example.org", password: "password") | ||
test_user.save if test_user.valid? | ||
@@user = test_user.valid? ? test_user : User.find(username).first | ||
end | ||
|
||
def self._create_onts | ||
ont = Ontology.new(acronym: @@acronym, name: @@name, administeredBy: [@@user]) | ||
ont.save | ||
end | ||
|
||
def submit_ontology | ||
post "/ontologies/#{@@acronym}/submissions", @@file_params | ||
assert_equal(201, last_response.status, msg=get_errors(last_response)) | ||
sub = MultiJson.load(last_response.body) | ||
get "/ontologies/#{@@acronym}" | ||
ont = MultiJson.load(last_response.body) | ||
assert ont["acronym"].eql?(@@acronym) | ||
end | ||
|
||
|
||
def test_imad_controller | ||
submit_ontology() | ||
|
||
post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim" } | ||
puts | ||
puts last_response.body | ||
puts | ||
assert last_response.ok? | ||
|
||
|
||
post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "json"} | ||
puts | ||
puts last_response.body | ||
puts | ||
assert last_response.ok? | ||
|
||
|
||
post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "xml"} | ||
puts | ||
puts last_response.body | ||
puts | ||
assert last_response.ok? | ||
|
||
|
||
post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "ntriples"} | ||
puts | ||
puts last_response.body | ||
puts | ||
assert last_response.ok? | ||
|
||
|
||
post "/dereference_resource", { acronym: "http://data.bioontology.org/ontologies/TST/submissions/1", uri: "http://data.bioontology.org/users/tim", output_format: "turtle"} | ||
puts | ||
puts last_response.body | ||
puts | ||
assert last_response.ok? | ||
|
||
# Cleanup | ||
#delete "/ontologies/#{@@acronym}/submissions/#{sub['submissionId']}" | ||
#assert_equal(204, last_response.status, msg=get_errors(last_response)) | ||
|
||
end | ||
|
||
end |