|
| 1 | +package com.azure.spring.sample.servicebus.messaging; |
| 2 | + |
| 3 | + |
| 4 | +import com.azure.messaging.servicebus.ServiceBusMessage; |
| 5 | +import com.azure.messaging.servicebus.ServiceBusSenderClient; |
| 6 | +import com.azure.spring.cloud.autoconfigure.implementation.context.AzureGlobalPropertiesAutoConfiguration; |
| 7 | +import com.azure.spring.cloud.autoconfigure.implementation.servicebus.AzureServiceBusAutoConfiguration; |
| 8 | +import com.azure.spring.cloud.autoconfigure.implementation.servicebus.AzureServiceBusMessagingAutoConfiguration; |
| 9 | +import com.azure.spring.cloud.autoconfigure.implementation.servicebus.properties.AzureServiceBusConnectionDetails; |
| 10 | +import com.azure.spring.cloud.service.servicebus.consumer.ServiceBusErrorHandler; |
| 11 | +import com.azure.spring.cloud.service.servicebus.consumer.ServiceBusRecordMessageListener; |
| 12 | +import com.azure.spring.messaging.servicebus.core.ServiceBusTemplate; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.Configuration; |
| 19 | +import org.springframework.messaging.support.MessageBuilder; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.awaitility.Awaitility.waitAtMost; |
| 27 | + |
| 28 | +@SpringBootTest(properties = { |
| 29 | + "spring.docker.compose.skip.in-tests=false", |
| 30 | + "spring.docker.compose.file=classpath:servicebus-compose.yaml", |
| 31 | + "spring.docker.compose.stop.command=down", |
| 32 | + "spring.docker.compose.readiness.timeout=PT5M", |
| 33 | + "spring.cloud.azure.servicebus.namespace=sbemulatorns", |
| 34 | + "spring.cloud.azure.servicebus.entity-name=queue.1", |
| 35 | + "spring.cloud.azure.servicebus.entity-type=queue", |
| 36 | + "spring.cloud.azure.servicebus.producer.entity-name=queue.1", |
| 37 | + "spring.cloud.azure.servicebus.producer.entity-type=queue", |
| 38 | + "spring.cloud.azure.servicebus.processor.entity-name=queue.1", |
| 39 | + "spring.cloud.azure.servicebus.processor.entity-type=queue" |
| 40 | +}) |
| 41 | +class ServiceBusDockerComposeTest { |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private AzureServiceBusConnectionDetails connectionDetails; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private ServiceBusSenderClient senderClient; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private ServiceBusTemplate serviceBusTemplate; |
| 51 | + |
| 52 | + @Test |
| 53 | + void connectionDetailsShouldBeProvidedByFactory() { |
| 54 | + assertThat(connectionDetails).isNotNull(); |
| 55 | + assertThat(connectionDetails.getConnectionString()) |
| 56 | + .isNotBlank() |
| 57 | + .startsWith("Endpoint=sb://"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void senderClientCanSendMessage() { |
| 62 | + // Wait for Service Bus emulator to be fully ready and queue entity to be available |
| 63 | + // The emulator depends on SQL Edge and needs time to initialize the messaging entities |
| 64 | + waitAtMost(Duration.ofSeconds(120)).pollInterval(Duration.ofSeconds(2)).untilAsserted(() -> { |
| 65 | + this.senderClient.sendMessage(new ServiceBusMessage("Hello World!")); |
| 66 | + }); |
| 67 | + |
| 68 | + waitAtMost(Duration.ofSeconds(30)).pollDelay(Duration.ofSeconds(5)).untilAsserted(() -> { |
| 69 | + assertThat(Config.MESSAGES).contains("Hello World!"); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void serviceBusTemplateCanSendMessage() { |
| 75 | + // Wait for Service Bus emulator to be fully ready and queue entity to be available |
| 76 | + // The emulator depends on SQL Edge and needs time to initialize the messaging entities |
| 77 | + waitAtMost(Duration.ofSeconds(120)).pollInterval(Duration.ofSeconds(2)).untilAsserted(() -> { |
| 78 | + this.serviceBusTemplate.sendAsync("queue.1", MessageBuilder.withPayload("Hello from ServiceBusTemplate!").build()).block(); |
| 79 | + }); |
| 80 | + |
| 81 | + waitAtMost(Duration.ofSeconds(30)).pollDelay(Duration.ofSeconds(5)).untilAsserted(() -> { |
| 82 | + assertThat(Config.MESSAGES).contains("Hello from ServiceBusTemplate!"); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Configuration(proxyBeanMethods = false) |
| 87 | + @ImportAutoConfiguration(classes = { |
| 88 | + AzureGlobalPropertiesAutoConfiguration.class, |
| 89 | + AzureServiceBusAutoConfiguration.class, |
| 90 | + AzureServiceBusMessagingAutoConfiguration.class}) |
| 91 | + static class Config { |
| 92 | + |
| 93 | + private static final Set<String> MESSAGES = ConcurrentHashMap.newKeySet(); |
| 94 | + |
| 95 | + @Bean |
| 96 | + ServiceBusRecordMessageListener processMessage() { |
| 97 | + return context -> { |
| 98 | + MESSAGES.add(context.getMessage().getBody().toString()); |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + ServiceBusErrorHandler errorHandler() { |
| 104 | + // No-op error handler for tests: acknowledge errors without affecting test execution. |
| 105 | + return (context) -> { |
| 106 | + }; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments