From 422e0a6a96802ca6a858ad2836a25539dd0ff4af Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Wed, 7 Feb 2024 23:34:33 +0100 Subject: [PATCH] Feature: api endpoint returns json-ld for the element with that URI --- Dockerfile | 1 + Gemfile | 6 +- Gemfile.lock | 14 ++- bin/ontoportal | 4 +- .../dereference_resource_controller.rb | 89 ++++++++++++++ docker-compose.yml | 9 +- models/simple_wrappers.rb | 2 + .../test_dereference_resource_controller.rb | 115 ++++++++++++++++++ 8 files changed, 231 insertions(+), 9 deletions(-) create mode 100644 controllers/dereference_resource_controller.rb create mode 100644 test/controllers/test_dereference_resource_controller.rb diff --git a/Dockerfile b/Dockerfile index 6294e102..91e7bced 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \ openjdk-11-jre-headless \ raptor2-utils \ wait-for-it \ + libraptor2-dev \ && rm -rf /var/lib/apt/lists/* RUN mkdir -p /srv/ontoportal/ontologies_api diff --git a/Gemfile b/Gemfile index caa9818a..78b59262 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,8 @@ gem 'sinatra', '~> 1.0' gem 'sinatra-advanced-routes' gem 'sinatra-contrib', '~> 1.0' gem 'request_store' +gem 'rdf-raptor' + # Rack middleware gem 'ffi' @@ -44,12 +46,12 @@ gem 'haml', '~> 5.2.2' # pin see https://github.com/ncbo/ontologies_api/pull/107 gem 'redcarpet' # NCBO gems (can be from a local dev path or from rubygems/git) -gem 'goo', git: 'https://github.com/ontoportal-lirmm/goo.git', branch: 'development' +gem 'goo', github: 'ontoportal-lirmm/goo', branch: 'development' gem 'ncbo_annotator', git: 'https://github.com/ontoportal-lirmm/ncbo_annotator.git', branch: 'master' gem 'ncbo_cron', git: 'https://github.com/ontoportal-lirmm/ncbo_cron.git', branch: 'master' gem 'ncbo_ontology_recommender', git: 'https://github.com/ncbo/ncbo_ontology_recommender.git', branch: 'master' gem 'sparql-client', github: 'ontoportal-lirmm/sparql-client', branch: 'master' -gem 'ontologies_linked_data', git: 'https://github.com/ontoportal-lirmm/ontologies_linked_data.git', branch: 'development' +gem 'ontologies_linked_data', git: 'https://github.com/ontoportal-lirmm/ontologies_linked_data.git', branch: 'feature/sparql-fetch-related-triples' group :development do # bcrypt_pbkdf and ed35519 is required for capistrano deployments when using ed25519 keys; see https://github.com/miloserdow/capistrano-deploy/issues/42 diff --git a/Gemfile.lock b/Gemfile.lock index 5d92b7be..a1ca7fb2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,7 @@ GIT GIT remote: https://github.com/ontoportal-lirmm/goo.git - revision: 27a38376d7372d46cd39790dbae3eeed0fec15bb + revision: 6187c205a1310c2f8a72fe8e07fcf99477060fa9 branch: development specs: goo (0.0.2) @@ -54,8 +54,8 @@ GIT GIT remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git - revision: 0de5f3b2db02a5d2c008ace73a34d586c24e67a7 - branch: development + revision: 3745f308776d82bff5b346cfa6eebc143daf14bf + branch: feature/sparql-fetch-related-triples specs: ontologies_linked_data (0.0.1) activesupport @@ -245,7 +245,7 @@ GEM mlanett-redis-lock (0.2.7) redis multi_json (1.15.0) - multipart-post (2.3.0) + multipart-post (2.4.0) mutex_m (0.2.0) net-http-persistent (2.9.4) net-imap (0.4.10) @@ -295,6 +295,9 @@ GEM rake (10.5.0) rdf (1.0.8) addressable (>= 2.2) + rdf-raptor (1.0.1) + ffi (>= 1.0) + rdf (>= 1.0.0) redcarpet (3.6.0) redis (4.8.1) redis-activesupport (5.3.0) @@ -418,6 +421,7 @@ DEPENDENCIES rack-test rack-timeout rake (~> 10.0) + rdf-raptor redcarpet redis (~> 4.8.1) redis-activesupport @@ -436,4 +440,4 @@ DEPENDENCIES webmock BUNDLED WITH - 2.3.23 + 2.4.22 diff --git a/bin/ontoportal b/bin/ontoportal index 4840dad3..d488d0a4 100755 --- a/bin/ontoportal +++ b/bin/ontoportal @@ -207,7 +207,9 @@ test() { local custom_command="bundle exec rake test TEST='$test_path' TESTOPTS='$test_options'" echo "run : $custom_command" - run_command "$custom_command" "${all_arguments[@]}" + #run_command "$custom_command" "${all_arguments[@]}" + run_command "$custom_command" --old-path /home/bourouche/PFE/backend/ontologies_linked_data --goo-path /home/bourouche/PFE/backend/goo + } # Function to handle the "run" option diff --git a/controllers/dereference_resource_controller.rb b/controllers/dereference_resource_controller.rb new file mode 100644 index 00000000..e2fcedb3 --- /dev/null +++ b/controllers/dereference_resource_controller.rb @@ -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: "test3@example.org"}], + 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 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index f7325381..94d84143 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,12 @@ x-app: &app - image: agroportal/ontologies_api:development + #image: agroportal/ontologies_api:development + build: + context: . + args: + RUBY_VERSION: '2.7' + # Increase the version number in the image tag every time Dockerfile or its arguments is changed + image: ontologies_api-dev:0.0.2 + environment: &env BUNDLE_PATH: /srv/ontoportal/bundle # default bundle config resolves to /usr/local/bundle/config inside of the container diff --git a/models/simple_wrappers.rb b/models/simple_wrappers.rb index e4097aff..f6aeb027 100644 --- a/models/simple_wrappers.rb +++ b/models/simple_wrappers.rb @@ -29,3 +29,5 @@ ProvisionalRelation = LinkedData::Models::ProvisionalRelation SearchHelper = Sinatra::Helpers::SearchHelper + +Resource = LinkedData::Models::Resource \ No newline at end of file diff --git a/test/controllers/test_dereference_resource_controller.rb b/test/controllers/test_dereference_resource_controller.rb new file mode 100644 index 00000000..77e8b7b9 --- /dev/null +++ b/test/controllers/test_dereference_resource_controller.rb @@ -0,0 +1,115 @@ +require_relative '../test_case' + +class TestImadController < TestCase + + def self.before_suite + #LinkedData::TestCase.backend_4s_delete +=begin + data = %( + @prefix ex: . + @prefix rdf: <#{Goo.vocabulary(:rdf)}> . + @prefix owl: <#{Goo.vocabulary(:owl)}> . + @prefix xsd: . + + 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: "test3@example.org"}], + 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 \ No newline at end of file