Module: Soaspec::ExeHelpers

Defined in:
lib/soaspec/exe_helpers.rb

Overview

Help with tasks common to soaspec executables

Instance Method Summary collapse

Instance Method Details

#create_file(filename: nil, content: nil, ignore_if_present: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/soaspec/exe_helpers.rb', line 9

def create_file(filename: nil, content: nil, ignore_if_present: false)
  #filename = options[:filename]
  raise 'Need to pass filename' unless filename
  #content = options[:content]
  raise 'Need to pass contents to insert into file' unless content
  if File.exist? filename
    old_content = File.read(filename)
    if old_content != content && !ignore_if_present
      warn "!! #{filename} already exists and differs from template"
    end
  else
    File.open(filename, 'w') { |f| f.puts content }
    puts 'Created: ' + filename
  end
end

#create_folder(folder) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/soaspec/exe_helpers.rb', line 25

def create_folder(folder)
  if File.exist? folder
    unless File.directory? folder
      warn "!! #{folder} already exists and is not a directory"
    end
  else
    FileUtils.mkdir folder
    puts "Created folder: #{folder}/"
  end
end

#gem_contentObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/soaspec/exe_helpers.rb', line 80

def gem_content
  "source 'https://rubygems.org'\n\ngem 'data_magic'\ngem 'require_all'\ngem 'rspec_junit_formatter'\ngem 'rake'\ngem 'soaspec'\ngem 'sinatra'\n\n"
end

#rake_contentObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/soaspec/exe_helpers.rb', line 36

def rake_content
  "# The list of task for a Rake file can be seen with `rake -T`\nrequire 'rspec/core/rake_task' # See See https://relishapp.com/rspec/rspec-core/docs/command-line/rake-task for details\n\n# This runs `rspec` command with the following options. Type `rake spec` to run this task\nRSpec::Core::RakeTask.new(:spec) do |t|\n  t.pattern = \"spec/*_spec.rb\" # Run all specs in 'spec' folder ending in '_spec'\n  # Next line shows output on the screen, Junit xml report and an HTML report\n  t.rspec_opts = \"--format documentation --format RspecJunitFormatter --out logs/spec.xml --format html --out logs/spec.html\"\n  t.fail_on_error = false\nend\n\ntask :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed\n\n  RAKE\nend\n"

#rake_virtual_contentObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/soaspec/exe_helpers.rb', line 54

def rake_virtual_content
  "# The list of task for a Rake file can be seen with `rake -T`\nrequire 'rspec/core/rake_task' # See See https://relishapp.com/rspec/rspec-core/docs/command-line/rake-task for details\n\n# This runs `rspec` command with the following options. Type `rake spec` to run this task\nRSpec::Core::RakeTask.new(:run_spec) do |t|\n  t.pattern = \"spec/*_spec.rb\" # Run all specs in 'spec' folder ending in '_spec'\n  # Next line shows output on the screen, Junit xml report and an HTML report\n  t.rspec_opts = \"--format documentation --format RspecJunitFormatter --out logs/spec.xml --format html --out logs/spec.html\"\n  t.fail_on_error = false\nend\n\ntask :spec => %w[start_test_server run_spec]\n\ntask :default => :spec # This runs the 'spec' task by default when no task is mentioned. E.g., if only `rake` is typed\n\ndesc 'Start virtual web service'\ntask :start_test_server do\n  ENV['test_server_pid'] = Process.spawn('ruby', 'spec/test_server.rb', err: %w[logs/test_server.log w]).to_s\n  puts 'Running test server at pid ' + ENV['test_server_pid']\nend\n\n"
end

#readme_contentString



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/soaspec/exe_helpers.rb', line 121

def readme_content
  "\n# Installation\n\nRun `bundle install` to install the gems mentioned in the Gemfile.\n\nTo avoid conflict with gems on the machine globally it may be better to specify gem location with:\n\n`bundle install --path ~/.gem`\n\n# Running tests\nOn the command line type:\n`bundle exec rake spec` or simply `bundle exec rake`\n\n# Structure\n\n## Tests\nTests are within the 'spec' folder and end in '_spec'. Setup and teardown for tests is in 'spec/spec_helper'\n\n## Templates\nThese are the base requests with ERB inside them to create smartly changing requests accoring to the test.yml\n\n## Data\nData used in the test is stored in `config/data` folder. The `data_magic` gem is used by default to read files here.\n\n## Libaries\nLibaries to be installed are in 'Gemfile'. Specific gem versions can be specified here and enforeced with `bundle exec rake`\n\n## Reports\nReports 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\n\n"
end

#spec_helper_contentObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/soaspec/exe_helpers.rb', line 94

def spec_helper_content
"\nrequire 'soaspec'\nrequire 'require_all'\nrequire_all 'lib'\nrequire 'data_magic'\n\ninclude DataMagic # Used as example of loading data smartly. Use 'data_for' method to load yml data\n\nRSpec.configure do |config|\n  # This will make backtrace much shorter by removing many lines from rspec failure message\n  config.backtrace_exclusion_patterns = [\n/rspec/\n  ]\n\n  # Close test server after all RSpec tests have run\n  config.after(:suite) do\nProcess.kill(:QUIT, ENV['test_server_pid'].to_i) if ENV['test_server_pid'] # && Process.wait - causes failure\n  end\n\nend\n\n"
end

#test_server_contentObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/soaspec/exe_helpers.rb', line 156

def test_server_content
  "# Used to run virtual web service on localhost. This makes tests more reliable and faster\n\n  require 'sinatra'\n  require 'nokogiri'\n  require 'erb'\n\n# Representing a GetBank SOAP service\n  class GetBank\n\n    def self.success_response_template\n      <<-EOF\n<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n  <soapenv:Body>\n<ns1:getBankResponse xmlns:ns1=\"http://thomas-bayer.com/blz/\">\n  <ns1:details>\n    <ns1:bezeichnung>Deutsche Bank</ns1:bezeichnung>\n    <ns1:bic>DEUTDEMMXXX <%= soap_action %></ns1:bic>\n    <ns1:ort>M\u00FCnchen</ns1:ort>\n    <ns1:plz><%= @test_id %></ns1:plz>\n  </ns1:details>\n</ns1:getBankResponse>\n  </soapenv:Body>\n</soapenv:Envelope>\n      EOF\n    end\n\n    def self.error_response_template\n      <<-EOF\n<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n  <soapenv:Body>\n<soapenv:Fault>\n  <soapenv:Code>\n    <soapenv:Value>soapenv:Receiver</soapenv:Value>\n  </soapenv:Code>\n  <soapenv:Reason>\n    <soapenv:Text xml:lang=\"en-US\">org.apache.axis2.databinding.ADBException: Unexpected subelement getBank</soapenv:Text>\n  </soapenv:Reason>\n  <soapenv:Detail>\n    <Exception>org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank\n  at org.apache.axis2.AxisFault.makeFault(AxisFault.java:417)\n  at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.fromOM(BLZServiceMessageReceiverInOut.java:124)\n  at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.invokeBusinessLogic(BLZServiceMessageReceiverInOut.java:43)\n  at org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver.invokeBusinessLogic(AbstractInOutSyncMessageReceiver.java:42)\n  at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:96)\n  at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:145)\n  at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)\n  at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:120)\n  at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)\n  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)\n  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)\n  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)\n  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)\n  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)\n  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)\n  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)\n  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)\n  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)\n  at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)\n  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)\n  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)\n  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)\n  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)\n  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)\n  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)\n  at java.lang.Thread.run(Thread.java:745)\nCaused by: java.lang.Exception: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank\n  at com.thomas_bayer.adb.GetBankType$Factory.parse(GetBankType.java:423)\n  at com.thomas_bayer.adb.GetBank$Factory.parse(GetBank.java:304)\n  at com.thomas_bayer.blz.BLZServiceMessageReceiverInOut.fromOM(BLZServiceMessageReceiverInOut.java:117)\n  ... 25 more\nCaused by: org.apache.axis2.databinding.ADBException: Unexpected subelement getBank\n  at com.thomas_bayer.adb.GetBankType$Factory.parse(GetBankType.java:410)\n  ... 27 more\n</Exception>\n  </soapenv:Detail>\n</soapenv:Fault>\n  </soapenv:Body>\n</soapenv:Envelope>\n      EOF\n    end\n\n    def self.response_for(request)\n      request_body = request.body\n      soap_action = request.env['HTTP_SOAPACTION'].strip\n      doc = Nokogiri::XML(request_body)\n      blz_element = doc.xpath('//tns:blz').first\n      return 500, error_response_template unless blz_element\n      @test_id = blz_element.inner_text\n      ERB.new(success_response_template).result(binding)\n    end\n  end\n\n# This is the one being hit\n  post '/BLZService' do\n    GetBank.response_for request\n  end\n  FILEEOF\nend\n"