Skip to content

Commit

Permalink
Fixed indexing policy getting reset when not provided (#22246)
Browse files Browse the repository at this point in the history
  • Loading branch information
kushagraThapar authored Jun 11, 2021
1 parent 0cbb108 commit e6efc7d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.cosmos.models.IndexingPolicy;
import com.azure.spring.data.cosmos.IntegrationTestCollectionManager;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity;
import com.azure.spring.data.cosmos.domain.IndexPolicyEntity;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
Expand Down Expand Up @@ -45,9 +46,11 @@ public class IndexPolicyUpdateIT {

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

CosmosEntityInformation<Address, String> addressEntityInformation = new CosmosEntityInformation<>(Address.class);

@Before
public void setup() {
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class);
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class, Address.class);
}

@Test
Expand Down Expand Up @@ -104,4 +107,12 @@ public void testContainerReplaceShouldNotOccurIfComplexIndexIsUnchanged() {
new SimpleCosmosRepository<>(complexIndexPolicyEntityInformation, spyTemplate);
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
}

@Test
public void testContainerReplaceShouldNotOccurIfIndexingPolicyIsNotSpecified() {
new SimpleCosmosRepository<>(addressEntityInformation, template);
CosmosTemplate spyTemplate = Mockito.spy(template);
new SimpleCosmosRepository<>(addressEntityInformation, spyTemplate);
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.cosmos.models.IndexingPolicy;
import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager;
import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate;
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity;
import com.azure.spring.data.cosmos.domain.IndexPolicyEntity;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class ReactiveIndexPolicyUpdateIT {

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

CosmosEntityInformation<Address, String> addressEntityInformation = new CosmosEntityInformation<>(Address.class);

@Before
public void setup() {
collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class);
Expand Down Expand Up @@ -105,4 +108,12 @@ public void testContainerReplaceShouldNotOccurIfComplexIndexIsUnchanged() {
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
}

@Test
public void testContainerReplaceShouldNotOccurIfIndexingPolicyIsNotSpecified() {
new SimpleReactiveCosmosRepository<>(addressEntityInformation, template);
ReactiveCosmosTemplate spyTemplate = Mockito.spy(template);
new SimpleReactiveCosmosRepository<>(addressEntityInformation, spyTemplate);
Mockito.verify(spyTemplate, Mockito.never()).replaceContainerProperties(Mockito.any(), Mockito.any());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class CosmosEntityInformation<T, ID> extends AbstractEntityInformation<T,
private final boolean autoGenerateId;
private final boolean persitable;
private final boolean autoScale;
private final boolean isIndexingPolicySpecified;


/**
Expand Down Expand Up @@ -100,6 +101,7 @@ public CosmosEntityInformation(Class<T> domainType) {
this.autoCreateContainer = getIsAutoCreateContainer(domainType);
this.persitable = Persistable.class.isAssignableFrom(domainType);
this.autoScale = getIsAutoScale(domainType);
this.isIndexingPolicySpecified = isIndexingPolicySpecified(domainType);
}

@Override
Expand Down Expand Up @@ -268,6 +270,14 @@ public boolean isAutoScale() {
return autoScale;
}

public boolean isIndexingPolicySpecified() {
return this.isIndexingPolicySpecified;
}

private boolean isIndexingPolicySpecified(Class<?> domainType) {
return domainType.getAnnotation(CosmosIndexingPolicy.class) != null;
}

private IndexingPolicy getIndexingPolicy(Class<?> domainType) {
final IndexingPolicy policy = new IndexingPolicy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public SimpleCosmosRepository(CosmosEntityInformation<T, ID> metadata,

CosmosContainerProperties currentProperties = getContainerProperties();
if (currentProperties != null
&& information.isIndexingPolicySpecified()
&& policyNeedsUpdate(currentProperties.getIndexingPolicy(), information.getIndexingPolicy())) {
currentProperties.setIndexingPolicy(information.getIndexingPolicy());
replaceContainerProperties(currentProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public SimpleReactiveCosmosRepository(CosmosEntityInformation<T, K> metadata,

CosmosContainerProperties currentProperties = getContainerProperties();
if (currentProperties != null
&& entityInformation.isIndexingPolicySpecified()
&& policyNeedsUpdate(currentProperties.getIndexingPolicy(), entityInformation.getIndexingPolicy())) {
currentProperties.setIndexingPolicy(entityInformation.getIndexingPolicy());
replaceContainerProperties(currentProperties);
Expand Down

0 comments on commit e6efc7d

Please sign in to comment.