diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index e3406d56cb2..91611d86a6b 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -18,12 +18,15 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Moq; using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; using OpenTelemetry.Internal; using OpenTelemetry.Logs; using OpenTelemetry.Tests; using OpenTelemetry.Trace; using Xunit; +using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1; using OtlpCommon = OpenTelemetry.Proto.Common.V1; using OtlpLogs = OpenTelemetry.Proto.Logs.V1; @@ -500,6 +503,69 @@ public void CheckToOtlpLogRecordRespectsAttributeLimits() Assert.Null(exceptionStackTraceAtt); } + [Fact] + public void Export_WhenExportClientIsProvidedInCtor_UsesProvidedExportClient() + { + // Arrange. + var fakeExportClient = new Mock>(); + var emptyLogRecords = Array.Empty(); + var emptyBatch = new Batch(emptyLogRecords, emptyLogRecords.Length); + var sut = new OtlpLogExporter( + new OtlpExporterOptions(), + new SdkLimitOptions(), + fakeExportClient.Object); + + // Act. + var result = sut.Export(emptyBatch); + + // Assert. + fakeExportClient.Verify(x => x.SendExportRequest(It.IsAny(), default), Times.Once()); + } + + [Fact] + public void Export_WhenExportClientThrowsException_ReturnsExportResultFailure() + { + // Arrange. + var fakeExportClient = new Mock>(); + var emptyLogRecords = Array.Empty(); + var emptyBatch = new Batch(emptyLogRecords, emptyLogRecords.Length); + fakeExportClient + .Setup(_ => _.SendExportRequest(It.IsAny(), default)) + .Throws(new Exception("Test Exception")); + var sut = new OtlpLogExporter( + new OtlpExporterOptions(), + new SdkLimitOptions(), + fakeExportClient.Object); + + // Act. + var result = sut.Export(emptyBatch); + + // Assert. + Assert.Equal(ExportResult.Failure, result); + } + + [Fact] + public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess() + { + // Arrange. + var fakeExportClient = new Mock>(); + var emptyLogRecords = Array.Empty(); + var emptyBatch = new Batch(emptyLogRecords, emptyLogRecords.Length); + fakeExportClient + .Setup(_ => _.SendExportRequest(It.IsAny(), default)) + .Returns(true); + var sut = new OtlpLogExporter( + new OtlpExporterOptions(), + new SdkLimitOptions(), + fakeExportClient.Object); + + // Act. + var result = sut.Export(emptyBatch); + + // Assert. + Assert.Equal(ExportResult.Success, result); + } + private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key) { return record.Attributes.FirstOrDefault(att => att.Key == key);