summaryrefslogtreecommitdiff
path: root/test/toxbank-investigation-biosearch.rb
blob: 30b10243fa079a1cd4fa8b4f454947a921b4e47c (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
95
96
97
98
99
100
require_relative "toxbank-setup.rb"
require File.join(File.expand_path(File.dirname(__FILE__)),".." ,".." ,"toxbank-investigation", "util.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

class TBInvestigationUploadBio < MiniTest::Test
  i_suck_and_my_tests_are_order_dependent!


  # define different users
  $owner = $pi[:subjectid]
  $user1 = $secondpi[:subjectid]
  $user2 = $guestid

  def test_00_select_user
    OpenTox::RestClientWrapper.subjectid = $owner
  end

  # create a new investigation by uploading a zip file,
  # Summary is not searchable, but published. { access=ToxBank group }
  def test_01_post_investigation
    $uri1 = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "BII-I-1-tb2_ftp.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file)}, { :subjectid => $owner }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    $uri1 = URI(uri)
    # update
    res = OpenTox::RestClientWrapper.put $uri1.to_s, { :published => "true", :allowReadByGroup => "#{$user_service[:uri]}/project/G2"}, { :subjectid => $pi[:subjectid] }
    task_uri = res.chomp
    task = OpenTox::Task.new task_uri
    task.wait
  end

  # create a new investigation by uploading a zip file,
  # Summary is not searchable, but published. { access=ToxBank group }
  def test_02_post_investigation
    $uri2 = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "BII-I-1-tb2_ftp.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file)}, { :subjectid => $owner }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    $uri2 = URI(uri)
    # update
    res = OpenTox::RestClientWrapper.put $uri2.to_s, { :published => "true"}, { :subjectid => $pi[:subjectid] }
    task_uri = res.chomp
    task = OpenTox::Task.new task_uri
    task.wait
  end

  def test_04_biosearch_owner
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_and_characteristics", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
    result = JSON.parse(response)
    inv_chars = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["propValue"]["value"]}:::#{n["ontouri"]["value"]}"}
    assert inv_chars.include?("#{$uri1}:::Label:::#{$uri1}/CV2:::http://purl.obolibrary.org/chebi/15956")
    assert inv_chars.include?("#{$uri1}:::organism:::#{$uri1}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
    assert inv_chars.include?("#{$uri2}:::Label:::#{$uri2}/CV2:::http://purl.obolibrary.org/chebi/15956")
    assert inv_chars.include?("#{$uri2}:::organism:::#{$uri2}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
  end

  def test_05_biosearch_user1
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_and_characteristics", {}, {:accept => "application/json", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
    result = JSON.parse(response)
    inv_chars = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["propValue"]["value"]}:::#{n["ontouri"]["value"]}"}
    assert inv_chars.include?("#{$uri1}:::Label:::#{$uri1}/CV2:::http://purl.obolibrary.org/chebi/15956")
    assert inv_chars.include?("#{$uri1}:::organism:::#{$uri1}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
    refute inv_chars.include?("#{$uri2}:::Label:::#{$uri2}/CV2:::http://purl.obolibrary.org/chebi/15956")
    refute inv_chars.include?("#{$uri2}:::organism:::#{$uri2}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
  end
  
  def test_06_biosearch_user2
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_and_characteristics", {}, {:accept => "application/json", :subjectid => $guestid}
    result = JSON.parse(response)
    inv_chars = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["propValue"]["value"]}:::#{n["ontouri"]["value"]}"}
    refute inv_chars.include?("#{$uri1}:::Label:::#{$uri1}/CV2:::http://purl.obolibrary.org/chebi/15956")
    refute inv_chars.include?("#{$uri1}:::organism:::#{$uri1}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
    refute inv_chars.include?("#{$uri2}:::Label:::#{$uri2}/CV2:::http://purl.obolibrary.org/chebi/15956")
    refute inv_chars.include?("#{$uri2}:::organism:::#{$uri2}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
  end

  def test_99_delete
    [$uri1, $uri2].each do |uri|
      response = OpenTox::RestClientWrapper.delete uri.to_s, {}, { :subjectid => $owner }
      assert_equal 200, response.code
    end
  end
end