// Code generated by mdatagen. DO NOT EDIT.

package metadata

import (
	"path/filepath"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/collector/confmap"
	"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestMetricsBuilderConfig(t *testing.T) {
	tests := []struct {
		name string
		want MetricsBuilderConfig
	}{
		{
			name: "default",
			want: NewDefaultMetricsBuilderConfig(),
		},
		{
			name: "all_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					DefaultMetric: DefaultMetricMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []DefaultMetricMetricAttributeKey{DefaultMetricMetricAttributeKeyStringAttr, DefaultMetricMetricAttributeKeyOverriddenIntAttr, DefaultMetricMetricAttributeKeyEnumAttr, DefaultMetricMetricAttributeKeySliceAttr, DefaultMetricMetricAttributeKeyMapAttr, DefaultMetricMetricAttributeKeyConditionalIntAttr, DefaultMetricMetricAttributeKeyConditionalStringAttr, DefaultMetricMetricAttributeKeyOptInBoolAttr},
					},
					DefaultMetricToBeRemoved: DefaultMetricToBeRemovedMetricConfig{
						Enabled: true,
					},
					MetricInputType: MetricInputTypeMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []MetricInputTypeMetricAttributeKey{MetricInputTypeMetricAttributeKeyStringAttr, MetricInputTypeMetricAttributeKeyOverriddenIntAttr, MetricInputTypeMetricAttributeKeyEnumAttr, MetricInputTypeMetricAttributeKeySliceAttr, MetricInputTypeMetricAttributeKeyMapAttr},
					},
					OptionalMetric: OptionalMetricMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []OptionalMetricMetricAttributeKey{OptionalMetricMetricAttributeKeyStringAttr, OptionalMetricMetricAttributeKeyBooleanAttr, OptionalMetricMetricAttributeKeyBooleanAttr2, OptionalMetricMetricAttributeKeyConditionalStringAttr},
					},
					OptionalMetricEmptyUnit: OptionalMetricEmptyUnitMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []OptionalMetricEmptyUnitMetricAttributeKey{OptionalMetricEmptyUnitMetricAttributeKeyStringAttr, OptionalMetricEmptyUnitMetricAttributeKeyBooleanAttr},
					},
					ReaggregateMetric: ReaggregateMetricMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []ReaggregateMetricMetricAttributeKey{ReaggregateMetricMetricAttributeKeyStringAttr, ReaggregateMetricMetricAttributeKeyBooleanAttr},
					},
					ReaggregateMetricWithRequired: ReaggregateMetricWithRequiredMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []ReaggregateMetricWithRequiredMetricAttributeKey{ReaggregateMetricWithRequiredMetricAttributeKeyRequiredStringAttr, ReaggregateMetricWithRequiredMetricAttributeKeyStringAttr, ReaggregateMetricWithRequiredMetricAttributeKeyBooleanAttr},
					},
					SystemCPUTime: SystemCPUTimeMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemCPUTimeMetricAttributeKey{SystemCPUTimeMetricAttributeKeyCPU},
					},
					SystemMemoryUsage: SystemMemoryUsageMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemMemoryUsageMetricAttributeKey{SystemMemoryUsageMetricAttributeKeyState},
					},
				},
				ResourceAttributes: ResourceAttributesConfig{
					HostArch:                              HostArchResourceAttributeConfig{Enabled: true},
					MapResourceAttr:                       MapResourceAttrResourceAttributeConfig{Enabled: true},
					OptionalResourceAttr:                  OptionalResourceAttrResourceAttributeConfig{Enabled: true},
					SliceResourceAttr:                     SliceResourceAttrResourceAttributeConfig{Enabled: true},
					StringEnumResourceAttr:                StringEnumResourceAttrResourceAttributeConfig{Enabled: true},
					StringResourceAttr:                    StringResourceAttrResourceAttributeConfig{Enabled: true},
					StringResourceAttrDisableWarning:      StringResourceAttrDisableWarningResourceAttributeConfig{Enabled: true},
					StringResourceAttrRemoveWarning:       StringResourceAttrRemoveWarningResourceAttributeConfig{Enabled: true},
					StringResourceAttrToBeRemoved:         StringResourceAttrToBeRemovedResourceAttributeConfig{Enabled: true},
					StringResourceDisabledAttrToBeRemoved: StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{Enabled: true},
				},
			},
		},
		{
			name: "none_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					DefaultMetric: DefaultMetricMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []DefaultMetricMetricAttributeKey{DefaultMetricMetricAttributeKeyStringAttr, DefaultMetricMetricAttributeKeyOverriddenIntAttr, DefaultMetricMetricAttributeKeyEnumAttr, DefaultMetricMetricAttributeKeySliceAttr, DefaultMetricMetricAttributeKeyMapAttr, DefaultMetricMetricAttributeKeyConditionalIntAttr, DefaultMetricMetricAttributeKeyConditionalStringAttr, DefaultMetricMetricAttributeKeyOptInBoolAttr},
					},
					DefaultMetricToBeRemoved: DefaultMetricToBeRemovedMetricConfig{
						Enabled: false,
					},
					MetricInputType: MetricInputTypeMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []MetricInputTypeMetricAttributeKey{MetricInputTypeMetricAttributeKeyStringAttr, MetricInputTypeMetricAttributeKeyOverriddenIntAttr, MetricInputTypeMetricAttributeKeyEnumAttr, MetricInputTypeMetricAttributeKeySliceAttr, MetricInputTypeMetricAttributeKeyMapAttr},
					},
					OptionalMetric: OptionalMetricMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []OptionalMetricMetricAttributeKey{OptionalMetricMetricAttributeKeyStringAttr, OptionalMetricMetricAttributeKeyBooleanAttr, OptionalMetricMetricAttributeKeyBooleanAttr2, OptionalMetricMetricAttributeKeyConditionalStringAttr},
					},
					OptionalMetricEmptyUnit: OptionalMetricEmptyUnitMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []OptionalMetricEmptyUnitMetricAttributeKey{OptionalMetricEmptyUnitMetricAttributeKeyStringAttr, OptionalMetricEmptyUnitMetricAttributeKeyBooleanAttr},
					},
					ReaggregateMetric: ReaggregateMetricMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []ReaggregateMetricMetricAttributeKey{ReaggregateMetricMetricAttributeKeyStringAttr, ReaggregateMetricMetricAttributeKeyBooleanAttr},
					},
					ReaggregateMetricWithRequired: ReaggregateMetricWithRequiredMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []ReaggregateMetricWithRequiredMetricAttributeKey{ReaggregateMetricWithRequiredMetricAttributeKeyRequiredStringAttr, ReaggregateMetricWithRequiredMetricAttributeKeyStringAttr, ReaggregateMetricWithRequiredMetricAttributeKeyBooleanAttr},
					},
					SystemCPUTime: SystemCPUTimeMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemCPUTimeMetricAttributeKey{SystemCPUTimeMetricAttributeKeyCPU},
					},
					SystemMemoryUsage: SystemMemoryUsageMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemMemoryUsageMetricAttributeKey{SystemMemoryUsageMetricAttributeKeyState},
					},
				},
				ResourceAttributes: ResourceAttributesConfig{
					HostArch:                              HostArchResourceAttributeConfig{Enabled: false},
					MapResourceAttr:                       MapResourceAttrResourceAttributeConfig{Enabled: false},
					OptionalResourceAttr:                  OptionalResourceAttrResourceAttributeConfig{Enabled: false},
					SliceResourceAttr:                     SliceResourceAttrResourceAttributeConfig{Enabled: false},
					StringEnumResourceAttr:                StringEnumResourceAttrResourceAttributeConfig{Enabled: false},
					StringResourceAttr:                    StringResourceAttrResourceAttributeConfig{Enabled: false},
					StringResourceAttrDisableWarning:      StringResourceAttrDisableWarningResourceAttributeConfig{Enabled: false},
					StringResourceAttrRemoveWarning:       StringResourceAttrRemoveWarningResourceAttributeConfig{Enabled: false},
					StringResourceAttrToBeRemoved:         StringResourceAttrToBeRemovedResourceAttributeConfig{Enabled: false},
					StringResourceDisabledAttrToBeRemoved: StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{Enabled: false},
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := loadMetricsBuilderConfig(t, tt.name)
			diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(DefaultMetricMetricConfig{}, DefaultMetricToBeRemovedMetricConfig{}, MetricInputTypeMetricConfig{}, OptionalMetricMetricConfig{}, OptionalMetricEmptyUnitMetricConfig{}, ReaggregateMetricMetricConfig{}, ReaggregateMetricWithRequiredMetricConfig{}, SystemCPUTimeMetricConfig{}, SystemMemoryUsageMetricConfig{}, HostArchResourceAttributeConfig{}, MapResourceAttrResourceAttributeConfig{}, OptionalResourceAttrResourceAttributeConfig{}, SliceResourceAttrResourceAttributeConfig{}, StringEnumResourceAttrResourceAttributeConfig{}, StringResourceAttrResourceAttributeConfig{}, StringResourceAttrDisableWarningResourceAttributeConfig{}, StringResourceAttrRemoveWarningResourceAttributeConfig{}, StringResourceAttrToBeRemovedResourceAttributeConfig{}, StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{}))
			require.Emptyf(t, diff, "Config mismatch (-expected +actual):\n%s", diff)
		})
	}
}
func TestDefaultMetricMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().DefaultMetric
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []DefaultMetricMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric default.metric doesn't have an attribute invalid, valid attributes: [string_attr, state, enum_attr, slice_attr, map_attr, conditional_int_attr, conditional_string_attr, opt_in_bool_attr]")

	cfg = DefaultMetricsConfig().DefaultMetric
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestMetricInputTypeMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().MetricInputType
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []MetricInputTypeMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric metric.input_type doesn't have an attribute invalid, valid attributes: [string_attr, state, enum_attr, slice_attr, map_attr]")

	cfg = DefaultMetricsConfig().MetricInputType
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestOptionalMetricMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().OptionalMetric
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []OptionalMetricMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric optional.metric doesn't have an attribute invalid, valid attributes: [string_attr, boolean_attr, boolean_attr2, conditional_string_attr]")

	cfg = DefaultMetricsConfig().OptionalMetric
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestOptionalMetricEmptyUnitMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().OptionalMetricEmptyUnit
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []OptionalMetricEmptyUnitMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric optional.metric.empty_unit doesn't have an attribute invalid, valid attributes: [string_attr, boolean_attr]")

	cfg = DefaultMetricsConfig().OptionalMetricEmptyUnit
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestReaggregateMetricMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().ReaggregateMetric
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []ReaggregateMetricMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric reaggregate.metric doesn't have an attribute invalid, valid attributes: [string_attr, boolean_attr]")

	cfg = DefaultMetricsConfig().ReaggregateMetric
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestReaggregateMetricWithRequiredMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().ReaggregateMetricWithRequired
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []ReaggregateMetricWithRequiredMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric reaggregate.metric.with_required doesn't have an attribute invalid, valid attributes: [required_string_attr, string_attr, boolean_attr]")

	cfg = DefaultMetricsConfig().ReaggregateMetricWithRequired
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestSystemCPUTimeMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().SystemCPUTime
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []SystemCPUTimeMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric system.cpu.time doesn't have an attribute invalid, valid attributes: [cpu]")

	cfg = DefaultMetricsConfig().SystemCPUTime
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestSystemMemoryUsageMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().SystemMemoryUsage
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []SystemMemoryUsageMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric system.memory.usage doesn't have an attribute invalid, valid attributes: [state]")

	cfg = DefaultMetricsConfig().SystemMemoryUsage
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func loadMetricsBuilderConfig(t *testing.T, name string) MetricsBuilderConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	cfg := NewDefaultMetricsBuilderConfig()
	require.NoError(t, sub.Unmarshal(&cfg, confmap.WithIgnoreUnused()))
	return cfg
}

func loadLogsBuilderConfig(t *testing.T, name string) LogsBuilderConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	cfg := DefaultLogsBuilderConfig()
	require.NoError(t, sub.Unmarshal(&cfg, confmap.WithIgnoreUnused()))
	return cfg
}

func TestResourceAttributesConfig(t *testing.T) {
	tests := []struct {
		name string
		want ResourceAttributesConfig
	}{
		{
			name: "default",
			want: DefaultResourceAttributesConfig(),
		},
		{
			name: "all_set",
			want: ResourceAttributesConfig{
				HostArch:                              HostArchResourceAttributeConfig{Enabled: true},
				MapResourceAttr:                       MapResourceAttrResourceAttributeConfig{Enabled: true},
				OptionalResourceAttr:                  OptionalResourceAttrResourceAttributeConfig{Enabled: true},
				SliceResourceAttr:                     SliceResourceAttrResourceAttributeConfig{Enabled: true},
				StringEnumResourceAttr:                StringEnumResourceAttrResourceAttributeConfig{Enabled: true},
				StringResourceAttr:                    StringResourceAttrResourceAttributeConfig{Enabled: true},
				StringResourceAttrDisableWarning:      StringResourceAttrDisableWarningResourceAttributeConfig{Enabled: true},
				StringResourceAttrRemoveWarning:       StringResourceAttrRemoveWarningResourceAttributeConfig{Enabled: true},
				StringResourceAttrToBeRemoved:         StringResourceAttrToBeRemovedResourceAttributeConfig{Enabled: true},
				StringResourceDisabledAttrToBeRemoved: StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{Enabled: true},
			},
		},
		{
			name: "none_set",
			want: ResourceAttributesConfig{
				HostArch:                              HostArchResourceAttributeConfig{Enabled: false},
				MapResourceAttr:                       MapResourceAttrResourceAttributeConfig{Enabled: false},
				OptionalResourceAttr:                  OptionalResourceAttrResourceAttributeConfig{Enabled: false},
				SliceResourceAttr:                     SliceResourceAttrResourceAttributeConfig{Enabled: false},
				StringEnumResourceAttr:                StringEnumResourceAttrResourceAttributeConfig{Enabled: false},
				StringResourceAttr:                    StringResourceAttrResourceAttributeConfig{Enabled: false},
				StringResourceAttrDisableWarning:      StringResourceAttrDisableWarningResourceAttributeConfig{Enabled: false},
				StringResourceAttrRemoveWarning:       StringResourceAttrRemoveWarningResourceAttributeConfig{Enabled: false},
				StringResourceAttrToBeRemoved:         StringResourceAttrToBeRemovedResourceAttributeConfig{Enabled: false},
				StringResourceDisabledAttrToBeRemoved: StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{Enabled: false},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := loadResourceAttributesConfig(t, tt.name)
			diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(HostArchResourceAttributeConfig{}, MapResourceAttrResourceAttributeConfig{}, OptionalResourceAttrResourceAttributeConfig{}, SliceResourceAttrResourceAttributeConfig{}, StringEnumResourceAttrResourceAttributeConfig{}, StringResourceAttrResourceAttributeConfig{}, StringResourceAttrDisableWarningResourceAttributeConfig{}, StringResourceAttrRemoveWarningResourceAttributeConfig{}, StringResourceAttrToBeRemovedResourceAttributeConfig{}, StringResourceDisabledAttrToBeRemovedResourceAttributeConfig{}))
			require.Emptyf(t, diff, "Config mismatch (-expected +actual):\n%s", diff)
		})
	}
}

func TestResourceAttributesOverrideConfig(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	assert.NotNil(t, cfg.HostArch.OverrideValue, "override_value should be set for host.arch")
	assert.NotNil(t, cfg.MapResourceAttr.OverrideValue, "override_value should be set for map.resource.attr")
	assert.NotNil(t, cfg.OptionalResourceAttr.OverrideValue, "override_value should be set for optional.resource.attr")
	assert.NotNil(t, cfg.SliceResourceAttr.OverrideValue, "override_value should be set for slice.resource.attr")
	assert.NotNil(t, cfg.StringEnumResourceAttr.OverrideValue, "override_value should be set for string.enum.resource.attr")
	assert.NotNil(t, cfg.StringResourceAttr.OverrideValue, "override_value should be set for string.resource.attr")
	assert.NotNil(t, cfg.StringResourceAttrDisableWarning.OverrideValue, "override_value should be set for string.resource.attr_disable_warning")
	assert.NotNil(t, cfg.StringResourceAttrRemoveWarning.OverrideValue, "override_value should be set for string.resource.attr_remove_warning")
	assert.NotNil(t, cfg.StringResourceAttrToBeRemoved.OverrideValue, "override_value should be set for string.resource.attr_to_be_removed")
	assert.NotNil(t, cfg.StringResourceDisabledAttrToBeRemoved.OverrideValue, "override_value should be set for string.resource.disabled_attr_to_be_removed")
}

func TestResourceAttributesOverrideEnumValidation(t *testing.T) {
	{
		invalidVal := "invalid-enum-value"
		cfg := StringEnumResourceAttrResourceAttributeConfig{
			Enabled:       true,
			OverrideValue: &invalidVal,
		}
		assert.Error(t, cfg.Validate())
	}
}

func loadResourceAttributesConfig(t *testing.T, name string) ResourceAttributesConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	sub, err = sub.Sub("resource_attributes")
	require.NoError(t, err)
	cfg := DefaultResourceAttributesConfig()
	require.NoError(t, sub.Unmarshal(&cfg))
	return cfg
}
