// Code generated by mdatagen. DO NOT EDIT.

{{- if len .Status.UnsupportedPlatforms }}
//go:build {{ range $i, $v := .Status.UnsupportedPlatforms }}{{ if $i }} && {{ end }}!{{ . }}{{ end }}
{{- end }}

package {{ .Package }}

import (
	"context"
	"testing"
	{{- if or isExporter isProcessor }}
	"time"
	{{- end }}

	"github.com/stretchr/testify/require"
	"go.opentelemetry.io/collector/component"
	{{- if not .Tests.SkipLifecycle }}
	"go.opentelemetry.io/collector/component/componenttest"
	{{- end }}
	{{- if isExporter }}
	"go.opentelemetry.io/collector/exporter"
	"go.opentelemetry.io/collector/exporter/exportertest"
	{{- end }}
	{{- if isProcessor }}
	"go.opentelemetry.io/collector/consumer/consumertest"
	"go.opentelemetry.io/collector/processor"
	"go.opentelemetry.io/collector/processor/processortest"
	{{- end }}
	{{- if isReceiver }}
	"go.opentelemetry.io/collector/consumer/consumertest"
	"go.opentelemetry.io/collector/receiver"
	"go.opentelemetry.io/collector/receiver/receivertest"
	{{- end }}
	{{- if isExtension }}
	"go.opentelemetry.io/collector/extension/extensiontest"
	{{- end }}
	{{- if isConnector }}
	"go.opentelemetry.io/collector/consumer/consumertest"
	"go.opentelemetry.io/collector/connector"
	"go.opentelemetry.io/collector/connector/connectortest"
	{{- end }}
	"go.opentelemetry.io/collector/confmap/confmaptest"
	{{- if or isExporter isProcessor }}
	"go.opentelemetry.io/collector/pdata/pcommon"
	"go.opentelemetry.io/collector/pdata/plog"
	"go.opentelemetry.io/collector/pdata/pmetric"
	"go.opentelemetry.io/collector/pdata/ptrace"
	{{- end }}
)

{{ if isExporter -}}
func TestComponentLifecycle(t *testing.T) {
	factory := NewFactory()

	tests := []struct{
		name string
		createFn func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error)
	}{
{{ if supportsLogs }}
		{
			name: "logs",
			createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsExporter(ctx, set, cfg)
			},
		},
{{ end }}
{{ if supportsMetrics }}
		{
			name: "metrics",
			createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsExporter(ctx, set, cfg)
			},
		},
{{ end }}
{{ if supportsTraces }}
		{
			name: "traces",
			createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesExporter(ctx, set, cfg)
			},
		},
{{ end }}
	}

	cm, err := confmaptest.LoadConf("metadata.yaml")
	require.NoError(t, err)
	cfg := factory.CreateDefaultConfig()
	sub, err := cm.Sub("tests::config")
	require.NoError(t, err)
	require.NoError(t, component.UnmarshalConfig(sub, cfg))

	for _, test := range tests {
		{{- if not .Tests.SkipShutdown }}
		t.Run(test.name + "-shutdown", func(t *testing.T) {
			c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}

		{{- if not .Tests.SkipLifecycle }}
		t.Run(test.name + "-lifecycle", func(t *testing.T) {
			c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			host := componenttest.NewNopHost()
			err = c.Start(context.Background(), host)
			require.NoError(t, err)
			require.NotPanics(t, func() {
				switch test.name {
				case "logs":
					e, ok := c.(exporter.Logs)
					require.True(t, ok)				
					logs := generateLifecycleTestLogs()
					if !e.Capabilities().MutatesData {
						logs.MarkReadOnly()
					}
					err = e.ConsumeLogs(context.Background(), logs)
				case "metrics":
					e, ok := c.(exporter.Metrics)
					require.True(t, ok)
					metrics := generateLifecycleTestMetrics()
					if !e.Capabilities().MutatesData {
						metrics.MarkReadOnly()
					}
					err = e.ConsumeMetrics(context.Background(), metrics)
				case "traces":
					e, ok := c.(exporter.Traces)
					require.True(t, ok)
					traces := generateLifecycleTestTraces()
					if !e.Capabilities().MutatesData {
						traces.MarkReadOnly()
					}
					err = e.ConsumeTraces(context.Background(), traces)
				}
			})
			{{ if not expectConsumerError }}
			require.NoError(t, err)
			{{ end }}
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}
	}
}
{{ end }}

{{ if isProcessor }}
func TestComponentLifecycle(t *testing.T) {
	factory := NewFactory()

	tests := []struct{
		name string
		createFn func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error)
	}{
{{ if supportsLogs }}
		{
			name: "logs",
			createFn: func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsProcessor(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsMetrics }}
		{
			name: "metrics",
			createFn: func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsProcessor(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsTraces }}
		{
			name: "traces",
			createFn: func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesProcessor(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
	}

	cm, err := confmaptest.LoadConf("metadata.yaml")
	require.NoError(t, err)
	cfg := factory.CreateDefaultConfig()
	sub, err := cm.Sub("tests::config")
	require.NoError(t, err)
	require.NoError(t, component.UnmarshalConfig(sub, cfg))

	for _, test := range tests {
		{{- if not .Tests.SkipShutdown }}
		t.Run(test.name + "-shutdown", func(t *testing.T) {
			c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}

		{{- if not .Tests.SkipLifecycle }}
		t.Run(test.name + "-lifecycle", func(t *testing.T) {
			c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			host := componenttest.NewNopHost()
			err = c.Start(context.Background(), host)
			require.NoError(t, err)
			require.NotPanics(t, func() {
				switch test.name {
				case "logs":
					e, ok := c.(processor.Logs)
					require.True(t, ok)
					logs := generateLifecycleTestLogs()
					if !e.Capabilities().MutatesData {
						logs.MarkReadOnly()
					}
					err = e.ConsumeLogs(context.Background(), logs)
				case "metrics":
					e, ok := c.(processor.Metrics)
					require.True(t, ok)
					metrics := generateLifecycleTestMetrics()
					if !e.Capabilities().MutatesData {
						metrics.MarkReadOnly()
					}
					err = e.ConsumeMetrics(context.Background(), metrics)
				case "traces":
					e, ok := c.(processor.Traces)
					require.True(t, ok)
					traces := generateLifecycleTestTraces()
					if !e.Capabilities().MutatesData {
						traces.MarkReadOnly()
					}
					err = e.ConsumeTraces(context.Background(), traces)
				}
			})
			require.NoError(t, err)
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}
	}
}
{{ end }}

{{ if isReceiver }}
func TestComponentLifecycle(t *testing.T) {
	factory := NewFactory()

	tests := []struct{
		name string
		createFn func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error)
	}{
{{ if supportsLogs }}
		{
			name: "logs",
			createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsReceiver(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsMetrics }}
		{
			name: "metrics",
			createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsReceiver(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsTraces }}
		{
			name: "traces",
			createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesReceiver(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
	}

	cm, err := confmaptest.LoadConf("metadata.yaml")
	require.NoError(t, err)
	cfg := factory.CreateDefaultConfig()
	sub, err := cm.Sub("tests::config")
	require.NoError(t, err)
	require.NoError(t, component.UnmarshalConfig(sub, cfg))

	for _, test := range tests {
		{{- if not .Tests.SkipShutdown }}
		t.Run(test.name + "-shutdown", func(t *testing.T) {
			c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}

		{{- if not .Tests.SkipLifecycle }}
		t.Run(test.name + "-lifecycle", func(t *testing.T) {
			firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			host := componenttest.NewNopHost()
			require.NoError(t, err)
			require.NoError(t, firstRcvr.Start(context.Background(), host))
			require.NoError(t, firstRcvr.Shutdown(context.Background()))
			secondRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			require.NoError(t, secondRcvr.Start(context.Background(), host))
			require.NoError(t, secondRcvr.Shutdown(context.Background()))
		})
		{{- end }}
	}
}
{{ end }}

{{ if isExtension }}
func TestComponentLifecycle(t *testing.T) {
	factory := NewFactory()

	cm, err := confmaptest.LoadConf("metadata.yaml")
	require.NoError(t, err)
	cfg := factory.CreateDefaultConfig()
	sub, err := cm.Sub("tests::config")
	require.NoError(t, err)
	require.NoError(t, component.UnmarshalConfig(sub, cfg))

	{{- if not .Tests.SkipShutdown }}
	t.Run("shutdown", func(t *testing.T) {
		e, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
		require.NoError(t, err)
		err = e.Shutdown(context.Background())
		require.NoError(t, err)
	})
	{{- end }}

	{{- if not .Tests.SkipLifecycle }}
	t.Run("lifecycle", func(t *testing.T) {
		firstExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
		require.NoError(t, err)
		require.NoError(t, firstExt.Start(context.Background(), componenttest.NewNopHost()))
		require.NoError(t, firstExt.Shutdown(context.Background()))

		secondExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
		require.NoError(t, err)
		require.NoError(t, secondExt.Start(context.Background(), componenttest.NewNopHost()))
		require.NoError(t, secondExt.Shutdown(context.Background()))
	})
	{{- end }}
}
{{ end }}

{{ if isConnector }}
func TestComponentLifecycle(t *testing.T) {
	factory := NewFactory()

	tests := []struct{
		name string
		createFn func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error)
	}{
{{ if supportsLogsToLogs }}
		{
			name: "logs_to_logs",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsToLogs(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsLogsToMetrics }}
		{
			name: "logs_to_metrics",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsToMetrics(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsLogsToTraces }}
		{
			name: "logs_to_traces",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateLogsToTraces(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsMetricsToLogs }}
		{
			name: "metrics_to_logs",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsToLogs(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsMetricsToMetrics }}
		{
			name: "metrics_to_metrics",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsToMetrics(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsMetricsToTraces }}
		{
			name: "metrics_to_traces",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateMetricsToTraces(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsTracesToLogs }}
		{
			name: "traces_to_logs",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesToLogs(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsTracesToMetrics }}
		{
			name: "traces_to_metrics",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesToMetrics(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
{{ if supportsTracesToTraces }}
		{
			name: "traces_to_traces",
			createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
				return factory.CreateTracesToTraces(ctx, set, cfg, consumertest.NewNop())
			},
		},
{{ end }}
	}

	cm, err := confmaptest.LoadConf("metadata.yaml")
	require.NoError(t, err)
	cfg := factory.CreateDefaultConfig()
	sub, err := cm.Sub("tests::config")
	require.NoError(t, err)
	require.NoError(t, component.UnmarshalConfig(sub, cfg))

	for _, test := range tests {
		{{- if not .Tests.SkipShutdown }}
		t.Run(test.name + "-shutdown", func(t *testing.T) {
			c, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			err = c.Shutdown(context.Background())
			require.NoError(t, err)
		})
		{{- end }}

		{{- if not .Tests.SkipLifecycle }}
		t.Run(test.name + "-lifecycle", func(t *testing.T) {
			firstConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			host := componenttest.NewNopHost()
			require.NoError(t, err)
			require.NoError(t, firstConnector.Start(context.Background(), host))
			require.NoError(t, firstConnector.Shutdown(context.Background()))
			secondConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
			require.NoError(t, err)
			require.NoError(t, secondConnector.Start(context.Background(), host))
			require.NoError(t, secondConnector.Shutdown(context.Background()))
		})
		{{- end }}
	}
}
{{ end }}

{{ if or isExporter isProcessor -}}
func generateLifecycleTestLogs() plog.Logs {
	logs := plog.NewLogs()
	rl := logs.ResourceLogs().AppendEmpty()
	rl.Resource().Attributes().PutStr("resource", "R1")
	l := rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
	l.Body().SetStr("test log message")
	l.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
	return logs
}

func generateLifecycleTestMetrics() pmetric.Metrics {
	metrics := pmetric.NewMetrics()
	rm := metrics.ResourceMetrics().AppendEmpty()
	rm.Resource().Attributes().PutStr("resource", "R1")
	m := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
	m.SetName("test_metric")
	dp := m.SetEmptyGauge().DataPoints().AppendEmpty()
	dp.Attributes().PutStr("test_attr", "value_1")
	dp.SetIntValue(123)
	dp.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
	return metrics
}

func generateLifecycleTestTraces() ptrace.Traces {
	traces := ptrace.NewTraces()
	rs := traces.ResourceSpans().AppendEmpty()
	rs.Resource().Attributes().PutStr("resource", "R1")
	span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
	span.Attributes().PutStr("test_attr", "value_1")
	span.SetName("test_span")
	span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(-1 * time.Second)))
	span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
	return traces
}
{{- end }}
