Refactor cucumber tests

This commit is contained in:
Patrick Niklaus
2015-12-08 23:00:11 +01:00
parent 7e722db3ee
commit 4ec3102df2
19 changed files with 211 additions and 341 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ Before do |scenario|
end
@load_method = DEFAULT_LOAD_METHOD
@query_params = {}
@query_params = []
@scenario_time = Time.now.strftime("%Y-%m-%dT%H:%m:%SZ")
reset_data
@has_logged_preprocess_info = false
+21 -18
View File
@@ -1,29 +1,32 @@
require 'net/http'
def generate_request_url path
if @http_method.eql? "POST"
pos = path.index('?') - 1
service = path[0..pos]
uri = URI.parse "#{HOST}/#{service}"
else
uri = URI.parse "#{HOST}/#{path}"
# Converts an array [["param","val1"], ["param","val2"]] into ?param=val1&param=val2
def params_to_url params
kv_pairs = params.map { |kv| kv[0].to_s + "=" + kv[1].to_s }
url = kv_pairs.size > 0 ? ("?" + kv_pairs.join("&")) : ""
return url
end
# Converts an array [["param","val1"], ["param","val2"]] into ["param"=>["val1", "val2"]]
def params_to_map params
result = {}
params.each do |pair|
if not result.has_key? pair[0]
result[pair[0]] = []
end
result[pair[0]] << [pair[1]]
end
end
def send_request uri, waypoints=[], options={}, timestamps=[]
@query = uri.to_s
def send_request base_uri, parameters
Timeout.timeout(OSRM_TIMEOUT) do
if @http_method.eql? "POST"
datas = {}
if waypoints.length > 0
datas[:loc] = waypoints.compact.map { |w| "#{w.lat},#{w.lon}" }
end
if timestamps.length > 0
datas[:t] = timestamps.compact.map { |t| "#{t}" }
end
datas.merge! options
response = Net::HTTP.post_form uri, datas
uri = URI.parse base_uri
@query = uri.to_s
response = Net::HTTP.post_form uri, (params_to_map parameters)
else
uri = URI.parse base_uri+(params_to_url parameters)
@query = uri.to_s
response = Net::HTTP.get_response uri
end
end
-12
View File
@@ -1,12 +0,0 @@
require 'net/http'
def request_locate_url path, node
@query = path
uri = generate_request_url path
response = send_request uri, [node]
end
def request_locate node
request_locate_url "locate?loc=#{node.lat},#{node.lon}", node
end
-20
View File
@@ -1,20 +0,0 @@
require 'net/http'
HOST = "http://127.0.0.1:#{OSRM_PORT}"
def request_matching trace=[], timestamps=[], options={}
defaults = { 'output' => 'json', 'instructions' => 'true' }
locs = trace.compact.map { |w| "loc=#{w.lat},#{w.lon}" }
ts = timestamps.compact.map { |t| "t=#{t}" }
if ts.length > 0
trace_params = locs.zip(ts).map { |a| a.join('&')}
else
trace_params = locs
end
params = (trace_params + defaults.merge(options).to_param).join('&')
params = nil if params==""
uri = generate_request_url ("match" + '?' + params)
response = send_request uri, trace, options, timestamps
end
-12
View File
@@ -1,12 +0,0 @@
require 'net/http'
def request_nearest_url path, node
@query = path
uri = generate_request_url path
response = send_request uri, [node]
end
def request_nearest node
request_nearest_url "nearest?loc=#{node.lat},#{node.lon}", node
end
+85 -31
View File
@@ -3,25 +3,10 @@ require 'net/http'
HOST = "http://127.0.0.1:#{OSRM_PORT}"
DESTINATION_REACHED = 15 #OSRM instruction code
class Hash
def to_param(namespace = nil)
collect do |key, value|
"#{key}=#{value}"
end.sort
end
end
def request_path path, waypoints=[], options={}
locs = waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" }
params = (locs + options.to_param).join('&')
params = nil if params==""
if params == nil
uri = generate_request_url (path)
else
uri = generate_request_url (path + '?' + params)
end
response = send_request uri, waypoints, options
def request_path service, params
uri = "#{HOST}/" + service
response = send_request uri, params
return response
end
def request_url path
@@ -36,18 +21,87 @@ rescue Timeout::Error
raise "*** osrm-routed did not respond."
end
def request_route waypoints, params={}
defaults = { 'output' => 'json', 'instructions' => true, 'alt' => false }
request_path "viaroute", waypoints, defaults.merge(params)
# Overwriters the default values in defaults.
# e.g. [[a, 1], [b, 2]], [[a, 5], [d, 10]] => [[a, 5], [b, 2], [d, 10]]
def overwrite_params defaults, other
merged = []
defaults.each do |k,v|
idx = other.index { |p| p[0] == k }
if idx == nil then
merged << [k, v]
else
merged << [k, other[idx][1]]
end
end
other.each do |k,v|
if merged.index { |pair| pair[0] == k} == nil then
merged << [k, v]
end
end
return merged
end
def request_table waypoints, sources, params={}
defaults = { 'output' => 'json' }
options = defaults.merge(params)
locs = (sources.size > 0) ? (waypoints.compact.map { |w| "dst=#{w.lat},#{w.lon}" } + sources.compact.map { |w| "src=#{w.lat},#{w.lon}" }) : waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" }
params = (locs + options.to_param).join('&')
uri = generate_request_url ("table" + '?' + params)
response = send_request uri, waypoints, options, sources
def request_route waypoints, bearings, user_params
raise "*** number of bearings does not equal the number of waypoints" unless bearings.size == 0 || bearings.size == waypoints.size
defaults = [['output','json'], ['instructions',true], ['alt',false]]
params = overwrite_params defaults, user_params
encoded_waypoint = waypoints.map{ |w| ["loc","#{w.lat},#{w.lon}"] }
if bearings.size > 0
encoded_bearings = bearings.map { |b| ["b", b.to_s]}
parasm = params.concat encoded_waypoint.zip(encoded_bearings).flatten! 1
else
params = params.concat encoded_waypoint
end
return request_path "viaroute", params
end
def request_locate node, user_params
defaults = [['output', 'json']]
params = overwrite_params defaults, user_params
params << ["loc", "#{node.lat},#{node.lon}"]
return request_path "locate", params
end
def request_nearest node, user_params
defaults = [['output', 'json']]
params = overwrite_params defaults, user_params
params << ["loc", "#{node.lat},#{node.lon}"]
return request_path "nearest", params
end
def request_table waypoints, user_params
defaults = [['output', 'json']]
params = overwrite_params defaults, user_params
params = params.concat waypoints.map{ |w| [w[:type],"#{w[:coord].lat},#{w[:coord].lon}"] }
return request_path "table", params
end
def request_trip waypoints, user_params
defaults = [['output', 'json']]
params = overwrite_params defaults, user_params
params = params.concat waypoints.map{ |w| ["loc","#{w.lat},#{w.lon}"] }
return request_path "trip", params
end
def request_matching waypoints, timestamps, user_params
defaults = [['output', 'json']]
params = overwrite_params defaults, user_params
encoded_waypoint = waypoints.map{ |w| ["loc","#{w.lat},#{w.lon}"] }
if timestamps.size > 0
encoded_timestamps = timestamps.map { |t| ["t", t.to_s]}
parasm = params.concat encoded_waypoint.zip(encoded_timestamps).flatten! 1
else
params = params.concat encoded_waypoint
end
return request_path "match", params
end
def got_route? response
@@ -57,14 +111,14 @@ def got_route? response
return way_list( json['route_instructions']).empty? == false
end
end
false
return false
end
def route_status response
if response.code == "200" && !response.body.empty?
json = JSON.parse response.body
if json['status'] == 0
if way_list( json['route_instructions']).empty?
if way_list(json['route_instructions']).empty?
return 'Empty route'
else
return 'x'
-14
View File
@@ -1,14 +0,0 @@
require 'net/http'
HOST = "http://127.0.0.1:#{OSRM_PORT}"
def request_trip waypoints=[], params={}
defaults = { 'output' => 'json' }
locs = waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" }
params = (locs + defaults.merge(params).to_param).join('&')
params = nil if params==""
uri = generate_request_url ("trip" + '?' + params)
response = send_request uri, waypoints, params
end