1 require File.dirname(__FILE__) + '/../test_helper'
5 class AmfControllerTest < ActionController::TestCase
8 # this should be what AMF controller returns when the bbox of a request
9 # is invalid or too large.
10 BOUNDARY_ERROR = [-2,"Sorry - I can't get the map for that area."]
14 id = current_ways(:visible_way).id
15 amf_content "getway", "/1", [id]
17 assert_response :success
19 assert_equal amf_result("/1")[0], id
22 def test_getway_invisible
23 # check an invisible way
24 id = current_ways(:invisible_way).id
25 amf_content "getway", "/1", [id]
27 assert_response :success
29 way = amf_result("/1")
30 assert_equal way[0], id
31 assert way[1].empty? and way[2].empty?
34 def test_getway_nonexistent
35 # check chat a non-existent way is not returned
36 amf_content "getway", "/1", [0]
38 assert_response :success
40 way = amf_result("/1")
41 assert_equal way[0], 0
42 assert way[1].empty? and way[2].empty?
46 node = current_nodes(:used_node_1)
51 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
53 assert_response :success
56 # check contents of message
58 assert_equal 0, map[0]
59 assert_equal Array, map[1].class
60 assert map[1].include?(current_ways(:used_way).id)
61 assert !map[1].include?(current_ways(:invisible_way).id)
65 # checks that too-large a bounding box will not be served.
66 def test_whichways_toobig
67 bbox = [-0.1,-0.1,1.1,1.1]
68 check_bboxes_are_bad [bbox] do |map|
69 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
74 # checks that an invalid bounding box will not be served. in this case
75 # one with max < min latitudes.
76 def test_whichways_badlat
77 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.34,0.25,54.33]]
78 check_bboxes_are_bad bboxes do |map|
79 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
84 # same as test_whichways_badlat, but for longitudes
85 def test_whichways_badlon
86 bboxes = [[80,-0.1,70,0.1], [54.34,0.24,54.33,0.25]]
87 check_bboxes_are_bad bboxes do |map|
88 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
92 def test_whichways_deleted
93 node = current_nodes(:used_node_1)
98 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
100 assert_response :success
103 # check contents of message
104 map = amf_result "/1"
105 assert_equal 0, map[0]
106 assert_equal Array, map[1].class
107 assert map[1].include?(current_ways(:used_way).id)
108 assert !map[1].include?(current_ways(:invisible_way).id)
111 def test_whichways_deleted_toobig
112 bbox = [-0.1,-0.1,1.1,1.1]
113 amf_content "whichways_deleted", "/1", bbox
115 assert_response :success
118 map = amf_result "/1"
119 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
123 id = current_relations(:visible_relation).id
124 amf_content "getrelation", "/1", [id]
126 assert_response :success
128 assert_equal amf_result("/1")[0], id
131 def test_getrelation_invisible
132 id = current_relations(:invisible_relation).id
133 amf_content "getrelation", "/1", [id]
135 assert_response :success
137 rel = amf_result("/1")
138 assert_equal rel[0], id
139 assert rel[1].empty? and rel[2].empty?
142 def test_getrelation_nonexistent
144 amf_content "getrelation", "/1", [id]
146 assert_response :success
148 rel = amf_result("/1")
149 assert_equal rel[0], id
150 assert rel[1].empty? and rel[2].empty?
154 # try to get the last visible version (specified by <0) (should be current version)
155 latest = current_ways(:way_with_versions)
156 # try to get version 1
157 v1 = ways(:way_with_versions_v1)
158 {latest => -1, v1 => v1.version}.each do |way, v|
159 amf_content "getway_old", "/1", [way.id, v]
161 assert_response :success
163 returned_way = amf_result("/1")
164 assert_equal returned_way[1], way.id
165 assert_equal returned_way[4], way.version
169 def test_getway_old_nonexistent
170 # try to get the last version+10 (shoudn't exist)
171 latest = current_ways(:way_with_versions)
172 # try to get last visible version of non-existent way
173 # try to get specific version of non-existent way
174 {nil => -1, nil => 1, latest => latest.version + 10}.each do |way, v|
175 amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, v]
177 assert_response :success
179 returned_way = amf_result("/1")
180 assert returned_way[2].empty?
181 assert returned_way[3].empty?
182 assert returned_way[4] < 0
186 def test_getway_history
187 latest = current_ways(:way_with_versions)
188 amf_content "getway_history", "/1", [latest.id]
190 assert_response :success
192 history = amf_result("/1")
194 # ['way',wayid,history]
195 assert_equal history[0], 'way'
196 assert_equal history[1], latest.id
197 assert_equal history[2].first[0], latest.version
198 assert_equal history[2].last[0], ways(:way_with_versions_v1).version
201 def test_getway_history_nonexistent
202 amf_content "getway_history", "/1", [0]
204 assert_response :success
206 history = amf_result("/1")
208 # ['way',wayid,history]
209 assert_equal history[0], 'way'
210 assert_equal history[1], 0
211 assert history[2].empty?
214 def test_getnode_history
215 latest = current_nodes(:node_with_versions)
216 amf_content "getnode_history", "/1", [latest.id]
218 assert_response :success
220 history = amf_result("/1")
222 # ['node',nodeid,history]
223 assert_equal history[0], 'node'
224 assert_equal history[1], latest.id
225 assert_equal history[2].first[0], latest.timestamp.to_i
226 assert_equal history[2].last[0], nodes(:node_with_versions_v1).timestamp.to_i
229 def test_getnode_history_nonexistent
230 amf_content "getnode_history", "/1", [0]
232 assert_response :success
234 history = amf_result("/1")
236 # ['node',nodeid,history]
237 assert_equal history[0], 'node'
238 assert_equal history[1], 0
239 assert history[2].empty?
242 # ************************************************************
244 def test_putpoi_update_valid
245 nd = current_nodes(:visible_node)
246 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", nd.changeset_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
248 assert_response :success
250 result = amf_result("/1")
252 assert_equal 0, result[0]
253 assert_equal nd.id, result[1]
254 assert_equal nd.id, result[2]
255 assert_equal nd.version+1, result[3]
257 # Now try to update again, with a different lat/lon, using the updated version number
260 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", nd.changeset_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
262 assert_response :success
264 result = amf_result("/2")
266 assert_equal 0, result[0]
267 assert_equal nd.id, result[1]
268 assert_equal nd.id, result[2]
269 assert_equal nd.version+2, result[3]
272 # Check that we can create a no valid poi
273 # Using similar method for the node controller test
274 def test_putpoi_create_valid
275 # This node has no tags
277 # create a node with random lat/lon
278 lat = rand(100)-50 + rand
279 lon = rand(100)-50 + rand
280 # normal user has a changeset open
281 changeset = changesets(:normal_user_first_change)
283 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, {}, nil]
285 assert_response :success
287 result = amf_result("/1")
289 # check the array returned by the amf
290 assert_equal 4, result.size
291 assert_equal 0, result[0], "expected to get the status ok from the amf"
292 assert_equal 0, result[1], "The old id should be 0"
293 assert result[2] > 0, "The new id should be greater than 0"
294 assert_equal 1, result[3], "The new version should be 1"
296 # Finally check that the node that was saved has saved the data correctly
297 # in both the current and history tables
298 # First check the current table
299 current_node = Node.find(result[2])
300 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
301 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
302 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
303 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
304 # Now check the history table
305 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
306 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
307 first_historic_node = historic_nodes.first
308 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
309 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
310 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
311 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
314 # This node has some tags
316 # create a node with random lat/lon
317 lat = rand(100)-50 + rand
318 lon = rand(100)-50 + rand
319 # normal user has a changeset open
320 changeset = changesets(:normal_user_first_change)
322 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
324 assert_response :success
326 result = amf_result("/2")
328 # check the array returned by the amf
329 assert_equal 4, result.size
330 assert_equal 0, result[0], "Expected to get the status ok in the amf"
331 assert_equal 0, result[1], "The old id should be 0"
332 assert result[2] > 0, "The new id should be greater than 0"
333 assert_equal 1, result[3], "The new version should be 1"
335 # Finally check that the node that was saved has saved the data correctly
336 # in both the current and history tables
337 # First check the current table
338 current_node = Node.find(result[2])
339 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
340 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
341 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
342 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
343 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
344 # Now check the history table
345 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
346 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
347 first_historic_node = historic_nodes.first
348 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
349 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
350 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
351 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
352 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
356 # ************************************************************
357 # AMF Helper functions
359 # Get the result record for the specified ID
360 # It's an assertion FAIL if the record does not exist
362 assert @amf_result.has_key?("#{ref}/onResult")
363 @amf_result["#{ref}/onResult"]
366 # Encode the AMF message to invoke "target" with parameters as
367 # the passed data. The ref is used to retrieve the results.
368 def amf_content(target, ref, data)
371 c.write 0.chr+0.chr # version 0
372 c.write 0.chr+0.chr # n headers
373 c.write a.chr+b.chr # n bodies
374 c.write AMF.encodestring(target)
375 c.write AMF.encodestring(ref)
376 c.write [-1].pack("N")
377 c.write AMF.encodevalue(data)
379 @request.env["RAW_POST_DATA"] = c.string
382 # Parses the @response object as an AMF messsage.
383 # The result is a hash of message_ref => data.
384 # The attribute @amf_result is initialised to this hash.
385 def amf_parse_response
386 if @response.body.class.to_s == 'Proc'
388 @response.body.call @response, res
389 req = StringIO.new(res.string)
391 req = StringIO.new(@response.body)
393 req.read(2) # version
395 # parse through any headers
396 headers=AMF.getint(req) # Read number of headers
397 headers.times do # Read each header
398 name=AMF.getstring(req) # |
399 req.getc # | skip boolean
400 value=AMF.getvalue(req) # |
403 # parse through responses
405 bodies=AMF.getint(req) # Read number of bodies
406 bodies.times do # Read each body
407 message=AMF.getstring(req) # | get message name
408 index=AMF.getstring(req) # | get index in response sequence
409 bytes=AMF.getlong(req) # | get total size in bytes
410 args=AMF.getvalue(req) # | get response (probably an array)
411 results[message] = args
413 @amf_result = results
418 # given an array of bounding boxes (each an array of 4 floats), call the
419 # AMF "whichways" controller for each and pass the result back to the
420 # caller's block for assertion testing.
421 def check_bboxes_are_bad(bboxes)
422 bboxes.each do |bbox|
423 amf_content "whichways", "/1", bbox
425 assert_response :success
428 # pass the response back to the caller's block to be tested
429 # against what the caller expected.
430 map = amf_result "/1"