#!/usr/bin/env ruby require 'thor' $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'savon' require 'soaspec' module Soaspec # Common executable for Soaspec class Exe < Thor include Soaspec::ExeHelpers include Soaspec::WsdlGenerator long_desc <<-LONGDESC `soaspec new` will generate the initial files and folders for starting a testing project using soaspec \x5 `soaspec new soap` will create example files testing against a SOAP service \x5 `soaspec new rest` will create example files testing against a REST service LONGDESC desc 'new [type]', 'Initialize soaspec repository' option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :virtual, type: :boolean, default: true, banner: 'Whether to set things up for a virtual server' def new(type = 'initial') @virtual = options[:virtual] @type = type puts "Creating files for soaspec. options are #{options}" create_file(filename: 'Gemfile') create_file(filename: 'Rakefile') create_file(filename: '.rspec') create_file(filename: 'README.md') create_file(filename: '.travis.yml') if options[:ci] == 'travis' create_folder 'lib' create_files_for type create_file(filename: 'config/data/default.yml') # Example of data file create_file(filename: 'spec/spec_helper.rb') puts "Run 'bundle install' to install necessary gems" puts "Run 'rake spec' to run the tests" end long_desc <<-LONGDESC `soaspec add rest` will generate the initial files and folders for starting a testing project using soaspec \x5 `soaspec add soap` will create example files testing against a virtual SOAP service \x5 LONGDESC desc 'add [type] [name]', 'Add new ExchangeHandler' def add(type = 'rest', name = 'TestService') raise "Type '#{type}' is not available" unless %w[rest soap].include? type @name = name # Use instance variable for ERB create_file filename: File.join('lib', "#{name.snakecase}.rb"), content: retrieve_contents(File.join('lib', "new_#{type}_service.rb")) end desc 'cucumber', 'Add cucumber generic steps template within step_definitions folder' def cucumber create_file filename: File.join('features', 'step_definitions', 'generic_steps.rb'), content: retrieve_contents(File.join('../cucumber', 'generic_steps.rb'), false) end desc 'generate', 'Generate initial test code from wsdl' long_desc <<-LONGDESC `soaspec generate wsdl=wsdl name=ServiceName ` will generate the initial files and folders to test each operation in a wsdl \x5 Additionally the auth parameter can be used to use basic authentication to retrieve the WSDL. To do use the following `soaspec generate --auth=basic` Note: This is still a work in progress and will only work for a very simple wsdl LONGDESC option :wsdl, required: true, aliases: :w option :name, default: 'Service', aliases: :n option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :auth option :string_default, default: 'test string' def generate enter_auth_details if options[:auth] == 'basic' @virtual = false savon_options = { wsdl: options[:wsdl] } savon_options[:basic_auth] = [@auth_name, @auth_password] if options[:auth] == 'basic' @wsdl_doc = Savon.client(**savon_options).wsdl @wsdl_schemas = @wsdl_doc.parser.schemas create_file filename: 'Rakefile', ignore_if_present: true create_file filename: 'Gemfile', ignore_if_present: true create_file(filename: '.rspec') create_file(filename: '.travis.yml') if options[:ci] == 'travis' create_file filename: 'README.md', ignore_if_present: true create_file filename: 'spec/spec_helper.rb', ignore_if_present: true create_folder 'logs' create_file filename: "lib/#{options[:name].snakecase}.rb", content: class_content # Files according to WSDL @wsdl_doc.operations.each do |operation, details| puts "Creating files for operation: #{operation}" @content = "default:\n" @use_camel_case = false puts 'Message params: ' + details.to_s # From namespace identifier, find namespace, and for that find schemaLocation xsd and use that to build request if details[:parameters] details[:parameters].each do |element, details| @use_camel_case = true if /[[:upper:]]/.match(element.to_s[0]) != nil @content += " #{element.to_s.snakecase}: #{fill_in_field_from_type(details[:type])} # #{details[:type]} \n" # TODO: If details is a Hash need to loop again end end wsdl_to_yaml_for root_elements_for(details) params = [] params << 'convert_request_keys_to: :camelcase' if @use_camel_case params_string = params == [] ? '' : ', ' + params.join(', ') @class_params = "'#{camel_case(operation)}'#{params_string}" create_file(filename: "config/data/#{operation}.yml", content: @content) create_file(filename: "spec/#{operation}_spec.rb", content: generated_soap_spec_for(operation)) end end long_desc <<-LONGDESC Run virtual web services on localhost. See root for documentation of web services provided. LONGDESC desc 'virtual_server [port]', 'Run virtual web service on localhost' def virtual_server(port = '4999') ENV['port'] = port require 'soaspec/virtual_server' Soaspec::VirtualServer.run!(Port: port) end end end Soaspec::Exe.start(ARGV)