<commit_before>describe MiqAeClassController do
  include CompressedIds
  context "#set_record_vars" do
    it "Namespace remains unchanged when a class is edited" do
      ns = FactoryGirl.create(:miq_ae_namespace)
      cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      ns_id = cls.namespace_id
      new = {:name => "New Name", :description => "New Description", :display_name => "Display Name", :inherits => "Some_Class"}
      controller.instance_variable_set(:@sb,
                                       :trees       => {
                                         :ae_tree => {:active_node => "aec-#{cls.id}"}
                                       },
                                       :active_tree => :ae_tree)
      controller.instance_variable_set(:@edit, {:new => new})
      controller.send(:set_record_vars, cls)
      expect(cls.namespace_id).to eq(ns_id)
    end
  end

  context "#set_right_cell_text" do
    it "check if correct namespace_path is being set" do
      ns = FactoryGirl.create(:miq_ae_namespace)
      cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      controller.instance_variable_set(:@sb, {})
      id = "aec-#{cls.id}"
      fq_name = cls.fqname
      controller.send(:set_right_cell_text, id, cls)
      expect(assigns(:sb)[:namespace_path]).to eq(fq_name.gsub!(%r{\/}, " / "))

      id = "root"
      fq_name = ""
      controller.send(:set_right_cell_text, id)
      expect(assigns(:sb)[:namespace_path]).to eq(fq_name)
    end
  end

  context "#domain_lock" do
    it "Marks domain as locked/readonly" do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_domain_enabled)
      controller.instance_variable_set(:@_params, :id => ns.id)
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domain_lock)
      ns.reload
      expect(ns.contents_locked?).to eq(true)
    end
  end

  context "#domain_unlock" do
    it "Marks domain as unlocked/editable" do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_domain_disabled)
      controller.instance_variable_set(:@_params, :id => ns.id)
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domain_unlock)
      ns.reload
      expect(ns.contents_locked?).to eq(false)
    end
  end

  context "#domains_priority_edit" do
    it "sets priority of domains" do
      stub_user(:features => :all)
      FactoryGirl.create(:miq_ae_domain, :name => "test1", :parent => nil, :priority => 1)
      FactoryGirl.create(:miq_ae_domain, :name => "test2", :parent => nil, :priority => 2)
      FactoryGirl.create(:miq_ae_domain, :name => "test3", :parent => nil, :priority => 3)
      FactoryGirl.create(:miq_ae_domain, :name => "test4", :parent => nil, :priority => 4)
      order = %w(test3 test2 test4 test1)
      edit = {
        :new     => {:domain_order => order},
        :key     => "priority__edit",
        :current => {:domain_order => order},
      }
      controller.instance_variable_set(:@_params, :button => "save")
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, {})
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domains_priority_edit)
      domain_order = []
      MiqAeDomain.order('priority ASC').collect { |d| domain_order.push(d.name) unless d.priority == 0 }
      expect(domain_order).to eq(edit[:new][:domain_order])
    end
  end

  context "#copy_objects" do
    it "do not replace left side explorer tree when copy form is loaded initially" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      node = "aec-#{cls1.id}"
      controller.instance_variable_set(:@sb,
                                       :active_tree => :ae_tree,
                                       :action      => "miq_ae_class_copy",
                                       :trees       => {:ae_tree => {:active_node => node}})
      controller.instance_variable_set(:@_params, :button => "reset", :id => cls1.id)
      allow(controller).to receive(:open_parent_nodes)
      expect(controller).to_not receive(:replace_trees_by_presenter)
      expect(controller).to receive(:render)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).not_to be_truthy
    end

    it "copies class under specified namespace" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      d2 = FactoryGirl.create(:miq_ae_domain, :name => "domain2")
      ns2 = FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => d2.id)

      new = {:domain => d2.id, :namespace => ns2.fqname, :overwrite_location => false}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).not_to be_truthy
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
      expect(MiqAeClass.find_by(:name => cls1.name, :namespace_id => ns2.id)).not_to be_nil
    end

    it "copy class under same namespace returns error when class exists" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      new = {:domain => d1.id, :namespace => ns1.fqname, :overwrite_location => false}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      expect(controller).to receive(:render)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_truthy
      expect(assigns(:flash_array).first[:message]).to include("Error during 'Automate Class copy':")
    end

    it "overwrite class under same namespace when class exists" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      d2 = FactoryGirl.create(:miq_ae_domain)
      ns2 = FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => d2.id)

      new = {:domain => d2.id, :namespace => ns2.fqname, :override_existing => true}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_falsey
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
    end

    it "copies a class with new name under same domain" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      new = {:domain => d1.id, :namespace => ns1.fqname, :override_existing => true, :new_name => 'foo'}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :old_name       => cls1.name,
        :fqname         => cls1.fqname,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_falsey
      expect(assigns(:record).name).to eq('foo')
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
    end
  end

  context "get selected Class/Instance/Method record back" do
    let(:miq_ae_domain) { double("MiqAeDomain", :name => "yet_another_fqname", :id => 1) }
    let(:miq_ae_domain2) { double("MiqAeDomain", :name => "yet_another_fqname2", :id => 2) }

    let(:miq_ae_class) do
      double("MiqAeClass",
                                    :id           => 1,
                                    :fqname       => "cls_fqname",
                                    :display_name => "FOO",
                                    :name         => "foo",
                                    :ae_fields    => [],
                                    :ae_instances => [],
                                    :ae_methods   => [],
                                    :domain       => miq_ae_domain2
                                   )
    end

    let(:miq_ae_instance) do
      double("MiqAeInstance",
                                    :id           => 123,
                                    :display_name => "some name",
                                    :name         => "some_name",
                                    :fqname       => "fqname",
                                    :created_on   => Time.now,
                                    :updated_on   => Time.current,
                                    :updated_by   => "some_user",
                                    :domain       => miq_ae_domain
                                   )
    end

    let(:miq_ae_method) do
      double("MiqAeMethod",
                                    :id           => 123,
                                    :display_name => "some name",
                                    :inputs       => [],
                                    :name         => "some_name",
                                    :fqname       => "fqname",
                                    :created_on   => Time.now,
                                    :updated_on   => Time.current,
                                    :updated_by   => "some_user",
                                    :domain       => miq_ae_domain
                                   )
    end

    let(:override) do
      double("MiqAeClass",
                                    :fqname => "another_fqname/fqname",
                                    :id     => 1,
                                    :domain => miq_ae_domain
                                   )
    end
    let(:override2) do
      double("MiqAeClass",
                                    :fqname => "another_fqname2/fqname",
                                    :id     => 2,
                                    :domain => miq_ae_domain2
                                   )
    end

    before do
      @user =  FactoryGirl.create(:user_with_group)
      login_as @user
      allow(MiqAeDomain).to receive(:find_by_name).with("another_fqname").and_return(miq_ae_domain)
      allow(MiqAeDomain).to receive(:find_by_name).with("another_fqname2").and_return(miq_ae_domain2)
    end

    context "#node_info" do
      it "collect namespace info" do
        stub_user(:features => :all)
        d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
        ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
        FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => ns1.id)
        FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)
        node = "aen-#{ns1.id}"
        controller.instance_variable_set(:@sb,
                                         :active_tree => :ae_tree,
                                         :trees       => {:ae_tree => {:active_node => node}})
        controller.send(:get_node_info, node)
        expect(assigns(:sb)[:namespace_path]).to eq(ns1.fqname.gsub!(%r{\/}, " / "))
      end
    end

    context "#get_instance_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aei some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aei-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeInstance).to receive(:find_by_id).with(123).and_return(miq_ae_instance)
          allow(miq_ae_instance).to receive(:ae_class).and_return(miq_ae_class)
          allow(MiqAeInstance).to receive(:get_homonymic_across_domains)
            .with(@user, "fqname").and_return([override, override2])
        end

        it "return instance record and check count of override instances being returned" do
          id = ["aei", miq_ae_instance.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_instance.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include("Automate Instance [#{miq_ae_instance.display_name}")
        end
      end
    end

    context "#get_class_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aec some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aec-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeClass).to receive(:find_by_id).with(1).and_return(miq_ae_class)
          allow(MiqAeClass).to receive(:get_homonymic_across_domains)
            .with(@user, "cls_fqname").and_return([override, override2])
        end

        it "returns class record and check count of override classes being returned" do
          id = ["aec", miq_ae_class.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_class_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_class.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include(miq_ae_class.display_name)
        end
      end
    end

    context "#get_method_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aem some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aem-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeMethod).to receive(:find_by_id).with(123).and_return(miq_ae_method)
          allow(miq_ae_method).to receive(:ae_class).and_return(miq_ae_class)
          allow(MiqAeMethod).to receive(:get_homonymic_across_domains)
            .with(@user, "fqname").and_return([override, override2])
        end

        it "returns method record and check count of override methods being returned" do
          id = ["aem", miq_ae_method.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_method_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_method.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include("Automate Method [#{miq_ae_method.display_name}")
        end
      end
    end
  end

  context "#delete_domain" do
    it "Should only delete editable domains" do
      stub_user(:features => :all)
      domain1 = FactoryGirl.create(:miq_ae_system_domain_enabled)

      domain2 = FactoryGirl.create(:miq_ae_domain_enabled)
      domain3 = FactoryGirl.create(:miq_ae_git_domain)
      controller.instance_variable_set(:@_params,
                                       :miq_grid_checks => "aen-#{domain1.id}, aen-#{domain2.id}, aen-#{domain3.id}, aen-someid"
                                      )
      allow(controller).to receive(:replace_right_cell)
      expect_any_instance_of(GitBasedDomainImportService).to receive(:destroy_repository).with(domain3.git_repository.id)
      controller.send(:delete_domain)
      flash_messages = assigns(:flash_array)
      expect(flash_messages.first[:message]).to include("cannot be deleted")
      expect(flash_messages.first[:level]).to eq(:error)
      expect(flash_messages.second[:message]).to include("Delete successful")
      expect(flash_messages.second[:level]).to eq(:success)
      expect(flash_messages.last[:message]).to include("Delete successful")
      expect(flash_messages.last[:level]).to eq(:success)
    end
  end

  context "#ae_class_validation" do
    before(:each) do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_namespace)
      @cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      @cls.ae_fields << FactoryGirl.create(:miq_ae_field, :name => 'fred',
                                           :class_id => @cls.id, :priority => 1)
      @cls.save
      @method = FactoryGirl.create(:miq_ae_method, :name => "method01", :scope => "class",
        :language => "ruby", :class_id => @cls.id, :data => "exit MIQ_OK", :location => "inline")
      expect(controller).to receive(:render)
      controller.instance_variable_set(:@sb, :trees       => {:ae_tree => {:active_node => "aec-#{@cls.id}"}},
                                             :active_tree => :ae_tree)<commit_msg>PR review changes<commit_after>describe MiqAeClassController do
  include CompressedIds
  context "#set_record_vars" do
    it "Namespace remains unchanged when a class is edited" do
      ns = FactoryGirl.create(:miq_ae_namespace)
      cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      ns_id = cls.namespace_id
      new = {:name => "New Name", :description => "New Description", :display_name => "Display Name", :inherits => "Some_Class"}
      controller.instance_variable_set(:@sb,
                                       :trees       => {
                                         :ae_tree => {:active_node => "aec-#{cls.id}"}
                                       },
                                       :active_tree => :ae_tree)
      controller.instance_variable_set(:@edit, {:new => new})
      controller.send(:set_record_vars, cls)
      expect(cls.namespace_id).to eq(ns_id)
    end
  end

  context "#set_right_cell_text" do
    it "check if correct namespace_path is being set" do
      ns = FactoryGirl.create(:miq_ae_namespace)
      cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      controller.instance_variable_set(:@sb, {})
      id = "aec-#{cls.id}"
      fq_name = cls.fqname
      controller.send(:set_right_cell_text, id, cls)
      expect(assigns(:sb)[:namespace_path]).to eq(fq_name.gsub!(%r{\/}, " / "))

      id = "root"
      fq_name = ""
      controller.send(:set_right_cell_text, id)
      expect(assigns(:sb)[:namespace_path]).to eq(fq_name)
    end
  end

  context "#domain_lock" do
    it "Marks domain as locked/readonly" do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_domain_enabled)
      controller.instance_variable_set(:@_params, :id => ns.id)
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domain_lock)
      ns.reload
      expect(ns.contents_locked?).to eq(true)
    end
  end

  context "#domain_unlock" do
    it "Marks domain as unlocked/editable" do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_domain_disabled)
      controller.instance_variable_set(:@_params, :id => ns.id)
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domain_unlock)
      ns.reload
      expect(ns.contents_locked?).to eq(false)
    end
  end

  context "#domains_priority_edit" do
    it "sets priority of domains" do
      stub_user(:features => :all)
      FactoryGirl.create(:miq_ae_domain, :name => "test1", :parent => nil, :priority => 1)
      FactoryGirl.create(:miq_ae_domain, :name => "test2", :parent => nil, :priority => 2)
      FactoryGirl.create(:miq_ae_domain, :name => "test3", :parent => nil, :priority => 3)
      FactoryGirl.create(:miq_ae_domain, :name => "test4", :parent => nil, :priority => 4)
      order = %w(test3 test2 test4 test1)
      edit = {
        :new     => {:domain_order => order},
        :key     => "priority__edit",
        :current => {:domain_order => order},
      }
      controller.instance_variable_set(:@_params, :button => "save")
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, {})
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:domains_priority_edit)
      domain_order = []
      MiqAeDomain.order('priority ASC').collect { |d| domain_order.push(d.name) unless d.priority == 0 }
      expect(domain_order).to eq(edit[:new][:domain_order])
    end
  end

  context "#copy_objects" do
    it "do not replace left side explorer tree when copy form is loaded initially" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      node = "aec-#{cls1.id}"
      controller.instance_variable_set(:@sb,
                                       :active_tree => :ae_tree,
                                       :action      => "miq_ae_class_copy",
                                       :trees       => {:ae_tree => {:active_node => node}})
      controller.instance_variable_set(:@_params, :button => "reset", :id => cls1.id)
      allow(controller).to receive(:open_parent_nodes)
      expect(controller).to_not receive(:replace_trees_by_presenter)
      expect(controller).to receive(:render)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).not_to be_truthy
    end

    it "copies class under specified namespace" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      d2 = FactoryGirl.create(:miq_ae_domain, :name => "domain2")
      ns2 = FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => d2.id)

      new = {:domain => d2.id, :namespace => ns2.fqname, :overwrite_location => false}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).not_to be_truthy
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
      expect(MiqAeClass.find_by(:name => cls1.name, :namespace_id => ns2.id)).not_to be_nil
    end

    it "copy class under same namespace returns error when class exists" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      new = {:domain => d1.id, :namespace => ns1.fqname, :overwrite_location => false}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      expect(controller).to receive(:render)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_truthy
      expect(assigns(:flash_array).first[:message]).to include("Error during 'Automate Class copy':")
    end

    it "overwrite class under same namespace when class exists" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      d2 = FactoryGirl.create(:miq_ae_domain)
      ns2 = FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => d2.id)

      new = {:domain => d2.id, :namespace => ns2.fqname, :override_existing => true}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_falsey
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
    end

    it "copies a class with new name under same domain" do
      stub_user(:features => :all)
      d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
      ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
      cls1 = FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)

      new = {:domain => d1.id, :namespace => ns1.fqname, :override_existing => true, :new_name => 'foo'}
      selected_items = {cls1.id => cls1.name}
      edit = {
        :new            => new,
        :old_name       => cls1.name,
        :fqname         => cls1.fqname,
        :typ            => MiqAeClass,
        :rec_id         => cls1.id,
        :key            => "copy_objects__#{cls1.id}",
        :current        => new,
        :selected_items => selected_items,
      }
      controller.instance_variable_set(:@_params, :button => "copy", :id => cls1.id)
      controller.instance_variable_set(:@edit, edit)
      controller.instance_variable_set(:@sb, :action => "miq_ae_class_copy")
      session[:edit] = edit
      allow(controller).to receive(:replace_right_cell)
      controller.send(:copy_objects)
      expect(controller.send(:flash_errors?)).to be_falsey
      expect(assigns(:record).name).to eq('foo')
      expect(assigns(:flash_array).first[:message]).to include("Copy selected Automate Class was saved")
    end
  end

  context "get selected Class/Instance/Method record back" do
    let(:miq_ae_domain) { double("MiqAeDomain", :name => "yet_another_fqname", :id => 1) }
    let(:miq_ae_domain2) { double("MiqAeDomain", :name => "yet_another_fqname2", :id => 2) }

    let(:miq_ae_class) do
      double("MiqAeClass",
                                    :id           => 1,
                                    :fqname       => "cls_fqname",
                                    :display_name => "FOO",
                                    :name         => "foo",
                                    :ae_fields    => [],
                                    :ae_instances => [],
                                    :ae_methods   => [],
                                    :domain       => miq_ae_domain2
                                   )
    end

    let(:miq_ae_instance) do
      double("MiqAeInstance",
                                    :id           => 123,
                                    :display_name => "<NAME>",
                                    :name         => "some_name",
                                    :fqname       => "fqname",
                                    :created_on   => Time.now,
                                    :updated_on   => Time.current,
                                    :updated_by   => "some_user",
                                    :domain       => miq_ae_domain
                                   )
    end

    let(:miq_ae_method) do
      double("MiqAeMethod",
                                    :id           => 123,
                                    :display_name => "<NAME>",
                                    :inputs       => [],
                                    :name         => "some_name",
                                    :fqname       => "fqname",
                                    :created_on   => Time.now,
                                    :updated_on   => Time.current,
                                    :updated_by   => "some_user",
                                    :domain       => miq_ae_domain
                                   )
    end

    let(:override) do
      double("MiqAeClass",
                                    :fqname => "another_fqname/fqname",
                                    :id     => 1,
                                    :domain => miq_ae_domain
                                   )
    end
    let(:override2) do
      double("MiqAeClass",
                                    :fqname => "another_fqname2/fqname",
                                    :id     => 2,
                                    :domain => miq_ae_domain2
                                   )
    end

    before do
      @user =  FactoryGirl.create(:user_with_group)
      login_as @user
      allow(MiqAeDomain).to receive(:find_by_name).with("another_fqname").and_return(miq_ae_domain)
      allow(MiqAeDomain).to receive(:find_by_name).with("another_fqname2").and_return(miq_ae_domain2)
    end

    context "#node_info" do
      it "collect namespace info" do
        stub_user(:features => :all)
        d1 = FactoryGirl.create(:miq_ae_domain, :name => "domain1")
        ns1 = FactoryGirl.create(:miq_ae_namespace, :name => "ns1", :parent_id => d1.id)
        FactoryGirl.create(:miq_ae_namespace, :name => "ns2", :parent_id => ns1.id)
        FactoryGirl.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)
        node = "aen-#{ns1.id}"
        controller.instance_variable_set(:@sb,
                                         :active_tree => :ae_tree,
                                         :trees       => {:ae_tree => {:active_node => node}})
        controller.send(:get_node_info, node)
        expect(assigns(:sb)[:namespace_path]).to eq(ns1.fqname.gsub!(%r{\/}, " / "))
      end
    end

    context "#get_instance_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aei some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aei-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeInstance).to receive(:find_by_id).with(123).and_return(miq_ae_instance)
          allow(miq_ae_instance).to receive(:ae_class).and_return(miq_ae_class)
          allow(MiqAeInstance).to receive(:get_homonymic_across_domains)
            .with(@user, "fqname").and_return([override, override2])
        end

        it "return instance record and check count of override instances being returned" do
          id = ["aei", miq_ae_instance.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_instance.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include("Automate Instance [#{miq_ae_instance.display_name}")
        end
      end
    end

    context "#get_class_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aec some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aec-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeClass).to receive(:find_by_id).with(1).and_return(miq_ae_class)
          allow(MiqAeClass).to receive(:get_homonymic_across_domains)
            .with(@user, "cls_fqname").and_return([override, override2])
        end

        it "returns class record and check count of override classes being returned" do
          id = ["aec", miq_ae_class.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_class_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_class.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include(miq_ae_class.display_name)
        end
      end
    end

    context "#get_method_node_info" do
      context "when record does not exist" do
        it "sets active node back to root" do
          id = %w(aem some_id)
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => "aem-some_id"}})
          controller.send(:get_instance_node_info, id)
          expect(assigns(:sb)[:trees][:ae_tree][:active_node]).to eq("root")
        end
      end

      context "when the record exists" do
        before do
          allow(MiqAeMethod).to receive(:find_by_id).with(123).and_return(miq_ae_method)
          allow(miq_ae_method).to receive(:ae_class).and_return(miq_ae_class)
          allow(MiqAeMethod).to receive(:get_homonymic_across_domains)
            .with(@user, "fqname").and_return([override, override2])
        end

        it "returns method record and check count of override methods being returned" do
          id = ["aem", miq_ae_method.id]
          controller.instance_variable_set(:@sb,
                                           :active_tree => :ae_tree,
                                           :trees       => {:ae_tree => {:active_node => id.join("-")}})
          controller.send(:get_method_node_info, id)
          expect(assigns(:record).name).to eq(miq_ae_method.name)
          expect(assigns(:domain_overrides).count).to eq(2)
          expect(assigns(:right_cell_text)).to include("Automate Method [#{miq_ae_method.display_name}")
        end
      end
    end
  end

  context "#delete_domain" do
    let(:domain1) { FactoryGirl.create(:miq_ae_system_domain_enabled) }
    let(:domain2) { FactoryGirl.create(:miq_ae_domain_enabled) }
    let(:domain3) { FactoryGirl.create(:miq_ae_git_domain) }
    let(:ids) { "aen-#{domain1.id}, aen-#{domain2.id}, aen-#{domain3.id}, aen-someid" }
    let(:git_service) { double("GitBasedDomainImportService") }

    before do
      allow(GitBasedDomainImportService).to receive(:new).and_return(git_service)
      stub_user(:features => :all)
      controller.instance_variable_set(:@_params, :miq_grid_checks => ids)
      allow(controller).to receive(:replace_right_cell)
    end

    it "Should only delete editable domains" do
      expect(git_service).to receive(:destroy_repository).with(domain3.git_repository.id)
      controller.send(:delete_domain)

      flash_messages = assigns(:flash_array)
      expect(flash_messages.first[:message]).to include("cannot be deleted")
      expect(flash_messages.first[:level]).to eq(:error)
      expect(flash_messages.second[:message]).to include("Delete successful")
      expect(flash_messages.second[:level]).to eq(:success)
      expect(flash_messages.last[:message]).to include("Delete successful")
      expect(flash_messages.last[:level]).to eq(:success)
    end
  end

  context "#ae_class_validation" do
    before(:each) do
      stub_user(:features => :all)
      ns = FactoryGirl.create(:miq_ae_namespace)
      @cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id)
      @cls.ae_fields << FactoryGirl.create(:miq_ae_field, :name => 'fred',
                                           :class_id => @cls.id, :priority => 1)
      @cls.save
      @method = FactoryGirl.create(:miq_ae_method, :name => "method01", :scope => "class",
        :language => "ruby", :class_id => @cls.id, :data => "exit MIQ_OK", :location => "inline")
      expect(controller).to receive(:render)
      controller.instance_variable_set(:@sb, :trees       => {:ae_tree => {:active_node => "aec-#{@cls.id}"}},
                                             :active_tree => :ae_tree)
{'fraction_non_alphanumeric': 0.1282703488372093, 'fraction_numerical': 0.007218992248062016, 'mean_word_length': 2.0901265064750354, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 447, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 164, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209017', 'n_tokens_mistral': 12585, 'n_tokens_neox': 12270, 'n_words': 2258}