20
20
import static org .mockito .Mockito .verify ;
21
21
import static software .amazon .lambda .powertools .tracing .TracingUtils .withEntitySubsegment ;
22
22
23
- import ch .qos .logback .classic .Level ;
24
- import ch .qos .logback .classic .Logger ;
25
- import ch .qos .logback .classic .spi .ILoggingEvent ;
26
- import ch .qos .logback .core .read .ListAppender ;
27
23
import com .amazonaws .services .lambda .runtime .Context ;
28
24
import com .amazonaws .xray .AWSXRay ;
29
25
import com .amazonaws .xray .entities .Entity ;
30
- import java .util .List ;
31
26
import org .junit .jupiter .api .AfterEach ;
32
27
import org .junit .jupiter .api .BeforeEach ;
33
28
import org .junit .jupiter .api .Test ;
34
- import org .slf4j .LoggerFactory ;
35
29
36
30
class TracingUtilsTest {
37
-
38
31
@ BeforeEach
39
32
void setUp () {
40
33
AWSXRay .beginSegment ("test" );
@@ -130,45 +123,21 @@ void shouldInvokeCodeBlockWrappedWithinSubsegment() {
130
123
}
131
124
132
125
@ Test
133
- void shouldEmitNoLogWarnIfValidCharacterInKey () {
126
+ void shouldNotAddAnnotationIfInvalidCharacterInKey () {
134
127
AWSXRay .beginSubsegment ("subSegment" );
135
- Logger logger = (Logger ) LoggerFactory .getLogger (TracingUtils .class );
136
-
137
- // create and start a ListAppender
138
- ListAppender <ILoggingEvent > listAppender = new ListAppender <>();
139
- listAppender .start ();
140
-
141
- // add the appender to the logger
142
- logger .addAppender (listAppender );
143
-
144
- TracingUtils .putAnnotation ("stringKey" , "val" );
145
-
146
- List <ILoggingEvent > logsList = listAppender .list ;
147
- assertThat (AWSXRay .getTraceEntity ().getAnnotations ())
148
- .hasSize (1 )
149
- .contains (
150
- entry ("stringKey" , "val" )
151
- );
152
- assertThat (logsList .size ()).isZero ();
128
+ String inputKey = "stringKey with spaces" ;
129
+ TracingUtils .putAnnotation (inputKey , "val" );
130
+ AWSXRay .getCurrentSubsegmentOptional ()
131
+ .ifPresent (segment -> assertThat (segment .getAnnotations ()).size ().isEqualTo (0 ));
153
132
}
154
133
155
134
@ Test
156
- void shouldEmitLogWarnIfInvalidCharacterInKey () {
135
+ void shouldAddAnnotationIfValidCharactersInKey () {
157
136
AWSXRay .beginSubsegment ("subSegment" );
158
- Logger logger = (Logger ) LoggerFactory .getLogger (TracingUtils .class );
159
-
160
- // create and start a ListAppender
161
- ListAppender <ILoggingEvent > listAppender = new ListAppender <>();
162
- listAppender .start ();
163
-
164
- // add the appender to the logger
165
- logger .addAppender (listAppender );
166
- String inputKey = "stringKey with spaces" ;
137
+ String inputKey = "validKey" ;
167
138
TracingUtils .putAnnotation (inputKey , "val" );
168
-
169
- List <ILoggingEvent > logsList = listAppender .list ;
170
- assertThat (logsList .get (0 ).getLevel ()).isEqualTo (Level .WARN );
171
- assertThat (logsList .get (0 ).getMessage ()).isEqualTo ("Ignoring annotation with unsupported characters in key: {}" ,inputKey );
139
+ AWSXRay .getCurrentSubsegmentOptional ()
140
+ .ifPresent (segment -> assertThat (segment .getAnnotations ()).size ().isEqualTo (1 ));
172
141
}
173
142
174
143
@ Test
@@ -269,4 +238,4 @@ void shouldInvokeCodeBlockWrappedWithinNamespacedEntitySubsegment() throws Inter
269
238
.containsEntry ("key" , "val" );
270
239
});
271
240
}
272
- }
241
+ }
0 commit comments