I am trying to creat a SNS mock to do unit tests, but when I run the test I get the following error: “Topic does not exist”
This is the class I am using to create the mock:
public class SnsResource implements QuarkusTestResourceLifecycleManager {
private static final DockerImageName LOCALSTACK_IMAGE_NAME = DockerImageName
.parse("localstack/localstack").withTag("0.14.3");
private LocalStackContainer container;
@Override
public Map<String, String> start() {
DockerClientFactory.instance().client();
try {
container = new LocalStackContainer(LOCALSTACK_IMAGE_NAME).withServices(LocalStackContainer.Service.SNS);
container.start();
URI endpointOverride = container.getEndpointOverride(LocalStackContainer.Service.SNS);
SnsClient client = createClient(endpointOverride);
String topicName = "notify-email-received";
createSnsTopic(client, topicName);
return generatePropertiesMapToOverride(endpointOverride);
} catch (Exception e) {
throw new RuntimeException("Could not start Sns localstack server", e);
}
}
@NotNull
private Map<String, String> generatePropertiesMapToOverride(final URI endpointOverride) {
Map<String, String> properties = new HashMap<>();
properties.put("quarkus.sns.endpoint-override", endpointOverride.toString());
properties.put("quarkus.sns.aws.region", "us-east-1");
properties.put("quarkus.sns.aws.credentials.type", "static");
properties.put("quarkus.sns.aws.credentials.static-provider.access-key-id", "accessKey");
properties.put("quarkus.sns.aws.credentials.static-provider.secret-access-key", "secretKey");
return properties;
}
private SnsClient createClient(final URI endpointOverride) {
return SnsClient.builder()
.endpointOverride(endpointOverride)
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create("accesskey", "secretKey")))
.httpClientBuilder(UrlConnectionHttpClient.builder())
.region(Region.US_EAST_1)
.build();
}
private void createSnsTopic(final SnsClient snsClient, final String topicName) {
try {
snsClient.createTopic(topicRequest -> topicRequest.name(topicName));
System.out.println("SNS Topic Created: " + topicName);
} catch (Exception e) {
throw new RuntimeException("Error creating SNS Topic: " + topicName, e);
}
}
@Override
public void stop() {
if (container != null) {
container.stop();
}
}
}