1+ using System ;
2+ using System . Text . Json ;
3+ using Xunit ;
4+
5+ namespace AWS . Lambda . Powertools . Metrics . Tests ;
6+
7+ [ Collection ( "Sequential" ) ]
8+ public class SerializationTests
9+ {
10+ [ Fact ]
11+ public void Metrics_Resolution_JsonConverter ( )
12+ {
13+ // Arrange
14+ var options = new JsonSerializerOptions ( ) ;
15+ options . Converters . Add ( new MetricResolutionJsonConverter ( ) ) ;
16+
17+ {
18+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( "1" , options ) ;
19+ Assert . Equal ( MetricResolution . High , myInt ) ;
20+ Assert . Equal ( "1" , JsonSerializer . Serialize ( myInt , options ) ) ;
21+ }
22+
23+ {
24+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( "60" , options ) ;
25+ Assert . Equal ( MetricResolution . Standard , myInt ) ;
26+ Assert . Equal ( "60" , JsonSerializer . Serialize ( myInt , options ) ) ;
27+ }
28+
29+ {
30+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( "0" , options ) ;
31+ Assert . Equal ( MetricResolution . Default , myInt ) ;
32+ Assert . Equal ( "0" , JsonSerializer . Serialize ( myInt , options ) ) ;
33+ }
34+
35+ {
36+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( @"""1""" , options ) ;
37+ Assert . Equal ( MetricResolution . High , myInt ) ;
38+ Assert . Equal ( "1" , JsonSerializer . Serialize ( myInt , options ) ) ;
39+ }
40+ {
41+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( @"""60""" , options ) ;
42+ Assert . Equal ( MetricResolution . Standard , myInt ) ;
43+ Assert . Equal ( "60" , JsonSerializer . Serialize ( myInt , options ) ) ;
44+ }
45+ {
46+ var myInt = JsonSerializer . Deserialize < MetricResolution > ( @"""0""" , options ) ;
47+ Assert . Equal ( MetricResolution . Default , myInt ) ;
48+ Assert . Equal ( "0" , JsonSerializer . Serialize ( myInt , options ) ) ;
49+ }
50+ }
51+
52+ [ Fact ]
53+ public void Metrics_Resolution_JsonConverter_Exception ( )
54+ {
55+ // Arrange
56+ var options = new JsonSerializerOptions ( ) ;
57+ options . Converters . Add ( new MetricResolutionJsonConverter ( ) ) ;
58+
59+ // Act
60+ Action act = ( ) => JsonSerializer . Deserialize < MetricResolution > ( "abd" , options ) ;
61+
62+ // Assert
63+ Assert . Throws < JsonException > ( act ) ;
64+ }
65+ }
0 commit comments