45
45
import org .junit .jupiter .api .AfterAll ;
46
46
import org .junit .jupiter .api .BeforeAll ;
47
47
import org .junit .jupiter .api .Test ;
48
+ import org .junit .jupiter .api .extension .ExtendWith ;
48
49
import org .junit .jupiter .api .extension .RegisterExtension ;
50
+ import org .mockito .Mock ;
51
+ import org .mockito .junit .jupiter .MockitoExtension ;
49
52
53
+ @ ExtendWith (MockitoExtension .class )
50
54
class TwilioClientTest {
51
55
52
56
@ RegisterExtension
@@ -119,7 +123,9 @@ class TwilioClientTest {
119
123
+ " \" more_info\" : \" Testing\" \n "
120
124
+ " }" ;
121
125
122
- private TwilioRestClient twilioRestClient ;
126
+ @ Mock private TwilioRestClient twilioRestClient ;
127
+
128
+ @ Mock private CloseableHttpClient httpClient ;
123
129
124
130
@ BeforeAll
125
131
static void setUp () {
@@ -135,7 +141,6 @@ static void tearDown() {
135
141
136
142
@ Test
137
143
void synchronousMessage () {
138
- twilioRestClient = mock (TwilioRestClient .class );
139
144
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
140
145
when (twilioRestClient .request (any ()))
141
146
.thenReturn (
@@ -162,6 +167,7 @@ void synchronousMessage() {
162
167
span ->
163
168
span .hasName ("MessageCreator.create" )
164
169
.hasKind (CLIENT )
170
+ .hasParent (trace .getSpan (0 ))
165
171
.hasAttributesSatisfyingExactly (
166
172
equalTo (
167
173
stringKey ("twilio.type" ),
@@ -174,7 +180,6 @@ void synchronousMessage() {
174
180
175
181
@ Test
176
182
void synchronousCall () throws URISyntaxException {
177
- twilioRestClient = mock (TwilioRestClient .class );
178
183
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
179
184
when (twilioRestClient .request (any ()))
180
185
.thenReturn (
@@ -201,6 +206,7 @@ void synchronousCall() throws URISyntaxException {
201
206
span ->
202
207
span .hasName ("CallCreator.create" )
203
208
.hasKind (CLIENT )
209
+ .hasParent (trace .getSpan (0 ))
204
210
.hasAttributesSatisfyingExactly (
205
211
equalTo (
206
212
stringKey ("twilio.type" ), "com.twilio.rest.api.v2010.account.Call" ),
@@ -212,9 +218,8 @@ void synchronousCall() throws URISyntaxException {
212
218
213
219
@ Test
214
220
void httpClient () throws IOException {
215
- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
216
- CloseableHttpResponse response1 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
217
- when (httpClient .execute (any ())).thenReturn (response1 );
221
+ CloseableHttpResponse response = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
222
+ when (httpClient .execute (any ())).thenReturn (response );
218
223
219
224
HttpClientBuilder clientBuilder = getHttpClientBuilder (httpClient );
220
225
@@ -245,6 +250,7 @@ void httpClient() throws IOException {
245
250
span ->
246
251
span .hasName ("MessageCreator.create" )
247
252
.hasKind (CLIENT )
253
+ .hasParent (trace .getSpan (0 ))
248
254
.hasAttributesSatisfyingExactly (
249
255
equalTo (
250
256
stringKey ("twilio.type" ),
@@ -264,7 +270,6 @@ void httpClient() throws IOException {
264
270
265
271
@ Test
266
272
void httpClientRetry () throws IOException {
267
- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
268
273
CloseableHttpResponse response1 = mockResponse (ERROR_RESPONSE_BODY , 500 );
269
274
CloseableHttpResponse response2 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
270
275
when (httpClient .execute (any ())).thenReturn (response1 , response2 );
@@ -297,6 +302,7 @@ void httpClientRetry() throws IOException {
297
302
span -> span .hasName ("test" ).hasNoParent ().hasAttributes (Attributes .empty ()),
298
303
span ->
299
304
span .hasName ("MessageCreator.create" )
305
+ .hasParent (trace .getSpan (0 ))
300
306
.hasKind (CLIENT )
301
307
.hasAttributesSatisfyingExactly (
302
308
equalTo (
@@ -310,7 +316,6 @@ void httpClientRetry() throws IOException {
310
316
311
317
@ Test
312
318
void httpClientRetryAsync () throws Exception {
313
- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
314
319
CloseableHttpResponse response1 = mockResponse (ERROR_RESPONSE_BODY , 500 );
315
320
CloseableHttpResponse response2 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
316
321
when (httpClient .execute (any ())).thenReturn (response1 , response2 );
@@ -352,6 +357,7 @@ void httpClientRetryAsync() throws Exception {
352
357
span ->
353
358
span .hasName ("MessageCreator.createAsync" )
354
359
.hasKind (CLIENT )
360
+ .hasParent (trace .getSpan (0 ))
355
361
.hasAttributesSatisfyingExactly (
356
362
equalTo (
357
363
stringKey ("twilio.type" ),
@@ -364,7 +370,6 @@ void httpClientRetryAsync() throws Exception {
364
370
365
371
@ Test
366
372
void syncFailure () {
367
- twilioRestClient = mock (TwilioRestClient .class );
368
373
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
369
374
when (twilioRestClient .request (any ()))
370
375
.thenReturn (
@@ -395,13 +400,13 @@ void syncFailure() {
395
400
span ->
396
401
span .hasName ("MessageCreator.create" )
397
402
.hasKind (CLIENT )
403
+ .hasParent (trace .getSpan (0 ))
398
404
.hasStatus (StatusData .error ())
399
405
.hasException (new ApiException ("Testing Failure" ))));
400
406
}
401
407
402
408
@ Test
403
409
void rootSpan () {
404
- twilioRestClient = mock (TwilioRestClient .class );
405
410
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
406
411
when (twilioRestClient .request (any ()))
407
412
.thenReturn (
@@ -436,7 +441,6 @@ void rootSpan() {
436
441
437
442
@ Test
438
443
void asynchronousCall () throws Exception {
439
- twilioRestClient = mock (TwilioRestClient .class );
440
444
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
441
445
when (twilioRestClient .request (any ()))
442
446
.thenReturn (
@@ -472,6 +476,7 @@ void asynchronousCall() throws Exception {
472
476
span ->
473
477
span .hasName ("MessageCreator.createAsync" )
474
478
.hasKind (CLIENT )
479
+ .hasParent (trace .getSpan (0 ))
475
480
.hasAttributesSatisfyingExactly (
476
481
equalTo (
477
482
stringKey ("twilio.type" ),
@@ -484,7 +489,6 @@ void asynchronousCall() throws Exception {
484
489
485
490
@ Test
486
491
void asynchronousError () {
487
- twilioRestClient = mock (TwilioRestClient .class );
488
492
when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
489
493
when (twilioRestClient .request (any ()))
490
494
.thenReturn (
@@ -523,6 +527,7 @@ void asynchronousError() {
523
527
span ->
524
528
span .hasName ("MessageCreator.createAsync" )
525
529
.hasKind (CLIENT )
530
+ .hasParent (trace .getSpan (0 ))
526
531
.hasStatus (StatusData .error ())
527
532
.hasException (new ApiException ("Testing Failure" ))));
528
533
}
0 commit comments