// Code generated by mdatagen. DO NOT EDIT.

package metadata

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

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

func TestResourceBuilder(t *testing.T) {
	for _, tt := range []string{"default", "all_set", "none_set"} {
		t.Run(tt, func(t *testing.T) {
			cfg := loadResourceAttributesConfig(t, tt)
			rb := NewResourceBuilder(cfg)
			rb.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})
			rb.SetOptionalResourceAttr("optional.resource.attr-val")
			rb.SetSliceResourceAttr([]any{"slice.resource.attr-item1", "slice.resource.attr-item2"})
			rb.SetStringEnumResourceAttrOne()
			rb.SetStringResourceAttr("string.resource.attr-val")
			rb.SetStringResourceAttrDisableWarning("string.resource.attr_disable_warning-val")
			rb.SetStringResourceAttrRemoveWarning("string.resource.attr_remove_warning-val")
			rb.SetStringResourceAttrToBeRemoved("string.resource.attr_to_be_removed-val")

			res := rb.Emit()
			assert.Equal(t, 0, rb.Emit().Attributes().Len()) // Second call should return empty Resource

			switch tt {
			case "default":
				assert.Equal(t, 6, res.Attributes().Len())
			case "all_set":
				assert.Equal(t, 8, res.Attributes().Len())
			case "none_set":
				assert.Equal(t, 0, res.Attributes().Len())
				return
			default:
				assert.Failf(t, "unexpected test case: %s", tt)
			}
			mapResourceAttrAttrVal, ok := res.Attributes().Get("map.resource.attr")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"}, mapResourceAttrAttrVal.Map().AsRaw())
			}
			optionalResourceAttrAttrVal, ok := res.Attributes().Get("optional.resource.attr")
			assert.Equal(t, tt == "all_set", ok)
			if ok {
				assert.Equal(t, "optional.resource.attr-val", optionalResourceAttrAttrVal.Str())
			}
			sliceResourceAttrAttrVal, ok := res.Attributes().Get("slice.resource.attr")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, []any{"slice.resource.attr-item1", "slice.resource.attr-item2"}, sliceResourceAttrAttrVal.Slice().AsRaw())
			}
			stringEnumResourceAttrAttrVal, ok := res.Attributes().Get("string.enum.resource.attr")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "one", stringEnumResourceAttrAttrVal.Str())
			}
			stringResourceAttrAttrVal, ok := res.Attributes().Get("string.resource.attr")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "string.resource.attr-val", stringResourceAttrAttrVal.Str())
			}
			stringResourceAttrDisableWarningAttrVal, ok := res.Attributes().Get("string.resource.attr_disable_warning")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "string.resource.attr_disable_warning-val", stringResourceAttrDisableWarningAttrVal.Str())
			}
			stringResourceAttrRemoveWarningAttrVal, ok := res.Attributes().Get("string.resource.attr_remove_warning")
			assert.Equal(t, tt == "all_set", ok)
			if ok {
				assert.Equal(t, "string.resource.attr_remove_warning-val", stringResourceAttrRemoveWarningAttrVal.Str())
			}
			stringResourceAttrToBeRemovedAttrVal, ok := res.Attributes().Get("string.resource.attr_to_be_removed")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "string.resource.attr_to_be_removed-val", stringResourceAttrToBeRemovedAttrVal.Str())
			}
		})
	}
}

func TestResourceBuilderOverrideValue(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	require.NoError(t, xconfmap.Validate(cfg))
	rb := NewResourceBuilder(cfg)
	rb.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})
	rb.SetOptionalResourceAttr("optional.resource.attr-val")
	rb.SetSliceResourceAttr([]any{"slice.resource.attr-item1", "slice.resource.attr-item2"})
	rb.SetStringEnumResourceAttrOne()
	rb.SetStringResourceAttr("string.resource.attr-val")
	rb.SetStringResourceAttrDisableWarning("string.resource.attr_disable_warning-val")
	rb.SetStringResourceAttrRemoveWarning("string.resource.attr_remove_warning-val")
	rb.SetStringResourceAttrToBeRemoved("string.resource.attr_to_be_removed-val")

	res := rb.Emit()
	{
		val, ok := res.Attributes().Get("map.resource.attr")
		assert.True(t, ok, "map.resource.attr should be present")
		if ok {
			assert.Equal(t, map[string]any{"override_key": "override-val"}, val.Map().AsRaw())
		}
	}
	{
		val, ok := res.Attributes().Get("optional.resource.attr")
		assert.True(t, ok, "optional.resource.attr should be present")
		if ok {
			assert.Equal(t, "override-optional.resource.attr", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("slice.resource.attr")
		assert.True(t, ok, "slice.resource.attr should be present")
		if ok {
			assert.Equal(t, []any{"override-item1", "override-item2"}, val.Slice().AsRaw())
		}
	}
	{
		val, ok := res.Attributes().Get("string.enum.resource.attr")
		assert.True(t, ok, "string.enum.resource.attr should be present")
		if ok {
			assert.Equal(t, "one", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr")
		assert.True(t, ok, "string.resource.attr should be present")
		if ok {
			assert.Equal(t, "override-string.resource.attr", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_disable_warning")
		assert.True(t, ok, "string.resource.attr_disable_warning should be present")
		if ok {
			assert.Equal(t, "override-string.resource.attr_disable_warning", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_remove_warning")
		assert.True(t, ok, "string.resource.attr_remove_warning should be present")
		if ok {
			assert.Equal(t, "override-string.resource.attr_remove_warning", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_to_be_removed")
		assert.True(t, ok, "string.resource.attr_to_be_removed should be present")
		if ok {
			assert.Equal(t, "override-string.resource.attr_to_be_removed", val.Str())
		}
	}
}

// TestResourceBuilderOverrideWithoutSet does not call any Set* methods, but override should still apply via Emit().
func TestResourceBuilderOverrideWithoutSet(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	require.NoError(t, xconfmap.Validate(cfg))
	rb := NewResourceBuilder(cfg)

	res := rb.Emit()
	{
		val, ok := res.Attributes().Get("map.resource.attr")
		assert.True(t, ok, "map.resource.attr should be present even without calling Set")
		if ok {
			assert.Equal(t, map[string]any{"override_key": "override-val"}, val.Map().AsRaw())
		}
	}
	{
		val, ok := res.Attributes().Get("optional.resource.attr")
		assert.True(t, ok, "optional.resource.attr should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-optional.resource.attr", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("slice.resource.attr")
		assert.True(t, ok, "slice.resource.attr should be present even without calling Set")
		if ok {
			assert.Equal(t, []any{"override-item1", "override-item2"}, val.Slice().AsRaw())
		}
	}
	{
		val, ok := res.Attributes().Get("string.enum.resource.attr")
		assert.True(t, ok, "string.enum.resource.attr should be present even without calling Set")
		if ok {
			assert.Equal(t, "one", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr")
		assert.True(t, ok, "string.resource.attr should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-string.resource.attr", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_disable_warning")
		assert.True(t, ok, "string.resource.attr_disable_warning should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-string.resource.attr_disable_warning", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_remove_warning")
		assert.True(t, ok, "string.resource.attr_remove_warning should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-string.resource.attr_remove_warning", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("string.resource.attr_to_be_removed")
		assert.True(t, ok, "string.resource.attr_to_be_removed should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-string.resource.attr_to_be_removed", val.Str())
		}
	}
}

// TestResourceBuilderOverrideDisabled disables all attributes, so override should not apply.
func TestResourceBuilderOverrideDisabled(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	cfg.MapResourceAttr.Enabled = false
	cfg.OptionalResourceAttr.Enabled = false
	cfg.SliceResourceAttr.Enabled = false
	cfg.StringEnumResourceAttr.Enabled = false
	cfg.StringResourceAttr.Enabled = false
	cfg.StringResourceAttrDisableWarning.Enabled = false
	cfg.StringResourceAttrRemoveWarning.Enabled = false
	cfg.StringResourceAttrToBeRemoved.Enabled = false
	require.NoError(t, xconfmap.Validate(cfg))
	rb := NewResourceBuilder(cfg)

	res := rb.Emit()
	assert.Equal(t, 0, res.Attributes().Len(), "disabled attributes with override should not be emitted")
}

// TestResourceBuilderNoOverride has no override_value set, Validate should still succeed.
func TestResourceBuilderNoOverride(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "all_set")
	require.NoError(t, xconfmap.Validate(cfg))
	assert.Nil(t, cfg.MapResourceAttr.OverrideValue, "OverrideValue should be nil for map.resource.attr")
	assert.Nil(t, cfg.OptionalResourceAttr.OverrideValue, "OverrideValue should be nil for optional.resource.attr")
	assert.Nil(t, cfg.SliceResourceAttr.OverrideValue, "OverrideValue should be nil for slice.resource.attr")
	assert.Nil(t, cfg.StringEnumResourceAttr.OverrideValue, "OverrideValue should be nil for string.enum.resource.attr")
	assert.Nil(t, cfg.StringResourceAttr.OverrideValue, "OverrideValue should be nil for string.resource.attr")
	assert.Nil(t, cfg.StringResourceAttrDisableWarning.OverrideValue, "OverrideValue should be nil for string.resource.attr_disable_warning")
	assert.Nil(t, cfg.StringResourceAttrRemoveWarning.OverrideValue, "OverrideValue should be nil for string.resource.attr_remove_warning")
	assert.Nil(t, cfg.StringResourceAttrToBeRemoved.OverrideValue, "OverrideValue should be nil for string.resource.attr_to_be_removed")
	rb := NewResourceBuilder(cfg)
	rb.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})
	rb.SetOptionalResourceAttr("optional.resource.attr-val")
	rb.SetSliceResourceAttr([]any{"slice.resource.attr-item1", "slice.resource.attr-item2"})
	rb.SetStringEnumResourceAttrOne()
	rb.SetStringResourceAttr("string.resource.attr-val")
	rb.SetStringResourceAttrDisableWarning("string.resource.attr_disable_warning-val")
	rb.SetStringResourceAttrRemoveWarning("string.resource.attr_remove_warning-val")
	rb.SetStringResourceAttrToBeRemoved("string.resource.attr_to_be_removed-val")

	res := rb.Emit()
	assert.Equal(t, 8, res.Attributes().Len())
	mapResourceAttrAttrVal, ok := res.Attributes().Get("map.resource.attr")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"}, mapResourceAttrAttrVal.Map().AsRaw())
	}
	optionalResourceAttrAttrVal, ok := res.Attributes().Get("optional.resource.attr")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "optional.resource.attr-val", optionalResourceAttrAttrVal.Str())
	}
	sliceResourceAttrAttrVal, ok := res.Attributes().Get("slice.resource.attr")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, []any{"slice.resource.attr-item1", "slice.resource.attr-item2"}, sliceResourceAttrAttrVal.Slice().AsRaw())
	}
	stringEnumResourceAttrAttrVal, ok := res.Attributes().Get("string.enum.resource.attr")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "one", stringEnumResourceAttrAttrVal.Str())
	}
	stringResourceAttrAttrVal, ok := res.Attributes().Get("string.resource.attr")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "string.resource.attr-val", stringResourceAttrAttrVal.Str())
	}
	stringResourceAttrDisableWarningAttrVal, ok := res.Attributes().Get("string.resource.attr_disable_warning")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "string.resource.attr_disable_warning-val", stringResourceAttrDisableWarningAttrVal.Str())
	}
	stringResourceAttrRemoveWarningAttrVal, ok := res.Attributes().Get("string.resource.attr_remove_warning")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "string.resource.attr_remove_warning-val", stringResourceAttrRemoveWarningAttrVal.Str())
	}
	stringResourceAttrToBeRemovedAttrVal, ok := res.Attributes().Get("string.resource.attr_to_be_removed")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "string.resource.attr_to_be_removed-val", stringResourceAttrToBeRemovedAttrVal.Str())
	}
}
