Skip to content

Commit 592b931

Browse files
committed
fix sonar add coverage
1 parent 98659ce commit 592b931

File tree

3 files changed

+191
-149
lines changed

3 files changed

+191
-149
lines changed

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Serializers/PowertoolsLoggingSerializerTests.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -456,19 +456,6 @@ public void Serialize_WithValidObject_ReturnsJsonString()
456456
}
457457

458458
#if NET8_0_OR_GREATER
459-
[Fact]
460-
public void AddSerializerContext_AddsContext()
461-
{
462-
// Arrange
463-
var serializer = new PowertoolsLoggingSerializer();
464-
var context = new TestJsonContext(new JsonSerializerOptions());
465-
466-
// Act
467-
serializer.AddSerializerContext(context);
468-
469-
// No immediate assertion - the context is added internally
470-
// We'll verify it works through serialization tests
471-
}
472459

473460
[Fact]
474461
public void SetOptions_WithTypeInfoResolver_SetsCustomResolver()
@@ -492,24 +479,6 @@ public void SetOptions_WithTypeInfoResolver_SetsCustomResolver()
492479
// Assert - options are properly configured
493480
Assert.NotNull(serializerOptions.TypeInfoResolver);
494481
}
495-
496-
[Fact]
497-
public void SetOptions_WithContextAsResolver_AddsToContexts()
498-
{
499-
// Arrange
500-
var serializer = new PowertoolsLoggingSerializer();
501-
var context = new TestJsonContext(new JsonSerializerOptions());
502-
var options = new JsonSerializerOptions
503-
{
504-
TypeInfoResolver = context
505-
};
506-
507-
// Act - This adds the context automatically
508-
serializer.SetOptions(options);
509-
510-
// No direct assertion possible for internal state, but we can test it works
511-
// through proper serialization
512-
}
513482
#endif
514483

515484
[Fact]

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Utilities/Converters.cs

Lines changed: 170 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,101 +8,174 @@
88
namespace AWS.Lambda.Powertools.Logging.Tests.Utilities;
99

1010
public class ByteArrayConverterTests
11+
{
12+
private readonly JsonSerializerOptions _options;
13+
14+
public ByteArrayConverterTests()
15+
{
16+
_options = new JsonSerializerOptions();
17+
_options.Converters.Add(new ByteArrayConverter());
18+
}
19+
20+
[Fact]
21+
public void Write_WhenByteArrayIsNull_WritesNullValue()
22+
{
23+
// Arrange
24+
var testObject = new TestClass { Data = null };
25+
26+
// Act
27+
var json = JsonSerializer.Serialize(testObject, _options);
28+
29+
// Assert
30+
Assert.Contains("\"data\":null", json);
31+
}
32+
33+
[Fact]
34+
public void Write_WithByteArray_WritesBase64String()
35+
{
36+
// Arrange
37+
byte[] testData = { 1, 2, 3, 4, 5 };
38+
var testObject = new TestClass { Data = testData };
39+
var expectedBase64 = Convert.ToBase64String(testData);
40+
41+
// Act
42+
var json = JsonSerializer.Serialize(testObject, _options);
43+
44+
// Assert
45+
Assert.Contains($"\"data\":\"{expectedBase64}\"", json);
46+
}
47+
48+
[Fact]
49+
public void Read_WithBase64String_ReturnsByteArray()
50+
{
51+
// Arrange
52+
byte[] expectedData = { 1, 2, 3, 4, 5 };
53+
var base64 = Convert.ToBase64String(expectedData);
54+
var json = $"{{\"data\":\"{base64}\"}}";
55+
56+
// Act
57+
var result = JsonSerializer.Deserialize<TestClass>(json, _options);
58+
59+
// Assert
60+
Assert.Equal(expectedData, result.Data);
61+
}
62+
63+
[Fact]
64+
public void Read_WithInvalidType_ThrowsJsonException()
65+
{
66+
// Arrange
67+
var json = "{\"data\":123}";
68+
69+
// Act & Assert
70+
Assert.Throws<JsonException>(() =>
71+
JsonSerializer.Deserialize<TestClass>(json, _options));
72+
}
73+
74+
[Fact]
75+
public void Read_WithEmptyString_ReturnsEmptyByteArray()
76+
{
77+
// Arrange
78+
var json = "{\"data\":\"\"}";
79+
80+
// Act
81+
var result = JsonSerializer.Deserialize<TestClass>(json, _options);
82+
83+
// Assert
84+
Assert.NotNull(result.Data);
85+
Assert.Empty(result.Data);
86+
}
87+
88+
[Fact]
89+
public void WriteAndRead_RoundTrip_PreservesData()
90+
{
91+
// Arrange
92+
byte[] originalData = Encoding.UTF8.GetBytes("Test data with special chars: !@#$%^&*()");
93+
var testObject = new TestClass { Data = originalData };
94+
95+
// Act
96+
var json = JsonSerializer.Serialize(testObject, _options);
97+
var deserializedObject = JsonSerializer.Deserialize<TestClass>(json, _options);
98+
99+
// Assert
100+
Assert.Equal(originalData, deserializedObject.Data);
101+
}
102+
103+
private class TestClass
104+
{
105+
[JsonPropertyName("data")] public byte[] Data { get; set; }
106+
}
107+
108+
[Fact]
109+
public void ByteArrayConverter_Write_ShouldHandleNullValue()
110+
{
111+
// Arrange
112+
var converter = new ByteArrayConverter();
113+
var options = new JsonSerializerOptions();
114+
var testObject = new { Data = (byte[])null };
115+
116+
// Act
117+
var json = JsonSerializer.Serialize(testObject, options);
118+
119+
// Assert
120+
Assert.Contains("\"Data\":null", json);
121+
}
122+
123+
[Fact]
124+
public void ByteArrayConverter_Read_ShouldHandleNullToken()
125+
{
126+
// Arrange
127+
var converter = new ByteArrayConverter();
128+
var json = "{\"Data\":null}";
129+
var options = new JsonSerializerOptions();
130+
options.Converters.Add(converter);
131+
132+
// Act
133+
var result = JsonSerializer.Deserialize<TestByteArrayClass>(json, options);
134+
135+
// Assert
136+
Assert.Null(result.Data);
137+
}
138+
139+
[Fact]
140+
public void ByteArrayConverter_Read_ShouldHandleStringToken()
141+
{
142+
// Arrange
143+
var converter = new ByteArrayConverter();
144+
var expectedBytes = new byte[] { 1, 2, 3, 4 };
145+
var base64String = Convert.ToBase64String(expectedBytes);
146+
var json = $"{{\"Data\":\"{base64String}\"}}";
147+
148+
var options = new JsonSerializerOptions();
149+
options.Converters.Add(converter);
150+
151+
// Act
152+
var result = JsonSerializer.Deserialize<TestByteArrayClass>(json, options);
153+
154+
// Assert
155+
Assert.NotNull(result.Data);
156+
Assert.Equal(expectedBytes, result.Data);
157+
}
158+
159+
[Fact]
160+
public void ByteArrayConverter_Read_ShouldThrowOnInvalidToken()
161+
{
162+
// Arrange
163+
var converter = new ByteArrayConverter();
164+
var json = "{\"Data\":123}"; // Number instead of string
165+
166+
var options = new JsonSerializerOptions();
167+
options.Converters.Add(converter);
168+
169+
// Act & Assert
170+
var ex = Assert.Throws<JsonException>(() =>
171+
JsonSerializer.Deserialize<TestByteArrayClass>(json, options));
172+
173+
Assert.Contains("Expected string value for byte array", ex.Message);
174+
}
175+
176+
// Helper class for testing byte array deserialization
177+
private class TestByteArrayClass
11178
{
12-
private readonly JsonSerializerOptions _options;
13-
14-
public ByteArrayConverterTests()
15-
{
16-
_options = new JsonSerializerOptions();
17-
_options.Converters.Add(new ByteArrayConverter());
18-
}
19-
20-
[Fact]
21-
public void Write_WhenByteArrayIsNull_WritesNullValue()
22-
{
23-
// Arrange
24-
var testObject = new TestClass { Data = null };
25-
26-
// Act
27-
var json = JsonSerializer.Serialize(testObject, _options);
28-
29-
// Assert
30-
Assert.Contains("\"data\":null", json);
31-
}
32-
33-
[Fact]
34-
public void Write_WithByteArray_WritesBase64String()
35-
{
36-
// Arrange
37-
byte[] testData = { 1, 2, 3, 4, 5 };
38-
var testObject = new TestClass { Data = testData };
39-
var expectedBase64 = Convert.ToBase64String(testData);
40-
41-
// Act
42-
var json = JsonSerializer.Serialize(testObject, _options);
43-
44-
// Assert
45-
Assert.Contains($"\"data\":\"{expectedBase64}\"", json);
46-
}
47-
48-
[Fact]
49-
public void Read_WithBase64String_ReturnsByteArray()
50-
{
51-
// Arrange
52-
byte[] expectedData = { 1, 2, 3, 4, 5 };
53-
var base64 = Convert.ToBase64String(expectedData);
54-
var json = $"{{\"data\":\"{base64}\"}}";
55-
56-
// Act
57-
var result = JsonSerializer.Deserialize<TestClass>(json, _options);
58-
59-
// Assert
60-
Assert.Equal(expectedData, result.Data);
61-
}
62-
63-
[Fact]
64-
public void Read_WithInvalidType_ThrowsJsonException()
65-
{
66-
// Arrange
67-
var json = "{\"data\":123}";
68-
69-
// Act & Assert
70-
Assert.Throws<JsonException>(() =>
71-
JsonSerializer.Deserialize<TestClass>(json, _options));
72-
}
73-
74-
[Fact]
75-
public void Read_WithEmptyString_ReturnsEmptyByteArray()
76-
{
77-
// Arrange
78-
var json = "{\"data\":\"\"}";
79-
80-
// Act
81-
var result = JsonSerializer.Deserialize<TestClass>(json, _options);
82-
83-
// Assert
84-
Assert.NotNull(result.Data);
85-
Assert.Empty(result.Data);
86-
}
87-
88-
[Fact]
89-
public void WriteAndRead_RoundTrip_PreservesData()
90-
{
91-
// Arrange
92-
byte[] originalData = Encoding.UTF8.GetBytes("Test data with special chars: !@#$%^&*()");
93-
var testObject = new TestClass { Data = originalData };
94-
95-
// Act
96-
var json = JsonSerializer.Serialize(testObject, _options);
97-
var deserializedObject = JsonSerializer.Deserialize<TestClass>(json, _options);
98-
99-
// Assert
100-
Assert.Equal(originalData, deserializedObject.Data);
101-
}
102-
103-
private class TestClass
104-
{
105-
[JsonPropertyName("data")]
106-
public byte[] Data { get; set; }
107-
}
108-
}
179+
public byte[] Data { get; set; }
180+
}
181+
}

libraries/tests/e2e/infra-aot/CoreAotStack.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,14 @@ namespace InfraAot;
88

99
public class ConstructArgs
1010
{
11-
public ConstructArgs(Construct scope, string id, Runtime runtime, Architecture architecture, string name, string sourcePath, string distPath, string handler)
12-
{
13-
Scope = scope;
14-
Id = id;
15-
Runtime = runtime;
16-
Architecture = architecture;
17-
Name = name;
18-
SourcePath = sourcePath;
19-
DistPath = distPath;
20-
Handler = handler;
21-
}
22-
23-
public Construct Scope { get; private set; }
24-
public string Id { get; private set; }
25-
public Runtime Runtime { get; private set; }
26-
public Architecture Architecture { get; private set; }
27-
public string Name { get; private set; }
28-
public string SourcePath { get; private set; }
29-
public string DistPath { get; private set; }
30-
public string Handler { get; private set; }
11+
public Construct Scope { get; set; }
12+
public string Id { get; set; }
13+
public Runtime Runtime { get; set; }
14+
public Architecture Architecture { get; set; }
15+
public string Name { get; set; }
16+
public string SourcePath { get; set; }
17+
public string DistPath { get; set; }
18+
public string Handler { get; set; }
3119
}
3220

3321
public class CoreAotStack : Stack
@@ -50,7 +38,19 @@ private void CreateFunctionConstructs(string utility, string function = "AOT-Fun
5038
var distAotPath = $"../functions/core/{utility}/{function}/dist/{function}";
5139
var arch = _architecture == Architecture.X86_64 ? "X64" : "ARM";
5240

53-
CreateFunctionConstruct(new ConstructArgs(this, $"{utility}_{arch}_aot_net8_{function}", Runtime.DOTNET_8, _architecture, $"E2ETestLambda_{arch}_AOT_NET8_{utility}_{function}", baseAotPath, distAotPath, function));
41+
var construct = new ConstructArgs
42+
{
43+
Scope = this,
44+
Id = $"{utility}_{arch}_aot_net8_{function}",
45+
Runtime = Runtime.DOTNET_8,
46+
Architecture = _architecture,
47+
Name = $"E2ETestLambda_{arch}_AOT_NET8_{utility}_{function}",
48+
SourcePath = baseAotPath,
49+
DistPath = distAotPath,
50+
Handler = $"{function}.Function::AWS.Lambda.Powertools.{utility}.{function}.Function.FunctionHandler"
51+
};
52+
53+
CreateFunctionConstruct(construct);
5454
}
5555

5656
private void CreateFunctionConstruct(ConstructArgs constructArgs)

0 commit comments

Comments
 (0)