From 5c472639bd256c64184528cf5dc1ec0624e97c2c Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Tue, 21 Jan 2025 15:25:26 +0800 Subject: [PATCH 1/7] convert twilio unit test from groovy to java --- .../test/groovy/test/TwilioClientTest.groovy | 628 ------------------ .../twilio/TwilioClientTest.java | 567 ++++++++++++++++ 2 files changed, 567 insertions(+), 628 deletions(-) delete mode 100644 instrumentation/twilio-6.6/javaagent/src/test/groovy/test/TwilioClientTest.groovy create mode 100644 instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java diff --git a/instrumentation/twilio-6.6/javaagent/src/test/groovy/test/TwilioClientTest.groovy b/instrumentation/twilio-6.6/javaagent/src/test/groovy/test/TwilioClientTest.groovy deleted file mode 100644 index 6c00a4790876..000000000000 --- a/instrumentation/twilio-6.6/javaagent/src/test/groovy/test/TwilioClientTest.groovy +++ /dev/null @@ -1,628 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package test - -import com.fasterxml.jackson.databind.ObjectMapper -import com.google.common.util.concurrent.ListenableFuture -import com.twilio.Twilio -import com.twilio.exception.ApiException -import com.twilio.http.NetworkHttpClient -import com.twilio.http.Response -import com.twilio.http.TwilioRestClient -import com.twilio.rest.api.v2010.account.Call -import com.twilio.rest.api.v2010.account.Message -import com.twilio.type.PhoneNumber - -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import io.opentelemetry.semconv.ServerAttributes -import io.opentelemetry.semconv.ErrorAttributes -import io.opentelemetry.semconv.HttpAttributes -import io.opentelemetry.semconv.NetworkAttributes -import io.opentelemetry.semconv.UrlAttributes -import org.apache.http.HttpEntity -import org.apache.http.StatusLine -import org.apache.http.client.methods.CloseableHttpResponse -import org.apache.http.impl.client.CloseableHttpClient -import org.apache.http.impl.client.HttpClientBuilder - -import java.util.concurrent.ExecutionException -import java.util.concurrent.TimeUnit - -import static io.opentelemetry.api.trace.SpanKind.CLIENT -import static io.opentelemetry.api.trace.StatusCode.ERROR - -class TwilioClientTest extends AgentInstrumentationSpecification { - final static String ACCOUNT_SID = "abc" - final static String AUTH_TOKEN = "efg" - - final static String MESSAGE_RESPONSE_BODY = """ - { - "account_sid": "AC14984e09e497506cf0d5eb59b1f6ace7", - "api_version": "2010-04-01", - "body": "Hello, World!", - "date_created": "Thu, 30 Jul 2015 20:12:31 +0000", - "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000", - "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000", - "direction": "outbound-api", - "from": "+14155552345", - "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "num_media": "0", - "num_segments": "1", - "price": -0.00750, - "price_unit": "USD", - "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "status": "sent", - "subresource_uris": { - "media": "/2010-04-01/Accounts/AC14984e09e497506cf0d5eb59b1f6ace7/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json" - }, - "to": "+14155552345", - "uri": "/2010-04-01/Accounts/AC14984e09e497506cf0d5eb59b1f6ace7/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json" - } - """ - - final static String CALL_RESPONSE_BODY = """ - { - "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "annotation": null, - "answered_by": null, - "api_version": "2010-04-01", - "caller_name": null, - "date_created": "Tue, 31 Aug 2010 20:36:28 +0000", - "date_updated": "Tue, 31 Aug 2010 20:36:44 +0000", - "direction": "inbound", - "duration": "15", - "end_time": "Tue, 31 Aug 2010 20:36:44 +0000", - "forwarded_from": "+141586753093", - "from": "+15017122661", - "from_formatted": "(501) 712-2661", - "group_sid": null, - "parent_call_sid": null, - "phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "price": -0.03000, - "price_unit": "USD", - "sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "start_time": "Tue, 31 Aug 2010 20:36:29 +0000", - "status": "completed", - "subresource_uris": { - "notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json", - "recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json", - "feedback": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Feedback.json", - "feedback_summaries": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/FeedbackSummary.json" - }, - "to": "+15558675310", - "to_formatted": "(555) 867-5310", - "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json" - } - """ - - final static String ERROR_RESPONSE_BODY = """ - { - "code": 123, - "message": "Testing Failure", - "code": 567, - "more_info": "Testing" - } - """ - - TwilioRestClient twilioRestClient = Mock() - - def setupSpec() { - Twilio.init(ACCOUNT_SID, AUTH_TOKEN) - } - - def cleanup() { - Twilio.getExecutorService().shutdown() - Twilio.setExecutorService(null) - Twilio.setRestClient(null) - } - - def "synchronous message"() { - setup: - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes()), 200) - - Message message = runWithSpan("test") { - Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).create(twilioRestClient) - } - - expect: - - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 2) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "MessageCreator.create" - kind CLIENT - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - } - } - } - - def "synchronous call"() { - setup: - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(CALL_RESPONSE_BODY.getBytes()), 200) - - Call call = runWithSpan("test") { - Call.creator( - new PhoneNumber("+15558881234"), // To number - new PhoneNumber("+15559994321"), // From number - - // Read TwiML at this URL when a call connects (hold music) - new URI("http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") - ).create(twilioRestClient) - } - - expect: - - call.status == Call.Status.COMPLETED - - assertTraces(1) { - trace(0, 2) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "CallCreator.create" - kind CLIENT - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Call" - "twilio.account" "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.sid" "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "completed" - } - } - } - } - } - - def "http client"() { - setup: - CloseableHttpClient httpClient = Mock() - httpClient.execute(_) >> mockResponse(MESSAGE_RESPONSE_BODY, 200) - - HttpClientBuilder clientBuilder = Mock() - clientBuilder.build() >> httpClient - - NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder) - - TwilioRestClient realTwilioRestClient = - new TwilioRestClient.Builder("username", "password") - .accountSid(ACCOUNT_SID) - .httpClient(networkHttpClient) - .build() - - Message message = runWithSpan("test") { - Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).create(realTwilioRestClient) - } - - expect: - - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 3) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "MessageCreator.create" - kind CLIENT - childOf(span(0)) - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - span(2) { - name "POST" - kind CLIENT - childOf span(1) - attributes { - "$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1" - "$ServerAttributes.SERVER_ADDRESS.key" "api.twilio.com" - "$HttpAttributes.HTTP_REQUEST_METHOD.key" "POST" - "$UrlAttributes.URL_FULL.key" "https://api.twilio.com/2010-04-01/Accounts/abc/Messages.json" - "$HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key" 200 - } - } - } - } - } - - def "http client retry"() { - setup: - CloseableHttpClient httpClient = Mock() - httpClient.execute(_) >>> [ - mockResponse(ERROR_RESPONSE_BODY, 500), - mockResponse(MESSAGE_RESPONSE_BODY, 200) - ] - - HttpClientBuilder clientBuilder = Mock() - clientBuilder.build() >> httpClient - - NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder) - - TwilioRestClient realTwilioRestClient = - new TwilioRestClient.Builder("username", "password") - .accountSid(ACCOUNT_SID) - .httpClient(networkHttpClient) - .build() - - Message message = runWithSpan("test") { - Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).create(realTwilioRestClient) - } - - expect: - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 4) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "MessageCreator.create" - kind CLIENT - childOf(span(0)) - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - span(2) { - name "POST" - kind CLIENT - childOf span(1) - status ERROR - attributes { - "$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1" - "$ServerAttributes.SERVER_ADDRESS.key" "api.twilio.com" - "$HttpAttributes.HTTP_REQUEST_METHOD.key" "POST" - "$UrlAttributes.URL_FULL.key" "https://api.twilio.com/2010-04-01/Accounts/abc/Messages.json" - "$HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key" 500 - "$ErrorAttributes.ERROR_TYPE" "500" - } - } - span(3) { - name "POST" - kind CLIENT - childOf span(1) - attributes { - "$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1" - "$ServerAttributes.SERVER_ADDRESS.key" "api.twilio.com" - "$HttpAttributes.HTTP_REQUEST_METHOD.key" "POST" - "$UrlAttributes.URL_FULL.key" "https://api.twilio.com/2010-04-01/Accounts/abc/Messages.json" - "$HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key" 200 - } - } - } - } - } - - def "http client retry async"() { - setup: - CloseableHttpClient httpClient = Mock() - httpClient.execute(_) >>> [ - mockResponse(ERROR_RESPONSE_BODY, 500), - mockResponse(MESSAGE_RESPONSE_BODY, 200) - ] - - HttpClientBuilder clientBuilder = Mock() - clientBuilder.build() >> httpClient - - NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder) - - TwilioRestClient realTwilioRestClient = - new TwilioRestClient.Builder("username", "password") - .accountSid(ACCOUNT_SID) - .httpClient(networkHttpClient) - .build() - - Message message = runWithSpan("test") { - ListenableFuture future = Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).createAsync(realTwilioRestClient) - - try { - return future.get(10, TimeUnit.SECONDS) - } finally { - // Give the future callback a chance to run - Thread.sleep(1000) - } - } - - expect: - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 4) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "MessageCreator.createAsync" - kind CLIENT - childOf(span(0)) - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - span(2) { - name "POST" - kind CLIENT - childOf span(1) - status ERROR - attributes { - "$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1" - "$ServerAttributes.SERVER_ADDRESS.key" "api.twilio.com" - "$HttpAttributes.HTTP_REQUEST_METHOD.key" "POST" - "$UrlAttributes.URL_FULL.key" "https://api.twilio.com/2010-04-01/Accounts/abc/Messages.json" - "$HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key" 500 - "$ErrorAttributes.ERROR_TYPE" "500" - } - } - span(3) { - name "POST" - kind CLIENT - childOf span(1) - attributes { - "$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1" - "$ServerAttributes.SERVER_ADDRESS.key" "api.twilio.com" - "$HttpAttributes.HTTP_REQUEST_METHOD.key" "POST" - "$UrlAttributes.URL_FULL.key" "https://api.twilio.com/2010-04-01/Accounts/abc/Messages.json" - "$HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key" 200 - } - } - } - } - - cleanup: - Twilio.getExecutorService().shutdown() - Twilio.setExecutorService(null) - Twilio.setRestClient(null) - } - - def "Sync Failure"() { - setup: - - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes()), 500) - - when: - runWithSpan("test") { - Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).create(twilioRestClient) - } - - then: - thrown(ApiException) - - expect: - assertTraces(1) { - trace(0, 2) { - span(0) { - name "test" - status ERROR - errorEvent(ApiException, "Testing Failure") - hasNoParent() - } - span(1) { - name "MessageCreator.create" - kind CLIENT - status ERROR - errorEvent(ApiException, "Testing Failure") - } - } - } - } - - def "root span"() { - setup: - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes()), 200) - - Message message = Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).create(twilioRestClient) - - expect: - - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 1) { - span(0) { - name "MessageCreator.create" - kind CLIENT - hasNoParent() - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - } - } - } - - def "asynchronous call"(a) { - setup: - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes()), 200) - - when: - - Message message = runWithSpan("test") { - - ListenableFuture future = Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).createAsync(twilioRestClient) - - try { - return future.get(10, TimeUnit.SECONDS) - } finally { - // Give the future callback a chance to run - Thread.sleep(1000) - } - } - - then: - - message != null - message.body == "Hello, World!" - - assertTraces(1) { - trace(0, 2) { - span(0) { - name "test" - hasNoParent() - attributes { - } - } - span(1) { - name "MessageCreator.createAsync" - kind CLIENT - attributes { - "twilio.type" "com.twilio.rest.api.v2010.account.Message" - "twilio.account" "AC14984e09e497506cf0d5eb59b1f6ace7" - "twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" - "twilio.status" "sent" - } - } - } - } - - cleanup: - Twilio.getExecutorService().shutdown() - Twilio.setExecutorService(null) - Twilio.setRestClient(null) - - where: - a | _ - 1 | _ - 2 | _ - } - - def "asynchronous error"() { - setup: - twilioRestClient.getObjectMapper() >> new ObjectMapper() - - 1 * twilioRestClient.request(_) >> new Response(new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes()), 500) - - when: - runWithSpan("test") { - ListenableFuture future = Message.creator( - new PhoneNumber("+1 555 720 5913"), // To number - new PhoneNumber("+1 555 555 5215"), // From number - "Hello world!" // SMS body - ).createAsync(twilioRestClient) - - try { - return future.get(10, TimeUnit.SECONDS) - } finally { - Thread.sleep(1000) - } - } - - then: - thrown(ExecutionException) - - expect: - - assertTraces(1) { - trace(0, 2) { - span(0) { - name "test" - status ERROR - errorEvent(ApiException, "Testing Failure") - hasNoParent() - } - span(1) { - name "MessageCreator.createAsync" - kind CLIENT - status ERROR - errorEvent(ApiException, "Testing Failure") - } - } - } - } - - private CloseableHttpResponse mockResponse(String body, int statusCode) { - HttpEntity httpEntity = Mock() - httpEntity.getContent() >> { new ByteArrayInputStream(body.getBytes()) } - httpEntity.isRepeatable() >> true - httpEntity.getContentLength() >> body.length() - - StatusLine statusLine = Mock() - statusLine.getStatusCode() >> statusCode - - CloseableHttpResponse httpResponse = Mock() - httpResponse.getEntity() >> httpEntity - httpResponse.getStatusLine() >> statusLine - - return httpResponse - } -} diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java new file mode 100644 index 000000000000..539c7ead1d8a --- /dev/null +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -0,0 +1,567 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.twilio; + +import static io.opentelemetry.api.trace.SpanKind.CLIENT; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.util.concurrent.ListenableFuture; +import com.twilio.Twilio; +import com.twilio.exception.ApiException; +import com.twilio.http.NetworkHttpClient; +import com.twilio.http.Response; +import com.twilio.http.TwilioRestClient; +import com.twilio.rest.api.v2010.account.Call; +import com.twilio.rest.api.v2010.account.Message; +import com.twilio.type.PhoneNumber; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.sdk.trace.data.StatusData; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class TwilioClientTest { + + @RegisterExtension + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + private static final String ACCOUNT_SID = "abc"; + private static final String AUTH_TOKEN = "efg"; + + private static final String MESSAGE_RESPONSE_BODY = + " {\n" + + " \"account_sid\": \"AC14984e09e497506cf0d5eb59b1f6ace7\",\n" + + " \"api_version\": \"2010-04-01\",\n" + + " \"body\": \"Hello, World!\",\n" + + " \"date_created\": \"Thu, 30 Jul 2015 20:12:31 +0000\",\n" + + " \"date_sent\": \"Thu, 30 Jul 2015 20:12:33 +0000\",\n" + + " \"date_updated\": \"Thu, 30 Jul 2015 20:12:33 +0000\",\n" + + " \"direction\": \"outbound-api\",\n" + + " \"from\": \"+14155552345\",\n" + + " \"messaging_service_sid\": \"MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\",\n" + + " \"num_media\": \"0\",\n" + + " \"num_segments\": \"1\",\n" + + " \"price\": -0.00750,\n" + + " \"price_unit\": \"USD\",\n" + + " \"sid\": \"MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\",\n" + + " \"status\": \"sent\",\n" + + " \"subresource_uris\": {\n" + + " \"media\": \"/2010-04-01/Accounts/AC14984e09e497506cf0d5eb59b1f6ace7/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json\"\n" + + " },\n" + + " \"to\": \"+14155552345\",\n" + + " \"uri\": \"/2010-04-01/Accounts/AC14984e09e497506cf0d5eb59b1f6ace7/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json\"\n" + + " }"; + private static final String CALL_RESPONSE_BODY = + " {\n" + + " \"account_sid\": \"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\",\n" + + " \"annotation\": null,\n" + + " \"answered_by\": null,\n" + + " \"api_version\": \"2010-04-01\",\n" + + " \"caller_name\": null,\n" + + " \"date_created\": \"Tue, 31 Aug 2010 20:36:28 +0000\",\n" + + " \"date_updated\": \"Tue, 31 Aug 2010 20:36:44 +0000\",\n" + + " \"direction\": \"inbound\",\n" + + " \"duration\": \"15\",\n" + + " \"end_time\": \"Tue, 31 Aug 2010 20:36:44 +0000\",\n" + + " \"forwarded_from\": \"+141586753093\",\n" + + " \"from\": \"+15017122661\",\n" + + " \"from_formatted\": \"(501) 712-2661\",\n" + + " \"group_sid\": null,\n" + + " \"parent_call_sid\": null,\n" + + " \"phone_number_sid\": \"PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\",\n" + + " \"price\": -0.03000,\n" + + " \"price_unit\": \"USD\",\n" + + " \"sid\": \"CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\",\n" + + " \"start_time\": \"Tue, 31 Aug 2010 20:36:29 +0000\",\n" + + " \"status\": \"completed\",\n" + + " \"subresource_uris\": {\n" + + " \"notifications\": \"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json\",\n" + + " \"recordings\": \"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json\",\n" + + " \"feedback\": \"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Feedback.json\",\n" + + " \"feedback_summaries\": \"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/FeedbackSummary.json\"\n" + + " },\n" + + " \"to\": \"+15558675310\",\n" + + " \"to_formatted\": \"(555) 867-5310\",\n" + + " \"uri\": \"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json\"\n" + + " }"; + private static final String ERROR_RESPONSE_BODY = + "{\n" + + " \"code\": 123,\n" + + " \"message\": \"Testing Failure\",\n" + + " \"code\": 567,\n" + + " \"more_info\": \"Testing\"\n" + + " }"; + + private TwilioRestClient twilioRestClient; + + @BeforeAll + static void setUp() { + Twilio.init(ACCOUNT_SID, AUTH_TOKEN); + } + + @AfterAll + static void tearDown() { + Twilio.getExecutorService().shutdown(); + Twilio.setExecutorService(null); + Twilio.setRestClient(null); + } + + @Test + public void synchronousMessage() { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 200)); + + Message message = + testing.runWithSpan( + "test", + () -> + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(twilioRestClient)); + + assertThat(message.getBody()).isEqualTo("Hello, World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("MessageCreator.create") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @Test + public void synchronousCall() throws URISyntaxException { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(CALL_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 200)); + + Call call = + testing.runWithSpan( + "test", + () -> + Call.creator( + new PhoneNumber("+15558881234"), + new PhoneNumber("+15559994321"), + new URI("http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")) + .create(twilioRestClient)); + + assertThat(call.getStatus()).isEqualTo(Call.Status.COMPLETED); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("CallCreator.create") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Call"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "completed")))); + } + + @Test + public void httpClient() throws IOException { + CloseableHttpClient httpClient = mock(CloseableHttpClient.class); + CloseableHttpResponse response1 = mockResponse(MESSAGE_RESPONSE_BODY, 200); + when(httpClient.execute(any())).thenReturn(response1); + + HttpClientBuilder clientBuilder = getHttpClientBuilder(httpClient); + + NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder); + + TwilioRestClient realTwilioRestClient = + new TwilioRestClient.Builder("username", "password") + .accountSid(ACCOUNT_SID) + .httpClient(networkHttpClient) + .build(); + + Message message = + testing.runWithSpan( + "test", + () -> + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(realTwilioRestClient)); + + assertThat(message.getBody()).isEqualTo("Hello, World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("MessageCreator.create") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @SuppressWarnings("CannotMockMethod") + private static @NotNull HttpClientBuilder getHttpClientBuilder(CloseableHttpClient httpClient) { + HttpClientBuilder clientBuilder = spy(HttpClientBuilder.create()); + when(clientBuilder.build()).thenReturn(httpClient); + return clientBuilder; + } + + @Test + public void httpClientRetry() throws IOException { + CloseableHttpClient httpClient = mock(CloseableHttpClient.class); + CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); + CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); + when(httpClient.execute(any())).thenReturn(response1, response2); + + HttpClientBuilder clientBuilder = getHttpClientBuilder(httpClient); + + NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder); + + TwilioRestClient realTwilioRestClient = + new TwilioRestClient.Builder("username", "password") + .accountSid(ACCOUNT_SID) + .httpClient(networkHttpClient) + .build(); + + Message message = + testing.runWithSpan( + "test", + () -> + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(realTwilioRestClient)); + + assertThat(message.getBody()).isEqualTo("Hello, World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("MessageCreator.create") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @Test + public void httpClientRetryAsync() throws Exception { + CloseableHttpClient httpClient = mock(CloseableHttpClient.class); + CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); + CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); + when(httpClient.execute(any())).thenReturn(response1, response2); + + HttpClientBuilder clientBuilder = getHttpClientBuilder(httpClient); + + NetworkHttpClient networkHttpClient = new NetworkHttpClient(clientBuilder); + + TwilioRestClient realTwilioRestClient = + new TwilioRestClient.Builder("username", "password") + .accountSid(ACCOUNT_SID) + .httpClient(networkHttpClient) + .build(); + + Message message = + testing.runWithSpan( + "test", + () -> { + ListenableFuture future = + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .createAsync(realTwilioRestClient); + + try { + return future.get(10, TimeUnit.SECONDS); + } finally { + Thread.sleep(1000); + } + }); + + assertThat(message.getBody()).isEqualTo("Hello, World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("MessageCreator.createAsync") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @Test + public void syncFailure() { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 500)); + + Assertions.assertThrows( + ApiException.class, + () -> + testing.runWithSpan( + "test", + () -> + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(twilioRestClient))); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("test") + .hasNoParent() + .hasStatus(StatusData.error()) + .hasException(new ApiException("Testing Failure")), + span -> + span.hasName("MessageCreator.create") + .hasKind(CLIENT) + .hasStatus(StatusData.error()) + .hasException(new ApiException("Testing Failure")))); + } + + @Test + public void rootSpan() { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 200)); + + Message message = + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(twilioRestClient); + + assertThat(message.getBody()).isEqualTo("Hello, World!"); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("MessageCreator.create") + .hasKind(CLIENT) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @Test + public void asynchronousCall() throws Exception { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(MESSAGE_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 200)); + + Message message = + testing.runWithSpan( + "test", + () -> { + ListenableFuture future = + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .createAsync(twilioRestClient); + + try { + return future.get(10, TimeUnit.SECONDS); + } finally { + Thread.sleep(1000); + } + }); + + Assertions.assertNotNull(message); + assertThat(message.getBody()).isEqualTo("Hello, World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("test").hasNoParent(), + span -> + span.hasName("MessageCreator.createAsync") + .hasKind(CLIENT) + .hasAttributesSatisfyingExactly( + equalTo( + AttributeKey.stringKey("twilio.type"), + "com.twilio.rest.api.v2010.account.Message"), + equalTo( + AttributeKey.stringKey("twilio.account"), + "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo( + AttributeKey.stringKey("twilio.sid"), + "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + } + + @Test + public void asynchronousError() { + twilioRestClient = mock(TwilioRestClient.class); + when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); + when(twilioRestClient.request(any())) + .thenReturn( + new Response( + new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), + 500)); + + Assertions.assertThrows( + ExecutionException.class, + () -> + testing.runWithSpan( + "test", + () -> { + ListenableFuture future = + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .createAsync(twilioRestClient); + + try { + return future.get(10, TimeUnit.SECONDS); + } finally { + Thread.sleep(1000); + } + })); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("test") + .hasNoParent() + .hasStatus(StatusData.error()) + .hasException(new ApiException("Testing Failure")), + span -> + span.hasName("MessageCreator.createAsync") + .hasKind(CLIENT) + .hasStatus(StatusData.error()) + .hasException(new ApiException("Testing Failure")))); + } + + private static CloseableHttpResponse mockResponse(String body, int statusCode) + throws IOException { + HttpEntity httpEntity = mock(HttpEntity.class); + when(httpEntity.getContent()) + .thenReturn(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8))); + when(httpEntity.isRepeatable()).thenReturn(true); + + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(statusCode); + + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + + return httpResponse; + } +} From 8784c2cccaa1824ed4d831fac9c3fe5c54e46d0b Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Tue, 21 Jan 2025 15:40:01 +0800 Subject: [PATCH 2/7] remove junit assertions --- .../twilio/TwilioClientTest.java | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index 539c7ead1d8a..a6b8c9954798 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.trace.SpanKind.CLIENT; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -41,7 +42,6 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -387,17 +387,17 @@ public void syncFailure() { new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), 500)); - Assertions.assertThrows( - ApiException.class, - () -> - testing.runWithSpan( - "test", - () -> - Message.creator( - new PhoneNumber("+1 555 720 5913"), - new PhoneNumber("+1 555 555 5215"), - "Hello world!") - .create(twilioRestClient))); + assertThatThrownBy( + () -> + testing.runWithSpan( + "test", + () -> + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .create(twilioRestClient))) + .isInstanceOf(ApiException.class); testing.waitAndAssertTraces( trace -> @@ -480,7 +480,7 @@ public void asynchronousCall() throws Exception { } }); - Assertions.assertNotNull(message); + assertThat(message).isNotNull(); assertThat(message.getBody()).isEqualTo("Hello, World!"); testing.waitAndAssertTraces( @@ -513,25 +513,25 @@ public void asynchronousError() { new ByteArrayInputStream(ERROR_RESPONSE_BODY.getBytes(StandardCharsets.UTF_8)), 500)); - Assertions.assertThrows( - ExecutionException.class, - () -> - testing.runWithSpan( - "test", - () -> { - ListenableFuture future = - Message.creator( - new PhoneNumber("+1 555 720 5913"), - new PhoneNumber("+1 555 555 5215"), - "Hello world!") - .createAsync(twilioRestClient); - - try { - return future.get(10, TimeUnit.SECONDS); - } finally { - Thread.sleep(1000); - } - })); + assertThatThrownBy( + () -> + testing.runWithSpan( + "test", + () -> { + ListenableFuture future = + Message.creator( + new PhoneNumber("+1 555 720 5913"), + new PhoneNumber("+1 555 555 5215"), + "Hello world!") + .createAsync(twilioRestClient); + + try { + return future.get(10, TimeUnit.SECONDS); + } finally { + Thread.sleep(1000); + } + })) + .isInstanceOf(ExecutionException.class); testing.waitAndAssertTraces( trace -> From c590597d6fdc0d92f787b1b8a66c533c8f9d9821 Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 22 Jan 2025 14:14:12 +0800 Subject: [PATCH 3/7] Apply suggestions from code review Co-authored-by: Jean Bisutti --- .../twilio/TwilioClientTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index a6b8c9954798..e839a083e214 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -46,7 +46,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -public class TwilioClientTest { +class TwilioClientTest { @RegisterExtension private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); @@ -133,7 +133,7 @@ static void tearDown() { } @Test - public void synchronousMessage() { +void synchronousMessage() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -175,7 +175,7 @@ public void synchronousMessage() { } @Test - public void synchronousCall() throws URISyntaxException { +void synchronousCall() throws URISyntaxException { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -217,7 +217,7 @@ public void synchronousCall() throws URISyntaxException { } @Test - public void httpClient() throws IOException { +void httpClient() throws IOException { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(MESSAGE_RESPONSE_BODY, 200); when(httpClient.execute(any())).thenReturn(response1); @@ -272,7 +272,7 @@ public void httpClient() throws IOException { } @Test - public void httpClientRetry() throws IOException { +void httpClientRetry() throws IOException { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); @@ -321,7 +321,7 @@ public void httpClientRetry() throws IOException { } @Test - public void httpClientRetryAsync() throws Exception { +void httpClientRetryAsync() throws Exception { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); @@ -378,7 +378,7 @@ public void httpClientRetryAsync() throws Exception { } @Test - public void syncFailure() { +void syncFailure() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -415,7 +415,7 @@ public void syncFailure() { } @Test - public void rootSpan() { +void rootSpan() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -453,7 +453,7 @@ public void rootSpan() { } @Test - public void asynchronousCall() throws Exception { +void asynchronousCall() throws Exception { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -504,7 +504,7 @@ public void asynchronousCall() throws Exception { } @Test - public void asynchronousError() { +void asynchronousError() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) From 158f8d497f72710fd96c1bd539f7c88a703aff01 Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 22 Jan 2025 14:15:19 +0800 Subject: [PATCH 4/7] Apply suggestions from code review Co-authored-by: Jay DeLuca --- .../instrumentation/twilio/TwilioClientTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index e839a083e214..e94acda48797 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -157,13 +157,13 @@ void synchronousMessage() { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( AttributeKey.stringKey("twilio.account"), @@ -203,6 +203,7 @@ void synchronousCall() throws URISyntaxException { span -> span.hasName("CallCreator.create") .hasKind(CLIENT) + .hasAttributes(Attributes.empty()) .hasAttributesSatisfyingExactly( equalTo( AttributeKey.stringKey("twilio.type"), @@ -247,7 +248,7 @@ void httpClient() throws IOException { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) @@ -303,7 +304,7 @@ void httpClientRetry() throws IOException { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) @@ -360,7 +361,7 @@ void httpClientRetryAsync() throws Exception { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.createAsync") .hasKind(CLIENT) @@ -486,7 +487,7 @@ void asynchronousCall() throws Exception { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.createAsync") .hasKind(CLIENT) From 77c762466a8d93102f7fa01eb28b09cf66f52fca Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 22 Jan 2025 14:16:57 +0800 Subject: [PATCH 5/7] refine ut --- .../twilio/TwilioClientTest.java | 99 ++++++++----------- 1 file changed, 39 insertions(+), 60 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index e94acda48797..cd88040add9b 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -5,6 +5,7 @@ package io.opentelemetry.javaagent.instrumentation.twilio; +import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.api.trace.SpanKind.CLIENT; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static org.assertj.core.api.Assertions.assertThat; @@ -24,7 +25,7 @@ import com.twilio.rest.api.v2010.account.Call; import com.twilio.rest.api.v2010.account.Message; import com.twilio.type.PhoneNumber; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.sdk.trace.data.StatusData; @@ -133,7 +134,7 @@ static void tearDown() { } @Test -void synchronousMessage() { + void synchronousMessage() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -163,19 +164,16 @@ void synchronousMessage() { .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @Test -void synchronousCall() throws URISyntaxException { + void synchronousCall() throws URISyntaxException { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -206,19 +204,15 @@ void synchronousCall() throws URISyntaxException { .hasAttributes(Attributes.empty()) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), - "com.twilio.rest.api.v2010.account.Call"), - equalTo( - AttributeKey.stringKey("twilio.account"), - "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Call"), equalTo( - AttributeKey.stringKey("twilio.sid"), - "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "completed")))); + stringKey("twilio.account"), "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.sid"), "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "completed")))); } @Test -void httpClient() throws IOException { + void httpClient() throws IOException { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(MESSAGE_RESPONSE_BODY, 200); when(httpClient.execute(any())).thenReturn(response1); @@ -254,15 +248,12 @@ void httpClient() throws IOException { .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @SuppressWarnings("CannotMockMethod") @@ -273,7 +264,7 @@ void httpClient() throws IOException { } @Test -void httpClientRetry() throws IOException { + void httpClientRetry() throws IOException { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); @@ -310,19 +301,16 @@ void httpClientRetry() throws IOException { .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @Test -void httpClientRetryAsync() throws Exception { + void httpClientRetryAsync() throws Exception { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); @@ -367,19 +355,16 @@ void httpClientRetryAsync() throws Exception { .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @Test -void syncFailure() { + void syncFailure() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -416,7 +401,7 @@ void syncFailure() { } @Test -void rootSpan() { + void rootSpan() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -442,19 +427,16 @@ void rootSpan() { .hasNoParent() .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @Test -void asynchronousCall() throws Exception { + void asynchronousCall() throws Exception { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) @@ -493,19 +475,16 @@ void asynchronousCall() throws Exception { .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( - AttributeKey.stringKey("twilio.type"), + stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Message"), equalTo( - AttributeKey.stringKey("twilio.account"), - "AC14984e09e497506cf0d5eb59b1f6ace7"), - equalTo( - AttributeKey.stringKey("twilio.sid"), - "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), - equalTo(AttributeKey.stringKey("twilio.status"), "sent")))); + stringKey("twilio.account"), "AC14984e09e497506cf0d5eb59b1f6ace7"), + equalTo(stringKey("twilio.sid"), "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"), + equalTo(stringKey("twilio.status"), "sent")))); } @Test -void asynchronousError() { + void asynchronousError() { twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) From 3b22c2a2d9258f8cdc3f73f5657c51f30fb0ec6b Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 22 Jan 2025 17:40:14 +0800 Subject: [PATCH 6/7] fix ut --- .../javaagent/instrumentation/twilio/TwilioClientTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index cd88040add9b..d4f9648d5b98 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -197,11 +197,10 @@ void synchronousCall() throws URISyntaxException { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> span.hasName("test").hasNoParent(), + span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("CallCreator.create") .hasKind(CLIENT) - .hasAttributes(Attributes.empty()) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Call"), From f629c707a0c9a39b053523b5cb3357eb2edd28ee Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Fri, 24 Jan 2025 10:53:02 +0800 Subject: [PATCH 7/7] refine ut --- .../twilio/TwilioClientTest.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java index d4f9648d5b98..ff110acff97d 100644 --- a/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java +++ b/instrumentation/twilio-6.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/twilio/TwilioClientTest.java @@ -45,8 +45,12 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) class TwilioClientTest { @RegisterExtension @@ -119,7 +123,9 @@ class TwilioClientTest { + " \"more_info\": \"Testing\"\n" + " }"; - private TwilioRestClient twilioRestClient; + @Mock private TwilioRestClient twilioRestClient; + + @Mock private CloseableHttpClient httpClient; @BeforeAll static void setUp() { @@ -135,7 +141,6 @@ static void tearDown() { @Test void synchronousMessage() { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -162,6 +167,7 @@ void synchronousMessage() { span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), @@ -174,7 +180,6 @@ void synchronousMessage() { @Test void synchronousCall() throws URISyntaxException { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -201,6 +206,7 @@ void synchronousCall() throws URISyntaxException { span -> span.hasName("CallCreator.create") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), "com.twilio.rest.api.v2010.account.Call"), @@ -212,9 +218,8 @@ void synchronousCall() throws URISyntaxException { @Test void httpClient() throws IOException { - CloseableHttpClient httpClient = mock(CloseableHttpClient.class); - CloseableHttpResponse response1 = mockResponse(MESSAGE_RESPONSE_BODY, 200); - when(httpClient.execute(any())).thenReturn(response1); + CloseableHttpResponse response = mockResponse(MESSAGE_RESPONSE_BODY, 200); + when(httpClient.execute(any())).thenReturn(response); HttpClientBuilder clientBuilder = getHttpClientBuilder(httpClient); @@ -245,6 +250,7 @@ void httpClient() throws IOException { span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), @@ -264,7 +270,6 @@ void httpClient() throws IOException { @Test void httpClientRetry() throws IOException { - CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); when(httpClient.execute(any())).thenReturn(response1, response2); @@ -297,6 +302,7 @@ void httpClientRetry() throws IOException { span -> span.hasName("test").hasNoParent().hasAttributes(Attributes.empty()), span -> span.hasName("MessageCreator.create") + .hasParent(trace.getSpan(0)) .hasKind(CLIENT) .hasAttributesSatisfyingExactly( equalTo( @@ -310,7 +316,6 @@ void httpClientRetry() throws IOException { @Test void httpClientRetryAsync() throws Exception { - CloseableHttpClient httpClient = mock(CloseableHttpClient.class); CloseableHttpResponse response1 = mockResponse(ERROR_RESPONSE_BODY, 500); CloseableHttpResponse response2 = mockResponse(MESSAGE_RESPONSE_BODY, 200); when(httpClient.execute(any())).thenReturn(response1, response2); @@ -352,6 +357,7 @@ void httpClientRetryAsync() throws Exception { span -> span.hasName("MessageCreator.createAsync") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), @@ -364,7 +370,6 @@ void httpClientRetryAsync() throws Exception { @Test void syncFailure() { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -395,13 +400,13 @@ void syncFailure() { span -> span.hasName("MessageCreator.create") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasStatus(StatusData.error()) .hasException(new ApiException("Testing Failure")))); } @Test void rootSpan() { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -436,7 +441,6 @@ void rootSpan() { @Test void asynchronousCall() throws Exception { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -472,6 +476,7 @@ void asynchronousCall() throws Exception { span -> span.hasName("MessageCreator.createAsync") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasAttributesSatisfyingExactly( equalTo( stringKey("twilio.type"), @@ -484,7 +489,6 @@ void asynchronousCall() throws Exception { @Test void asynchronousError() { - twilioRestClient = mock(TwilioRestClient.class); when(twilioRestClient.getObjectMapper()).thenReturn(new ObjectMapper()); when(twilioRestClient.request(any())) .thenReturn( @@ -523,6 +527,7 @@ void asynchronousError() { span -> span.hasName("MessageCreator.createAsync") .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) .hasStatus(StatusData.error()) .hasException(new ApiException("Testing Failure")))); }