Skip to content

Commit e6efc7d

Browse files
Fixed indexing policy getting reset when not provided (Azure#22246)
1 parent 0cbb108 commit e6efc7d

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IndexPolicyUpdateIT.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.azure.cosmos.models.IndexingPolicy;
1010
import com.azure.spring.data.cosmos.IntegrationTestCollectionManager;
1111
import com.azure.spring.data.cosmos.core.CosmosTemplate;
12+
import com.azure.spring.data.cosmos.domain.Address;
1213
import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity;
1314
import com.azure.spring.data.cosmos.domain.IndexPolicyEntity;
1415
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
@@ -45,9 +46,11 @@ public class IndexPolicyUpdateIT {
4546

4647
CosmosEntityInformation<ComplexIndexPolicyEntity, String> complexIndexPolicyEntityInformation = new CosmosEntityInformation<>(ComplexIndexPolicyEntity.class);
4748

49+
CosmosEntityInformation<Address, String> addressEntityInformation = new CosmosEntityInformation<>(Address.class);
50+
4851
@Before
4952
public void setup() {
50-
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class);
53+
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class, Address.class);
5154
}
5255

5356
@Test
@@ -104,4 +107,12 @@ public void testContainerReplaceShouldNotOccurIfComplexIndexIsUnchanged() {
104107
new SimpleCosmosRepository<>(complexIndexPolicyEntityInformation, spyTemplate);
105108
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
106109
}
110+
111+
@Test
112+
public void testContainerReplaceShouldNotOccurIfIndexingPolicyIsNotSpecified() {
113+
new SimpleCosmosRepository<>(addressEntityInformation, template);
114+
CosmosTemplate spyTemplate = Mockito.spy(template);
115+
new SimpleCosmosRepository<>(addressEntityInformation, spyTemplate);
116+
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
117+
}
107118
}

sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveIndexPolicyUpdateIT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.azure.cosmos.models.IndexingPolicy;
1010
import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager;
1111
import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate;
12+
import com.azure.spring.data.cosmos.domain.Address;
1213
import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity;
1314
import com.azure.spring.data.cosmos.domain.IndexPolicyEntity;
1415
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
@@ -45,6 +46,8 @@ public class ReactiveIndexPolicyUpdateIT {
4546

4647
CosmosEntityInformation<ComplexIndexPolicyEntity, String> complexIndexPolicyEntityInformation = new CosmosEntityInformation<>(ComplexIndexPolicyEntity.class);
4748

49+
CosmosEntityInformation<Address, String> addressEntityInformation = new CosmosEntityInformation<>(Address.class);
50+
4851
@Before
4952
public void setup() {
5053
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class);
@@ -105,4 +108,12 @@ public void testContainerReplaceShouldNotOccurIfComplexIndexIsUnchanged() {
105108
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
106109
}
107110

111+
@Test
112+
public void testContainerReplaceShouldNotOccurIfIndexingPolicyIsNotSpecified() {
113+
new SimpleReactiveCosmosRepository<>(addressEntityInformation, template);
114+
ReactiveCosmosTemplate spyTemplate = Mockito.spy(template);
115+
new SimpleReactiveCosmosRepository<>(addressEntityInformation, spyTemplate);
116+
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
117+
}
118+
108119
}

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/CosmosEntityInformation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class CosmosEntityInformation<T, ID> extends AbstractEntityInformation<T,
6666
private final boolean autoGenerateId;
6767
private final boolean persitable;
6868
private final boolean autoScale;
69+
private final boolean isIndexingPolicySpecified;
6970

7071

7172
/**
@@ -100,6 +101,7 @@ public CosmosEntityInformation(Class<T> domainType) {
100101
this.autoCreateContainer = getIsAutoCreateContainer(domainType);
101102
this.persitable = Persistable.class.isAssignableFrom(domainType);
102103
this.autoScale = getIsAutoScale(domainType);
104+
this.isIndexingPolicySpecified = isIndexingPolicySpecified(domainType);
103105
}
104106

105107
@Override
@@ -268,6 +270,14 @@ public boolean isAutoScale() {
268270
return autoScale;
269271
}
270272

273+
public boolean isIndexingPolicySpecified() {
274+
return this.isIndexingPolicySpecified;
275+
}
276+
277+
private boolean isIndexingPolicySpecified(Class<?> domainType) {
278+
return domainType.getAnnotation(CosmosIndexingPolicy.class) != null;
279+
}
280+
271281
private IndexingPolicy getIndexingPolicy(Class<?> domainType) {
272282
final IndexingPolicy policy = new IndexingPolicy();
273283

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/SimpleCosmosRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public SimpleCosmosRepository(CosmosEntityInformation<T, ID> metadata,
5151

5252
CosmosContainerProperties currentProperties = getContainerProperties();
5353
if (currentProperties != null
54+
&& information.isIndexingPolicySpecified()
5455
&& policyNeedsUpdate(currentProperties.getIndexingPolicy(), information.getIndexingPolicy())) {
5556
currentProperties.setIndexingPolicy(information.getIndexingPolicy());
5657
replaceContainerProperties(currentProperties);

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/SimpleReactiveCosmosRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public SimpleReactiveCosmosRepository(CosmosEntityInformation<T, K> metadata,
4747

4848
CosmosContainerProperties currentProperties = getContainerProperties();
4949
if (currentProperties != null
50+
&& entityInformation.isIndexingPolicySpecified()
5051
&& policyNeedsUpdate(currentProperties.getIndexingPolicy(), entityInformation.getIndexingPolicy())) {
5152
currentProperties.setIndexingPolicy(entityInformation.getIndexingPolicy());
5253
replaceContainerProperties(currentProperties);

0 commit comments

Comments
 (0)