Project

General

Profile

Download (1.57 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / apipie_resource_mock.rb @ d9498f54

1
class ApipieResourceMock
2

    
3
  def initialize(resource)
4
    @return_values = {}
5
    @resource = resource
6
    @resource.doc["methods"].each do |method|
7
      stub_method(method)
8
    end
9
  end
10

    
11
  def doc
12
    @resource.doc
13
  end
14

    
15
  def new(attrs)
16
    return self
17
  end
18

    
19
  def expects_with(method_name, params)
20
    self.expects(method_name).with(params).returns(return_value_for(method_name))
21
  end
22

    
23
  private
24

    
25
  def return_value_for(method_name)
26
    @return_values[method_name.to_s]
27
  end
28

    
29
  def stub_method(method)
30
    value = get_return_value(method)
31
    self.stubs(method["name"]).returns(value)
32
    @return_values[method["name"].to_s] = value
33
  end
34

    
35
  def get_return_value(method)
36
    return [nil, nil] if method["examples"].empty?
37

    
38
    #parse actual json from the example string
39
    #examples are in format:
40
    # METHOD /api/some/route
41
    # <input params in json, multiline>
42
    # RETURN_CODE
43
    # <output in json, multiline>
44
    parse_re = /.*(\n\d+\n)(.*)/m
45
    json_string = method["examples"][0][parse_re, 2]
46
    response = JSON.parse(json_string) rescue json_string
47

    
48
    [response, nil]
49
  end
50

    
51
end
52

    
53

    
54
class ApipieDisabledResourceMock
55

    
56
  def initialize(resource)
57
    @resource = resource
58
    @resource.doc["methods"].each do |method|
59
      self.stubs(method["name"]).raises(RestClient::ResourceNotFound)
60
    end
61
  end
62

    
63
  def doc
64
    @resource.doc
65
  end
66

    
67
  def new(attrs)
68
    return self
69
  end
70

    
71
end
72

    
73
def mock_resource_method(method, response)
74
  resource_mock = ApipieResourceMock.new(cmd.class.resource.resource_class)
75
  resource_mock.stubs(method).returns(response)
76
  cmd.class.resource resource_mock
77
end