Skip to content

Commit 2140040

Browse files
committed
add unit tests for lambda serlialization json converters
1 parent b649ed2 commit 2140040

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Globalization;
19+
using System.IO;
1920
using System.Linq;
2021
using AWS.Lambda.Powertools.Common;
2122
using AWS.Lambda.Powertools.Logging.Internal;
@@ -1094,5 +1095,85 @@ public void Log_WhenNestedException_LogsExceptionDetails()
10941095
)
10951096
), Times.Once);
10961097
}
1098+
1099+
[Fact]
1100+
public void Log_WhenByteArray_LogsByteArrayNumbers()
1101+
{
1102+
// Arrange
1103+
var loggerName = Guid.NewGuid().ToString();
1104+
var service = Guid.NewGuid().ToString();
1105+
var bytes = new byte[10];
1106+
new Random().NextBytes(bytes);
1107+
var logLevel = LogLevel.Information;
1108+
var randomSampleRate = 0.5;
1109+
1110+
var configurations = new Mock<IPowertoolsConfigurations>();
1111+
configurations.Setup(c => c.Service).Returns(service);
1112+
configurations.Setup(c => c.LogLevel).Returns(logLevel.ToString);
1113+
1114+
var systemWrapper = new Mock<ISystemWrapper>();
1115+
systemWrapper.Setup(c => c.GetRandom()).Returns(randomSampleRate);
1116+
1117+
var logger = new PowertoolsLogger(loggerName, configurations.Object, systemWrapper.Object, () =>
1118+
new LoggerConfiguration
1119+
{
1120+
Service = null,
1121+
MinimumLevel = null
1122+
});
1123+
1124+
// Act
1125+
logger.LogInformation(new { Name = "Test Object", Bytes = bytes });
1126+
1127+
// Assert
1128+
systemWrapper.Verify(v =>
1129+
v.LogLine(
1130+
It.Is<string>
1131+
(s =>
1132+
s.Contains("\"bytes\":[" + string.Join(",", bytes) + "]")
1133+
)
1134+
), Times.Once);
1135+
}
1136+
1137+
[Fact]
1138+
public void Log_WhenMemoryStream_LogsBase64String()
1139+
{
1140+
// Arrange
1141+
var loggerName = Guid.NewGuid().ToString();
1142+
var service = Guid.NewGuid().ToString();
1143+
var bytes = new byte[10];
1144+
new Random().NextBytes(bytes);
1145+
var memoryStream = new MemoryStream(bytes)
1146+
{
1147+
Position = 0
1148+
};
1149+
var logLevel = LogLevel.Information;
1150+
var randomSampleRate = 0.5;
1151+
1152+
var configurations = new Mock<IPowertoolsConfigurations>();
1153+
configurations.Setup(c => c.Service).Returns(service);
1154+
configurations.Setup(c => c.LogLevel).Returns(logLevel.ToString);
1155+
1156+
var systemWrapper = new Mock<ISystemWrapper>();
1157+
systemWrapper.Setup(c => c.GetRandom()).Returns(randomSampleRate);
1158+
1159+
var logger = new PowertoolsLogger(loggerName, configurations.Object, systemWrapper.Object, () =>
1160+
new LoggerConfiguration
1161+
{
1162+
Service = null,
1163+
MinimumLevel = null
1164+
});
1165+
1166+
// Act
1167+
logger.LogInformation(new { Name = "Test Object", Stream = memoryStream });
1168+
1169+
// Assert
1170+
systemWrapper.Verify(v =>
1171+
v.LogLine(
1172+
It.Is<string>
1173+
(s =>
1174+
s.Contains("\"stream\":\"" + Convert.ToBase64String(bytes) + "\"")
1175+
)
1176+
), Times.Once);
1177+
}
10971178
}
10981179
}

0 commit comments

Comments
 (0)