<commit_before>module Vcloud
  module Tools
    module Tester
      class FixtureParameters
        attr_reader :fixture_params

        def initialize(user_params)
          @vcloud_api = Vcloud::Fog::ModelInterface.new
          @user_params = user_params
          ensure_vcloud_fixtures
          extract_fixture_params
        end

        private

        def ensure_vcloud_fixtures
          generate_fixtures_config
          correct_networks = ensure_networks_correct(@expected_fixtures_config[:networks])
          @fixtures = correct_networks
        end

        def generate_fixtures_config
          @expected_fixtures_config = {
            :networks => {
              :network_1 => {
                :edge_gateway => @user_params["edge_gateway"],
                :vdc_name     => @user_params["vdc_1_name"],
                :name         => @user_params["network_1"],
                :type         => 'application/vnd.vmware.vcloud.orgVdcNetwork+xml',
                :description  => '',
                :is_inherited => 'false',
                :is_shared    => 'true',
                :fence_mode   => 'natRouted',
                :gateway      => '192.168.1.1',
                :netmask      => '255.255.255.0',
                :dns1         => nil,
                :dns2         => nil,
                :dns_suffix   => nil,
                :ip_ranges    => [
                  {
                    :start_address  => "192.168.1.2",
                    :end_address    => "192.168.1.254"
                  }
                ],
              },
              :network_2 => {
                :edge_gateway => @user_params["edge_gateway"],
                :vdc_name     => @user_params["vdc_2_name"],
                :name         => @user_params["network_2"],
                :type         => 'application/vnd.vmware.vcloud.orgVdcNetwork+xml',
                :description  => '',
                :is_inherited => 'false',
                :is_shared    => 'true',
                :fence_mode   => 'isolated',
                :gateway      => '10.0.0.1',
                :netmask      => '255.255.0.0',
                :dns1         => nil,
                :dns2         => nil,
                :dns_suffix   => nil,
                :ip_ranges    => [
                  {
                    :start_address  => "10.0.0.2",
                    :end_address    => "10.0.255.254"
                  }
                ],
              },
            },
          }
        end

        def ensure_networks_correct(expected_network_config)
          existing_networks = @vcloud_api.current_organization.networks.all(false)

          correct_networks = []

          expected_network_config.each_value do |expected_config|
            # find an existing network matching the expected configuration
            found_network = existing_networks.detect { |n| n.name == expected_config[:name] }

            unless found_network
              new_network = Vcloud::Core::OrgVdcNetwork.provision(expected_config)
              correct_networks << new_network
              next
            end

            unless network_matches_expected?(found_network, expected_config)
              raise "Network '#{expected_config[:name]}' already exists but is not configured as expected.
                You should delete this network before re-running the tests; it will be re-created by the tests."
            end

            correct_networks << found_network
          end

          correct_networks
        end

        def extract_fixture_params
          raise "No fixtures present" if @fixtures.empty?

          @fixture_params = {}
          expected_networks = @expected_fixtures_config[:networks]

          @fixtures.each do |fixture|
            case fixture
            when ::Fog::Compute::VcloudDirector::Network
              expected_networks.each do |network_ref, expected_network_config|
                if expected_network_config[:name] == fixture.name
                  @fixture_params["#{network_ref}_id"] = fixture.id
                end
              end
            end
          end
        end

        def network_matches_expected?(found_network, expected_network_config)
            network_config_matches_expected?(found_network, expected_network_config) &&
              network_available_to_correct_vdc?(found_network, expected_network_config)
        end

        def network_config_matches_expected?(found_network, expected_network_config)
            found_network_config = found_network.instance_variable_get(:@attributes)

            expected_network_config = expected_network_config.reject do |key|
              %w{edge_gateway vdc_name}.include?(key.to_s)
            end

            found_network_config = found_network_config.reject do |key|
              %w{id edge_gateway href}.include?(key.to_s)
            end

            # sort hashes ready for comparison
            found_network_config = Hash[found_network_config.sort]<commit_msg>[#74863398] Extract fixture parameters regardless of class<commit_after>module Vcloud
  module Tools
    module Tester
      class FixtureParameters
        attr_reader :fixture_params

        def initialize(user_params)
          @vcloud_api = Vcloud::Fog::ModelInterface.new
          @user_params = user_params
          ensure_vcloud_fixtures
          extract_fixture_params
        end

        private

        def ensure_vcloud_fixtures
          generate_fixtures_config
          correct_networks = ensure_networks_correct(@expected_fixtures_config[:networks])
          @fixtures = correct_networks
        end

        def generate_fixtures_config
          @expected_fixtures_config = {
            :networks => {
              :network_1 => {
                :edge_gateway => @user_params["edge_gateway"],
                :vdc_name     => @user_params["vdc_1_name"],
                :name         => @user_params["network_1"],
                :type         => 'application/vnd.vmware.vcloud.orgVdcNetwork+xml',
                :description  => '',
                :is_inherited => 'false',
                :is_shared    => 'true',
                :fence_mode   => 'natRouted',
                :gateway      => '192.168.1.1',
                :netmask      => '255.255.255.0',
                :dns1         => nil,
                :dns2         => nil,
                :dns_suffix   => nil,
                :ip_ranges    => [
                  {
                    :start_address  => "192.168.1.2",
                    :end_address    => "192.168.1.254"
                  }
                ],
              },
              :network_2 => {
                :edge_gateway => @user_params["edge_gateway"],
                :vdc_name     => @user_params["vdc_2_name"],
                :name         => @user_params["network_2"],
                :type         => 'application/vnd.vmware.vcloud.orgVdcNetwork+xml',
                :description  => '',
                :is_inherited => 'false',
                :is_shared    => 'true',
                :fence_mode   => 'isolated',
                :gateway      => '10.0.0.1',
                :netmask      => '255.255.0.0',
                :dns1         => nil,
                :dns2         => nil,
                :dns_suffix   => nil,
                :ip_ranges    => [
                  {
                    :start_address  => "10.0.0.2",
                    :end_address    => "10.0.255.254"
                  }
                ],
              },
            },
          }
        end

        def ensure_networks_correct(expected_network_config)
          existing_networks = @vcloud_api.current_organization.networks.all(false)

          correct_networks = []

          expected_network_config.each_value do |expected_config|
            # find an existing network matching the expected configuration
            found_network = existing_networks.detect { |n| n.name == expected_config[:name] }

            unless found_network
              new_network = Vcloud::Core::OrgVdcNetwork.provision(expected_config)
              correct_networks << new_network
              next
            end

            unless network_matches_expected?(found_network, expected_config)
              raise "Network '#{expected_config[:name]}' already exists but is not configured as expected.
                You should delete this network before re-running the tests; it will be re-created by the tests."
            end

            correct_networks << found_network
          end

          correct_networks
        end

        def extract_fixture_params
          raise "No fixtures present" if @fixtures.empty?

          @fixture_params = {}
          expected_networks = @expected_fixtures_config[:networks]

          @fixtures.each do |fixture|
            case fixture
            when ::Fog::Compute::VcloudDirector::Network, Vcloud::Core::OrgVdcNetwork
              expected_networks.each do |network_ref, expected_network_config|
                if expected_network_config[:name] == fixture.name
                  @fixture_params["#{network_ref}_id"] = fixture.id
                end
              end
            end
          end
        end

        def network_matches_expected?(found_network, expected_network_config)
            network_config_matches_expected?(found_network, expected_network_config) &&
              network_available_to_correct_vdc?(found_network, expected_network_config)
        end

        def network_config_matches_expected?(found_network, expected_network_config)
            found_network_config = found_network.instance_variable_get(:@attributes)

            expected_network_config = expected_network_config.reject do |key|
              %w{edge_gateway vdc_name}.include?(key.to_s)
            end

            found_network_config = found_network_config.reject do |key|
              %w{id edge_gateway href}.include?(key.to_s)
            end

            # sort hashes ready for comparison
            found_network_config = Hash[found_network_config.sort]
{'fraction_non_alphanumeric': 0.0879077167859984, 'fraction_numerical': 0.015314240254574384, 'mean_word_length': 1.5787179487179488, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 16, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '815935', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2618, 'n_words': 460}