summaryrefslogtreecommitdiff
path: root/test/toxbank-investigation-curl.rb
blob: cee6b1478f9d9fc4320e800c6342f83536004210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require_relative "toxbank-setup.rb"

begin
  puts "Service URI is: #{$investigation[:uri]}"
rescue
  puts "Configuration Error: $investigation[:uri] is not defined in: " + File.join(ENV["HOME"],".opentox","config","test.rb")
  exit
end

#TODO: check 4store entries/errors

class UploadTest < MiniTest::Test

  def setup
    @tmpdir = File.join(File.dirname(__FILE__),"tmp")
    FileUtils.mkdir_p @tmpdir
    FileUtils.rm_r Dir[File.join @tmpdir, '*']
  end

  def test_01_get_all
    response = `curl -Lk -H "Accept:text/uri-list" -H "subjectid:#{$pi[:subjectid]}" -i #{$investigation[:uri]}`
    assert_match /200/, response
  end

  def test_02_get_inexisting
    response = `curl -Lk -H "Accept:text/uri-list" -i -H "subjectid:#{$pi[:subjectid]}" #{$investigation[:uri]}/foo`.chomp
    assert_match /401|404/, response
    response = `curl -Lk -H "Accept:application/rdf+xml" -i -H "subjectid:#{$pi[:subjectid]}" #{$investigation[:uri]}/999999/metadata`.chomp
    assert_match /401|404/, response
  end

  def test_03_valid_zip_upload
    # upload
    OpenTox::RestClientWrapper.subjectid = $pi[:subjectid]
    ["BII-I-1-tb2.zip","E-MTAB-798_philippe-tb2.zip"].each do |f|
      puts f
      file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", f
      response = `curl -Lk -X POST -i -F file="@#{file};type=application/zip" -H "subjectid:#{$pi[:subjectid]}" #{$investigation[:uri]}`.chomp
      assert_match /202/, response
      taskuri = response.split("\n")[-1]
      t = OpenTox::Task.new taskuri
      t.wait
      puts t.uri
      assert_equal true, t.completed?
      assert_match t.hasStatus, "Completed"
      uri = t.resultURI
      metadata = `curl -Lk -H accept:application/rdf+xml -H "subjectid:#{$pi[:subjectid]}" #{uri}/metadata`
      assert_match /#{uri}/, metadata
      zip = File.join @tmpdir,"tmp.zip"
      `curl -Lk -H "Accept:application/zip" -H "subjectid:#{$pi[:subjectid]}" #{uri} > #{zip}`
      `unzip -o #{zip} -d #{@tmpdir}`
      files = `unzip -l #{File.join File.dirname(__FILE__),"data/toxbank-investigation/valid",f}|grep txt|cut -c 31- | sed 's#^.*/##'`.split("\n")
      files.each{|f| assert_equal true, File.exists?(File.join(File.expand_path(@tmpdir),f)) }
      # get isatab files
      urilist = `curl -Lk -H "subjectid:#{$pi[:subjectid]}" -H "Accept:text/uri-list" #{uri}`.split("\n")
      urilist.each do |u|
        unless u.match(/[nt|zip]$/)
          response = `curl -Lk -i -H "Accept:text/tab-separated-values" -H "subjectid:#{$pi[:subjectid]}" #{u}`
          assert_match /HTTP\/1.1 200 OK/, response.to_s.encode!('UTF-8', 'UTF-8', :invalid => :replace) 
        end
      end
      # delete
      response = `curl -Lk -i -X DELETE -H "subjectid:#{$pi[:subjectid]}" #{uri}`
      assert_match /200/, response
      response = `curl -Lk -i -H "Accept:text/uri-list" -H "subjectid:#{$pi[:subjectid]}" #{uri}`
      assert_match /401|404/, response
    end
  end

  def test_04_invalid_zip_upload
    OpenTox::RestClientWrapper.subjectid = $pi[:subjectid]
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/invalid/isa_TB_ACCUTOX.zip"
    response = `curl -Lk -X POST -i -F file="@#{file};type=application/zip" -H "subjectid:#{$pi[:subjectid]}" #{$investigation[:uri]}`.chomp
    assert_match /202/, response
    uri = response.split("\n")[-1]
    t = OpenTox::Task.new(uri)
    t.wait
    puts t.uri
    assert_match t.hasStatus, "Error"
    # TODO: test errorReport, rdf output of tasks has to be fixed for that purpose
  end

end