3
3
import java .io .ByteArrayOutputStream ;
4
4
import java .io .PrintStream ;
5
5
import java .util .Map ;
6
+ import java .util .function .Consumer ;
6
7
7
8
import com .fasterxml .jackson .core .JsonProcessingException ;
8
9
import com .fasterxml .jackson .databind .ObjectMapper ;
12
13
import org .junit .jupiter .api .Test ;
13
14
import org .mockito .MockedStatic ;
14
15
import software .amazon .cloudwatchlogs .emf .config .SystemWrapper ;
16
+ import software .amazon .cloudwatchlogs .emf .logger .MetricsLogger ;
15
17
import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
16
18
import software .amazon .cloudwatchlogs .emf .model .Unit ;
17
19
@@ -124,13 +126,29 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
124
126
125
127
@ Test
126
128
void metricsLoggerCaptureUtilityWithDefaultNameSpace () {
129
+ testWithNewLogger (MetricsUtils ::withMetricsLogger );
130
+ }
131
+
132
+ @ Test
133
+ void deprecatedMetricLoggerCaptureUtilityWithDefaultNameSpace () {
134
+ testWithNewLogger (MetricsUtils ::withMetricLogger );
135
+ }
136
+
137
+ @ Test
138
+ void shouldThrowExceptionWhenDefaultDimensionIsNull () {
139
+ assertThatNullPointerException ()
140
+ .isThrownBy (() -> MetricsUtils .defaultDimensionSet (null ))
141
+ .withMessage ("Null dimension set not allowed" );
142
+ }
143
+
144
+ private void testWithNewLogger (Consumer <Consumer <MetricsLogger >> methodToTest ) {
127
145
try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class );
128
146
MockedStatic <software .amazon .lambda .powertools .core .internal .SystemWrapper > internalWrapper = mockStatic (software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
129
147
mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
130
148
mocked .when (() -> SystemWrapper .getenv ("POWERTOOLS_METRICS_NAMESPACE" )).thenReturn ("GlobalName" );
131
149
internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" )).thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
132
150
133
- MetricsUtils . withMetricsLogger (metricsLogger -> {
151
+ methodToTest . accept (metricsLogger -> {
134
152
metricsLogger .setDimensions (DimensionSet .of ("Dimension1" , "Value1" ));
135
153
metricsLogger .putMetric ("Metric1" , 1 , Unit .COUNT );
136
154
});
@@ -154,13 +172,6 @@ void metricsLoggerCaptureUtilityWithDefaultNameSpace() {
154
172
}
155
173
}
156
174
157
- @ Test
158
- void shouldThrowExceptionWhenDefaultDimensionIsNull () {
159
- assertThatNullPointerException ()
160
- .isThrownBy (() -> MetricsUtils .defaultDimensionSet (null ))
161
- .withMessage ("Null dimension set not allowed" );
162
- }
163
-
164
175
private Map <String , Object > readAsJson (String s ) {
165
176
try {
166
177
return mapper .readValue (s , Map .class );
0 commit comments