#!/usr/bin/env ruby
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'savon'
require 'soaspec'
include Soaspec::ExeHelpers
puts 'Creating files for soaspec'
weather_web_service = <<-EOF
require 'soaspec'
# This class is not part of the gem. It's an example of a class you can make
# to describe your APIs. Usually this would exist in the 'lib' directory
# Common configuration for the Savon client should go here
class BLZService < Soaspec::SoapHandler
# Add to or override default Savon client options
def savon_options
{
# wsdl: 'http://www.thomas-bayer.com/axis2/services/BLZService?wsdl' # External
wsdl: File.join('spec', 'wsdl', 'get_bank.wsdl') # Virtual service
}
end
# # Specifying that get_weather_result must be present in the SOAP response
mandatory_elements [:plz]
# Example of xpath value that must be true for all success scenarios
mandatory_xpath_values 'ns1:bezeichnung' => 'Deutsche Bank'
# Example of setting an attribute on the root XML element
root_attributes 'Version' => '1' # TODO: Create test on request for this
end
EOF
soap_spec_content = <<-EOF
require 'spec_helper'
Soaspec.strip_namespaces = true # This allows namespace not to be used. Be careful with this
hash_example = BLZService.new('Get Bank')
hash_example.operation = :get_bank
id = '70070010'
#hash_example.template_name = 'soap_template' Use this instead of default_hash to use template approach
hash_example.default_hash = { blz: id }
context 'Test Examples' do
context hash_example do
describe Exchange.new(:default) do
it { is_expected.to contain_value id }
it { is_expected.to include_in_body id }
it_behaves_like 'success scenario'
after(:all) { hash_example.store(:bezeichnung, described_class['bezeichnung']) }
end
describe Exchange.new(:xpath_eg, blz: 100000) do
its(['plz']) { is_expected.to eq '100000' }
it { is_expected.to have_xpath_value '//ns1:bezeichnung' => 'Deutsche Bank' }
context 'Handle retrieving stored value' do
it { is_expected.to have_xpath_value 'bezeichnung' => hash_example.bezeichnung }
end
end
describe Exchange.new(:yaml_eg, data_for(:small_id)) do
it_behaves_like 'success scenario'
end
# Retry for success more for web services that intermittently fail
describe Exchange.new(:short_hand_xpath).retry_for_success do
# Be careful. If you call a method that does not use namespaces, calling one that does may not find the element
its(['ns1:bezeichnung']) { is_expected.to eq 'Deutsche Bank' } # '//' is not required at the beginning
end
describe Exchange.new('Check existence of elements') do
it { is_expected.to have_element_at_xpath '//ns1:bezeichnung' }
it { is_expected.not_to have_element_at_xpath '//ns1:bezeichnung_pretend' }
end
end
end
error_example = BLZService.new('Error example')
error_example.operation = :get_bank
error_example.default_hash = {}
context 'Error Examples' do
context error_example do
describe Exchange.new(:no_blz_error) do
it_behaves_like 'error scenario'
end
end
end
EOF
shared_examples_content = <<-EOF
require 'rspec'
shared_examples_for 'error scenario' do
it 'does not have status code of 200' do
expect(described_class.status_code).not_to eq 200
end
end
EOF
soap_template_content = <<-EOF
<%= test_values[:blz] || '70070010' %>
EOF
default_yaml_content = <<-EOF
small_id:
blz: 100
EOF
test_wsdl_content = <
BLZService
EOF
create_file(filename: 'Gemfile', content: gem_content)
create_file(filename: 'Rakefile', content: rake__virtual_content)
create_file(filename: 'README.md', content: readme_content)
create_folder 'lib'
create_file(filename: 'lib/blz_service.rb', content: weather_web_service)
create_file filename: 'lib/shared_example.rb', content: shared_examples_content
create_folder 'config'
create_folder 'config/data'
create_file(filename: 'config/data/default.yml', content: default_yaml_content)
create_folder 'spec'
create_folder 'spec/wsdl'
create_file(filename: 'spec/spec_helper.rb', content: spec_helper_content)
create_file(filename: 'spec/soap_spec.rb', content: soap_spec_content)
create_file(filename: 'spec/test_server.rb', content: test_server_content)
create_file(filename: 'spec/wsdl/get_bank.wsdl', content: test_wsdl_content)
create_folder 'template'
create_file(filename: 'template/soap_template.xml', content: soap_template_content)
create_folder 'logs'
puts "Run 'bundle install' to install necessary gems"
puts "Run 'rake spec' to run the tests"