lib/soaspec/exe_helpers.rb in soaspec-0.0.38 vs lib/soaspec/exe_helpers.rb in soaspec-0.0.39

- old
+ new

@@ -37,18 +37,27 @@ <<-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| +RSpec::Core::RakeTask.new(:run_spec) do |t| 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 :spec => %w[start_test_server run_spec] + task :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed + +desc 'Start virtual web service' +task :start_test_server do + ENV['test_server_pid'] = Process.spawn('ruby', 'spec/test_server.rb').to_s #, err: "/dev/null").to_s - TODO: use to turn off debugging + puts 'Running test server at pid ' + ENV['test_server_pid'] +end + EOF end def gem_content <<-EOF @@ -57,10 +66,11 @@ gem 'data_magic' gem 'require_all' gem 'rspec_junit_formatter' gem 'rake' gem 'soaspec' +gem 'sinatra' EOF end def spec_helper_content @@ -76,10 +86,16 @@ RSpec.configure do |config| # This will make backtrace much shorter by removing many lines from rspec failure message config.backtrace_exclusion_patterns = [ /rspec/ ] + + # Close test server after all RSpec tests have run + config.after(:suite) do + Process.kill(:QUIT, ENV['test_server_pid'].to_i) # && Process.wait - causes failure + end + end EOF end @@ -120,8 +136,111 @@ ## Reports Reports are shown in the 'logs' folder. By default Rake produces a junit, an html report, and a traffic.log file with the API request and responses in it EOF end + + def test_server_content + <<-FILEEOF +# Used to run virtual web service on localhost. This makes tests more reliable and faster + + require 'sinatra' + require 'nokogiri' + require 'erb' + +# Representing a GetBank SOAP service + class GetBank + + def self.success_response_template + <<-EOF +<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> + <soapenv:Body> + <ns1:getBankResponse xmlns:ns1="http://thomas-bayer.com/blz/"> + <ns1:details> + <ns1:bezeichnung>Deutsche Bank</ns1:bezeichnung> + <ns1:bic>DEUTDEMMXXX <%= soap_action %></ns1:bic> + <ns1:ort>München</ns1:ort> + <ns1:plz><%= @test_id %></ns1:plz> + </ns1:details> + </ns1:getBankResponse> + </soapenv:Body> +</soapenv:Envelope> + EOF + end + + def self.error_response_template + <<-EOF +<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> + <soapenv:Body> + <soapenv:Fault> + <soapenv:Code> + <soapenv:Value>soapenv:Receiver</soapenv:Value> + </soapenv:Code> + <soapenv:Reason> + <soapenv:Text xml:lang="en-US">org.apache.axis2.databinding.ADBException: Unexpected subelement getBank</soapenv:Text> + </soapenv:Reason> + <soapenv:Detail> + <Exception>org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank + at org.apache.axis2.AxisFault.makeFault(AxisFault.java:417) + at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.fromOM(BLZServiceMessageReceiverInOut.java:124) + at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.invokeBusinessLogic(BLZServiceMessageReceiverInOut.java:43) + at org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver.invokeBusinessLogic(AbstractInOutSyncMessageReceiver.java:42) + at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:96) + at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:145) + at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275) + at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:120) + at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) + at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) + at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) + at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) + at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) + at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) + at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) + at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) + at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) + at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683) + at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) + at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) + at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) + at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315) + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) + at java.lang.Thread.run(Thread.java:745) +Caused by: java.lang.Exception: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank + at com.thomas_bayer.adb.GetBankType$Factory.parse(GetBankType.java:423) + at com.thomas_bayer.adb.GetBank$Factory.parse(GetBank.java:304) + at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.fromOM(BLZServiceMessageReceiverInOut.java:117) + ... 25 more +Caused by: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank + at com.thomas_bayer.adb.GetBankType$Factory.parse(GetBankType.java:410) + ... 27 more +</Exception> + </soapenv:Detail> + </soapenv:Fault> + </soapenv:Body> +</soapenv:Envelope> + EOF + end + + def self.response_for(request) + request_body = request.body + soap_action = request.env['HTTP_SOAPACTION'].strip + doc = Nokogiri::XML(request_body) + blz_element = doc.xpath('//tns:blz').first + return 500, error_response_template unless blz_element + @test_id = blz_element.inner_text + ERB.new(success_response_template).result(binding) + end + end + +# This is the one being hit + post '/BLZService' do + GetBank.response_for request + end + FILEEOF + end + end end \ No newline at end of file