|
| 1 | +package io.smallrye.reactive.messaging.providers.connectors; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.Properties; |
| 6 | + |
| 7 | +import jakarta.enterprise.event.Observes; |
| 8 | +import jakarta.enterprise.inject.Any; |
| 9 | +import jakarta.enterprise.inject.Default; |
| 10 | +import jakarta.enterprise.inject.spi.AfterBeanDiscovery; |
| 11 | +import jakarta.enterprise.inject.spi.BeanManager; |
| 12 | +import jakarta.enterprise.inject.spi.Extension; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import io.smallrye.common.annotation.Identifier; |
| 17 | +import io.smallrye.reactive.messaging.WeldTestBaseWithoutTails; |
| 18 | +import io.vertx.mutiny.core.Vertx; |
| 19 | + |
| 20 | +public class VertxInstanceSelectTest extends WeldTestBaseWithoutTails { |
| 21 | + |
| 22 | + private static final Properties CONNECTOR_VERTX_CDI_IDENTIFIER_PROPERTIES = new Properties(); |
| 23 | + static { |
| 24 | + CONNECTOR_VERTX_CDI_IDENTIFIER_PROPERTIES.setProperty("mp.messaging.connector.vertx.cdi.identifier", "vertx"); |
| 25 | + } |
| 26 | + |
| 27 | + private static final Vertx vertxForCDI = Vertx.vertx(); |
| 28 | + |
| 29 | + // Test the case that 'mp.messaging.connector.vertx.cdi.identifier' is NOT specified and NO Vertx Bean exposed |
| 30 | + // from CDI container. The vertx instance should be internal instance. |
| 31 | + @Test |
| 32 | + public void testNoVertxBeanNoIdentifier() { |
| 33 | + initialize(); |
| 34 | + ExecutionHolder executionHolder = get(ExecutionHolder.class); |
| 35 | + assertThat(executionHolder).isNotNull(); |
| 36 | + assertThat(executionHolder.vertx()).isNotNull(); |
| 37 | + assertThat(executionHolder.vertx()).isNotEqualTo(vertxForCDI); |
| 38 | + assertThat(executionHolder.isInternalVertxInstance()).isTrue(); |
| 39 | + } |
| 40 | + |
| 41 | + // Test the case that 'mp.messaging.connector.vertx.cdi.identifier' IS specified and NO Vertx Bean exposed |
| 42 | + // from CDI container. The vertx instance should be internal instance. |
| 43 | + @Test |
| 44 | + public void testNoVertxBeanWithIdentifier() { |
| 45 | + installConfigFromProperties(CONNECTOR_VERTX_CDI_IDENTIFIER_PROPERTIES); |
| 46 | + initialize(); |
| 47 | + ExecutionHolder executionHolder = get(ExecutionHolder.class); |
| 48 | + assertThat(executionHolder).isNotNull(); |
| 49 | + assertThat(executionHolder.vertx()).isNotNull(); |
| 50 | + assertThat(executionHolder.vertx()).isNotEqualTo(vertxForCDI); |
| 51 | + assertThat(executionHolder.isInternalVertxInstance()).isTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + // Test the case that 'mp.messaging.connector.vertx.cdi.identifier' is NOT specified but has Vertx Bean exposed |
| 55 | + // from CDI container using Default qualifier. The vertx instance should be vertxForCDI. |
| 56 | + @Test |
| 57 | + public void testWithVertxBeanDefaultNoIdentifier() { |
| 58 | + addExtensionClass(VertxCDIDefaultExtension.class); |
| 59 | + initialize(); |
| 60 | + ExecutionHolder executionHolder = get(ExecutionHolder.class); |
| 61 | + assertThat(executionHolder).isNotNull(); |
| 62 | + assertThat(executionHolder.vertx()).isNotNull(); |
| 63 | + assertThat(executionHolder.vertx()).isEqualTo(vertxForCDI); |
| 64 | + assertThat(executionHolder.isInternalVertxInstance()).isFalse(); |
| 65 | + } |
| 66 | + |
| 67 | + // Test the case that 'mp.messaging.connector.vertx.cdi.identifier' IS specified but has Vertx Bean exposed |
| 68 | + // from CDI container using Default qualifier. The vertx instance should be internal instance. |
| 69 | + @Test |
| 70 | + public void testWithVertxBeanDefaultWithIdentifier() { |
| 71 | + installConfigFromProperties(CONNECTOR_VERTX_CDI_IDENTIFIER_PROPERTIES); |
| 72 | + addExtensionClass(VertxCDIDefaultExtension.class); |
| 73 | + initialize(); |
| 74 | + ExecutionHolder executionHolder = get(ExecutionHolder.class); |
| 75 | + assertThat(executionHolder).isNotNull(); |
| 76 | + assertThat(executionHolder.vertx()).isNotNull(); |
| 77 | + assertThat(executionHolder.vertx()).isNotEqualTo(vertxForCDI); |
| 78 | + assertThat(executionHolder.isInternalVertxInstance()).isTrue(); |
| 79 | + } |
| 80 | + |
| 81 | + // Test the case that 'mp.messaging.connector.vertx.cdi.identifier' is specified and has Vertx Bean exposed |
| 82 | + // from CDI container using the Identifier qualifier. The vertx instance should be vertxForCDI. |
| 83 | + @Test |
| 84 | + public void testWithVertxBeanIdentifierWithIdentifier() { |
| 85 | + installConfigFromProperties(CONNECTOR_VERTX_CDI_IDENTIFIER_PROPERTIES); |
| 86 | + addExtensionClass(VertxCDIIdentifierExtension.class); |
| 87 | + initialize(); |
| 88 | + ExecutionHolder executionHolder = get(ExecutionHolder.class); |
| 89 | + assertThat(executionHolder).isNotNull(); |
| 90 | + assertThat(executionHolder.vertx()).isNotNull(); |
| 91 | + assertThat(executionHolder.vertx()).isEqualTo(vertxForCDI); |
| 92 | + assertThat(executionHolder.isInternalVertxInstance()).isFalse(); |
| 93 | + } |
| 94 | + |
| 95 | + public static class VertxCDIDefaultExtension implements Extension { |
| 96 | + public void registerVertxBean(@Observes AfterBeanDiscovery abd, BeanManager beanManager) { |
| 97 | + abd.addBean() |
| 98 | + .beanClass(Vertx.class).types(Vertx.class) |
| 99 | + .addQualifiers(Any.Literal.INSTANCE, Default.Literal.INSTANCE) |
| 100 | + .createWith(cc -> vertxForCDI); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static class VertxCDIIdentifierExtension implements Extension { |
| 105 | + public void registerVertxBean(@Observes AfterBeanDiscovery abd, BeanManager beanManager) { |
| 106 | + abd.addBean() |
| 107 | + .beanClass(Vertx.class).types(Vertx.class) |
| 108 | + .addQualifiers(Any.Literal.INSTANCE, Identifier.Literal.of("vertx")) |
| 109 | + .createWith(cc -> vertxForCDI); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments