Skip to content

Commit

Permalink
Merge pull request #334 from lfu/null_coalescing_1698184
Browse files Browse the repository at this point in the history
Fix the issue with null coalescing fields as input parameters.
  • Loading branch information
gmcculloug authored Aug 21, 2019
2 parents 3cf59e2 + d3c2f6d commit 69b58ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def self.invoke(obj, aem, args)
aem.inputs.each do |f|
key = f.name
value = args[key]
value = obj.attributes[key] || obj.substitute_value(f.default_value) if value.nil?
value = obj.attributes[key] || obj.get_value(f) if value.nil?
inputs[key] = MiqAeObject.convert_value_based_on_datatype(value, f["datatype"])

if obj.attributes[key] && f["datatype"] != "string"
Expand Down
16 changes: 7 additions & 9 deletions lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ def substitute_value(value, _type = nil, required = false)
end
end

def get_value(field, type = nil, required = false)
field['datatype'] == MiqAeField::NULL_COALESCING_DATATYPE ? get_null_coalesced_value(field) : get_field_value(field, type, required)
end

private

def call_method(obj, method)
Expand Down Expand Up @@ -517,10 +521,10 @@ def method_override(namespace, klass, method_name, aem)
aem
end

def get_value(f, type = nil, required = false)
def get_field_value(f, type = nil, required = false)
value = f['value']
value = f['default_value'] if value.blank?
value = substitute_value(value, type, required) if f['substitute'] == true
value = substitute_value(value, type, required) if f['substitute']
value
end

Expand Down Expand Up @@ -609,13 +613,7 @@ def process_assertion(f, message, args)
def process_attribute(f, _message, _args, value = nil)
Benchmark.current_realtime[:attribute_count] += 1
Benchmark.realtime_block(:attribute_time) do
if value.nil?
value = if f['datatype'] == MiqAeField::NULL_COALESCING_DATATYPE
get_null_coalesced_value(f)
else
get_value(f)
end
end
value = get_value(f) if value.nil?
value = MiqAeObject.convert_value_based_on_datatype(value, f['datatype'])
@attributes[f['name'].downcase] = value unless value.nil?
process_collect(f['collect'], nil) unless f['collect'].blank?
Expand Down
24 changes: 24 additions & 0 deletions spec/miq_ae_method_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe MiqAeMethod do
include Spec::Support::AutomationHelper
context "needing datatore" do
before(:each) do
MiqAeDatastore.reset
Expand Down Expand Up @@ -127,4 +128,27 @@ def setup_methods
# exit 0;
end
end

context "null coalescing" do
let(:user) { FactoryBot.create(:user_with_group) }
let(:m_params) { {'arg1' => {'datatype' => MiqAeField::NULL_COALESCING_DATATYPE, 'default_value' => '${#field1} || location'}} }

before do
method_script = "$evm.root['result'] = $evm.inputs"
create_ae_model_with_method(:method_script => method_script, :ae_class => 'AUTOMATE',
:ae_namespace => 'EVM', :instance_name => 'test1',
:method_params => m_params,
:method_name => 'test', :name => 'TEST_DOMAIN')
end

it "uses default when variable missing" do
ws = MiqAeEngine.instantiate("/EVM/AUTOMATE/test1", user)
expect(ws.root['result']['arg1']).to eql('location')
end

it "first non nil value" do
ws = MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?field1=department", user)
expect(ws.root['result']['arg1']).to eql('department')
end
end
end

0 comments on commit 69b58ee

Please sign in to comment.