Skip to content

Commit

Permalink
Properly handle @MongoClientName qualifier
Browse files Browse the repository at this point in the history
Fixes: #11491
  • Loading branch information
geoand committed Aug 25, 2020
1 parent dffe5dd commit 96a019d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.processor.BuildExtension;
Expand Down Expand Up @@ -59,11 +58,6 @@ public class MongoClientProcessor {
private static final DotName MONGO_CLIENT = DotName.createSimple(MongoClient.class.getName());
private static final DotName REACTIVE_MONGO_CLIENT = DotName.createSimple(ReactiveMongoClient.class.getName());

@BuildStep
BeanDefiningAnnotationBuildItem registerConnectionBean() {
return new BeanDefiningAnnotationBuildItem(MONGO_CLIENT_ANNOTATION);
}

@BuildStep
CodecProviderBuildItem collectCodecProviders(CombinedIndexBuildItem indexBuildItem) {
Collection<ClassInfo> codecProviderClasses = indexBuildItem.getIndex()
Expand Down Expand Up @@ -144,6 +138,9 @@ void build(
BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer,
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {

// add the @MongoClientName class otherwise it won't registered as a qualifier
additionalBeans.produce(AdditionalBeanBuildItem.builder().addBeanClass(MongoClientName.class).build());

List<ConnectionPoolListener> poolListenerList = connectionPoolListenerProvider.stream()
.map(MongoConnectionPoolListenerBuildItem::getConnectionPoolListener)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import com.mongodb.client.MongoClient;
import com.mongodb.client.internal.MongoClientImpl;

import io.quarkus.arc.Arc;
import io.quarkus.mongodb.runtime.MongoClientName;
Expand Down Expand Up @@ -44,6 +45,9 @@ void cleanup() {

@Test
public void testNamedDataSourceInjection() {
assertProperConnection(client, 27018);
assertProperConnection(client2, 27019);

assertThat(client.listDatabases().first()).isNotEmpty();
assertThat(client2.listDatabases().first()).isNotEmpty();

Expand All @@ -52,4 +56,12 @@ public void testNamedDataSourceInjection() {
assertThat(Arc.container().instance(MongoClient.class, NamedLiteral.of("cluster2")).get()).isNotNull();
assertThat(Arc.container().instance(MongoClient.class, NamedLiteral.of("cluster3")).get()).isNull();
}

private void assertProperConnection(MongoClient client, int expectedPort) {
assertThat(client).isInstanceOfSatisfying(MongoClientImpl.class, c -> {
assertThat(c.getCluster().getSettings().getHosts()).hasOnlyOneElementSatisfying(sa -> {
assertThat(sa.getPort()).isEqualTo(expectedPort);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.enterprise.inject.Default;
import javax.inject.Inject;
Expand All @@ -14,9 +15,13 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.internal.MongoClientImpl;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.mongodb.impl.ReactiveMongoClientImpl;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.mongodb.runtime.MongoClientName;
import io.quarkus.test.QuarkusUnitTest;
Expand Down Expand Up @@ -48,12 +53,36 @@ void cleanup() {

@Test
public void testNamedDataSourceInjection() {
assertProperConnection(client, 27018);
assertProperConnection(client2, 27019);

assertThat(client.listDatabases().collectItems().first().await().indefinitely()).isNotEmpty();
assertThat(client2.listDatabases().collectItems().first().await().indefinitely()).isNotEmpty();

assertNoDefaultClient();
}

private void assertProperConnection(ReactiveMongoClient client, int expectedPort) {
assertThat(client).isInstanceOfSatisfying(ReactiveMongoClientImpl.class, rc -> {
Field mongoClientField;
try {
mongoClientField = ReactiveMongoClientImpl.class.getDeclaredField("client");
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
mongoClientField.setAccessible(true);
MongoClient c;
try {
c = (MongoClientImpl) mongoClientField.get(rc);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
assertThat(c.getClusterDescription().getClusterSettings().getHosts()).hasOnlyOneElementSatisfying(sa -> {
assertThat(sa.getPort()).isEqualTo(expectedPort);
});
});
}

private void assertNoDefaultClient() {
boolean hasDefault = false;
for (InstanceHandle<ReactiveMongoClient> handle : Arc.container().select(ReactiveMongoClient.class).handles()) {
Expand Down

0 comments on commit 96a019d

Please sign in to comment.