Skip to content

Commit 6ee5647

Browse files
authored
Resolve SNS topic ARN by full topic name (#756)
1 parent d7a63f0 commit 6ee5647

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

spring-cloud-aws-sns/src/main/java/io/awspring/cloud/sns/core/TopicsListingTopicArnResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private Arn doRecursiveCall(@Nullable String token, String topicName) {
7171

7272
private Arn checkIfArnIsInList(String topicName, ListTopicsResponse listTopicsResponse) {
7373
Optional<String> arn = listTopicsResponse.topics().stream().map(Topic::topicArn)
74-
.filter(ta -> ta.contains(topicName)).findFirst();
74+
.filter(ta -> ta.endsWith(":" + topicName)).findFirst();
7575
if (arn.isPresent()) {
7676
return Arn.fromString(arn.get());
7777
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.sns.core;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.BDDMockito.given;
20+
import static org.mockito.Mockito.mock;
21+
22+
import java.util.Arrays;
23+
import java.util.List;
24+
import org.junit.jupiter.api.Test;
25+
import software.amazon.awssdk.arns.Arn;
26+
import software.amazon.awssdk.services.sns.SnsClient;
27+
import software.amazon.awssdk.services.sns.model.ListTopicsResponse;
28+
import software.amazon.awssdk.services.sns.model.Topic;
29+
30+
class TopicsListingTopicArnResolverTest {
31+
private final SnsClient snsClient = mock(SnsClient.class);
32+
33+
private final TopicsListingTopicArnResolver resolver = new TopicsListingTopicArnResolver(snsClient);
34+
35+
/**
36+
* SNS topic ARN should be resolved by full topic name rather by just a substring. i.e. "topic1" should not be
37+
* resolved to arn:aws:sns:eu-west-1:123456789012:topic11 but rather to arn:aws:sns:eu-west-1:123456789012:topic1
38+
*/
39+
@Test
40+
void shouldResolveArnBasedOnTopicName() {
41+
given(snsClient.listTopics()).willReturn(aResponseContainingArnsForTopicNames("topic11", "topic1"));
42+
43+
Arn arn = resolver.resolveTopicArn("topic1");
44+
45+
assertThat(arn.toString()).isEqualTo("arn:aws:sns:eu-west-1:123456789012:topic1");
46+
}
47+
48+
private ListTopicsResponse aResponseContainingArnsForTopicNames(String... topicNames) {
49+
return ListTopicsResponse.builder().topics(stubTopics(topicNames)).build();
50+
}
51+
52+
private List<Topic> stubTopics(String... topicNames) {
53+
return Arrays.stream(topicNames).map(this::createTopic).toList();
54+
}
55+
56+
private Topic createTopic(String name) {
57+
return Topic.builder().topicArn("arn:aws:sns:eu-west-1:123456789012:" + name).build();
58+
}
59+
}

0 commit comments

Comments
 (0)