Skip to content

Instantly share code, notes, and snippets.

@ZhouMeichen
Last active May 10, 2019 05:19
Show Gist options
  • Save ZhouMeichen/ce8c12198c1c9a6c7cbc to your computer and use it in GitHub Desktop.
Save ZhouMeichen/ce8c12198c1c9a6c7cbc to your computer and use it in GitHub Desktop.
Generate XML data from SQL Server, post it to MQ, and save response to local XML file
#encoding: utf-8
class MSSQL
attr_accessor :connect_username
attr_accessor :connect_password
attr_accessor :connect_host
attr_accessor :connect_database
attr_accessor :connect_encode
attr_accessor :sql
def connect
begin
#sql server default encode: GBK
@db_client = TinyTds::Client.new(:username => @connect_username, :password => @connect_password, :host => @connect_host, :database => @connect_database,:encode=>@connect_encode)
rescue
puts "error:#{$!} at:#{$@}"
end
end
def execute
begin
connect.execute(@sql)
rescue
puts "error:#{$!} at:#{$@}"
end
end
def close
begin
@db_client.close
rescue
puts "error:#{$!} at:#{$@}"
end
end
end
#encoding: utf-8
require 'tiny_tds'
require 'open-uri'
require 'net/http'
require 'minitest/autorun'
#encoding: utf-8
class InteractWMQ
attr_accessor :url
attr_accessor :localPath
def getToXML
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 postFromXML
begin
file = File.read(@localPath)
file.force_encoding("UTF-8")
puts file
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
end
#encoding: utf-8
db = MSSQL.new
wmq = InteractWMQ.new
Given /^I send message from Local Path "(.*)" to Queue Manager "(.*)" and Message Queue "(.*)" in IP "(.*)" and Port "(.*)"$/ do |path,manager,queue,ip,port|
wmq.localPath = path
wmq.url = "http://#{ip}:#{port}/#{manager}/#{queue}/"
wmq.postFromXML
end
Given /^I update data using SQL "(.*)" in Host "(.*)" and Database "(.*)" with encode "(.*)", through User "(.*)" and Password "(.*)"$/ do |sql,host,database,encode,user,pwd|
db.connect_username = user
db.connect_password = pwd
db.connect_host = host
db.connect_database = database
db.connect_encode = encode
db.sql = sql
db.execute
end
When /^I post message from Local Path "(.*)" to Queue Manager "(.*)" and Message Queue "(.*)" in IP "(.*)" and Port "(.*)"$/ do |path,manager,queue,ip,port|
wmq.localPath = path
wmq.url = "http://#{ip}:#{port}/#{manager}/#{queue}/"
wmq.postFromXML
end
When /^I manage data using SQL "(.*)" in Host "(.*)" and Database "(.*)" with encode "(.*)", through User "(.*)" and Password "(.*)"$/ do |sql,host,database,encode,user,pwd|
db.connect_username = user
db.connect_password = pwd
db.connect_host = host
db.connect_database = database
db.connect_encode = encode
db.sql = sql
db.execute
end
Then /^I will get the message "(.*)" from Queue Manager "(.*)" and Message Queue "(.*)" in IP "(.*)" and Port "(.*)", and save this message to Local Path "(.*)"$/ do |message,manager,queue,ip,port,path|
wmq.url = "https://#{ip}:#{port}/#{manager}/#{queue}/"
wmq.localPath = path
wmq.getToXML
rsmesg = File.read(path)
boo = rsmesg.include? message
assert(boo,"Failure Message")
db.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment