summaryrefslogtreecommitdiff
path: root/test/rest
diff options
context:
space:
mode:
Diffstat (limited to 'test/rest')
-rw-r--r--test/rest/compound.rb47
-rw-r--r--test/rest/dataset.rb88
-rw-r--r--test/rest/feature.rb98
-rw-r--r--test/rest/task.rb101
4 files changed, 334 insertions, 0 deletions
diff --git a/test/rest/compound.rb b/test/rest/compound.rb
new file mode 100644
index 0000000..c47c0be
--- /dev/null
+++ b/test/rest/compound.rb
@@ -0,0 +1,47 @@
+require_relative "setup.rb"
+
+begin
+ puts "Service URI is: #{$compound[:uri]}"
+rescue
+ puts "Configuration Error: $compound[:uri] is not defined in: " + File.join(ENV["HOME"],".opentox","config","test.rb")
+ exit
+end
+
+class CompoundTest < MiniTest::Test
+
+ def test_compound_ambit
+ c = OpenTox::Compound.new "https://apps.ideaconsult.net/ambit2/compound/144036"
+ assert_equal "InChI=1S/C6H11NO2/c1-3-5-6(4-2)7(8)9/h5H,3-4H2,1-2H3", c.inchi
+ assert_equal "CCC=C(CC)[N+](=O)[O-]", c.smiles
+ end
+end
+
+class CompoundServiceTest < MiniTest::Test
+
+ def test_formats # test supported formats from service
+ formats = ["chemical/x-daylight-smiles", "chemical/x-inchi", "chemical/x-mdl-sdfile", "chemical/x-mdl-molfile", "image/png", "text/html"]
+ formats.each do |format|
+ response = OpenTox::RestClientWrapper.get "#{$compound[:uri]}/InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H", {}, { :accept => format }
+ assert_equal format, response.headers[:content_type]
+ assert_equal 200, response.code
+ end
+ end
+
+end
+
+class CompoundAPITest < MiniTest::Test
+
+ def test_apifile # test if api file is present
+ format = "application/json"
+ response = OpenTox::RestClientWrapper.get "#{$compound[:uri]}/api/compound.json", {}, { :accept => format }
+ assert_equal format, response.headers[:content_type]
+ assert_equal 200, response.code
+ end
+
+ def test_swagger_valid
+ res = OpenTox::RestClientWrapper.get "http://online.swagger.io/validator/debug?url=#{$compound[:uri]}/api/compound.json"
+ assert_equal res, "[]", "Swagger API document #{$compound[:uri]}/api/compound.json is not valid: \n#{res}"
+ end
+
+end
+
diff --git a/test/rest/dataset.rb b/test/rest/dataset.rb
new file mode 100644
index 0000000..4401a36
--- /dev/null
+++ b/test/rest/dataset.rb
@@ -0,0 +1,88 @@
+# TODO: check json specification at https://github.com/opentox-api/api-specification/issues/2
+
+require_relative "setup.rb"
+
+class DatasetTest < MiniTest::Test
+
+=begin
+
+# TODO: and add Egons example
+ def test_sdf_with_multiple_features
+ @dataset = OpenTox::Dataset.new nil
+ @dataset.upload "#{DATA_DIR}/CPDBAS_v5c_1547_29Apr2008part.sdf"
+ assert_equal OpenTox::Dataset, @dataset.class
+ puts @dataset.features.size
+ puts @dataset.compounds.size
+ @dataset.delete
+ end
+
+# TODO: create unordered example file with working references
+# e.g. download from ambit, upload
+ def test_create_from_ntriples
+ d = OpenTox::Dataset.new nil
+ d.upload File.join(DATA_DIR,"hamster_carcinogenicity.ntriples")
+ assert_equal OpenTox::Dataset, d.class
+ assert_equal "hamster_carcinogenicity.ntriples", d.title
+ assert_equal 1, d.features.size
+ assert_equal 76, d.compounds.size
+ assert_equal 76, d.data_entries.size
+ d.delete
+ assert_equal false, URI.accessible?(d.uri)
+ end
+=end
+
+ def test_head_id
+ d = OpenTox::Dataset.new nil
+ d.title = "head test"
+ d.put
+ response = `curl -Lki -H subjectid:#{OpenTox::RestClientWrapper.subjectid} #{d.uri}`
+ assert_match /200/, response
+ d.delete
+ end
+
+ def test_from_xls
+ d = OpenTox::Dataset.new
+ d.upload "#{DATA_DIR}/hamster_carcinogenicity.xls"
+ assert_equal OpenTox::Dataset, d.class
+ assert_equal 1, d.features.size
+ assert_equal 85, d.compounds.size
+ assert_equal 85, d.data_entries.size
+ d.delete
+ #assert_equal false, URI.accessible?(d.uri)
+ end
+
+end
+
+class DatasetRestTest < MiniTest::Test
+
+ def test_01_get_uri_list
+ result = OpenTox::RestClientWrapper.get $dataset[:uri], {}, { :accept => 'text/uri-list', :subjectid => SUBJECTID }
+ assert_equal 200, result.code
+ end
+
+ # check if default response header is text/uri-list
+ def test_02_get_datasetlist_type
+ result = OpenTox::RestClientWrapper.get $dataset[:uri], {}, { :accept => 'text/uri-list', :subjectid => SUBJECTID }
+ assert_equal "text/uri-list", result.headers[:content_type]
+ end
+
+ # check post to investigation service without file
+ def test_10_post_dataset_400_no_file
+ #result = OpenTox::RestClientWrapper.post $dataset[:uri], {}, { :subjectid => $pi[:subjectid] }
+ #assert_equal 200, result.code
+ end
+
+ def test_11_post_dataset
+ response = OpenTox::RestClientWrapper.post $dataset[:uri], {:file => File.join(File.dirname(__FILE__), "data", "hamster_carcinogenicity.csv") }, { :content_type => "text/csv", :subjectid => $pi[:subjectid] }
+ assert_equal 200, response.code
+ task_uri = response.chomp
+ puts task_uri
+ task = OpenTox::Task.new task_uri
+ task.wait
+ uri = task.resultURI
+ puts uri
+ @@uri = URI(uri)
+ end
+
+end
+
diff --git a/test/rest/feature.rb b/test/rest/feature.rb
new file mode 100644
index 0000000..6fde89e
--- /dev/null
+++ b/test/rest/feature.rb
@@ -0,0 +1,98 @@
+require_relative "setup.rb"
+
+class FeatureRestTest < MiniTest::Test
+
+ def serialize rdf, format
+ return rdf.to_json if format == 'application/json'
+ string = RDF::Writer.for(format).buffer do |writer|
+ rdf.each{|statement| writer << statement}
+ end
+ string
+ end
+
+ def parse string, format
+ rdf = RDF::Graph.new
+ RDF::Reader.for(format).new(string) do |reader|
+ reader.each_statement { |statement| rdf << statement }
+ end
+ rdf
+ end
+
+ # TODO: test supported accept/content-type formats
+ # TODO: test invalid rdfs
+ def test_rest_feature
+ #@rdf = RDF::Graph.new
+ #subject = RDF::URI.new File.join($feature[:uri], SecureRandom.uuid)
+ #@rdf << RDF::Statement.new(subject, RDF::DC.title, "tost" )
+ #@rdf << RDF::Statement.new(subject, RDF.type, RDF::OT.Feature)
+ metadata = {:uri => File.join($feature[:uri], SecureRandom.uuid), :title => "tost" , :type => "Feature" }
+
+ @formats = [
+ # TODO
+ [:ntriples, "text/plain"],
+ [:rdfxml, "application/rdf+xml"],
+ [:turtle, 'text/turtle']
+ [:json, 'application/json']
+ ]
+ @uris = []
+
+ @formats.each do |f|
+ @uris << metadata[:uri]
+ OpenTox::RestClientWrapper.put(metadata[:uri], metadata.to_json, {:content_type => f[1]}).chomp
+ #OpenTox::RestClientWrapper.put(subject.to_s, serialize(@rdf, f[0]), {:content_type => f[1]}).chomp
+ assert_equal true, URI.accessible?(@uris.last), "#{@uris.last} is not accessible."
+ end
+ r = OpenTox::RestClientWrapper.get($feature[:uri], {}, :accept => "text/uri-list").split("\n")
+
+ @uris.each do |uri|
+ assert_equal true, URI.accessible?(uri), "#{uri} is not accessible."
+ assert_equal true, r.include?(uri)
+ @formats.each do |f|
+ response = OpenTox::RestClientWrapper.get(uri, {}, :accept => f[1])
+ # TODO compare with rdf serialization
+ assert_match /#{uri}/, response
+ end
+ end
+
+ uri = @uris.first
+ metadata[:title] = "XYZ"
+ @formats.each do |f|
+ OpenTox::RestClientWrapper.post(uri, metadata.to_json, :content_type => f[1])
+ assert_match /XYZ/, OpenTox::RestClientWrapper.get(uri,{},:accept => f[1])
+ # TODO compare with rdf serialization
+ end
+
+ @formats.each do |f|
+ @uris.each do |uri|
+ OpenTox::RestClientWrapper.put(uri, metadata.to_json, :content_type => f[1])
+ assert_equal true, URI.accessible?(uri), "#{uri} is not accessible."
+ # CH: why refute? XYZ has been set as title for the first uri
+ # refute_match /XYZ/, OpenTox::RestClientWrapper.get(uri,{},:accept => f[1])
+ end
+ end
+
+ @uris.each do |uri|
+ OpenTox::RestClientWrapper.delete(uri)
+ assert_raises OpenTox::ResourceNotFoundError do
+ OpenTox::RestClientWrapper.get(uri)
+ end
+ end
+ end
+
+end
+=begin
+ def test_ambit_feature
+ uri = "http://apps.ideaconsult.net:8080/ambit2/feature/35796",
+ f = OpenTox::Feature.new(uri)
+ assert_equal RDF::OT1.TUM_CDK_nAtom, f[RDF::OWL.sameAs]
+ assert_equal RDF::OT1.TUM_CDK_nAtom, f.metadata[RDF::OWL.sameAs].first.to_s
+ assert_equal [RDF::OT1.Feature,RDF::OT1.NumericFeature].sort, f[RDF.type].sort
+ end
+ def test_owl
+ #@features.each do |uri|
+ validate_owl @features.first, SUBJECTID unless CONFIG[:services]["opentox-dataset"].match(/localhost/)
+ validate_owl @features.last, SUBJECTID unless CONFIG[:services]["opentox-dataset"].match(/localhost/)
+ # Ambit does not validate
+ #end
+ end
+=end
diff --git a/test/rest/task.rb b/test/rest/task.rb
new file mode 100644
index 0000000..5fb1c51
--- /dev/null
+++ b/test/rest/task.rb
@@ -0,0 +1,101 @@
+require_relative "setup.rb"
+
+begin
+ puts "Service URI is: #{$task[:uri]}"
+rescue
+ puts "Configuration Error: $task[:uri] is not defined in: " + File.join(ENV["HOME"],".opentox","config","test.rb")
+ exit
+end
+
+class String
+ def uri?
+ uri = URI.parse(self)
+ %w( http https ).include?(uri.scheme)
+ rescue URI::BadURIError
+ false
+ rescue URI::InvalidURIError
+ false
+ end
+end
+
+class TaskTest < MiniTest::Test
+
+ def test_07_create_and_fail_with_rest_not_found_error
+ task = OpenTox::Task.run __method__,"http://test.org/fake_creator" do
+ sleep 2
+ OpenTox::Feature.new.get
+ end
+ assert task.running?
+ assert_equal "Running", task.status
+ assert_equal 202, task.code
+ task.wait
+ assert task.error?
+ assert_equal 404, task.code
+ assert_equal "Error", task.status
+ #refute_empty task.error_report["errorCause"]
+ #assert_equal 404, task.error_report["statusCode"]
+ end
+
+ def test_11_wait_for_error_task
+ # testing two uris:
+ # ../dataset/test/error_in_task starts a task that produces an internal-error with message 'error_in_task_message'
+ # ../algorithm/test/wait_for_error_in_task starts a task that waits for ../dataset/test/error_in_task
+ # TODO: remove test uris from services, e.g. dynamically instantiate Sinatra routes instead
+
+ def check_msg(msg,complete)
+ assert msg=~/bad_request_error_in_task/,"original task error message ('bad_request_error_in_task') is lost: #{msg}"
+ assert((msg=~/\\/)==nil,"no backslashes please!")
+ assert complete=~/test.rb:9/,"code line number test.rb:9 is lost"
+ end
+
+ [ File.join($dataset[:uri],'test/error_in_task'),
+ File.join($algorithm[:uri],'test/wait_for_error_in_task')
+ ].each do |uri|
+
+ task_uri = OpenTox::RestClientWrapper.post uri
+ assert(URI.task?(task_uri) ,"no task uri: #{task_uri}")
+ task = OpenTox::Task.new task_uri
+
+ # test1: wait_for_task, this should abort
+ begin
+ wait_for_task task_uri
+ assert false,"should have thrown an error because there was an error in the task we have waited for"
+ rescue => ex
+ assert ex.is_a?(OpenTox::BadRequestError),"not a bad request error, instead: #{ex.class}"
+ p ex
+ p ex.message
+ p ex.error_cause
+ check_msg(ex.message,ex.error_cause)
+ end
+
+ ## test2: test if task is set accordingly
+ assert task.error?
+ assert task.error_report["errorCode"]==OpenTox::BadRequestError.to_s,"errorCode should be #{OpenTox::BadRequestError.to_s}, but is #{task.error_report["errorCode"]}"
+ check_msg(task.error_report["message"],task.error_report["errorCause"])
+ end
+ end
+
+ def test_11a_plain_errors
+ tests = [ { :uri=>File.join($dataset[:uri],'test/plain_error'),
+ :error=>OpenTox::BadRequestError,
+ :msg=>"plain_bad_request_error",
+ :line=>"16" },
+ { :uri=>File.join($dataset[:uri],'test/plain_no_ot_error'),
+ :error=>OpenTox::InternalServerError,
+ :msg=>"undefined method `no_method_for_nil' for nil:NilClass",
+ :line=>"20" } ]
+ tests.each do |test|
+ begin
+ OpenTox::RestClientWrapper.get test[:uri]
+ assert false,"there should have been an error"
+ rescue => ex
+ assert ex.is_a?(test[:error]),"error type should be a #{test[:error]}, but is a #{ex.class}"
+ assert ex.message=~/#{test[:msg]}/,"message should be #{test[:msg]}, but is #{ex.message}"
+ assert ex.error_cause=~/test.rb:#{test[:line]}/,"code line number test.rb:#{test[:line]} is lost or wrong: #{ex.error_cause}"
+ assert ex.uri==test[:uri]
+ end
+ end
+ end
+
+
+end