summaryrefslogtreecommitdiff
path: root/lib/test_util.rb
blob: ecab76c5b1084e53d9748480f65745033c37f022 (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

require 'test/unit'

module Lib
  # test utitily, to be included rack unit tests
  module TestUtil
    
    def wait_for_task(uri)
      return TestUtil.wait_for_task(uri)
    end
    
    def self.wait_for_task(uri)
      if OpenTox::Utils.task_uri?(uri)
        task = OpenTox::Task.find(uri)
        task.wait_for_completion
        raise "task failed: "+uri.to_s+", error is:\n"+task.description if task.error?
        uri = task.resultURI
      end
      return uri
    end
    
    # updloads a dataset
    def upload_data(ws, file)
        
      case file.path  
      when /yaml$/
        type = "application/x-yaml"
      when /owl$/
        type = "application/rdf+xml"
      else
        raise "unknown type for file: "+file.path.to_s
      end
         
      data = File.read(file.path)
      task_uri = RestClient.post ws, data, :content_type => type 
      data_uri = task_uri.body
      puts "done: "+data_uri.to_s
      add_resource(data_uri)
      return data_uri
    end

    # adds a resource to delete it later on
    def add_resource(res)
      @to_delete = [] unless @to_delete
      @to_delete.push(res)
    end

    # deletes all resources
    def delete_resources
      if @to_delete
        @to_delete.each do |d|
          puts "deleting "+d.to_s
          if d.to_s =~ /^http.*/
            ext("curl -X DELETE "+d.to_s)
          else
            delete d.to_s
          end
        end
      end
    end
    
    # execute an external program like curl
    def ext(call, indent="  ")
      response = "" 
      IO.popen(call.to_s+" 2> /dev/null") do |f| 
        while line = f.gets
          response += indent.to_s+line
        end
      end
      assert $?==0, "returns error "+call+" "+response
      return response
    end

  end
end