1515import static io .opentelemetry .semconv .UrlAttributes .URL_FULL ;
1616import static io .opentelemetry .semconv .incubating .AwsIncubatingAttributes .AWS_REQUEST_ID ;
1717import static io .opentelemetry .semconv .incubating .AwsIncubatingAttributes .AWS_SECRETSMANAGER_SECRET_ARN ;
18+ import static io .opentelemetry .semconv .incubating .AwsIncubatingAttributes .AWS_SNS_TOPIC_ARN ;
1819import static io .opentelemetry .semconv .incubating .AwsIncubatingAttributes .AWS_STEP_FUNCTIONS_ACTIVITY_ARN ;
1920import static io .opentelemetry .semconv .incubating .AwsIncubatingAttributes .AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN ;
2021import static io .opentelemetry .semconv .incubating .MessagingIncubatingAttributes .MESSAGING_DESTINATION_NAME ;
9899import software .amazon .awssdk .services .sns .SnsAsyncClientBuilder ;
99100import software .amazon .awssdk .services .sns .SnsClient ;
100101import software .amazon .awssdk .services .sns .SnsClientBuilder ;
102+ import software .amazon .awssdk .services .sns .model .CreateTopicRequest ;
103+ import software .amazon .awssdk .services .sns .model .CreateTopicResponse ;
101104import software .amazon .awssdk .services .sns .model .PublishRequest ;
105+ import software .amazon .awssdk .services .sns .model .PublishResponse ;
106+ import software .amazon .awssdk .services .sns .model .SubscribeRequest ;
107+ import software .amazon .awssdk .services .sns .model .SubscribeResponse ;
102108import software .amazon .awssdk .services .sqs .SqsAsyncClient ;
103109import software .amazon .awssdk .services .sqs .SqsAsyncClientBuilder ;
104110import software .amazon .awssdk .services .sqs .SqsClient ;
@@ -139,6 +145,36 @@ public abstract class AbstractAws2ClientTest extends AbstractAws2ClientCoreTest
139145 + " \" CreatedDate\" : \" 1.523477145713E9\" "
140146 + "}" ;
141147
148+ private static final String snsPublishResponseBody =
149+ "<PublishResponse xmlns=\" https://sns.amazonaws.com/doc/2010-03-31/\" >"
150+ + " <PublishResult>"
151+ + " <MessageId>567910cd-659e-55d4-8ccb-5aaf14679dc0</MessageId>"
152+ + " </PublishResult>"
153+ + " <ResponseMetadata>"
154+ + " <RequestId>d74b8436-ae13-5ab4-a9ff-ce54dfea72a0</RequestId>"
155+ + " </ResponseMetadata>"
156+ + "</PublishResponse>" ;
157+
158+ private static final String snsSubscribeResponseBody =
159+ "<SubscribeResponse xmlns=\" https://sns.amazonaws.com/doc/2010-03-31/\" >"
160+ + " <SubscribeResult>"
161+ + " <SubscriptionArn>arn:aws:sns:us-west-2:123456789012:MyTopic:abc123</SubscriptionArn>"
162+ + " </SubscribeResult>"
163+ + " <ResponseMetadata>"
164+ + " <RequestId>0ac9cda2-abcd-11d3-f92b-31fa5e8dbc67</RequestId>"
165+ + " </ResponseMetadata>"
166+ + " </SubscribeResponse>" ;
167+
168+ private static final String snsCreateTopicResponseBody =
169+ "<CreateTopicResponse xmlns=\" https://sns.amazonaws.com/doc/2010-03-31/\" >"
170+ + " <CreateTopicResult>"
171+ + " <TopicArn>arn:aws:sns:us-east-1:123456789012:sns-topic-name-foo</TopicArn>"
172+ + " </CreateTopicResult>"
173+ + " <ResponseMetadata>"
174+ + " <RequestId>d74b8436-ae13-5ab4-a9ff-ce54dfea72a0</RequestId>"
175+ + " </ResponseMetadata>"
176+ + "</CreateTopicResponse>" ;
177+
142178 private static void assumeSupportedConfig (String operation ) {
143179 Assumptions .assumeFalse (
144180 operation .equals ("SendMessage" ) && isSqsAttributeInjectionEnabled (),
@@ -223,7 +259,22 @@ private void clientAssertions(
223259 }
224260
225261 if (service .equals ("Sns" )) {
226- attributes .add (equalTo (MESSAGING_DESTINATION_NAME , "somearn" ));
262+ switch (operation ) {
263+ case "CreateTopic" :
264+ attributes .add (
265+ equalTo (AWS_SNS_TOPIC_ARN , "arn:aws:sns:us-east-1:123456789012:sns-topic-name-foo" ));
266+ break ;
267+ case "Publish" :
268+ attributes .add (equalTo (MESSAGING_DESTINATION_NAME , "sns-target-arn" ));
269+ break ;
270+ case "Subscribe" :
271+ attributes .add (equalTo (MESSAGING_DESTINATION_NAME , "sns-topic-arn" ));
272+ attributes .add (equalTo (AWS_SNS_TOPIC_ARN , "sns-topic-arn" ));
273+ break ;
274+ default :
275+ attributes .add (equalTo (AWS_SNS_TOPIC_ARN , "Bug-Unknown-Operation-ARN" ));
276+ break ;
277+ }
227278 }
228279
229280 if (service .equals ("Sqs" ) && operation .equals ("CreateQueue" )) {
@@ -516,22 +567,43 @@ private static Stream<Arguments> provideSnsArguments() {
516567 c ->
517568 c .publish (
518569 PublishRequest .builder ()
519- .message ("somemessage" )
520- .topicArn ("somearn" )
521- .build ())),
570+ .message ("sns-msg-foo" )
571+ .targetArn ("sns-target-arn" )
572+ .build ()),
573+ "Publish" ,
574+ "POST" ,
575+ snsPublishResponseBody ,
576+ "d74b8436-ae13-5ab4-a9ff-ce54dfea72a0" ),
522577 Arguments .of (
523578 (Function <SnsClient , Object >)
524579 c ->
525- c .publish (
526- PublishRequest .builder ()
527- .message ("somemessage" )
528- .targetArn ("somearn" )
529- .build ())));
580+ c .subscribe (
581+ SubscribeRequest .builder ()
582+ .topicArn ("sns-topic-arn" )
583+ .protocol ("email" )
584+ 585+ .build ()),
586+ "Subscribe" ,
587+ "POST" ,
588+ snsSubscribeResponseBody ,
589+ "0ac9cda2-abcd-11d3-f92b-31fa5e8dbc67" ),
590+ Arguments .of (
591+ (Function <SnsClient , Object >)
592+ c -> c .createTopic (CreateTopicRequest .builder ().name ("sns-topic-name-foo" ).build ()),
593+ "CreateTopic" ,
594+ "POST" ,
595+ snsCreateTopicResponseBody ,
596+ "d74b8436-ae13-5ab4-a9ff-ce54dfea72a0" ));
530597 }
531598
532599 @ ParameterizedTest
533600 @ MethodSource ("provideSnsArguments" )
534- void testSnsSendOperationRequestWithBuilder (Function <SnsClient , Object > call ) {
601+ void testSnsSendOperationRequestWithBuilder (
602+ Function <SnsClient , Object > call ,
603+ String operation ,
604+ String method ,
605+ String responseBody ,
606+ String requestId ) {
535607 SnsClientBuilder builder = SnsClient .builder ();
536608 configureSdkClient (builder );
537609 SnsClient client =
@@ -541,28 +613,25 @@ void testSnsSendOperationRequestWithBuilder(Function<SnsClient, Object> call) {
541613 .credentialsProvider (CREDENTIALS_PROVIDER )
542614 .build ();
543615
544- String body =
545- "<PublishResponse xmlns=\" https://sns.amazonaws.com/doc/2010-03-31/\" >"
546- + " <PublishResult>"
547- + " <MessageId>567910cd-659e-55d4-8ccb-5aaf14679dc0</MessageId>"
548- + " </PublishResult>"
549- + " <ResponseMetadata>"
550- + " <RequestId>d74b8436-ae13-5ab4-a9ff-ce54dfea72a0</RequestId>"
551- + " </ResponseMetadata>"
552- + "</PublishResponse>" ;
553-
554- server .enqueue (HttpResponse .of (HttpStatus .OK , MediaType .PLAIN_TEXT_UTF_8 , body ));
616+ server .enqueue (HttpResponse .of (HttpStatus .OK , MediaType .PLAIN_TEXT_UTF_8 , responseBody ));
555617 Object response = call .apply (client );
556618
557619 assertThat (response .getClass ().getSimpleName ())
558620 .satisfiesAnyOf (
559- v -> assertThat (v ).startsWith ("Publish" ),
560- v -> assertThat (response ).isInstanceOf (ResponseInputStream .class ));
561- clientAssertions ("Sns" , "Publish" , "POST" , response , "d74b8436-ae13-5ab4-a9ff-ce54dfea72a0" );
621+ v -> assertThat (response ).isInstanceOf (CreateTopicResponse .class ),
622+ v -> assertThat (response ).isInstanceOf (PublishResponse .class ),
623+ v -> assertThat (response ).isInstanceOf (SubscribeResponse .class ));
624+ clientAssertions ("Sns" , operation , method , response , requestId );
562625 }
563626
564- @ Test
565- void testSnsAsyncSendOperationRequestWithBuilder () {
627+ @ ParameterizedTest
628+ @ MethodSource ("provideSnsArguments" )
629+ void testSnsAsyncSendOperationRequestWithBuilder (
630+ Function <SnsClient , Object > call ,
631+ String operation ,
632+ String method ,
633+ String responseBody ,
634+ String requestId ) {
566635 SnsAsyncClientBuilder builder = SnsAsyncClient .builder ();
567636 configureSdkClient (builder );
568637 SnsAsyncClient client =
@@ -572,20 +641,15 @@ void testSnsAsyncSendOperationRequestWithBuilder() {
572641 .credentialsProvider (CREDENTIALS_PROVIDER )
573642 .build ();
574643
575- String body =
576- "<PublishResponse xmlns=\" https://sns.amazonaws.com/doc/2010-03-31/\" >"
577- + " <PublishResult>"
578- + " <MessageId>94f20ce6-13c5-43a0-9a9e-ca52d816e90b</MessageId>"
579- + " </PublishResult>"
580- + " <ResponseMetadata>"
581- + " <RequestId>f187a3c1-376f-11df-8963-01868b7c937a</RequestId>"
582- + " </ResponseMetadata>"
583- + "</PublishResponse>" ;
584-
585- server .enqueue (HttpResponse .of (HttpStatus .OK , MediaType .PLAIN_TEXT_UTF_8 , body ));
586- Object response = client .publish (r -> r .message ("hello" ).topicArn ("somearn" ));
644+ server .enqueue (HttpResponse .of (HttpStatus .OK , MediaType .PLAIN_TEXT_UTF_8 , responseBody ));
645+ Object response = call .apply (wrapClient (SnsClient .class , SnsAsyncClient .class , client ));
587646
588- clientAssertions ("Sns" , "Publish" , "POST" , response , "f187a3c1-376f-11df-8963-01868b7c937a" );
647+ assertThat (response .getClass ().getSimpleName ())
648+ .satisfiesAnyOf (
649+ v -> assertThat (response ).isInstanceOf (CreateTopicResponse .class ),
650+ v -> assertThat (response ).isInstanceOf (PublishResponse .class ),
651+ v -> assertThat (response ).isInstanceOf (SubscribeResponse .class ));
652+ clientAssertions ("Sns" , operation , method , response , requestId );
589653 }
590654
591655 @ Test
0 commit comments