aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/protocol_soap_test.rb
blob: c55b7f55af1a6672efd4887cce41aa6dc854e9f3 (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require File.dirname(__FILE__) + '/abstract_soap'

module ProtocolSoapTest
  class Person < ActionWebService::Struct
    member :id, Integer
    member :names, [String]
    member :lastname, String
    member :deleted, TrueClass

    def ==(other)
      id == other.id && names == other.names && lastname == other.lastname && deleted == other.deleted
    end
  end

  class EmptyAPI < ActionWebService::API::Base
  end

  class EmptyService < ActionWebService::Base
    web_service_api EmptyAPI
  end

  class API < ActionWebService::API::Base
    api_method :argument_passing, :expects => [{:int=>:int}, {:string=>:string}, {:array=>[:int]}], :returns => [:bool]
    api_method :array_returner, :returns => [[:int]]
    api_method :nil_returner
    api_method :struct_array_returner, :returns => [[Person]]
    api_method :exception_thrower
  
    default_api_method :default
  end

  class Service < ActionWebService::Base
    web_service_api API

    attr :int
    attr :string
    attr :array
    attr :values
    attr :person
    attr :default_args
  
    def initialize
      @int = 20
      @string = "wrong string value"
      @default_args = nil
    end
  
    def argument_passing(int, string, array)
      @int = int
      @string = string
      @array = array
      true
    end
  
    def array_returner
      @values = [1, 2, 3]
    end
  
    def nil_returner
      nil
    end
  
    def struct_array_returner
      @person = Person.new
      @person.id = 5
      @person.names = ["one", "two"]
      @person.lastname = "test"
      @person.deleted = false
      [@person]
    end
  
    def exception_thrower
      raise "Hi, I'm a SOAP error"
    end
  
    def default(*args)
      @default_args = args
      nil
    end 
  end

  class AbstractContainer < ActionController::Base
    wsdl_service_name 'Test'

    def dispatch_request(request)
      protocol_request = probe_request_protocol(request)
      dispatch_protocol_request(protocol_request)
    end
  end
  
  class DelegatedContainer < AbstractContainer
    web_service_dispatching_mode :delegated
    web_service :protocol_soap_service, Service.new
    web_service :empty_service, EmptyService.new
  end

  class DirectContainer < AbstractContainer
    web_service_api API
    web_service_dispatching_mode :direct

    attr :int
    attr :string
    attr :array
    attr :values
    attr :person
    attr :default_args
  
    def initialize
      @int = 20
      @string = "wrong string value"
      @default_args = nil
    end
  
    def argument_passing
      @int = @params['int']
      @string = @params['string']
      @array = @params['array']
      true
    end
  
    def array_returner
      @values = [1, 2, 3]
    end
  
    def nil_returner
      nil
    end
  
    def struct_array_returner
      @person = Person.new
      @person.id = 5
      @person.names = ["one", "two"]
      @person.lastname = "test"
      @person.deleted = false
      [@person]
    end
  
    def exception_thrower
      raise "Hi, I'm a SOAP error"
    end
  
    def default
      @default_args = @method_params
      nil
    end 
  end

  class EmptyContainer < AbstractContainer
    web_service_dispatching_mode :delegated
    web_service :empty_service, EmptyService.new
  end
end

class TC_ProtocolSoap < AbstractSoapTest
  def setup
    @delegated_container = ProtocolSoapTest::DelegatedContainer.new
    @direct_container = ProtocolSoapTest::DirectContainer.new
    @empty_container = ProtocolSoapTest::EmptyContainer.new
  end

  def test_argument_passing
    in_all_containers do
      assert(do_soap_call('ArgumentPassing', 5, "test string", [true, false]) == true)
      assert(service.int == 5)
      assert(service.string == "test string")
      assert(service.array == [true, false])
    end
  end

  def test_array_returner
    in_all_containers do
      assert(do_soap_call('ArrayReturner') == [1, 2, 3])
      assert(service.values == [1, 2, 3])
    end
  end

  def test_nil_returner
    in_all_containers do
      assert(do_soap_call('NilReturner') == nil)
    end
  end

  def test_struct_array_returner
    in_all_containers do
      assert(do_soap_call('StructArrayReturner') == [service.person])
    end
  end

  def test_nonexistent_method
    @container = @empty_container
    assert_raises(ActionWebService::Dispatcher::DispatcherError) do
      do_soap_call('NonexistentMethod')
    end
  end

  def test_exception_thrower
    in_all_containers do
      assert_raises(RuntimeError) do
        do_soap_call('ExceptionThrower')
      end
    end
  end

  def test_default_api_method
    in_all_containers do
      assert(do_soap_call('NonExistentMethodName', 50, false).nil?)
      assert(service.default_args == [50, false])
    end
  end

  def test_service_name_setting
    in_all_containers do
      assert(ProtocolSoapTest::DelegatedContainer.soap_mapper.custom_namespace == 'urn:Test')
    end
  end

  protected
    def service_name
      case
      when @container == @direct_container
        'api'
      when @container == @delegated_container
        'protocol_soap_service'
      when @container == @empty_container
        'empty_service'
      end
    end

    def service
      case
      when @container == @direct_container
        @container
      when @container == @delegated_container
        @container.web_service_object(:protocol_soap_service)
      when @container == @empty_container
        @container.web_service_object(:empty_service)
      end
    end

    def in_all_containers(&block)
      [@direct_container, @delegated_container].each do |container|
        @container = container
        block.call
      end
    end

    def do_soap_call(public_method_name, *args)
      super(public_method_name, *args) do |test_request, test_response|
        @container.dispatch_request(test_request)
      end
    end
end