// 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"
)

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.SetK8sNamespaceName("k8s.namespace.name-val")
			rb.SetK8sPodName("k8s.pod.name-val")
			rb.SetK8sPodUID("k8s.pod.uid-val")
			rb.SetK8sReplicasetName("k8s.replicaset.name-val")
			rb.SetK8sReplicasetUID("k8s.replicaset.uid-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, 5, res.Attributes().Len())
			case "all_set":
				assert.Equal(t, 5, res.Attributes().Len())
			case "none_set":
				assert.Equal(t, 0, res.Attributes().Len())
				return
			default:
				assert.Failf(t, "unexpected test case: %s", tt)
			}
			k8sNamespaceNameAttrVal, ok := res.Attributes().Get("k8s.namespace.name")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "k8s.namespace.name-val", k8sNamespaceNameAttrVal.Str())
			}
			k8sPodNameAttrVal, ok := res.Attributes().Get("k8s.pod.name")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "k8s.pod.name-val", k8sPodNameAttrVal.Str())
			}
			k8sPodUIDAttrVal, ok := res.Attributes().Get("k8s.pod.uid")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "k8s.pod.uid-val", k8sPodUIDAttrVal.Str())
			}
			k8sReplicasetNameAttrVal, ok := res.Attributes().Get("k8s.replicaset.name")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "k8s.replicaset.name-val", k8sReplicasetNameAttrVal.Str())
			}
			k8sReplicasetUIDAttrVal, ok := res.Attributes().Get("k8s.replicaset.uid")
			assert.True(t, ok)
			if ok {
				assert.Equal(t, "k8s.replicaset.uid-val", k8sReplicasetUIDAttrVal.Str())
			}
		})
	}
}

func TestResourceBuilderOverrideValue(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	require.NoError(t, confmap.Validate(cfg))
	rb := NewResourceBuilder(cfg)
	rb.SetK8sNamespaceName("k8s.namespace.name-val")
	rb.SetK8sPodName("k8s.pod.name-val")
	rb.SetK8sPodUID("k8s.pod.uid-val")
	rb.SetK8sReplicasetName("k8s.replicaset.name-val")
	rb.SetK8sReplicasetUID("k8s.replicaset.uid-val")

	res := rb.Emit()
	{
		val, ok := res.Attributes().Get("k8s.namespace.name")
		assert.True(t, ok, "k8s.namespace.name should be present")
		if ok {
			assert.Equal(t, "override-k8s.namespace.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.pod.name")
		assert.True(t, ok, "k8s.pod.name should be present")
		if ok {
			assert.Equal(t, "override-k8s.pod.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.pod.uid")
		assert.True(t, ok, "k8s.pod.uid should be present")
		if ok {
			assert.Equal(t, "override-k8s.pod.uid", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.replicaset.name")
		assert.True(t, ok, "k8s.replicaset.name should be present")
		if ok {
			assert.Equal(t, "override-k8s.replicaset.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.replicaset.uid")
		assert.True(t, ok, "k8s.replicaset.uid should be present")
		if ok {
			assert.Equal(t, "override-k8s.replicaset.uid", 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, confmap.Validate(cfg))
	rb := NewResourceBuilder(cfg)

	res := rb.Emit()
	{
		val, ok := res.Attributes().Get("k8s.namespace.name")
		assert.True(t, ok, "k8s.namespace.name should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-k8s.namespace.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.pod.name")
		assert.True(t, ok, "k8s.pod.name should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-k8s.pod.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.pod.uid")
		assert.True(t, ok, "k8s.pod.uid should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-k8s.pod.uid", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.replicaset.name")
		assert.True(t, ok, "k8s.replicaset.name should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-k8s.replicaset.name", val.Str())
		}
	}
	{
		val, ok := res.Attributes().Get("k8s.replicaset.uid")
		assert.True(t, ok, "k8s.replicaset.uid should be present even without calling Set")
		if ok {
			assert.Equal(t, "override-k8s.replicaset.uid", val.Str())
		}
	}
}

// TestResourceBuilderOverrideDisabled disables all attributes, so override should not apply.
func TestResourceBuilderOverrideDisabled(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	cfg.K8sNamespaceName.Enabled = false
	cfg.K8sPodName.Enabled = false
	cfg.K8sPodUID.Enabled = false
	cfg.K8sReplicasetName.Enabled = false
	cfg.K8sReplicasetUID.Enabled = false
	require.NoError(t, confmap.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, confmap.Validate(cfg))
	assert.Nil(t, cfg.K8sNamespaceName.OverrideValue, "OverrideValue should be nil for k8s.namespace.name")
	assert.Nil(t, cfg.K8sPodName.OverrideValue, "OverrideValue should be nil for k8s.pod.name")
	assert.Nil(t, cfg.K8sPodUID.OverrideValue, "OverrideValue should be nil for k8s.pod.uid")
	assert.Nil(t, cfg.K8sReplicasetName.OverrideValue, "OverrideValue should be nil for k8s.replicaset.name")
	assert.Nil(t, cfg.K8sReplicasetUID.OverrideValue, "OverrideValue should be nil for k8s.replicaset.uid")
	rb := NewResourceBuilder(cfg)
	rb.SetK8sNamespaceName("k8s.namespace.name-val")
	rb.SetK8sPodName("k8s.pod.name-val")
	rb.SetK8sPodUID("k8s.pod.uid-val")
	rb.SetK8sReplicasetName("k8s.replicaset.name-val")
	rb.SetK8sReplicasetUID("k8s.replicaset.uid-val")

	res := rb.Emit()
	assert.Equal(t, 5, res.Attributes().Len())
	k8sNamespaceNameAttrVal, ok := res.Attributes().Get("k8s.namespace.name")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "k8s.namespace.name-val", k8sNamespaceNameAttrVal.Str())
	}
	k8sPodNameAttrVal, ok := res.Attributes().Get("k8s.pod.name")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "k8s.pod.name-val", k8sPodNameAttrVal.Str())
	}
	k8sPodUIDAttrVal, ok := res.Attributes().Get("k8s.pod.uid")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "k8s.pod.uid-val", k8sPodUIDAttrVal.Str())
	}
	k8sReplicasetNameAttrVal, ok := res.Attributes().Get("k8s.replicaset.name")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "k8s.replicaset.name-val", k8sReplicasetNameAttrVal.Str())
	}
	k8sReplicasetUIDAttrVal, ok := res.Attributes().Get("k8s.replicaset.uid")
	assert.True(t, ok)
	if ok {
		assert.Equal(t, "k8s.replicaset.uid-val", k8sReplicasetUIDAttrVal.Str())
	}
}
