From b233df92d1c1af00048280188166498489da75af Mon Sep 17 00:00:00 2001
From: Alberto Bellotti <abellotti@users.noreply.github.com>
Date: Thu, 29 Aug 2019 12:55:17 -0400
Subject: [PATCH] Merge pull request #666 from
 lpichler/use_factory_bot_for_transformation_mapping_item

Create TransformationMapping before Item (FIX CI FAILURE)

(cherry picked from commit 567572881a3d61093341cf581bec4d0e2bd2abcb)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1768517
---
 .../api/transformation_mappings_controller.rb | 16 ++++---
 spec/requests/transformation_mappings_spec.rb | 48 +++++++++----------
 2 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb
index f3216db3b3..73b34e66f0 100644
--- a/app/controllers/api/transformation_mappings_controller.rb
+++ b/app/controllers/api/transformation_mappings_controller.rb
@@ -3,7 +3,7 @@ class TransformationMappingsController < BaseController
     def create_resource(_type, _id, data = {})
       raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"]
       TransformationMapping.new(data.except("transformation_mapping_items")).tap do |mapping|
-        mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"])
+        mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], mapping)
         mapping.save!
       end
     rescue StandardError => err
@@ -13,11 +13,9 @@ def create_resource(_type, _id, data = {})
     def edit_resource(type, id, data)
       raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"]
       transformation_mapping = resource_search(id, type, collection_class(type))
-
       updated_data = data.except("transformation_mapping_items")
       transformation_mapping.update_attributes!(updated_data) if updated_data.present?
-
-      transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"])
+      transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], transformation_mapping)
       transformation_mapping.save!
       transformation_mapping
     rescue StandardError => err
@@ -51,7 +49,7 @@ def vm_flavor_fit_resource(_type, _id, data)
 
     def add_mapping_item_resource(type, id, data)
       resource_search(id, type, collection_class(type)).tap do |mapping|
-        mapping.transformation_mapping_items.append(create_mapping_items([data]))
+        mapping.transformation_mapping_items.append(create_mapping_items([data], mapping))
         mapping.save!
       end
     rescue StandardError => err
@@ -60,10 +58,14 @@ def add_mapping_item_resource(type, id, data)
 
     private
 
-    def create_mapping_items(items)
+    def create_mapping_items(items, mapping)
       items.collect do |item|
         raise "Must specify source and destination hrefs" unless item["source"] && item["destination"]
-        TransformationMappingItem.new(:source => fetch_mapping_resource(item["source"]), :destination => fetch_mapping_resource(item["destination"]))
+        TransformationMappingItem.new(
+          :transformation_mapping => mapping,
+          :source                 => fetch_mapping_resource(item["source"]),
+          :destination            => fetch_mapping_resource(item["destination"])
+        )
       end
     end
 
diff --git a/spec/requests/transformation_mappings_spec.rb b/spec/requests/transformation_mappings_spec.rb
index 511eaa550f..dee8fc93f2 100644
--- a/spec/requests/transformation_mappings_spec.rb
+++ b/spec/requests/transformation_mappings_spec.rb
@@ -4,28 +4,25 @@
   let(:source_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => source_ems) }
   let(:destination_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => dest_ems) }
 
-  let(:source_storage) { FactoryBot.create(:storage) }
-  let(:destination_storage) { FactoryBot.create(:storage) }
+  let(:source_redhat_host) { FactoryBot.create(:host_redhat, :ems_cluster => source_cluster) }
+  let(:source_storage) { FactoryBot.create(:storage, :hosts => [source_redhat_host]) }
+
+  let(:destination_vmware_host) { FactoryBot.create(:host_vmware, :ems_cluster => destination_cluster) }
+  let(:destination_storage) { FactoryBot.create(:storage, :hosts => [destination_vmware_host]) }
 
   let(:source_lan) { FactoryBot.create(:lan) }
   let(:destination_lan) { FactoryBot.create(:lan) }
 
-  let(:transformation_mapping) do
-    FactoryBot.create(
-      :transformation_mapping,
-      :transformation_mapping_items => [
-        TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster),
-        TransformationMappingItem.new(:source => source_storage, :destination => destination_storage),
-        TransformationMappingItem.new(:source => source_lan, :destination => destination_lan)
-      ]
-    )
-  end
+  let(:transformation_mapping) { FactoryBot.create(:transformation_mapping) }
+  let!(:transformation_mapping_item_cluster) { FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping) }
+  let!(:transformation_mapping_item_storage) { FactoryBot.create(:transformation_mapping_item, :source => source_storage, :destination => destination_storage, :transformation_mapping => transformation_mapping) }
+  let!(:transformation_mapping_item_item)    { FactoryBot.create(:transformation_mapping_item, :source => source_lan, :destination => destination_lan, :transformation_mapping => transformation_mapping) }
 
   let(:source_cluster2) { FactoryBot.create(:ems_cluster, :ext_management_system => source_ems) }
   let(:destination_cluster2) { FactoryBot.create(:ems_cluster, :ext_management_system => dest_ems) }
 
-  let(:source_storage2) { FactoryBot.create(:storage) }
-  let(:destination_storage2) { FactoryBot.create(:storage) }
+  let(:source_storage2) { FactoryBot.create(:storage, :hosts => [source_redhat_host]) }
+  let(:destination_storage2) { FactoryBot.create(:storage, :hosts => [destination_vmware_host]) }
 
   let(:source_lan2) { FactoryBot.create(:lan) }
   let(:destination_lan2) { FactoryBot.create(:lan) }
@@ -67,7 +64,6 @@
     context "without an appropriate role" do
       it "is forbidden" do
         api_basic_authorize
-
         get(api_transformation_mapping_url(nil, transformation_mapping))
 
         expect(response).to have_http_status(:forbidden)
@@ -241,9 +237,9 @@ def href_slug(obj)
       context "with an appropriate role" do
         it "can validate vms with csv data specified" do
           api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post))
-          transformation_mapping =
-            FactoryBot.create(:transformation_mapping,
-                               :transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])
+          transformation_mapping = FactoryBot.create(:transformation_mapping)
+          FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)
+
           vm = FactoryBot.create(:vm_openstack, :name => "foo", :ems_cluster => source_cluster, :ext_management_system => source_ems)
 
           request = {
@@ -279,13 +275,14 @@ def href_slug(obj)
             'action'                       => "edit",
             "name"                         => "updated transformation mapping",
             "transformation_mapping_items" => [
-              { "source" => api_cluster_url(nil, source_cluster), "destination" => api_cluster_url(nil, destination_cluster2) },
+              { "source" => api_cluster_url(nil, source_cluster), "destination" => api_cluster_url(nil, destination_cluster) },
               { "source" => api_cluster_url(nil, source_cluster2), "destination" => api_cluster_url(nil, destination_cluster2) },
               { "source" => api_data_store_url(nil, source_storage), "destination" => api_data_store_url(nil, destination_storage) },
-              { "source" => api_data_store_url(nil, source_storage2), "destination" => api_data_store_url(nil, destination_storage) },
+              { "source" => api_data_store_url(nil, source_storage2), "destination" => api_data_store_url(nil, destination_storage2) },
               { "source" => api_lan_url(nil, source_lan2), "destination" => api_lan_url(nil, destination_lan) }
             ]
           }
+
           post(api_transformation_mapping_url(nil, transformation_mapping), :params => request)
 
           updated_mapping_source = transformation_mapping.reload.transformation_mapping_items.pluck(:source_id)
@@ -305,9 +302,9 @@ def href_slug(obj)
         context "can validate vms with csv data and service_template_id are specified" do
           it "vm belongs to the service_template record" do
             api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post))
-            transformation_mapping =
-              FactoryBot.create(:transformation_mapping,
-                                 :transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])
+
+            transformation_mapping = FactoryBot.create(:transformation_mapping)
+            FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)
             vm = FactoryBot.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => source_ems)
             service_template = FactoryBot.create(:service_template_transformation_plan)
 
@@ -338,9 +335,8 @@ def href_slug(obj)
 
           it "vm does not belong to the service_template record" do
             api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post))
-            transformation_mapping =
-              FactoryBot.create(:transformation_mapping,
-                                 :transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])
+            transformation_mapping = FactoryBot.create(:transformation_mapping)
+            FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)
             vm = FactoryBot.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => source_ems)
             service_template = FactoryBot.create(:service_template_transformation_plan)
             service_template2 = FactoryBot.create(:service_template_transformation_plan)