Skip to content

Commit

Permalink
Merge pull request #19005 from eclarizio/BZ1730813-CiscoAPIFix
Browse files Browse the repository at this point in the history
Fix for Default service dialog values not included in EVM when 'refresh_dialog_fields' action invoked

(cherry picked from commit a8a06f8)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1731977
  • Loading branch information
gtanzillo authored and simaishi committed Jul 22, 2019
1 parent e8bd1a3 commit 6817c33
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 33 deletions.
12 changes: 10 additions & 2 deletions app/models/dialog.rb
Expand Up @@ -88,13 +88,13 @@ def validate_field_data
result
end

def load_values_into_fields(values, overwrite = true)
def load_values_into_fields(values, ignore_nils = true)
values = values.with_indifferent_access

dialog_field_hash.each_value do |field|
field.dialog = self
new_value = values[field.automate_key_name] || values[field.name] || values.dig("parameters", field.name)
new_value ||= field.value unless overwrite
new_value ||= field.value unless ignore_nils

field.value = new_value
end
Expand All @@ -120,6 +120,14 @@ def initialize_value_context(_values)
dialog_field_hash.each_value(&:initialize_value_context)
end

def initialize_static_values
dialog_field_hash.each_value do |field|
field.dialog = self
end

dialog_field_hash.each_value(&:initialize_static_values)
end

def init_fields_with_values_for_request(values)
values = values.with_indifferent_access

Expand Down
6 changes: 6 additions & 0 deletions app/models/dialog_field.rb
Expand Up @@ -101,6 +101,12 @@ def initialize_value_context
end
end

def initialize_static_values
if @value.blank? && !dynamic
@value = default_value
end
end

def initialize_with_given_value(given_value)
self.default_value = given_value
end
Expand Down
57 changes: 32 additions & 25 deletions app/models/resource_action_workflow.rb
Expand Up @@ -93,31 +93,6 @@ def create_values_hash
}
end

def load_dialog(resource_action, values, options)
if resource_action.nil?
resource_action = load_resource_action(values)
@settings[:resource_action_id] = resource_action.id if resource_action
end

dialog = resource_action.dialog if resource_action
if dialog
dialog.target_resource = @target
if options[:display_view_only]
dialog.init_fields_with_values_for_request(values)
elsif options[:provision_workflow] || options[:init_defaults]
dialog.initialize_value_context(values)
dialog.load_values_into_fields(values, false)
elsif options[:refresh] || options[:submit_workflow]
dialog.load_values_into_fields(values)
elsif options[:reconfigure]
dialog.initialize_with_given_values(values)
else
dialog.initialize_value_context(values)
end
end
dialog
end

def init_field_hash
@dialog.dialog_fields.each_with_object({}) { |df, result| result[df.name] = df }
end
Expand Down Expand Up @@ -148,6 +123,38 @@ def validate(_values = nil)

private

def load_dialog(resource_action, values, options)
if resource_action.nil?
resource_action = load_resource_action(values)
@settings[:resource_action_id] = resource_action.id if resource_action
end

dialog = resource_action.dialog if resource_action
load_proper_dialog_values(dialog, options, values) if dialog

dialog
end

def load_proper_dialog_values(dialog, options, values)
dialog.target_resource = @target

if options[:display_view_only]
dialog.init_fields_with_values_for_request(values)
elsif options[:provision_workflow] || options[:init_defaults]
dialog.initialize_value_context(values)
dialog.load_values_into_fields(values, false)
elsif options[:submit_workflow]
dialog.load_values_into_fields(values)
elsif options[:refresh]
dialog.initialize_static_values
dialog.load_values_into_fields(values, false)
elsif options[:reconfigure]
dialog.initialize_with_given_values(values)
else
dialog.initialize_value_context(values)
end
end

def create_request?(values)
ra = load_resource_action(values)
!ra.resource.kind_of?(CustomButton) && has_request_class?
Expand Down
39 changes: 39 additions & 0 deletions spec/models/dialog_field_spec.rb
Expand Up @@ -162,6 +162,45 @@
end
end

describe "#initialize_static_values" do
let(:field) { described_class.new(:dynamic => dynamic, :value => value) }
let(:field_with_default) { described_class.new(:dynamic => dynamic, :value => value, :default_value => "test") }

context "when the field is dynamic" do
let(:dynamic) { true }
let(:value) { "value" }

it "does not change the value" do
field.initialize_static_values
expect(field.instance_variable_get(:@value)).to eq("value")
end
end

context "when the field is not dynamic" do
let(:dynamic) { false }

context "with a user-adjusted value" do
let(:value) { "not dynamic" }

it "does not adjust the value" do
field.initialize_static_values
expect(field.instance_variable_get(:@value)).to eq("not dynamic")
end
end

context "without a user-adjusted value" do
context "with a default value" do
let(:value) { nil }

it "does adjust the value" do
field_with_default.initialize_static_values
expect(field_with_default.instance_variable_get(:@value)).to eq("test")
end
end
end
end
end

describe "#initialize_with_given_value" do
let(:field) { described_class.new(:default_value => "not the given value") }

Expand Down
32 changes: 28 additions & 4 deletions spec/models/dialog_spec.rb
Expand Up @@ -59,6 +59,30 @@
end
end

describe "#initialize_static_values" do
let(:dialog) { described_class.new }
let(:field1) { DialogField.new(:name => "name1") }
let(:field2) { DialogField.new(:name => "name2") }

before do
allow(dialog).to receive(:dialog_fields).and_return([field1, field2])
allow(field1).to receive(:initialize_static_values)
allow(field2).to receive(:initialize_static_values)
end

it "sets the current dialog to each field" do
dialog.initialize_static_values
expect(field1.dialog).to eq(dialog)
expect(field2.dialog).to eq(dialog)
end

it "calls initialize_static_values on each field" do
expect(field1).to receive(:initialize_static_values)
expect(field2).to receive(:initialize_static_values)
dialog.initialize_static_values
end
end

describe "#content" do
it "returns the serialized content" do
dialog = FactoryGirl.create(:dialog, :description => "foo", :label => "bar")
Expand Down Expand Up @@ -494,16 +518,16 @@
let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1, dialog_field2]) }
let(:dialog_field2) { DialogField.new(:value => "321", :name => "field2") }

context "when overwrite is true" do
it "sets nil values" do
context "when ignore_nils is true" do
it "accepts nil values as a value" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field2.value).to eq(nil)
end
end

context "when overwrite is false" do
it "does not set nil values" do
context "when ignore_nils is false" do
it "does not set values to nil" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars, false)
expect(dialog_field2.value).to eq("321")
Expand Down
6 changes: 4 additions & 2 deletions spec/models/resource_action_workflow_spec.rb
Expand Up @@ -215,6 +215,7 @@
allow(ResourceAction).to receive(:find).and_return(resource_action)
allow(dialog).to receive(:load_values_into_fields).with(values)
allow(dialog).to receive(:initialize_value_context).with(values)
allow(dialog).to receive(:initialize_static_values)
allow(dialog).to receive(:init_fields_with_values_for_request).with(values)
allow(dialog).to receive(:target_resource=)
end
Expand All @@ -241,8 +242,9 @@
context "when the options are set to a refresh request" do
let(:options) { {:refresh => true} }

it "loads the values into fields" do
expect(dialog).to receive(:load_values_into_fields).with(values)
it "initializes the static values and then loads the values into fields" do
expect(dialog).to receive(:initialize_static_values).ordered
expect(dialog).to receive(:load_values_into_fields).with(values, false).ordered
ResourceActionWorkflow.new(values, nil, resource_action, options)
end
end
Expand Down

0 comments on commit 6817c33

Please sign in to comment.