|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.spring.data.cosmos.core; |
| 5 | + |
| 6 | +import com.azure.cosmos.CosmosAsyncClient; |
| 7 | +import com.azure.cosmos.CosmosAsyncDatabase; |
| 8 | +import com.azure.cosmos.CosmosClientBuilder; |
| 9 | +import com.azure.cosmos.CosmosException; |
| 10 | +import com.azure.cosmos.models.PartitionKey; |
| 11 | +import com.azure.spring.data.cosmos.CosmosFactory; |
| 12 | +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; |
| 13 | +import com.azure.spring.data.cosmos.config.CosmosConfig; |
| 14 | +import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; |
| 15 | +import com.azure.spring.data.cosmos.core.mapping.CosmosMappingContext; |
| 16 | +import com.azure.spring.data.cosmos.domain.Person; |
| 17 | +import com.azure.spring.data.cosmos.repository.MultiTenantTestRepositoryConfig; |
| 18 | +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.ClassRule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.autoconfigure.domain.EntityScanner; |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.data.annotation.Persistent; |
| 28 | +import org.springframework.test.context.ContextConfiguration; |
| 29 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static com.azure.spring.data.cosmos.common.TestConstants.ADDRESSES; |
| 35 | +import static com.azure.spring.data.cosmos.common.TestConstants.AGE; |
| 36 | +import static com.azure.spring.data.cosmos.common.TestConstants.FIRST_NAME; |
| 37 | +import static com.azure.spring.data.cosmos.common.TestConstants.HOBBIES; |
| 38 | +import static com.azure.spring.data.cosmos.common.TestConstants.ID_1; |
| 39 | +import static com.azure.spring.data.cosmos.common.TestConstants.ID_2; |
| 40 | +import static com.azure.spring.data.cosmos.common.TestConstants.LAST_NAME; |
| 41 | +import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY; |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 46 | +@ContextConfiguration(classes = MultiTenantTestRepositoryConfig.class) |
| 47 | +public class MultiTenantDBCosmosFactoryIT { |
| 48 | + |
| 49 | + private final String testDB1 = "Database1"; |
| 50 | + private final String testDB2 = "Database2"; |
| 51 | + |
| 52 | + private final Person TEST_PERSON_1 = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); |
| 53 | + private final Person TEST_PERSON_2 = new Person(ID_2, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); |
| 54 | + |
| 55 | + @ClassRule |
| 56 | + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private ApplicationContext applicationContext; |
| 60 | + @Autowired |
| 61 | + private CosmosConfig cosmosConfig; |
| 62 | + @Autowired |
| 63 | + private CosmosClientBuilder cosmosClientBuilder; |
| 64 | + |
| 65 | + private MultiTenantDBCosmosFactory cosmosFactory; |
| 66 | + private CosmosTemplate cosmosTemplate; |
| 67 | + private CosmosAsyncClient client; |
| 68 | + private CosmosEntityInformation<Person, String> personInfo; |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() throws ClassNotFoundException { |
| 72 | + /// Setup |
| 73 | + client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); |
| 74 | + cosmosFactory = new MultiTenantDBCosmosFactory(client, testDB1); |
| 75 | + final CosmosMappingContext mappingContext = new CosmosMappingContext(); |
| 76 | + |
| 77 | + try { |
| 78 | + mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); |
| 79 | + } catch (Exception e) { |
| 80 | + Assert.fail(); |
| 81 | + } |
| 82 | + |
| 83 | + final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext, null); |
| 84 | + cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, cosmosConverter, null); |
| 85 | + personInfo = new CosmosEntityInformation<>(Person.class); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testGetDatabaseFunctionality() { |
| 90 | + // Create DB1 and add TEST_PERSON_1 to it |
| 91 | + cosmosTemplate.createContainerIfNotExists(personInfo); |
| 92 | + cosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class); |
| 93 | + assertThat(cosmosFactory.getDatabaseName()).isEqualTo(testDB1); |
| 94 | + cosmosTemplate.insert(TEST_PERSON_1, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_1))); |
| 95 | + |
| 96 | + // Create DB2 and add TEST_PERSON_2 to it |
| 97 | + cosmosFactory.manuallySetDatabaseName = testDB2; |
| 98 | + cosmosTemplate.createContainerIfNotExists(personInfo); |
| 99 | + cosmosTemplate.deleteAll(personInfo.getContainerName(), Person.class); |
| 100 | + assertThat(cosmosFactory.getDatabaseName()).isEqualTo(testDB2); |
| 101 | + cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); |
| 102 | + |
| 103 | + // Check that DB2 has the correct contents |
| 104 | + List<Person> expectedResultsDB2 = new ArrayList<>(); |
| 105 | + expectedResultsDB2.add(TEST_PERSON_2); |
| 106 | + Iterable<Person> iterableDB2 = cosmosTemplate.findAll(personInfo.getContainerName(), Person.class); |
| 107 | + List<Person> resultDB2 = new ArrayList<>(); |
| 108 | + iterableDB2.forEach(resultDB2::add); |
| 109 | + Assert.assertEquals(expectedResultsDB2, resultDB2); |
| 110 | + |
| 111 | + // Check that DB1 has the correct contents |
| 112 | + cosmosFactory.manuallySetDatabaseName = testDB1; |
| 113 | + List<Person> expectedResultsDB1 = new ArrayList<>(); |
| 114 | + expectedResultsDB1.add(TEST_PERSON_1); |
| 115 | + Iterable<Person> iterableDB1 = cosmosTemplate.findAll(personInfo.getContainerName(), Person.class); |
| 116 | + List<Person> resultDB1 = new ArrayList<>(); |
| 117 | + iterableDB1.forEach(resultDB1::add); |
| 118 | + Assert.assertEquals(expectedResultsDB1, resultDB1); |
| 119 | + |
| 120 | + //Cleanup |
| 121 | + deleteDatabaseIfExists(testDB1); |
| 122 | + deleteDatabaseIfExists(testDB2); |
| 123 | + } |
| 124 | + |
| 125 | + private void deleteDatabaseIfExists(String dbName) { |
| 126 | + CosmosAsyncDatabase database = client.getDatabase(dbName); |
| 127 | + try { |
| 128 | + database.delete().block(); |
| 129 | + } catch (CosmosException ex) { |
| 130 | + assertEquals(ex.getStatusCode(), 404); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments