-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancing CosmosTemplate to Support Multi-Tenancy at a Container Level #33400
Merged
FabianMeiswinkel
merged 13 commits into
Azure:main
from
TheovanKraay:spring-cosmos-multitenant-container
Feb 16, 2023
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b16d641
commit initial changes
6b65735
add tests
aa3d532
add tests
0f5052f
update
1906cca
add reactive parts and tests + review
5de7a62
updates
f35383c
Merge pull request #2 from TheovanKraay/spring-multitenant-container-…
TheovanKraay 66bdc06
spring cosmos multitenant container level
e831b15
refactor getContainerName
d14749c
update change log
3fd50f0
add javadoc
f17ffa1
fix linting errors
40c4416
change per API review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...st/src/test/java/com/azure/spring/data/cosmos/core/MultiTenantContainerCosmosFactory.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,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.data.cosmos.core; | ||
|
||
import com.azure.cosmos.CosmosAsyncClient; | ||
import com.azure.spring.data.cosmos.CosmosFactory; | ||
|
||
/** | ||
* Example for extending CosmosFactory for Mutli-Tenancy at the container level | ||
*/ | ||
public class MultiTenantContainerCosmosFactory extends CosmosFactory { | ||
|
||
public String manuallySetDatabaseName; | ||
|
||
public String manuallySetContainerName; | ||
|
||
/** | ||
* Validate config and initialization | ||
* | ||
* @param cosmosAsyncClient cosmosAsyncClient | ||
* @param databaseName databaseName | ||
*/ | ||
public MultiTenantContainerCosmosFactory(CosmosAsyncClient cosmosAsyncClient, String databaseName) { | ||
super(cosmosAsyncClient, databaseName); | ||
|
||
this.manuallySetDatabaseName = databaseName; | ||
} | ||
|
||
@Override | ||
public String getContainerName() { | ||
return this.manuallySetContainerName; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
.../src/test/java/com/azure/spring/data/cosmos/core/MultiTenantContainerCosmosFactoryIT.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,135 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.data.cosmos.core; | ||
|
||
import com.azure.cosmos.CosmosAsyncClient; | ||
import com.azure.cosmos.CosmosAsyncDatabase; | ||
import com.azure.cosmos.CosmosClientBuilder; | ||
import com.azure.cosmos.CosmosException; | ||
import com.azure.cosmos.models.PartitionKey; | ||
import com.azure.spring.data.cosmos.CosmosFactory; | ||
import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; | ||
import com.azure.spring.data.cosmos.config.CosmosConfig; | ||
import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; | ||
import com.azure.spring.data.cosmos.core.mapping.CosmosMappingContext; | ||
import com.azure.spring.data.cosmos.domain.Person; | ||
import com.azure.spring.data.cosmos.repository.MultiTenantTestRepositoryConfig; | ||
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.domain.EntityScanner; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.data.annotation.Persistent; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.azure.spring.data.cosmos.common.TestConstants.ADDRESSES; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.AGE; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.FIRST_NAME; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.HOBBIES; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.ID_1; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.ID_2; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.LAST_NAME; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = MultiTenantTestRepositoryConfig.class) | ||
public class MultiTenantContainerCosmosFactoryIT { | ||
|
||
private final String testDB1 = "Database1"; | ||
|
||
private final String testContainer1= "Container1"; | ||
private final String testContainer2 = "Container2"; | ||
|
||
private final Person TEST_PERSON_1 = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); | ||
private final Person TEST_PERSON_2 = new Person(ID_2, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); | ||
|
||
@ClassRule | ||
public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
@Autowired | ||
private CosmosConfig cosmosConfig; | ||
@Autowired | ||
private CosmosClientBuilder cosmosClientBuilder; | ||
|
||
private MultiTenantContainerCosmosFactory cosmosFactory; | ||
private CosmosTemplate cosmosTemplate; | ||
private CosmosAsyncClient client; | ||
private CosmosEntityInformation<Person, String> personInfo; | ||
|
||
@Before | ||
public void setUp() throws ClassNotFoundException { | ||
/// Setup | ||
client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); | ||
cosmosFactory = new MultiTenantContainerCosmosFactory(client, testDB1); | ||
final CosmosMappingContext mappingContext = new CosmosMappingContext(); | ||
|
||
try { | ||
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); | ||
} catch (Exception e) { | ||
Assert.fail(); | ||
} | ||
|
||
final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext, null); | ||
cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, cosmosConverter, null); | ||
personInfo = new CosmosEntityInformation<>(Person.class); | ||
} | ||
|
||
@Test | ||
public void testGetContainerFunctionality() { | ||
// Create testContainer1 and add TEST_PERSON_1 to it | ||
cosmosFactory.manuallySetContainerName = testContainer1; | ||
cosmosTemplate.createContainerIfNotExists(personInfo); | ||
cosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class); | ||
assertThat(cosmosFactory.getContainerName()).isEqualTo(testContainer1); | ||
cosmosTemplate.insert(TEST_PERSON_1, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_1))); | ||
|
||
// Create testContainer1 and add TEST_PERSON_2 to it | ||
cosmosFactory.manuallySetContainerName = testContainer2; | ||
cosmosTemplate.createContainerIfNotExists(personInfo); | ||
cosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class); | ||
assertThat(cosmosFactory.getContainerName()).isEqualTo(testContainer2); | ||
cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); | ||
|
||
// Check that testContainer2 has the correct contents | ||
List<Person> expectedResultsContainer2 = new ArrayList<>(); | ||
expectedResultsContainer2.add(TEST_PERSON_2); | ||
Iterable<Person> iterableDB2 = cosmosTemplate.findAll(personInfo.getContainerName(), Person.class); | ||
List<Person> resultDB2 = new ArrayList<>(); | ||
iterableDB2.forEach(resultDB2::add); | ||
Assert.assertEquals(expectedResultsContainer2, resultDB2); | ||
|
||
// Check that testContainer1 has the correct contents | ||
cosmosFactory.manuallySetContainerName = testContainer1; | ||
List<Person> expectedResultsContainer1 = new ArrayList<>(); | ||
expectedResultsContainer1.add(TEST_PERSON_1); | ||
Iterable<Person> iterableDB1 = cosmosTemplate.findAll(personInfo.getContainerName(), Person.class); | ||
List<Person> resultDB1 = new ArrayList<>(); | ||
iterableDB1.forEach(resultDB1::add); | ||
Assert.assertEquals(expectedResultsContainer1, resultDB1); | ||
|
||
//Cleanup | ||
deleteDatabaseIfExists(testDB1); | ||
} | ||
|
||
private void deleteDatabaseIfExists(String dbName) { | ||
CosmosAsyncDatabase database = client.getDatabase(dbName); | ||
try { | ||
database.delete().block(); | ||
} catch (CosmosException ex) { | ||
assertEquals(ex.getStatusCode(), 404); | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...t/java/com/azure/spring/data/cosmos/core/ReactiveMultiTenantContainerCosmosFactoryIT.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,139 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.data.cosmos.core; | ||
|
||
import com.azure.cosmos.CosmosAsyncClient; | ||
import com.azure.cosmos.CosmosAsyncDatabase; | ||
import com.azure.cosmos.CosmosClientBuilder; | ||
import com.azure.cosmos.CosmosException; | ||
import com.azure.cosmos.models.PartitionKey; | ||
import com.azure.spring.data.cosmos.CosmosFactory; | ||
import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; | ||
import com.azure.spring.data.cosmos.config.CosmosConfig; | ||
import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; | ||
import com.azure.spring.data.cosmos.core.mapping.CosmosMappingContext; | ||
import com.azure.spring.data.cosmos.domain.Person; | ||
import com.azure.spring.data.cosmos.repository.MultiTenantTestRepositoryConfig; | ||
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.domain.EntityScanner; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.data.annotation.Persistent; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import reactor.core.publisher.Flux; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.azure.spring.data.cosmos.common.TestConstants.ADDRESSES; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.AGE; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.FIRST_NAME; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.HOBBIES; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.ID_1; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.ID_2; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.LAST_NAME; | ||
import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = MultiTenantTestRepositoryConfig.class) | ||
public class ReactiveMultiTenantContainerCosmosFactoryIT { | ||
|
||
private final String testDB1 = "Database1"; | ||
|
||
private final String testContainer1= "Container1"; | ||
private final String testContainer2 = "Container2"; | ||
|
||
private final Person TEST_PERSON_1 = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); | ||
private final Person TEST_PERSON_2 = new Person(ID_2, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); | ||
|
||
@ClassRule | ||
public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
@Autowired | ||
private CosmosConfig cosmosConfig; | ||
@Autowired | ||
private CosmosClientBuilder cosmosClientBuilder; | ||
|
||
private MultiTenantContainerCosmosFactory cosmosFactory; | ||
private ReactiveCosmosTemplate reactiveCosmosTemplate; | ||
private CosmosAsyncClient client; | ||
private CosmosEntityInformation<Person, String> personInfo; | ||
|
||
@Before | ||
public void setUp() throws ClassNotFoundException { | ||
/// Setup | ||
client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); | ||
cosmosFactory = new MultiTenantContainerCosmosFactory(client, testDB1); | ||
final CosmosMappingContext mappingContext = new CosmosMappingContext(); | ||
|
||
try { | ||
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); | ||
} catch (Exception e) { | ||
Assert.fail(); | ||
} | ||
|
||
final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext, null); | ||
reactiveCosmosTemplate = new ReactiveCosmosTemplate(cosmosFactory, cosmosConfig, cosmosConverter, null); | ||
personInfo = new CosmosEntityInformation<>(Person.class); | ||
} | ||
|
||
@Test | ||
public void testGetContainerFunctionality() { | ||
// Create testContainer1 and add TEST_PERSON_1 to it | ||
cosmosFactory.manuallySetContainerName = testContainer1; | ||
reactiveCosmosTemplate.createContainerIfNotExists(personInfo).block(); | ||
reactiveCosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class).block(); | ||
assertThat(cosmosFactory.getContainerName()).isEqualTo(testContainer1); | ||
reactiveCosmosTemplate.insert(TEST_PERSON_1, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_1))).block(); | ||
|
||
// Create testContainer1 and add TEST_PERSON_2 to it | ||
cosmosFactory.manuallySetContainerName = testContainer2; | ||
reactiveCosmosTemplate.createContainerIfNotExists(personInfo).block(); | ||
reactiveCosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class).block(); | ||
assertThat(cosmosFactory.getContainerName()).isEqualTo(testContainer2); | ||
reactiveCosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))).block(); | ||
|
||
// Check that testContainer2 has the correct contents | ||
List<Person> expectedResultsContainer2 = new ArrayList<>(); | ||
expectedResultsContainer2.add(TEST_PERSON_2); | ||
Flux<Person> fluxDB2 = reactiveCosmosTemplate.findAll(personInfo.getContainerName(), Person.class); | ||
StepVerifier.create(fluxDB2).expectNextCount(1).verifyComplete(); | ||
List<Person> resultDB2 = new ArrayList<>(); | ||
fluxDB2.toIterable().forEach(resultDB2::add); | ||
Assert.assertEquals(expectedResultsContainer2, resultDB2); | ||
|
||
// Check that testContainer1 has the correct contents | ||
cosmosFactory.manuallySetContainerName = testContainer1; | ||
List<Person> expectedResultsContainer1 = new ArrayList<>(); | ||
expectedResultsContainer1.add(TEST_PERSON_1); | ||
Flux<Person> fluxDB1 = reactiveCosmosTemplate.findAll(personInfo.getContainerName(), Person.class); | ||
StepVerifier.create(fluxDB1).expectNextCount(1).verifyComplete(); | ||
List<Person> resultDB1 = new ArrayList<>(); | ||
fluxDB1.toIterable().forEach(resultDB1::add); | ||
Assert.assertEquals(expectedResultsContainer1, resultDB1); | ||
|
||
//Cleanup | ||
deleteDatabaseIfExists(testDB1); | ||
} | ||
|
||
private void deleteDatabaseIfExists(String dbName) { | ||
CosmosAsyncDatabase database = client.getDatabase(dbName); | ||
try { | ||
database.delete().block(); | ||
} catch (CosmosException ex) { | ||
assertEquals(ex.getStatusCode(), 404); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, why we do not have an constructor accepting containerName? and also in Spring, we do not really use setter pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, container name is taken from either entity name, or containerName in @container within entity if defined. So it need not be passed to the constructor by default (but we add the setter/getter here so it can be overridden and called polymorphically in the case of multi-tenant scenario). For databaseName, this has to be passed in, as that has to be defined in config. For getter/setter, yes we should, but these are just sample classes to facilitate the unit tests, and will only be used by them, they are not part of the API (user would create their own, and we'll have examples for that in samples repo).