#!/usr/bin/env ruby # Generate Soaspec tests from a WSDL. Later Swagger, other definitions will be added # #http://www.webservicex.com/globalweather.asmx?wsdl $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'savon' require 'soaspec' def fill_in_field_from_type(type) case type when 'string' 'test string' else 'test' end end def self.ask_wsdl prompt = <<-EOF Enter WSDL: EOF print prompt.chop @wsdl = $stdin.gets.strip puts end def self.name_of_wsdl prompt = <<-EOF Enter what you would like to name WSDL (CamelCase): EOF print prompt.chop @name = $stdin.gets.strip puts end def camel_case(underscore_seperated) underscore_seperated.to_s.split('_').collect(&:capitalize).join end @class_content = <<-EOF require 'soaspec' class <%= @name %> < Soaspec::BasicSoapHandler # Add to or override default Savon client options def savon_options { wsdl: '<%= @wsdl %>', convert_request_keys_to: :camelcase # Remove this if request is not in camelcase } end 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 <%= operation %> = <%= @name %>.new('<%= camel_case(operation) %>') <%= operation %>.operation = :<%= operation %> <%= operation %>.default_hash = data_for '<%= operation %>/default' context <%= operation %> do describe Exchange.new(:default) do it_behaves_like 'success scenario' end end EOF rake_content = <<-EOF # The list of task for a Rake file can be seen with `rake -T` require 'rspec/core/rake_task' # See See https://relishapp.com/rspec/rspec-core/docs/command-line/rake-task for details # This runs `rspec` command with the following options. Type `rake spec` to run this task RSpec::Core::RakeTask.new(:spec) do |t, task_args| t.pattern = "spec/*_spec.rb" # Run all specs in 'spec' folder ending in '_spec' # Next line shows output on the screen, Junit xml report and an HTML report t.rspec_opts = "--format documentation --format RspecJunitFormatter --out logs/spec.xml --format html --out logs/spec.html" t.fail_on_error = false end task :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed EOF gem_content = <<-EOF source 'https://rubygems.org' gem 'data_magic' gem 'require_all' gem 'rspec_junit_formatter' gem 'rake' gem 'soaspec' EOF spec_helper_content = <<-EOF require 'soaspec' require 'require_all' require_all 'lib' require 'data_magic' include DataMagic # Used as example of loading data smartly. Use 'data_for' method to load yml data RSpec.configure do |config| # This will make backtrace much shorter by removing many lines from rspec failure message config.backtrace_exclusion_patterns = [ /rspec/ ] end EOF name_of_wsdl ask_wsdl wsdl_doc = Savon.client(wsdl: @wsdl).wsdl # Basics. May already be there Soaspec::FileHelpers.create_file filename: 'Rakefile', content: rake_content, ignore_if_present: true Soaspec::FileHelpers.create_file filename: 'Gemfile', content: gem_content, ignore_if_present: true Soaspec::FileHelpers.create_folder 'spec' Soaspec::FileHelpers.create_file(filename: 'spec/spec_helper.rb', content: spec_helper_content) module Soaspec::FileHelpers create_folder 'logs' create_folder 'config' create_folder 'config/data' create_folder 'lib' end Soaspec::FileHelpers.create_file filename: "lib/#{@name}.rb", content: ERB.new(@class_content).result(binding) wsdl_doc.operations.each do |operation, details| puts "Creating files for operation: #{operation}" @content = "default:\n" details[:parameters].each do |element, details| @content += " #{element.to_s}: #{fill_in_field_from_type(details[:type])} \n" end Soaspec::FileHelpers.create_file(filename: "config/data/#{operation}.yml", content: @content) Soaspec::FileHelpers.create_file(filename: "spec/#{operation}_spec.rb", content: ERB.new(@soap_spec_content).result(binding)) end