Skip to content

Instantly share code, notes, and snippets.

@ZhouMeichen
Last active May 10, 2019 05:26
Show Gist options
  • Select an option

  • Save ZhouMeichen/7424949 to your computer and use it in GitHub Desktop.

Select an option

Save ZhouMeichen/7424949 to your computer and use it in GitHub Desktop.
Bank API Test (Communicate with IBM WebSphere MQ)
#encoding: utf-8
require 'open-uri'
require 'net/http'
require 'net/ssh'
require 'net/scp'
require 'nokogiri-pretty'
require 'rexml/document'
include REXML
class Execute
attr_accessor :url #MQ队列url
attr_accessor :localPath #本地文件路径(rq时,为输入文件;rs时,为输出文件)
attr_accessor :expected_file #预期结果文件路径
attr_accessor :actual_file #实际结果文件路径
attr_accessor :log #本地日志文件路径
attr_accessor :tmp #本地临时文件路径
def postFromXML #MQ-request
begin
file = File.read(@localPath)
uri = URI.parse(@url)
req = Net::HTTP::Post.new(uri.path)
req.body = file
req.content_type = 'text/xml'
http = Net::HTTP.new(uri.host,uri.port)
http.start{|h| h.request(req)}
rescue
puts "error:#{$!} at:#{$@}"
end
end
def getToXML #MQ-responce
begin
uri = URI.parse(@url)
req = Net::HTTP::Get.new(uri.path)
req .content_type = 'text/xml'
http = Net::HTTP.new(uri.host,uri.port)
request = http.start{|h| h.request(req)}
file = File.new(@localPath,"w+")
file.puts request.read_body
file.close
rescue
puts "error:#{$!} at:#{$@}"
end
end
def log(s = "#{$!.message}#{$@[0]}")
begin
return if not s
if File.exist? "#{@log}#{(Time.now).strftime('%Y%m%d%H%M%S')}.log"
file = File.open("#{@log}#{(Time.now).strftime('%Y%m%d%H%M%S')}.log","w+")
file.puts s
file.close
else
file = File.new("#{@log}#{(Time.now).strftime('%Y%m%d%H%M%S')}.log","w+")
file.puts s
file.close
end
rescue
puts "error:#{$!} at:#{$@}"
end
end
def preprocess(array)
result = []
temp = []
for i in (0..array.size-1)
result = array[i].to_s.split("\n")
result.each do |r|
r = r.to_s.split(" ").join ''
r = r.to_s.split("\t").join ''
#r = r.to_s.split("\n").join ''
temp.push r unless r==''
end
end
return temp
end
def pretty(doc)
d = Nokogiri::XML(doc.to_s)
if File.exist? @tmp
file = File.open(@tmp,"w+")
file.puts d.human
else
file = File.new(@tmp,"w+")
file.puts d.human
end
file.close
file = File.open(@tmp)
dd = Document.new file
file.close
return dd
end
def match
begin
file = File.open(@actual_file)
doc = Document.new file
doc = pretty(doc)
actual = preprocess(doc.elements.to_a)
file.close
file = File.open(@expected_file)
doc = Document.new file
expected = preprocess(doc.elements.to_a)
file.close
msg = nil
if actual.size == expected.size
for i in (0..actual.size-1)
unless actual[i].to_s == expected[i].to_s
msg = "#{msg}ERROR: #{@actual_file}, Line #{i+2} failed.\n\n"
end
end
else
msg = "#{msg}ERROR: More/less data than expected one.\n\n"
end
if msg.nil?
return true
else
log(msg)
return false
end
rescue
puts "error:#{$!} at:#{$@}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment