class Drntest::ResponseNormalizer

Constants

COLUMN_PATH_COLUMN_INDEX
GROONGA_COMMANDS
TABLE_PATH_COLUMN_INDEX

Public Class Methods

new(request, response) click to toggle source
# File lib/drntest/response-normalizer.rb, line 18
def initialize(request, response)
  @request = request
  @response = response
end

Public Instance Methods

normalize() click to toggle source
# File lib/drntest/response-normalizer.rb, line 23
def normalize
  return @response if @response.nil?

  normalized_response = @response.dup
  normalize_droonga_message!(normalized_response)
  normalized_response
end

Private Instance Methods

groonga_command?() click to toggle source
# File lib/drntest/response-normalizer.rb, line 58
def groonga_command?
  GROONGA_COMMANDS.include?(@request["type"])
end
normalize_droonga_message!(droonga_message) click to toggle source
# File lib/drntest/response-normalizer.rb, line 32
def normalize_droonga_message!(droonga_message)
  normalize_droonga_message_envelope!(droonga_message)
  normalize_droonga_message_body!(droonga_message["body"])
end
normalize_droonga_message_body!(body) click to toggle source
# File lib/drntest/response-normalizer.rb, line 37
def normalize_droonga_message_body!(body)
  if groonga_command?
    normalize_groonga_command_response!(body)
  elsif search_command?
    normalize_search_command_response!(body)
  elsif system_status_command?
    normalize_system_status_command_response!(body)
  end
end
normalize_droonga_message_envelope!(message) click to toggle source
# File lib/drntest/response-normalizer.rb, line 70
def normalize_droonga_message_envelope!(message)
  normalized_in_reply_to = "request-id"
  in_reply_to = message["inReplyTo"]
  message["inReplyTo"] = normalized_in_reply_to if in_reply_to

  if message["statusCode"] != 200
    normalize_error_body!(message["body"])
  end

  if message["date"].is_a?(String)
    message["date"] = "0000-00-00T00:00:00.000000Z"
  end

  errors = message["errors"]
  message["errors"] = normalize_errors(errors) if errors
end
normalize_error_body!(body) click to toggle source
# File lib/drntest/response-normalizer.rb, line 99
def normalize_error_body!(body)
  return body unless body
  case body["name"]
  when "InvalidValue"
    message = body["message"]
    message = message.lines.first.chomp
    message = message.gsub(/\#<(Groonga::[a-zA-Z]+) .*>\z/, "\#<\\1 ...>")
    body["message"] = message
  end
end
normalize_errors(errors) click to toggle source
# File lib/drntest/response-normalizer.rb, line 87
def normalize_errors(errors)
  normalized_errors = {}
  error_details = errors.values
  error_details.each do |error_detail|
    normalize_error_body!(error_detail["body"])
  end
  errors.keys.each_with_index do |source, index|
    normalized_errors["sources#{index}"] = error_details[index]
  end
  normalized_errors
end
normalize_groonga_column_list_command_body!(body) click to toggle source
# File lib/drntest/response-normalizer.rb, line 156
def normalize_groonga_column_list_command_body!(body)
  return unless body.is_a?(Array)
  return if body.empty?
  columns = body[0][1..-1]
  return unless columns.is_a?(Array)
  columns.each do |column|
    value = column[COLUMN_PATH_COLUMN_INDEX]
    if value.is_a?(String) and not value.empty?
      column[COLUMN_PATH_COLUMN_INDEX] = "/path/to/column"
    end
  end
end
normalize_groonga_command_body!(body) click to toggle source
# File lib/drntest/response-normalizer.rb, line 130
def normalize_groonga_command_body!(body)
  return unless body.is_a?(Array)
  return if body.empty?

  case @request["type"]
  when "table_list"
    normalize_groonga_table_list_command_body!(body)
  when "column_list"
    normalize_groonga_column_list_command_body!(body)
  end
end
normalize_groonga_command_header!(header) click to toggle source
# File lib/drntest/response-normalizer.rb, line 124
def normalize_groonga_command_header!(header)
  return unless header.is_a?(Array)
  header[1] = normalized_start_time if valid_start_time?(header[1])
  header[2] = normalized_elapsed if valid_elapsed?(header[2])
end
normalize_groonga_command_response!(response) click to toggle source
# File lib/drntest/response-normalizer.rb, line 110
def normalize_groonga_command_response!(response)
  return unless response.is_a?(Array)
  normalize_groonga_command_header!(response[0])
  normalize_groonga_command_body!(response[1..-1])
end
normalize_groonga_table_list_command_body!(body) click to toggle source
# File lib/drntest/response-normalizer.rb, line 143
def normalize_groonga_table_list_command_body!(body)
  return unless body.is_a?(Array)
  return if body.empty?
  tables = body[0][1..-1]
  return unless tables.is_a?(Array)
  tables.each do |table|
    if table[TABLE_PATH_COLUMN_INDEX].is_a?(String)
      table[TABLE_PATH_COLUMN_INDEX] = "/path/to/table"
    end
  end
end
normalize_search_command_response!(response) click to toggle source
# File lib/drntest/response-normalizer.rb, line 169
def normalize_search_command_response!(response)
  response.each do |query_name, result|
    if valid_elapsed?(result["elapsedTime"])
      result["elapsedTime"] = normalized_elapsed
    end
  end
end
normalize_system_status_command_response!(response) click to toggle source
# File lib/drntest/response-normalizer.rb, line 177
def normalize_system_status_command_response!(response)
  reporter = response["reporter"]
  if reporter
    response["reporter"] = reporter.sub(/:\d+/, ":0")
  end
end
normalized_elapsed() click to toggle source
# File lib/drntest/response-normalizer.rb, line 120
def normalized_elapsed
  0.0
end
normalized_start_time() click to toggle source
# File lib/drntest/response-normalizer.rb, line 116
def normalized_start_time
  0.0
end
search_command?() click to toggle source
# File lib/drntest/response-normalizer.rb, line 62
def search_command?
  @request["type"] == "search"
end
system_status_command?() click to toggle source
# File lib/drntest/response-normalizer.rb, line 66
def system_status_command?
  @request["type"] == "system.status"
end
valid_elapsed?(elapsed) click to toggle source
# File lib/drntest/response-normalizer.rb, line 188
def valid_elapsed?(elapsed)
  elapsed.is_a?(Float) and elapsed > 0
end
valid_start_time?(start_time) click to toggle source
# File lib/drntest/response-normalizer.rb, line 184
def valid_start_time?(start_time)
  start_time.is_a?(Float) and start_time > 0
end