8
8
namespace AWS . Lambda . Powertools . Logging . Tests . Utilities ;
9
9
10
10
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
11
178
{
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
+ }
0 commit comments