|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.spring.data.cosmos.repository.integration; |
| 5 | + |
| 6 | +import com.azure.cosmos.models.CompositePath; |
| 7 | +import com.azure.cosmos.models.CompositePathSortOrder; |
| 8 | +import com.azure.cosmos.models.CosmosContainerProperties; |
| 9 | +import com.azure.cosmos.models.ExcludedPath; |
| 10 | +import com.azure.cosmos.models.IncludedPath; |
| 11 | +import com.azure.cosmos.models.IndexingPolicy; |
| 12 | +import com.azure.spring.data.cosmos.core.CosmosTemplate; |
| 13 | +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; |
| 14 | +import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity; |
| 15 | +import com.azure.spring.data.cosmos.domain.CompositeIndexEntity; |
| 16 | +import com.azure.spring.data.cosmos.domain.IndexPolicyEntity; |
| 17 | +import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; |
| 18 | +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; |
| 19 | +import com.azure.spring.data.cosmos.repository.support.SimpleCosmosRepository; |
| 20 | +import com.azure.spring.data.cosmos.repository.support.SimpleReactiveCosmosRepository; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.test.context.ContextConfiguration; |
| 26 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.assertj.core.api.Assertions.in; |
| 34 | + |
| 35 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 36 | +@ContextConfiguration(classes = TestRepositoryConfig.class) |
| 37 | +public class CompositeIndexIT { |
| 38 | + |
| 39 | + @Autowired |
| 40 | + CosmosTemplate template; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + ReactiveCosmosTemplate reactiveTemplate; |
| 44 | + |
| 45 | + CosmosEntityInformation<CompositeIndexEntity, String> information = new CosmosEntityInformation<>(CompositeIndexEntity.class); |
| 46 | + |
| 47 | + @Test |
| 48 | + public void canSetCompositeIndex() { |
| 49 | + new SimpleCosmosRepository<>(information, template); |
| 50 | + CosmosContainerProperties properties = template.getContainerProperties(information.getContainerName()); |
| 51 | + List<List<CompositePath>> indexes = properties.getIndexingPolicy().getCompositeIndexes(); |
| 52 | + |
| 53 | + assertThat(indexes.get(0).get(0).getPath()).isEqualTo("/fieldOne"); |
| 54 | + assertThat(indexes.get(0).get(0).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 55 | + assertThat(indexes.get(0).get(1).getPath()).isEqualTo("/fieldTwo"); |
| 56 | + assertThat(indexes.get(0).get(1).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 57 | + |
| 58 | + assertThat(indexes.get(1).get(0).getPath()).isEqualTo("/fieldThree"); |
| 59 | + assertThat(indexes.get(1).get(0).getOrder()).isEqualTo(CompositePathSortOrder.DESCENDING); |
| 60 | + assertThat(indexes.get(1).get(1).getPath()).isEqualTo("/fieldFour"); |
| 61 | + assertThat(indexes.get(1).get(1).getOrder()).isEqualTo(CompositePathSortOrder.DESCENDING); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void canSetCompositeIndexReactive() { |
| 66 | + new SimpleReactiveCosmosRepository<>(information, reactiveTemplate); |
| 67 | + CosmosContainerProperties properties = reactiveTemplate.getContainerProperties(information.getContainerName()).block(); |
| 68 | + List<List<CompositePath>> indexes = properties.getIndexingPolicy().getCompositeIndexes(); |
| 69 | + |
| 70 | + assertThat(indexes.get(0).get(0).getPath()).isEqualTo("/fieldOne"); |
| 71 | + assertThat(indexes.get(0).get(0).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 72 | + assertThat(indexes.get(0).get(1).getPath()).isEqualTo("/fieldTwo"); |
| 73 | + assertThat(indexes.get(0).get(1).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 74 | + |
| 75 | + assertThat(indexes.get(1).get(0).getPath()).isEqualTo("/fieldThree"); |
| 76 | + assertThat(indexes.get(1).get(0).getOrder()).isEqualTo(CompositePathSortOrder.DESCENDING); |
| 77 | + assertThat(indexes.get(1).get(1).getPath()).isEqualTo("/fieldFour"); |
| 78 | + assertThat(indexes.get(1).get(1).getOrder()).isEqualTo(CompositePathSortOrder.DESCENDING); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + public void canUpdateCompositeIndex() { |
| 84 | + // initialize policy on entity |
| 85 | + new SimpleCosmosRepository<>(information, template); |
| 86 | + |
| 87 | + // set new index policy |
| 88 | + IndexingPolicy newIndexPolicy = new IndexingPolicy(); |
| 89 | + List<List<CompositePath>> newCompositeIndex = new ArrayList<>(); |
| 90 | + List<CompositePath> innerList = new ArrayList<>(); |
| 91 | + innerList.add(new CompositePath().setPath("/fieldOne")); |
| 92 | + innerList.add(new CompositePath().setPath("/fieldFour")); |
| 93 | + newCompositeIndex.add(innerList); |
| 94 | + newIndexPolicy.setCompositeIndexes(newCompositeIndex); |
| 95 | + |
| 96 | + // apply new index policy |
| 97 | + CosmosEntityInformation<CompositeIndexEntity, String> spyEntityInformation = Mockito.spy(information); |
| 98 | + Mockito.doReturn(newIndexPolicy).when(spyEntityInformation).getIndexingPolicy(); |
| 99 | + new SimpleCosmosRepository<>(spyEntityInformation, template); |
| 100 | + |
| 101 | + // retrieve new policy |
| 102 | + CosmosContainerProperties properties = template.getContainerProperties(information.getContainerName()); |
| 103 | + List<List<CompositePath>> indexes = properties.getIndexingPolicy().getCompositeIndexes(); |
| 104 | + |
| 105 | + // assert |
| 106 | + assertThat(indexes.size()).isEqualTo(1); |
| 107 | + assertThat(indexes.get(0).get(0).getPath()).isEqualTo("/fieldOne"); |
| 108 | + assertThat(indexes.get(0).get(0).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 109 | + assertThat(indexes.get(0).get(1).getPath()).isEqualTo("/fieldFour"); |
| 110 | + assertThat(indexes.get(0).get(1).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void canUpdateCompositeIndexReactive() { |
| 115 | + // initialize policy on entity |
| 116 | + new SimpleReactiveCosmosRepository<>(information, reactiveTemplate); |
| 117 | + |
| 118 | + // set new index policy |
| 119 | + IndexingPolicy newIndexPolicy = new IndexingPolicy(); |
| 120 | + List<List<CompositePath>> newCompositeIndex = new ArrayList<>(); |
| 121 | + List<CompositePath> innerList = new ArrayList<>(); |
| 122 | + innerList.add(new CompositePath().setPath("/fieldOne")); |
| 123 | + innerList.add(new CompositePath().setPath("/fieldFour")); |
| 124 | + newCompositeIndex.add(innerList); |
| 125 | + newIndexPolicy.setCompositeIndexes(newCompositeIndex); |
| 126 | + |
| 127 | + // apply new index policy |
| 128 | + CosmosEntityInformation<CompositeIndexEntity, String> spyEntityInformation = Mockito.spy(information); |
| 129 | + Mockito.doReturn(newIndexPolicy).when(spyEntityInformation).getIndexingPolicy(); |
| 130 | + new SimpleReactiveCosmosRepository<>(spyEntityInformation, reactiveTemplate); |
| 131 | + |
| 132 | + // retrieve new policy |
| 133 | + CosmosContainerProperties properties = reactiveTemplate.getContainerProperties(information.getContainerName()).block(); |
| 134 | + List<List<CompositePath>> indexes = properties.getIndexingPolicy().getCompositeIndexes(); |
| 135 | + |
| 136 | + // assert |
| 137 | + assertThat(indexes.size()).isEqualTo(1); |
| 138 | + assertThat(indexes.get(0).get(0).getPath()).isEqualTo("/fieldOne"); |
| 139 | + assertThat(indexes.get(0).get(0).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 140 | + assertThat(indexes.get(0).get(1).getPath()).isEqualTo("/fieldFour"); |
| 141 | + assertThat(indexes.get(0).get(1).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments