// 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/connector/connectortest"
	"go.opentelemetry.io/collector/pdata/pcommon"
	"go.opentelemetry.io/collector/pdata/xpdata/entity"
)

func TestEntityBuilders(t *testing.T) {
	start := pcommon.Timestamp(1_000_000_000)
	ts := pcommon.Timestamp(1_000_001_000)
	settings := connectortest.NewNopSettings(connectortest.NopType)
	mb := NewMetricsBuilder(DefaultMetricsBuilderConfig(), settings, WithStartTime(start))

	t.Run("test.entity", func(t *testing.T) {
		e := NewTestEntityEntity("string.resource.attr-val")
		require.NotNil(t, e)
		e.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})

		eb := mb.ForTestEntity(e)
		eb.RecordDefaultMetricDataPoint(ts, 1, "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordDefaultMetricToBeRemovedDataPoint(ts, 1)
		eb.RecordMetricInputTypeDataPoint(ts, "1", "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordOptionalMetricDataPoint(ts, 1, "string_attr-val", true, false)
		eb.RecordOptionalMetricEmptyUnitDataPoint(ts, 1, "string_attr-val", true)
		eb.RecordReaggregateMetricDataPoint(ts, 1, "string_attr-val", true)
		eb.Emit()
		metrics := mb.Emit()

		require.Equal(t, 1, metrics.ResourceMetrics().Len())
		rm := metrics.ResourceMetrics().At(0)
		entityVal, ok := entity.ResourceEntities(rm.Resource()).Get("test.entity")
		require.True(t, ok)
		stringResourceAttrAttrVal, ok := entityVal.IdentifyingAttributes().Get("string.resource.attr")
		require.True(t, ok)
		assert.Equal(t, "string.resource.attr-val", stringResourceAttrAttrVal.Str())
		mapResourceAttrAttrVal, ok := entityVal.DescriptiveAttributes().Get("map.resource.attr")
		require.True(t, ok)
		assert.Equal(t, map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"}, mapResourceAttrAttrVal.Map().AsRaw())

		require.Equal(t, 1, rm.ScopeMetrics().Len())
		ms := rm.ScopeMetrics().At(0).Metrics()
		assert.Equal(t, 4, ms.Len())
	})
	t.Run("test.entity/disabled_identity_attr", func(t *testing.T) {
		// When an identity attribute is disabled, the entity is not produced but
		// other enabled attributes are still added to the resource directly.
		cfg := DefaultMetricsBuilderConfig()
		cfg.ResourceAttributes.StringResourceAttr.Enabled = false
		mb := NewMetricsBuilder(cfg, settings, WithStartTime(start))

		e := NewTestEntityEntity("string.resource.attr-val")
		e.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})

		eb := mb.ForTestEntity(e)
		eb.RecordDefaultMetricDataPoint(ts, 1, "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordDefaultMetricToBeRemovedDataPoint(ts, 1)
		eb.RecordMetricInputTypeDataPoint(ts, "1", "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordOptionalMetricDataPoint(ts, 1, "string_attr-val", true, false)
		eb.RecordOptionalMetricEmptyUnitDataPoint(ts, 1, "string_attr-val", true)
		eb.RecordReaggregateMetricDataPoint(ts, 1, "string_attr-val", true)
		eb.Emit()
		metrics := mb.Emit()

		require.Equal(t, 1, metrics.ResourceMetrics().Len())
		rm := metrics.ResourceMetrics().At(0)
		// Entity must not be present since its identity attribute is disabled.
		_, ok := entity.ResourceEntities(rm.Resource()).Get("test.entity")
		assert.False(t, ok)
		// Enabled descriptive attributes should still be on the resource directly.
		_, ok = rm.Resource().Attributes().Get("map.resource.attr")
		assert.True(t, ok)
	})
	t.Run("test.entity/disabled_descriptive_attr", func(t *testing.T) {
		// When a descriptive attribute is disabled, the entity is still produced
		// with its identity but the disabled attribute is not added.
		cfg := DefaultMetricsBuilderConfig()
		cfg.ResourceAttributes.MapResourceAttr.Enabled = false
		mb := NewMetricsBuilder(cfg, settings, WithStartTime(start))

		e := NewTestEntityEntity("string.resource.attr-val")
		e.SetMapResourceAttr(map[string]any{"key1": "map.resource.attr-val1", "key2": "map.resource.attr-val2"})

		eb := mb.ForTestEntity(e)
		eb.RecordDefaultMetricDataPoint(ts, 1, "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordDefaultMetricToBeRemovedDataPoint(ts, 1)
		eb.RecordMetricInputTypeDataPoint(ts, "1", "string_attr-val", 19, AttributeEnumAttrRed, []any{"slice_attr-item1", "slice_attr-item2"}, map[string]any{"key1": "map_attr-val1", "key2": "map_attr-val2"})
		eb.RecordOptionalMetricDataPoint(ts, 1, "string_attr-val", true, false)
		eb.RecordOptionalMetricEmptyUnitDataPoint(ts, 1, "string_attr-val", true)
		eb.RecordReaggregateMetricDataPoint(ts, 1, "string_attr-val", true)
		eb.Emit()
		metrics := mb.Emit()

		require.Equal(t, 1, metrics.ResourceMetrics().Len())
		rm := metrics.ResourceMetrics().At(0)
		// Entity must still be produced since identity attributes are enabled.
		entityVal, ok := entity.ResourceEntities(rm.Resource()).Get("test.entity")
		require.True(t, ok)
		stringResourceAttrAttrVal, ok := entityVal.IdentifyingAttributes().Get("string.resource.attr")
		require.True(t, ok)
		assert.Equal(t, "string.resource.attr-val", stringResourceAttrAttrVal.Str())
		// Disabled descriptive/extra attributes must not be present.
		_, ok = entityVal.DescriptiveAttributes().Get("map.resource.attr")
		assert.False(t, ok)
	})
}
