Skip to content

Commit c8684e8

Browse files
committed
add tests for LogLevelJsonConverter
1 parent 3534700 commit c8684e8

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System.Text.Json;
2+
using AWS.Lambda.Powertools.Logging.Serializers;
3+
using Microsoft.Extensions.Logging;
4+
using Xunit;
5+
6+
namespace AWS.Lambda.Powertools.Logging.Tests.Utilities;
7+
8+
public class LogLevelJsonConverterTests
9+
{
10+
private readonly LogLevelJsonConverter _converter;
11+
private readonly JsonSerializerOptions _options;
12+
13+
public LogLevelJsonConverterTests()
14+
{
15+
_converter = new LogLevelJsonConverter();
16+
_options = new JsonSerializerOptions
17+
{
18+
Converters = { _converter }
19+
};
20+
}
21+
22+
[Theory]
23+
[InlineData("Information", LogLevel.Information)]
24+
[InlineData("Error", LogLevel.Error)]
25+
[InlineData("Warning", LogLevel.Warning)]
26+
[InlineData("Debug", LogLevel.Debug)]
27+
[InlineData("Trace", LogLevel.Trace)]
28+
[InlineData("Critical", LogLevel.Critical)]
29+
[InlineData("None", LogLevel.None)]
30+
public void Read_ValidLogLevel_ReturnsCorrectEnum(string input, LogLevel expected)
31+
{
32+
// Arrange
33+
var json = $"\"{input}\"";
34+
35+
// Act
36+
var result = JsonSerializer.Deserialize<LogLevel>(json, _options);
37+
38+
// Assert
39+
Assert.Equal(expected, result);
40+
}
41+
42+
[Theory]
43+
[InlineData("information", LogLevel.Information)]
44+
[InlineData("ERROR", LogLevel.Error)]
45+
[InlineData("Warning", LogLevel.Warning)]
46+
[InlineData("deBUG", LogLevel.Debug)]
47+
public void Read_CaseInsensitive_ReturnsCorrectEnum(string input, LogLevel expected)
48+
{
49+
// Arrange
50+
var json = $"\"{input}\"";
51+
52+
// Act
53+
var result = JsonSerializer.Deserialize<LogLevel>(json, _options);
54+
55+
// Assert
56+
Assert.Equal(expected, result);
57+
}
58+
59+
[Theory]
60+
[InlineData("")]
61+
[InlineData("InvalidLevel")]
62+
[InlineData("NotALevel")]
63+
public void Read_InvalidLogLevel_ReturnsDefault(string input)
64+
{
65+
// Arrange
66+
var json = $"\"{input}\"";
67+
68+
// Act
69+
var result = JsonSerializer.Deserialize<LogLevel>(json, _options);
70+
71+
// Assert
72+
Assert.Equal(default(LogLevel), result);
73+
}
74+
75+
[Fact]
76+
public void Read_NullValue_ReturnsDefault()
77+
{
78+
// Arrange
79+
var json = "null";
80+
81+
// Act
82+
var result = JsonSerializer.Deserialize<LogLevel>(json, _options);
83+
84+
// Assert
85+
Assert.Equal(default(LogLevel), result);
86+
}
87+
88+
[Theory]
89+
[InlineData(LogLevel.Information, "Information")]
90+
[InlineData(LogLevel.Error, "Error")]
91+
[InlineData(LogLevel.Warning, "Warning")]
92+
[InlineData(LogLevel.Debug, "Debug")]
93+
[InlineData(LogLevel.Trace, "Trace")]
94+
[InlineData(LogLevel.Critical, "Critical")]
95+
[InlineData(LogLevel.None, "None")]
96+
public void Write_ValidLogLevel_WritesCorrectString(LogLevel input, string expected)
97+
{
98+
// Act
99+
var result = JsonSerializer.Serialize(input, _options);
100+
101+
// Assert
102+
Assert.Equal($"\"{expected}\"", result);
103+
}
104+
105+
[Fact]
106+
public void Write_DefaultLogLevel_WritesCorrectString()
107+
{
108+
// Arrange
109+
var input = default(LogLevel);
110+
111+
// Act
112+
var result = JsonSerializer.Serialize(input, _options);
113+
114+
// Assert
115+
Assert.Equal($"\"{input}\"", result);
116+
}
117+
118+
[Fact]
119+
public void Converter_CanConvert_LogLevelType()
120+
{
121+
// Act
122+
var canConvert = _converter.CanConvert(typeof(LogLevel));
123+
124+
// Assert
125+
Assert.True(canConvert);
126+
}
127+
128+
[Fact]
129+
public void SerializeAndDeserialize_RoundTrip_MaintainsValue()
130+
{
131+
// Arrange
132+
var originalValue = LogLevel.Information;
133+
134+
// Act
135+
var serialized = JsonSerializer.Serialize(originalValue, _options);
136+
var deserialized = JsonSerializer.Deserialize<LogLevel>(serialized, _options);
137+
138+
// Assert
139+
Assert.Equal(originalValue, deserialized);
140+
}
141+
}

0 commit comments

Comments
 (0)