#!/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 WeatherWebService < Soaspec::SoapHandler # Add to or override default Savon client options def savon_options { wsdl: 'http://www.webservicex.com/globalweather.asmx?wsdl', convert_request_keys_to: :camelcase } end # Specifying that get_weather_result must be present in the SOAP response mandatory_elements [:get_weather_result] # Example of xpath value that must be true for all success scenarios mandatory_xpath_values '//xmlns:GetWeatherResult' => 'Data Not Found' # Example of setting an attribute on the root XML element root_attributes 'Version' => '1' end EOF soap_spec_content = <<-EOF require 'spec_helper' Soaspec::Environment.strip_namespaces = true # This allows namespace not to be used. Be careful with this soap_example = WeatherWebService.new('Get Weather') soap_example.operation = :get_weather soap_example.default_hash = { city_name: 'Sydney', country_name: 'Australia' } # soap_example.template_name = 'soap_template' # Use this if you'd rather use template file and comment out previous line context soap_example do describe Exchange.new(:default) do it { is_expected.to contain_value 'Data Not Found' } it_behaves_like 'success scenario' end describe Exchange.new(:china, city_name: 'Shanghai', country_name: 'China') do it_behaves_like 'success scenario' context 'handle xpath' do its(['GetWeatherResult']) { is_expected.to eq 'Data Not Found' } # Don't need to use namespace it { is_expected.to have_element_at_xpath '//xmlns:GetWeatherResult' } it { is_expected.to have_xpath_value '//xmlns:GetWeatherResult' => 'Data Not Found' } end end describe Exchange.new(:japan, data_for(:japan)) do it_behaves_like 'success scenario' its(['//xmlns:GetWeatherResult']) { is_expected.to eq 'Data Not Found' } # Can specify namespace if wanted end end error_example = WeatherWebService.new('Get Weather') error_example.operation = :get_weather error_example.default_hash = { city_name: 'Washington' } context 'error scenarios' do context error_example do describe Exchange.new(: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[:city_name] || 'Wellington' %> <%= test_values[:country] || 'New Zealand' %> EOF default_yaml_content = <<-EOF japan: city_name: 'Tokyo' country_name: 'Japan' EOF create_file(filename: 'Gemfile', content: gem_content) create_file(filename: 'Rakefile', content: rake_content) create_file(filename: 'README.md', content: readme_content) create_folder 'lib' create_file(filename: 'lib/weather_web_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_file(filename: 'spec/spec_helper.rb', content: spec_helper_content) create_file(filename: 'spec/soap_spec.rb', content: soap_spec_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"