Skip to content

Commit

Permalink
Add tests for import
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Jul 27, 2018
1 parent 37a5598 commit 2d7fff4
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/task_helpers/imports/custom_buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module TaskHelpers
class Imports
class CustomButtons
class ImportArInstances
DEBUG_MODE = false

def self.import(obj_hash)
new.import(obj_hash)
end
Expand All @@ -18,7 +16,7 @@ def create_object(class_name, obj_array)
klass = class_name.camelize.constantize

obj_array.collect do |obj|
create_unique_values(obj) if DEBUG_MODE
create_unique_values(obj)
begin
klass.create!(obj['attributes'].except('guid')).tap do |new_obj|
if obj['children'].present?
Expand All @@ -32,6 +30,10 @@ def create_object(class_name, obj_array)
new_obj.send("#{hoo.first}=", create_object(*hoo).first)
end
end

if obj['userid'].present?
check_user(obj)
end
try("#{class_name}_post", new_obj)
end
rescue StandardError
Expand All @@ -47,11 +49,9 @@ def custom_button_set_post(new_obj)
new_obj.save!
end

def create_unique_values(obj)
%w(name description).each do |attr_name|
attr_value = obj.dig('attributes', attr_name)
obj.store_path('attributes', attr_name, "#{attr_value} #{Time.zone.now}") if attr_value.present?
end
def check_user(obj)
existing_user = User.find_by(:name => obj['userid'])
obj['userid'] = existing_user.nil? ? "admin" : existing_user
end
end

Expand All @@ -63,8 +63,7 @@ def import(options)
begin
import_custom_buttons(filename)
rescue
p "#{$ERROR_INFO} at #{$ERROR_POSITION}"
warn("Error importing #{options[:source]}")
warn("Error importing #{options[:source]} due to #{$ERROR_INFO} at #{$ERROR_POSITION}")
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/lib/task_helpers/imports/custom_buttons_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe TaskHelpers::Imports::CustomButtons do
let(:data_dir) { File.join(File.expand_path(__dir__), 'data', 'custom_buttons') }
let(:custom_button_file) { 'CustomButtons.yaml' }
let(:custom_button_set_name) { 'group1|Vm|' }
let(:custom_button_set_description) { 'group1' }
let(:resource_action_ae_namespace) { 'SYSTEM' }

describe "#import" do
let(:options) { {:source => source} }

describe "when the source is a directory" do
let(:source) { data_dir }

it 'imports all .yaml files in a specified directory' do
TaskHelpers::Imports::CustomButtons.new.import(options)
assert_test_custom_button_set_present
end
end

describe "when the source is a file" do
let(:source) { "#{data_dir}/#{custom_button_file}" }

it 'imports a specified file' do
TaskHelpers::Imports::CustomButtons.new.import(options)
assert_test_custom_button_set_present
end
end
end

def assert_test_custom_button_set_present
cbs = CustomButtonSet.find_by(:name => custom_button_set_name)
expect(cbs.custom_buttons.count).to eq(3)
expect(cbs.description).to eq(custom_button_set_description)
expect(cbs.custom_buttons.first.resource_action.ae_namespace).to eq(resource_action_ae_namespace)
end
end
134 changes: 134 additions & 0 deletions spec/lib/task_helpers/imports/data/custom_buttons/CustomButtons.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
custom_button_set:
- attributes:
name: group1|Vm|
description: group1
set_type: CustomButtonSet
guid: ba4ff235-75eb-4fa4-a9f9-b854d4186c3a
read_only:
set_data:
:button_order:
- 2
- 3
- 10
:button_icon: ff ff-class
:button_color: "#a341ab"
:display: true
:applies_to_class: Vm
:group_index: 1
mode:
owner_type:
owner_id:
userid:
group_id:
children:
custom_button:
- attributes:
guid: f059931f-8703-4bcf-b876-e482d38ce8ea
description: button 1
applies_to_class: Vm
visibility_expression:
options:
:button_icon: pficon pficon-cpu
:button_type: default
:display: true
:open_url: false
:display_for: single
:submit_how: one
userid: admin
wait_for_complete:
name: button 1
visibility:
:roles:
- _ALL_
applies_to_id:
enablement_expression:
disabled_text:
associations:
resource_action:
- attributes:
action:
resource_type: CustomButton
ae_namespace: SYSTEM
ae_class: PROCESS
ae_instance: Request
ae_message:
ae_attributes:
request: test1
configuration_template_id:
configuration_template_type:
- attributes:
guid: 3f50d617-851e-451f-95ae-a17fc548cb11
description: button 2
applies_to_class: Vm
visibility_expression:
options:
:button_icon: pficon pficon-home
:button_color: "#c03638"
:button_type: default
:display: true
:open_url: false
:display_for: single
:submit_how: one
userid: admin
wait_for_complete:
name: button 2
visibility:
:roles:
- _ALL_
applies_to_id:
enablement_expression:
disabled_text:
associations:
resource_action:
- attributes:
action:
resource_type: CustomButton
ae_namespace: SYSTEM
ae_class: PROCESS
ae_instance: Request
ae_message:
ae_attributes:
request: test2
configuration_template_id:
configuration_template_type:
- attributes:
guid: d3cd608a-f476-48b7-aa25-a930ec046e00
description: multiselect
applies_to_class: Vm
visibility_expression: !ruby/object:MiqExpression
exp:
"=":
field: Vm-power_state
value: 'on'
context_type:
options:
:button_icon: fa fa-users
:button_color: "#996633"
:button_type: default
:display: true
:open_url: false
:display_for: both
:submit_how: all
userid: admin
wait_for_complete:
name: multiselect
visibility:
:roles:
- _ALL_
applies_to_id:
enablement_expression:
disabled_text:
associations:
resource_action:
- attributes:
action:
resource_type: CustomButton
ae_namespace: SYSTEM
ae_class: PROCESS
ae_instance: Request
ae_message:
ae_attributes:
request: multiselect
configuration_template_id:
configuration_template_type:

0 comments on commit 2d7fff4

Please sign in to comment.