forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use proper package for @MongoClientName
For now we just deprecate the one annotation and support both Relates to: quarkusio#11568 (comment)
- Loading branch information
Showing
9 changed files
with
238 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ient/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientNameBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...-client/deployment/src/test/java/io/quarkus/mongodb/LegacyNamedMongoClientConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.quarkus.mongodb; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import javax.enterprise.inject.Default; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.mongodb.client.MongoClient; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.InjectableBean; | ||
import io.quarkus.arc.InstanceHandle; | ||
import io.quarkus.mongodb.runtime.MongoClientName; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class LegacyNamedMongoClientConfigTest extends MongoWithReplicasTestBase { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(MongoTestBase.class)) | ||
.withConfigurationResource("application-named-mongoclient.properties"); | ||
|
||
@Inject | ||
@MongoClientName("cluster1") | ||
MongoClient client; | ||
|
||
@Inject | ||
@MongoClientName("cluster2") | ||
MongoClient client2; | ||
|
||
@AfterEach | ||
void cleanup() { | ||
if (client != null) { | ||
client.close(); | ||
} | ||
if (client2 != null) { | ||
client2.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNamedDataSourceInjection() { | ||
assertThat(client.listDatabases().first()).isNotEmpty(); | ||
assertThat(client2.listDatabases().first()).isNotEmpty(); | ||
|
||
assertNoDefaultClient(); | ||
} | ||
|
||
private void assertNoDefaultClient() { | ||
boolean hasDefault = false; | ||
for (InstanceHandle<MongoClient> handle : Arc.container().select(MongoClient.class).handles()) { | ||
InjectableBean<MongoClient> bean = handle.getBean(); | ||
for (Annotation qualifier : bean.getQualifiers()) { | ||
if (qualifier.annotationType().equals(Default.class)) { | ||
hasDefault = true; | ||
} | ||
} | ||
} | ||
Assertions.assertFalse(hasDefault, | ||
"The default mongo client should not have been present as it is not used in any injection point"); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...deployment/src/test/java/io/quarkus/mongodb/LegacyNamedReactiveMongoClientConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.quarkus.mongodb; | ||
|
||
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; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
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; | ||
|
||
public class LegacyNamedReactiveMongoClientConfigTest extends MongoWithReplicasTestBase { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(MongoTestBase.class)) | ||
.withConfigurationResource("application-named-mongoclient.properties"); | ||
|
||
@Inject | ||
@MongoClientName("cluster1") | ||
ReactiveMongoClient client; | ||
|
||
@Inject | ||
@MongoClientName("cluster2") | ||
ReactiveMongoClient client2; | ||
|
||
@AfterEach | ||
void cleanup() { | ||
if (client != null) { | ||
client.close(); | ||
} | ||
if (client2 != null) { | ||
client2.close(); | ||
} | ||
} | ||
|
||
@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()) { | ||
InjectableBean<ReactiveMongoClient> bean = handle.getBean(); | ||
for (Annotation qualifier : bean.getQualifiers()) { | ||
if (qualifier.annotationType().equals(Default.class)) { | ||
hasDefault = true; | ||
} | ||
} | ||
} | ||
Assertions.assertFalse(hasDefault, | ||
"The default reactive mongo client should not have been present as it is not used in any injection point"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/MongoClientName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.quarkus.mongodb; | ||
|
||
import javax.inject.Qualifier; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Marker annotation to select mongo connection of cluster configuration | ||
* Use name parameter to select it | ||
* | ||
* For example, if a mongo connection is configured like so in {@code application.properties}: | ||
* | ||
* <pre> | ||
* quarkus.mongodb.cluster1.connection-string=mongodb://localhost:27018 | ||
* </pre> | ||
* | ||
* Then to inject the proper {@code MongoClient}, you would need to use {@code MongoClientName} like so: | ||
* | ||
* <pre> | ||
* @Inject | ||
* @MongoClientName("cluster1") | ||
* MongoClient client; | ||
* </pre> | ||
*/ | ||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Qualifier | ||
public @interface MongoClientName { | ||
/** | ||
* Specify the cluster name of the connection. | ||
* | ||
* @return the value | ||
*/ | ||
String value() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters