razorrisk-razor-connectivity 0.14.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,224 @@
1
+ # encoding: UTF-8
2
+
3
+ # ######################################################################## #
4
+ # File: lib/razor_risk/razor/connectivity/razor_3/response.rb
5
+ #
6
+ # Purpose: Definition of the
7
+ # ::RazorRisk::Razor::Connectivity::Razor3::RazorRequester
8
+ # class
9
+ #
10
+ # Created: 26th July 2017
11
+ # Updated: 27th July 2018
12
+ #
13
+ # Author: Matthew Wilson
14
+ #
15
+ # Copyright (c) 2017-2018, Razor Risk Technologies Pty Ltd
16
+ # All rights reserved.
17
+ #
18
+ # ######################################################################## #
19
+
20
+
21
+ # ######################################################################## #
22
+ # requires
23
+
24
+ require 'nokogiri'
25
+ require 'pantheios'
26
+ require 'xqsr3/quality/parameter_checking'
27
+
28
+ require 'date'
29
+
30
+ require 'razor_risk/core/diagnostics/logger'
31
+
32
+ # ######################################################################## #
33
+ # modules
34
+
35
+ module RazorRisk
36
+ module Razor
37
+ module Connectivity
38
+ module Razor3
39
+
40
+ # ######################################################################## #
41
+ # classes
42
+
43
+ class Response
44
+
45
+ include ::Pantheios
46
+ include ::Xqsr3::Quality::ParameterChecking
47
+ include ::RazorRisk::Core::Diagnostics::Logger
48
+ private
49
+
50
+ class Reason
51
+
52
+ def initialize code, message, details, **options
53
+
54
+ @code = code
55
+ @message = message
56
+ @details = details
57
+ @options = {}.merge! options
58
+ end
59
+
60
+ attr_reader :code
61
+ attr_reader :message
62
+ attr_reader :details
63
+ attr_reader :options
64
+
65
+ def to_s
66
+
67
+ "#{code}: #{message}: #{details}"
68
+ end
69
+ end
70
+
71
+ def obtain_ node, path, **options
72
+
73
+ trace ParamNames[ :node, :path, :options ], node, path, options
74
+
75
+ sub_node = node.at_xpath(path)
76
+
77
+ if sub_node && !options[:no_text]
78
+
79
+ r = sub_node.text
80
+ else
81
+
82
+ r = sub_node
83
+ end
84
+
85
+ r = nil if options[:nil] && r.to_s.strip.empty?
86
+
87
+ r
88
+ end
89
+
90
+ def obtain_reasons_ errors
91
+
92
+ x_errors = errors ? errors.xpath('error') : nil
93
+
94
+ reasons = (x_errors || []).map do |x_error|
95
+
96
+ Reason.new(obtain_(x_error, 'code'), obtain_(x_error, 'message'), obtain_(x_error, 'details'))
97
+ end
98
+
99
+ reasons.define_singleton_method(:lookup?) do |code|
100
+
101
+ code = code.code if Reason === code
102
+
103
+ self.each do |reason|
104
+
105
+ return reason if reason.code == code
106
+ end
107
+
108
+ nil
109
+ end
110
+
111
+ reasons
112
+ end
113
+
114
+ def to_time_ s
115
+
116
+ return nil if s.nil? || (::String === s && s.empty?)
117
+
118
+ DateTime.strptime(s, '%Y-%m-%d %H:%M:%S.%L')
119
+ end
120
+ public
121
+
122
+ # Initialises an instance from an XML node (of type
123
+ # +Nokogiri::XML::Node+)
124
+ #
125
+ # @param node (::Nokogiri::XML::Node)
126
+ #
127
+ def initialize node
128
+
129
+ trace ParamNames[ :node ], node
130
+
131
+ check_parameter node, 'node', types: [ ::Nokogiri::XML::Node ]
132
+
133
+ razor_outbound = node.xpath('/razorOutbound')
134
+
135
+ if razor_outbound.nil? || razor_outbound.empty?
136
+
137
+ log :failure, "node(#{node.class})='#{node.to_xml}'"
138
+
139
+ raise ArgumentError, "node does not contain a recognised Razor 3 response"
140
+ end
141
+
142
+ @s = node.to_s
143
+
144
+ @body = obtain_ razor_outbound, 'body', no_text: true
145
+
146
+ if @body
147
+
148
+ # convert to standalone document
149
+ @body = ::Nokogiri.XML(@body.to_s).root
150
+ end
151
+
152
+ @errors = obtain_ razor_outbound, 'errors', no_text: true
153
+
154
+ @caller = obtain_ razor_outbound, 'caller'
155
+
156
+ @request_received_time = to_time_ obtain_ razor_outbound, 'requestReceivedTime', nil: true
157
+
158
+ @response_sent_time = to_time_ obtain_ razor_outbound, 'responseSentTime', nil: true
159
+
160
+ @response_received_time = to_time_ obtain_ razor_outbound, 'responseReceivedTime', nil: true
161
+
162
+ @result_code = obtain_ razor_outbound, 'returnCode'
163
+
164
+ @reasons = obtain_reasons_ @errors
165
+
166
+ e_c = @errors ? @errors.xpath('error/code') : nil
167
+ @razor_code = e_c ? e_c.text.chomp.strip : ''
168
+ end
169
+
170
+ # A copy of the response document's <body/> node
171
+ attr_reader :body
172
+ attr_reader :errors
173
+ attr_reader :caller
174
+ attr_reader :request_received_time
175
+ attr_reader :response_sent_time
176
+ attr_reader :response_received_time
177
+ attr_reader :result_code
178
+ attr_reader :razor_code
179
+
180
+ # An array of 0 or more reasons as to why the response represents a
181
+ # failed operation
182
+ def reasons
183
+
184
+ @reasons
185
+ end
186
+
187
+ # +false+ if the response represents success (which means that the
188
+ # +result_code+ is +'ack'+); +true+ otherwise
189
+ def failed?
190
+
191
+ !succeeded?
192
+ end
193
+
194
+ # +true+ if the response represents success (which means that the
195
+ # +result_code+ is +'ack'+); +false+ otherwise
196
+ def succeeded?
197
+
198
+ result_code == 'ack'
199
+ end
200
+
201
+ # Full string form of the +node+ from which the instance was initialised
202
+ def to_s
203
+
204
+ @s
205
+ end
206
+
207
+ # Human-readable debug-like string form
208
+ def inspect
209
+
210
+ "#<#{self.class}:#{object_id.to_s(16).rjust(14, '0').upcase} : caller=#{caller}; result_code=#{result_code}>"
211
+ end
212
+ end
213
+
214
+ # ######################################################################## #
215
+ # modules
216
+
217
+ end # module Razor3
218
+ end # module Connectivity
219
+ end # module Razor
220
+ end # module RazorRisk
221
+
222
+ # ############################## end of file ############################# #
223
+
224
+
@@ -0,0 +1,5 @@
1
+
2
+ require 'razor_risk/razor/connectivity/razor_3/header_maker'
3
+ require 'razor_risk/razor/connectivity/razor_3/razor_requester'
4
+ require 'razor_risk/razor/connectivity/razor_3/response'
5
+
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+
3
+ # ##########################################################################
4
+ #
5
+ # Version for RazorRisk.Ruby.Razor.Connectivity library
6
+ #
7
+ # Copyright (c) 2017 Razor Risk Technologies Pty Limited. All rights reserved.
8
+ #
9
+ # ##########################################################################
10
+
11
+ module RazorRisk
12
+ module Razor
13
+ module Connectivity
14
+
15
+ # Current version of the RazorRisk::Razor::Connectivity library
16
+ VERSION = '0.14.10'
17
+
18
+ private
19
+ VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i }
20
+ public
21
+ # Major version of the RazorRisk::Razor::Connectivity library
22
+ VERSION_MAJOR = VERSION_PARTS_[0]
23
+ # Minor version of the RazorRisk::Razor::Connectivity library
24
+ VERSION_MINOR = VERSION_PARTS_[1]
25
+ # Patch version of the RazorRisk::Razor::Connectivity library
26
+ VERSION_PATCH = VERSION_PARTS_[2]
27
+ # Commit version of the RazorRisk::Razor::Connectivity library
28
+ VERSION_COMMIT = VERSION_PARTS_[3] || 0
29
+
30
+
31
+ # The description of the framework
32
+ DESCRIPTION = "Razor Risk's Razor Connectivity library"
33
+
34
+ end # module Connectivity
35
+ end # module Razor
36
+ end # module RazorRisk
37
+
38
+ # ############################## end of file ############################# #
39
+
40
+
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #############################################################################
4
+ # File: test/bin/razor_request_exe_empty_response.rb
5
+ #
6
+ # Purpose: COMPLETE_ME
7
+ #
8
+ # Created: 21st July 2017
9
+ # Updated: 12th March 2018
10
+ #
11
+ # Author: Matthew Wilson
12
+ #
13
+ # Copyright: <<TBD>>
14
+ #
15
+ #############################################################################
16
+
17
+
18
+ require 'pantheios'
19
+ require 'razor_risk/core/diagnostics/logger'
20
+
21
+ include ::Pantheios
22
+ include ::RazorRisk::Core::Diagnostics::Logger
23
+
24
+ log :informational, 'command-line: \'', ARGV.join(' '), ?' if severity_logged? :informational
25
+
26
+ # ############################## end of file ############################# #
27
+
28
+
@@ -0,0 +1,399 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # ##########################################################################
5
+ #
6
+ # Copyright (c) 2017 Razor Risk Technologies Pty Limited. All rights reserved.
7
+ #
8
+ # ##########################################################################
9
+
10
+
11
+ # ##########################################################
12
+ # load path
13
+
14
+ $:.unshift File.join(File.dirname(__FILE__), *(['..'] * 4), 'lib')
15
+
16
+
17
+ # ##########################################################
18
+ # diagnostics
19
+
20
+ unless $DEBUG
21
+
22
+ require 'pantheios/globals'
23
+ require 'pantheios/services/null_log_service'
24
+
25
+ ::Pantheios::Globals.INITIAL_SERVICE_CLASSES = [ ::Pantheios::Services::NullLogService ]
26
+ end
27
+
28
+
29
+ # ##########################################################
30
+ # code coverage
31
+
32
+ require 'simplecov'
33
+
34
+
35
+ # ##########################################################
36
+ # requires
37
+
38
+ require 'razor_risk/razor/connectivity/clarite_2/config_maker'
39
+
40
+ require 'xqsr3/xml/utilities/compare'
41
+ require 'xqsr3/extensions/test/unit'
42
+
43
+ require 'test/unit'
44
+
45
+
46
+ # ##########################################################
47
+ # tests
48
+
49
+ class Test_ConfigMaker_types_exist < Test::Unit::TestCase
50
+
51
+ def test_has_RazorRisk_module
52
+
53
+ assert Object.const_defined? :RazorRisk
54
+ end
55
+
56
+ def test_has_RazorRisk_Razor_module
57
+
58
+ assert RazorRisk.const_defined? :Razor
59
+ end
60
+
61
+ def test_has_RazorRisk_Razor_Connectivity_module
62
+
63
+ assert RazorRisk::Razor.const_defined? :Connectivity
64
+ end
65
+
66
+ def test_has_RazorRisk_Razor_Connectivity_ClarITe2_module
67
+
68
+ assert RazorRisk::Razor::Connectivity.const_defined? :ClarITe2
69
+ end
70
+
71
+ def test_has_ConfigMaker_class
72
+
73
+ assert RazorRisk::Razor::Connectivity::ClarITe2.const_defined? :ConfigMaker
74
+ end
75
+
76
+ def test_ConfigMaker_is_a_class
77
+
78
+ assert_kind_of ::Class, ::RazorRisk::Razor::Connectivity::ClarITe2::ConfigMaker
79
+ end
80
+ end
81
+
82
+
83
+ class Test_ConfigMaker_class_methods < Test::Unit::TestCase
84
+
85
+ include ::RazorRisk::Razor::Connectivity::ClarITe2
86
+
87
+ include ::Xqsr3::XML::Utilities::Compare
88
+
89
+ def test_missing
90
+
91
+ assert_raise_with_message(::ArgumentError, /option.*may not be nil/) { ConfigMaker.make nil, **Hash[] }
92
+
93
+ assert_raise_with_message(::ArgumentError, /option ':xml_client' may not be nil/) do
94
+
95
+ h = {
96
+
97
+ razor_server: {
98
+
99
+ environment: 'env',
100
+ space: 'env',
101
+ },
102
+ }
103
+
104
+ ConfigMaker.make nil, **h
105
+ end
106
+
107
+ assert_raise_with_message(::ArgumentError, /option ':razor_server' may not be nil/) do
108
+
109
+ h = {
110
+
111
+ xml_client: {
112
+
113
+ root_space: {
114
+
115
+ server: 'http://rssvr:4444',
116
+ },
117
+ sns: {
118
+
119
+ host: 'http://snssvr',
120
+ port: 5555,
121
+ },
122
+ },
123
+ }
124
+
125
+ ConfigMaker.make nil, **h
126
+ end
127
+
128
+ assert_raise_with_message(::ArgumentError, /'razor_server\/environment' option invalid or missing/) do
129
+
130
+ h = {
131
+
132
+ razor_server: {
133
+
134
+ space: 'env',
135
+ },
136
+ xml_client: {
137
+
138
+ root_space: {
139
+
140
+ server: 'http://rssvr:4444',
141
+ },
142
+ sns: {
143
+
144
+ host: 'http://snssvr',
145
+ port: 5555,
146
+ },
147
+ },
148
+ }
149
+
150
+ ConfigMaker.make nil, **h
151
+ end
152
+
153
+ assert_raise_with_message(::ArgumentError, /'xml_client\/root_space' option invalid or missing/) do
154
+
155
+ h = {
156
+
157
+ razor_server: {
158
+
159
+ environment: 'env',
160
+ space: 'env',
161
+ },
162
+ xml_client: {
163
+
164
+ sns: {
165
+
166
+ host: 'http://snssvr',
167
+ port: 5555,
168
+ },
169
+ },
170
+ }
171
+
172
+ ConfigMaker.make nil, **h
173
+ end
174
+
175
+ assert_raise_with_message(::ArgumentError, /'xml_client\/sns' option invalid or missing/) do
176
+
177
+ h = {
178
+
179
+ razor_server: {
180
+
181
+ environment: 'env',
182
+ space: 'env',
183
+ },
184
+ xml_client: {
185
+
186
+ root_space: {
187
+
188
+ server: 'http://rssvr:4444',
189
+ },
190
+ },
191
+ }
192
+
193
+ ConfigMaker.make nil, **h
194
+ end
195
+
196
+ assert_raise_with_message(::ArgumentError, /xml_client\/root_space/) do
197
+
198
+ h = {
199
+
200
+ razor_server: {
201
+
202
+ environment: 'env',
203
+ space: 'env',
204
+ },
205
+ xml_client: {
206
+
207
+ root_space: {
208
+
209
+ },
210
+ sns: {
211
+
212
+ host: 'http://snssvr',
213
+ port: 5555,
214
+ },
215
+ },
216
+ }
217
+
218
+ ConfigMaker.make nil, **h
219
+ end
220
+
221
+ assert_raise_with_message(::ArgumentError, /xml_client\/sns/) do
222
+
223
+ h = {
224
+
225
+ razor_server: {
226
+
227
+ environment: 'env',
228
+ space: 'env',
229
+ },
230
+ xml_client: {
231
+
232
+ root_space: {
233
+
234
+ server: 'http://rssvr:4444',
235
+ },
236
+ sns: {
237
+
238
+ host: 'http://snssvr',
239
+ },
240
+ },
241
+ }
242
+
243
+ ConfigMaker.make nil, **h
244
+ end
245
+
246
+ assert_raise_with_message(::ArgumentError, /xml_client\/sns/) do
247
+
248
+ h = {
249
+
250
+ razor_server: {
251
+
252
+ environment: 'env',
253
+ space: 'env',
254
+ },
255
+ xml_client: {
256
+
257
+ root_space: {
258
+
259
+ server: 'http://rssvr:4444',
260
+ },
261
+ sns: {
262
+
263
+ port: 5555,
264
+ },
265
+ },
266
+ }
267
+
268
+ ConfigMaker.make nil, **h
269
+ end
270
+
271
+ assert_raise_with_message(::ArgumentError, /xml_client\/sns/) do
272
+
273
+ h = {
274
+
275
+ razor_server: {
276
+
277
+ environment: 'env',
278
+ space: 'env',
279
+ },
280
+ xml_client: {
281
+
282
+ root_space: {
283
+
284
+ server: 'http://rssvr:4444',
285
+ },
286
+ sns: {
287
+
288
+ },
289
+ },
290
+ }
291
+
292
+ ConfigMaker.make nil, **h
293
+ end
294
+ end
295
+
296
+ def test_make_1
297
+
298
+ expected = <<END_OF_input
299
+ <clarite_config>
300
+ <xml_client>
301
+ <root_space host="http://rssvr" port="4444"/>
302
+ <sns>
303
+ <server host="http://snssvr" port="5555"/>
304
+ </sns>
305
+ <ssl>
306
+ <enabled>false</enabled>
307
+ <isDefault>false</isDefault>
308
+ <authenticateServer>false</authenticateServer>
309
+ <CAFile>ssl/serverCAcert.pem</CAFile>
310
+ </ssl>
311
+ <options>
312
+ <timers>
313
+ <clientPooledSockets>-1</clientPooledSockets>
314
+ </timers>
315
+ </options>
316
+ </xml_client>
317
+ <razor_server space="env" environment="env"/>
318
+ </clarite_config>
319
+ END_OF_input
320
+
321
+ o = {
322
+ razor_server: {
323
+
324
+ environment: 'env',
325
+ },
326
+ xml_client: {
327
+
328
+ root_space: 'http://rssvr:4444',
329
+ sns: {
330
+
331
+ host: 'http://snssvr',
332
+ port: 5555,
333
+ },
334
+ },
335
+ }
336
+
337
+ actual = ConfigMaker.make nil, **o
338
+
339
+ r = xml_compare expected, actual, normalise_whitespace: true
340
+
341
+ assert r.same?, "#{r.details}"
342
+ end
343
+
344
+ def test_make_2
345
+
346
+ expected = <<END_OF_input
347
+ <clarite_config>
348
+ <xml_client>
349
+ <root_space host="http://rssvr" port="4444"/>
350
+ <sns>
351
+ <server host="http://snssvr" port="5555"/>
352
+ </sns>
353
+ <ssl>
354
+ <enabled>false</enabled>
355
+ <isDefault>false</isDefault>
356
+ <authenticateServer>false</authenticateServer>
357
+ <CAFile>ssl/serverCAcert.pem</CAFile>
358
+ </ssl>
359
+ <options>
360
+ <timers>
361
+ <clientPooledSockets>-1</clientPooledSockets>
362
+ </timers>
363
+ </options>
364
+ </xml_client>
365
+ <razor_server space="env" environment="env"/>
366
+ </clarite_config>
367
+ END_OF_input
368
+
369
+ o = {
370
+ razor_server: {
371
+
372
+ environment: 'env',
373
+ space: 'env',
374
+ },
375
+ xml_client: {
376
+
377
+ root_space: {
378
+
379
+ server: 'http://rssvr:4444',
380
+ },
381
+ sns: {
382
+
383
+ host: 'http://snssvr',
384
+ port: 5555,
385
+ },
386
+ },
387
+ }
388
+
389
+ actual = ConfigMaker.make nil, **o
390
+
391
+ r = xml_compare expected, actual, normalise_whitespace: true
392
+
393
+ assert r.same?, "#{r.details}"
394
+ end
395
+ end
396
+
397
+ # ############################## end of file ############################# #
398
+
399
+