forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It provide ways to implement multi-tenancy using a database per tenant approch It'll select dynamically the database to be used Example: - MongoDatabaseResolver.resolve() returns "customerA" The selected mongo database to be used will be "customerA"
- Loading branch information
1 parent
16adf2c
commit 1e642a9
Showing
11 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...common/runtime/src/main/java/io/quarkus/mongodb/panache/common/MongoDatabaseResolver.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,8 @@ | ||
package io.quarkus.mongodb.panache.common; | ||
|
||
/* | ||
* This interface can be used to resolve the mongo database name in runtime, it'll help to implement multi-tenancy using tenant per database approach | ||
*/ | ||
public interface MongoDatabaseResolver { | ||
String resolve(); | ||
} |
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
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
8 changes: 8 additions & 0 deletions
8
...odb-panache/src/test/java/io/quarkus/it/mongodb/panache/ResolversPropertiesConstants.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,8 @@ | ||
package io.quarkus.it.mongodb.panache; | ||
|
||
public class ResolversPropertiesConstants { | ||
public static final String MONGO_DATABASE_RESOLVER_ENABLED_PROPERTY = "quarkus.mongodb.database-resolver.enabled"; | ||
|
||
private ResolversPropertiesConstants() { | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...rc/test/java/io/quarkus/it/mongodb/panache/profiles/MongoDatabaseResolverTestProfile.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,7 @@ | ||
package io.quarkus.it.mongodb.panache.profiles; | ||
|
||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
|
||
public class MongoDatabaseResolverTestProfile implements QuarkusTestProfile { | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...b/panache/reactive/resolvers/ReactiveMongodbPanacheResourceMongoDatabaseResolverTest.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,14 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.resolvers; | ||
|
||
import io.quarkus.it.mongodb.panache.resolvers.BaseMongodbPanacheResolversTest; | ||
import io.quarkus.mongodb.panache.reactive.runtime.JavaReactiveMongoOperations; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class ReactiveMongodbPanacheResourceMongoDatabaseResolverTest extends BaseMongodbPanacheResolversTest { | ||
|
||
public ReactiveMongodbPanacheResourceMongoDatabaseResolverTest() { | ||
super(JavaReactiveMongoOperations.INSTANCE); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
...rc/test/java/io/quarkus/it/mongodb/panache/resolvers/BaseMongodbPanacheResolversTest.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,92 @@ | ||
package io.quarkus.it.mongodb.panache.resolvers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledOnOs; | ||
import org.junit.jupiter.api.condition.OS; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.mongodb.panache.common.MongoDatabaseResolver; | ||
import io.quarkus.mongodb.panache.common.reactive.runtime.ReactiveMongoOperations; | ||
import io.quarkus.mongodb.panache.common.runtime.MongoOperations; | ||
import io.quarkus.test.Mock; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.mockito.InjectMock; | ||
import io.quarkus.test.mongodb.MongoReplicaSetTestResource; | ||
|
||
@QuarkusTestResource(MongoReplicaSetTestResource.class) | ||
@DisabledOnOs(OS.WINDOWS) | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public abstract class BaseMongodbPanacheResolversTest { | ||
|
||
private static final String EXPECTED_DATABASE_NAME_FROM_RESOLVER = "tenant-a"; | ||
|
||
@ConfigProperty(name = "quarkus.mongodb.database") | ||
String databaseNameFromProperty; | ||
|
||
@InjectMock | ||
private MongoDatabaseResolver mongoDatabaseResolver; | ||
|
||
private Object mongoOperations; | ||
|
||
public BaseMongodbPanacheResolversTest(Object mongoOperations) { | ||
this.mongoOperations = mongoOperations; | ||
} | ||
|
||
@Test | ||
public void testMongoDatabaseResolverUsingDatabaseFromResolve() throws Exception { | ||
testMongoDatabaseResolver(ResolverEntity.class, EXPECTED_DATABASE_NAME_FROM_RESOLVER, true, false); | ||
} | ||
|
||
@Test | ||
public void testMongoDatabaseResolverKeepingDatabaseFromAnnotation() throws Exception { | ||
testMongoDatabaseResolver(ResolverEntityWithDatabase.Entity.class, ResolverEntityWithDatabase.DATABASE_NAME, true, | ||
true); | ||
} | ||
|
||
@Test | ||
public void testMongoDatabaseResolverUsingDefaultDatabaseFromProperties() throws Exception { | ||
testMongoDatabaseResolver(ResolverEntity.class, databaseNameFromProperty, false, false); | ||
} | ||
|
||
private void testMongoDatabaseResolver(Class<?> entityClass, String expectedDatabaseName, boolean shouldMockResolveReturn, | ||
boolean expectedNoCallInResolveMethod) | ||
throws Exception { | ||
//arrange | ||
if (shouldMockResolveReturn) { | ||
Mockito.when(mongoDatabaseResolver.resolve()).thenReturn(expectedDatabaseName); | ||
} | ||
|
||
//act | ||
String databaseName = null; | ||
if (mongoOperations instanceof MongoOperations) { | ||
MongoOperations auxMongoOperations = (MongoOperations) mongoOperations; | ||
databaseName = auxMongoOperations.mongoDatabase(entityClass).getName(); | ||
} | ||
|
||
if (mongoOperations instanceof ReactiveMongoOperations) { | ||
ReactiveMongoOperations auxReactiveMongoOperations = (ReactiveMongoOperations) mongoOperations; | ||
databaseName = auxReactiveMongoOperations.mongoDatabase(entityClass).getName(); | ||
} | ||
|
||
//assert | ||
Mockito.verify(mongoDatabaseResolver, Mockito.times(expectedNoCallInResolveMethod ? 0 : 1)).resolve(); | ||
assertEquals(expectedDatabaseName, databaseName); | ||
} | ||
|
||
@Mock | ||
@ApplicationScoped | ||
public static class TestMongoDatabaseResolver implements MongoDatabaseResolver { | ||
|
||
@Override | ||
public String resolve() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...he/src/test/java/io/quarkus/it/mongodb/panache/resolvers/MongodbPanacheResolversTest.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,13 @@ | ||
package io.quarkus.it.mongodb.panache.resolvers; | ||
|
||
import io.quarkus.mongodb.panache.runtime.JavaMongoOperations; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class MongodbPanacheResolversTest extends BaseMongodbPanacheResolversTest { | ||
|
||
public MongodbPanacheResolversTest() { | ||
super(JavaMongoOperations.INSTANCE); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/resolvers/ResolverEntity.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,7 @@ | ||
package io.quarkus.it.mongodb.panache.resolvers; | ||
|
||
import io.quarkus.mongodb.panache.PanacheMongoEntityBase; | ||
|
||
public class ResolverEntity extends PanacheMongoEntityBase { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...che/src/test/java/io/quarkus/it/mongodb/panache/resolvers/ResolverEntityWithDatabase.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,12 @@ | ||
package io.quarkus.it.mongodb.panache.resolvers; | ||
|
||
import io.quarkus.mongodb.panache.common.MongoEntity; | ||
|
||
public class ResolverEntityWithDatabase { | ||
public static final String DATABASE_NAME = "database-from-anotation"; | ||
|
||
@MongoEntity(database = DATABASE_NAME) | ||
public static class Entity { | ||
|
||
} | ||
} |