Skip to content

Instantly share code, notes, and snippets.

@rpkraemer
Created October 24, 2012 23:16
Show Gist options
  • Select an option

  • Save rpkraemer/3949541 to your computer and use it in GitHub Desktop.

Select an option

Save rpkraemer/3949541 to your computer and use it in GitHub Desktop.
OdiUnit-11g Ruby DSL Project
require 'java'
require 'test/unit'
module JavaODIUnit
include_package "br.com.rpk.odiunit.implementation"
end
module ODIUnit
module Drivers
Oracle = "oracle.jdbc.OracleDriver"
end
class TestUnit < Test::Unit::TestCase
@@results = {}
# Avoiding TestUnit warning because no tests are defined here
undef_method :default_test
protected
def self.add_result(scenario_result)
scen_id = scenario_result.scen_id
@@results[scen_id] = scenario_result
end
def get_results_for_scenario(scenario)
@@results[scenario]
end
def execute(options = {}, &block)
testcase = options[:testcase] if options.has_key?(:testcase)
scenario = options[:scenario] if options.has_key?(:scenario)
Executor.execute testcase, scenario
if block_given?
yield get_results_for_scenario(scenario)
else
return get_results_for_scenario(scenario)
end
end
end
class Executor
@@testcases = Hash.new
protected
def self.execute(testcase_name, scenario)
testcase = @@testcases[testcase_name]
testcase.execute(scenario) if testcase.respond_to? :execute
end
def self.add_testcase(testcase)
@@testcases[testcase.name] = testcase
end
end
class Scenario
attr_reader :scen_id, :scen_name, :scen_version, :scen_variables
def initialize(id)
@scen_id = id
@scen_variables = []
end
def name(name)
@scen_name = name
end
def version(version)
@scen_version = version
end
def variables(variables)
raise "Must be a hash with scenarios variables!" unless variables.is_a? Hash
@scen_variables = variables
end
end
class LocalAgent
attr_reader :odi_user, :password, :scenarios_to_invoke
def initialize(credentials, testcase)
raise "Must be a hash with agent credentials (odi_user and password)!" unless credentials.is_a? Hash
raise "Hash must contain: odi_user, password" unless credentials.has_key? :odi_user and credentials.has_key? :password
@odi_user, @password = credentials.values
@scenarios_to_invoke = Hash.new # Hash containing all odi scenarios that this agent must invoke
@testcase = testcase # TestCase Reference, to access its attributes
end
def should_invoke(scenario_id, &block)
scenario = @testcase.scenarios.detect { |scen| scen.scen_id == scenario_id }
yield dynamically_modified(scenario)
@scenarios_to_invoke[scenario_id] = scenario
end
private
def dynamically_modified(scenario)
class << scenario
attr_reader :odi_context
def context(ctx)
@odi_context = ctx
end
end
scenario
end
end
class Repositories
attr_reader :master_repo, :work_repo
def master(config)
raise "Must be a hash with master repository configurations!" unless config.is_a? Hash
raise "Hash must contain: db_user, db_user_password, jdbc_driver and jdbc_url" unless config.has_key? :db_user and
config.has_key? :db_user_password and
config.has_key? :jdbc_driver and
config.has_key? :jdbc_url
@master_repo = config
end
def work(work_repo_name)
@work_repo = work_repo_name
end
end
class TestCase
attr_reader :name, :scenarios, :odi_repositories, :agent
def self.called(test_case_name, &block)
TestCase.new(test_case_name, &block)
end
def initialize(test_case_name, &block)
@name = test_case_name
@scenarios = []
instance_eval &block
# After testcase object was built (through block) add it to executor
Executor.add_testcase(self)
end
def odi_repositories
odi_repositories = Repositories.new
yield odi_repositories
@odi_repositories = odi_repositories
end
def scenario(identification, &block)
scenario = Scenario.new(identification)
yield scenario
@scenarios << scenario
end
def using_local_agent(credentials)
@agent = LocalAgent.new(credentials, self)
yield @agent
end
def execute(scenario)
scen = @agent.scenarios_to_invoke[scenario]
TestUnit.add_result(scen)
end
end
end
require 'odiunit-11g-dsl'
ODIUnit::TestCase.called :one do
odi_repositories do |repo|
repo.master :db_user => "db_user", :db_user_password => "pass",
:jdbc_driver => ODIUnit::Drivers::Oracle, :jdbc_url => "url"
repo.work "WORKREP"
end
# Scenarios
scenario "load customers" do |scen|
scen.name "LOAD_CUSTOMERS"
scen.version "001"
scen.variables :abc => 123, :def => "123"
end
scenario :load_orders do |scen|
scen.name "LOAD_ORDERS"
scen.version "003"
end
using_local_agent :odi_user => "SUPERVISOR", :password => "123" do |agent|
agent.should_invoke "load customers" do |invocation|
invocation.context "Global"
end
agent.should_invoke :load_orders do |invocation|
invocation.context "Global"
end
end
end
class UnitTests < ODIUnit::TestUnit
def test_load_customers
result = execute :testcase => :one, :scenario => "load customers"
assert_equal "load customers", result.scen_id
assert_equal "001", result.scen_version
end
def test_load_orders
execute :testcase => :one, :scenario => :load_orders do |result|
assert_equal :load_orders, result.scen_id
assert_equal "003", result.scen_version
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment