|
| 1 | +package io.quarkus.mongodb.panache; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | + |
| 7 | +import jakarta.enterprise.context.ApplicationScoped; |
| 8 | +import jakarta.inject.Inject; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 12 | + |
| 13 | +import com.mongodb.client.MongoClient; |
| 14 | + |
| 15 | +import io.quarkus.mongodb.MongoClientName; |
| 16 | +import io.quarkus.mongodb.panache.common.MongoEntity; |
| 17 | +import io.quarkus.mongodb.reactive.ReactiveMongoClient; |
| 18 | +import io.quarkus.test.QuarkusUnitTest; |
| 19 | + |
| 20 | +public class MultipleMongoClientsTest { |
| 21 | + @RegisterExtension |
| 22 | + static QuarkusUnitTest runner = new QuarkusUnitTest() |
| 23 | + .overrideConfigKey("quarkus.mongodb.devservices.enabled", "false") |
| 24 | + .withApplicationRoot((jar) -> jar |
| 25 | + .addAsResource("named-clients.properties")); |
| 26 | + |
| 27 | + @Inject |
| 28 | + WithDefaultMongoClientEntityRepository withDefaultMongoClientEntityRepository; |
| 29 | + |
| 30 | + @Inject |
| 31 | + WithMongoClient1EntityRepository withMongoClient1EntityRepository; |
| 32 | + |
| 33 | + @Inject |
| 34 | + WithMongoClient2EntityRepository withMongoClient2EntityRepository; |
| 35 | + |
| 36 | + @Inject |
| 37 | + @MongoClientName("mongoclient1") |
| 38 | + MongoClient mongoClient1; |
| 39 | + |
| 40 | + @Inject |
| 41 | + @MongoClientName("mongoclient1") |
| 42 | + ReactiveMongoClient reactiveMongoClient1; |
| 43 | + |
| 44 | + @Inject |
| 45 | + @MongoClientName("mongoclient2") |
| 46 | + MongoClient mongoClient2; |
| 47 | + |
| 48 | + @Inject |
| 49 | + @MongoClientName("mongoclient2") |
| 50 | + ReactiveMongoClient reactiveMongoClient2; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testMongoEntity() { |
| 54 | + assertAll( |
| 55 | + () -> assertEquals("default-mongoclient-db", withDefaultMongoClientEntityRepository.mongoDatabase().getName()), |
| 56 | + () -> assertEquals("mongoclient1-db", withMongoClient1EntityRepository.mongoDatabase().getName()), |
| 57 | + () -> assertEquals("mongoclient2-db", withMongoClient2EntityRepository.mongoDatabase().getName()), |
| 58 | + () -> assertNotNull(reactiveMongoClient1), |
| 59 | + () -> assertNotNull(reactiveMongoClient2)); |
| 60 | + } |
| 61 | + |
| 62 | + @MongoEntity(database = "default-mongoclient-db") |
| 63 | + public static class WithDefaultMongoClientEntity extends PanacheMongoEntity { |
| 64 | + public String field; |
| 65 | + } |
| 66 | + |
| 67 | + @MongoEntity(database = "mongoclient1-db", clientName = "mongoclient1") |
| 68 | + public static class WithMongoClient1Entity extends PanacheMongoEntity { |
| 69 | + public String field; |
| 70 | + } |
| 71 | + |
| 72 | + @MongoEntity(database = "mongoclient2-db", clientName = "mongoclient2") |
| 73 | + public static class WithMongoClient2Entity extends PanacheMongoEntity { |
| 74 | + public String field; |
| 75 | + } |
| 76 | + |
| 77 | + @ApplicationScoped |
| 78 | + public static class WithDefaultMongoClientEntityRepository implements PanacheMongoRepository<WithDefaultMongoClientEntity> { |
| 79 | + } |
| 80 | + |
| 81 | + @ApplicationScoped |
| 82 | + public static class WithMongoClient1EntityRepository implements PanacheMongoRepository<WithMongoClient1Entity> { |
| 83 | + } |
| 84 | + |
| 85 | + @ApplicationScoped |
| 86 | + public static class WithMongoClient2EntityRepository implements PanacheMongoRepository<WithMongoClient2Entity> { |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments