1
+ using System ;
2
+ using System . Collections . Generic ;
1
3
using AWS . Lambda . Powertools . Common ;
2
4
using NSubstitute ;
3
5
using Xunit ;
@@ -30,4 +32,122 @@ public void Metrics_Set_Execution_Environment_Context()
30
32
31
33
env . Received ( 1 ) . GetEnvironmentVariable ( "AWS_EXECUTION_ENV" ) ;
32
34
}
35
+
36
+ [ Fact ]
37
+ public void When_Constructor_With_Namespace_And_Service_Should_Set_Both ( )
38
+ {
39
+ // Arrange
40
+ var metricsMock = Substitute . For < IMetrics > ( ) ;
41
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
42
+
43
+ // Act
44
+ var metrics = new Metrics ( powertoolsConfigMock , "TestNamespace" , "TestService" ) ;
45
+
46
+ // Assert
47
+ Assert . Equal ( "TestNamespace" , metrics . GetNamespace ( ) ) ;
48
+ Assert . Equal ( "TestService" , metrics . Options . Service ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void When_Constructor_With_Null_Namespace_And_Service_Should_Not_Set ( )
53
+ {
54
+ // Arrange
55
+ var metricsMock = Substitute . For < IMetrics > ( ) ;
56
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
57
+ powertoolsConfigMock . MetricsNamespace . Returns ( ( string ) null ) ;
58
+ powertoolsConfigMock . Service . Returns ( "service_undefined" ) ;
59
+
60
+ // Act
61
+ var metrics = new Metrics ( powertoolsConfigMock , null , null ) ;
62
+
63
+ // Assert
64
+ Assert . Null ( metrics . GetNamespace ( ) ) ;
65
+ Assert . Null ( metrics . Options . Service ) ;
66
+ }
67
+
68
+ [ Fact ]
69
+ public void When_AddMetric_With_EmptyKey_Should_ThrowArgumentNullException ( )
70
+ {
71
+ // Arrange
72
+ var metricsMock = Substitute . For < IMetrics > ( ) ;
73
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
74
+ IMetrics metrics = new Metrics ( powertoolsConfigMock ) ;
75
+
76
+ // Act & Assert
77
+ var exception = Assert . Throws < ArgumentNullException > ( ( ) => metrics . AddMetric ( "" , 1.0 ) ) ;
78
+ Assert . Equal ( "key" , exception . ParamName ) ;
79
+ Assert . Contains ( "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." , exception . Message ) ;
80
+ }
81
+
82
+ [ Theory ]
83
+ [ InlineData ( null ) ]
84
+ [ InlineData ( "" ) ]
85
+ [ InlineData ( " " ) ]
86
+ public void When_AddMetric_With_InvalidKey_Should_ThrowArgumentNullException ( string key )
87
+ {
88
+ // Arrange
89
+ // var metricsMock = Substitute.For<IMetrics>();
90
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
91
+ IMetrics metrics = new Metrics ( powertoolsConfigMock ) ;
92
+
93
+ // Act & Assert
94
+ var exception = Assert . Throws < ArgumentNullException > ( ( ) => metrics . AddMetric ( key , 1.0 ) ) ;
95
+ Assert . Equal ( "key" , exception . ParamName ) ;
96
+ Assert . Contains ( "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." , exception . Message ) ;
97
+ }
98
+
99
+ [ Fact ]
100
+ public void When_SetDefaultDimensions_With_InvalidKeyOrValue_Should_ThrowArgumentNullException ( )
101
+ {
102
+ // Arrange
103
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
104
+ IMetrics metrics = new Metrics ( powertoolsConfigMock ) ;
105
+
106
+ var invalidDimensions = new Dictionary < string , string >
107
+ {
108
+ { "" , "value" } , // empty key
109
+ { "key" , "" } , // empty value
110
+ { " " , "value" } , // whitespace key
111
+ { "key1" , " " } , // whitespace value
112
+ { "key2" , null } // null value
113
+ } ;
114
+
115
+ // Act & Assert
116
+ foreach ( var dimension in invalidDimensions )
117
+ {
118
+ var dimensions = new Dictionary < string , string > { { dimension . Key , dimension . Value } } ;
119
+ var exception = Assert . Throws < ArgumentNullException > ( ( ) => metrics . SetDefaultDimensions ( dimensions ) ) ;
120
+ Assert . Equal ( "Key" , exception . ParamName ) ;
121
+ Assert . Contains ( "'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed." , exception . Message ) ;
122
+ }
123
+ }
124
+
125
+ [ Fact ]
126
+ public void When_PushSingleMetric_With_EmptyName_Should_ThrowArgumentNullException ( )
127
+ {
128
+ // Arrange
129
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
130
+ IMetrics metrics = new Metrics ( powertoolsConfigMock ) ;
131
+
132
+ // Act & Assert
133
+ var exception = Assert . Throws < ArgumentNullException > ( ( ) => metrics . PushSingleMetric ( "" , 1.0 , MetricUnit . Count ) ) ;
134
+ Assert . Equal ( "name" , exception . ParamName ) ;
135
+ Assert . Contains ( "'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." , exception . Message ) ;
136
+ }
137
+
138
+ [ Theory ]
139
+ [ InlineData ( null ) ]
140
+ [ InlineData ( "" ) ]
141
+ [ InlineData ( " " ) ]
142
+ public void When_PushSingleMetric_With_InvalidName_Should_ThrowArgumentNullException ( string name )
143
+ {
144
+ // Arrange
145
+ var powertoolsConfigMock = Substitute . For < IPowertoolsConfigurations > ( ) ;
146
+ IMetrics metrics = new Metrics ( powertoolsConfigMock ) ;
147
+
148
+ // Act & Assert
149
+ var exception = Assert . Throws < ArgumentNullException > ( ( ) => metrics . PushSingleMetric ( name , 1.0 , MetricUnit . Count ) ) ;
150
+ Assert . Equal ( "name" , exception . ParamName ) ;
151
+ Assert . Contains ( "'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." , exception . Message ) ;
152
+ }
33
153
}
0 commit comments