summaryrefslogtreecommitdiff
path: root/test/toxbank-investigation-ftp.rb
blob: a311cc499076997440af259a892774b0668e4c69 (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
84
85
86
87
88
89
90
91
92
93
94
require_relative "toxbank-setup.rb"
require File.join(File.expand_path(File.dirname(__FILE__)),".." ,".." ,"toxbank-investigation", "util.rb")
require 'net/ftp'

begin
  puts "Service URI is: #{$investigation[:uri]} with FTP server: #{$ftp[:uri]}" 
  $ftp = Net::FTP.open($ftp[:uri], $pi[:name], $pi[:password]) 
rescue
  puts "Configuration Error: $ftp[:uri] is not defined in: " + File.join(ENV["HOME"],".opentox","config","test.rb")
  exit
end

class TBInvestigationFTP < MiniTest::Test

  i_suck_and_my_tests_are_order_dependent!
  
  $testfile = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "unformated.zip"
  $testdir = "nightlytempdir#{Time.now.strftime("%Y%m%d")}" # test directory on ftp server e.G.: nightlytempdir20140528

  # check user root dir
  def test_00_checkftpconnection
    $ftp.chdir("/")
    assert_equal $ftp.pwd, "/"
  end

  # create a testdirectory
  def test_01_create_folder_and_upload_file
    begin
      $ftp.chdir($testdir)
    rescue Net::FTPPermError, NameError => boom
      $ftp.mkdir($testdir)
    end
    $ftp.chdir("/")
    $ftp.chdir($testdir)
    assert_equal $ftp.pwd, "/#{$testdir}"
  end

  # upload testfile
  def test_02_upload_file
    $ftp.putbinaryfile($testfile)
    f = $ftp.list(File.basename($testfile))
    assert_equal f[0].split[8], File.basename($testfile), "file #{File.basename($testfile)} do not exist on ftp server."
  end

  # check http://api.toxbank.net/index.php/Investigation#Get_a_list_of_uploaded_FTP_files
  def test_03_check_ftpfiles_urilist
    response = OpenTox::RestClientWrapper.get  $investigation[:uri]+"/ftpfiles", {}, {:accept => "text/uri-list", :subjectid => $pi[:subjectid] }
    assert_equal "200", response.code.to_s
    files_to_check = ["subdir/JIC37_Ethanol_0.07_Internal_1_3.txt","JIC37_Ethanol_0.07_Internal_1_3.txt","subdir/isttest.txt","isttest.txt","#{$testdir}/#{File.basename($testfile)}"]
    files_to_check.each do |ftc|
      refute_nil response.match("(^|\n)#{ftc}(\n|$)"), "File: #{ftc} is not in ftpfiles"
    end
  end

  # validate json output
  def test_04_check_ftpfiles_json
    response = OpenTox::RestClientWrapper.get  $investigation[:uri]+"/ftpfiles", {}, {:accept => "application/json", :subjectid => $pi[:subjectid] }
    assert_equal "200", response.code.to_s
    assert !!JSON.parse(response)
    result = JSON.parse(response)
    files_to_check = ["subdir/JIC37_Ethanol_0.07_Internal_1_3.txt","JIC37_Ethanol_0.07_Internal_1_3.txt","subdir/isttest.txt","isttest.txt","#{$testdir}/#{File.basename($testfile)}"]
    files_to_check.each do |ftc|
      chk = false
      result["results"]["bindings"].find{|f| chk = true if f["filename"]["value"] == ftc}
      assert chk, "File: #{ftc} is not in ftpfiles json list"
    end
  end

  # validate json output
  def test_10_check_ftpfiles_json_empty
    response = OpenTox::RestClientWrapper.get  $investigation[:uri]+"/ftpfiles", {}, {:accept => "application/json", :subjectid => $guestid }
    assert_equal "200", response.code.to_s
    assert !!JSON.parse(response)
  end

  # delete file from test_02
  def test_95_delete_file
    $ftp.delete(File.basename($testfile))
    assert_raises Net::FTPTempError do
      f = $ftp.list(File.basename($testfile))
    end
    $ftp.chdir("/")      
    $ftp.rmdir($testdir)
    assert_raises Net::FTPPermError do
      $ftp.chdir($testdir)
    end
  end

  # close connection and check if it is closed
  def test_99_close
    $ftp.close
    assert $ftp.closed?, "connection not closed"
  end
end