/
AddressRepositoryIT.java
305 lines (243 loc) · 12.4 KB
/
AddressRepositoryIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.repository.integration;
import com.azure.cosmos.models.CosmosPatchItemRequestOptions;
import com.azure.cosmos.models.CosmosPatchOperations;
import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.IntegrationTestCollectionManager;
import com.azure.spring.data.cosmos.common.ResponseDiagnosticsTestUtils;
import com.azure.spring.data.cosmos.common.TestConstants;
import com.azure.spring.data.cosmos.common.TestUtils;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.exception.CosmosAccessException;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.AddressRepository;
import org.assertj.core.util.Lists;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import static com.azure.spring.data.cosmos.common.TestConstants.CITY;
import static com.azure.spring.data.cosmos.domain.Address.TEST_ADDRESS1_PARTITION1;
import static com.azure.spring.data.cosmos.domain.Address.TEST_ADDRESS1_PARTITION2;
import static com.azure.spring.data.cosmos.domain.Address.TEST_ADDRESS2_PARTITION1;
import static com.azure.spring.data.cosmos.domain.Address.TEST_ADDRESS4_PARTITION3;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class AddressRepositoryIT {
@ClassRule
public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager();
@Autowired
AddressRepository repository;
@Autowired
private CosmosTemplate template;
@Autowired
private ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils;
@Rule
public ExpectedException expectedException = ExpectedException.none();
CosmosPatchOperations patchSetOperation = CosmosPatchOperations
.create()
.set("/street", TestConstants.NEW_STREET);
CosmosPatchOperations patchReplaceOperation = CosmosPatchOperations
.create()
.replace("/street", TestConstants.NEW_STREET);
CosmosPatchOperations patchRemoveOperation = CosmosPatchOperations
.create()
.remove("/street");
private static final CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
@Before
public void setUp() {
collectionManager.ensureContainersCreatedAndEmpty(template, Address.class);
repository.saveAll(Lists.newArrayList(TEST_ADDRESS1_PARTITION1, TEST_ADDRESS1_PARTITION2,
TEST_ADDRESS2_PARTITION1, TEST_ADDRESS4_PARTITION3));
}
@Test
public void testFindById() {
// test findById (ID id) cross partition
final Address result = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode()).get();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosDiagnostics().toString().contains("\"requestOperationType\":\"Query\"")).isTrue();
assertThat(result).isEqualTo(TEST_ADDRESS1_PARTITION1);
}
@Test
public void testFindAll() {
// findAll cross partition
final List<Address> result = TestUtils.toList(repository.findAll());
assertThat(result.size()).isEqualTo(4);
}
@Test
public void testFindByIdWithPartitionKey() {
final Optional<Address> addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(collectionManager.getEntityInformation(Address.class).getPartitionKeyFieldValue(TEST_ADDRESS1_PARTITION1)));
if (!addressById.isPresent()) {
fail("address not found");
return;
}
assertThat(addressById.get()).isEqualTo(TEST_ADDRESS1_PARTITION1);
}
@Test
public void testFindByIdForPartitionedCollection() {
final List<Address> addresses = TestUtils.toList(repository.findByPostalCode(TestConstants.POSTAL_CODE));
assertThat(addresses.size()).isEqualTo(2);
assertThat(addresses.get(0).getPostalCode()).isEqualTo(TestConstants.POSTAL_CODE);
assertThat(addresses.get(1).getPostalCode()).isEqualTo(TestConstants.POSTAL_CODE);
}
@Test
public void testFindByPartitionedCity() {
final String city = TEST_ADDRESS1_PARTITION1.getCity();
final List<Address> result = TestUtils.toList(repository.findByCity(city));
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getCity()).isEqualTo(city);
assertThat(result.get(1).getCity()).isEqualTo(city);
}
@Test
public void testFindByPartitionedCityIn() {
final String city = TEST_ADDRESS1_PARTITION1.getCity();
final List<Address> result = TestUtils.toList(repository.findByCityIn(Lists.newArrayList(city)));
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getCity()).isEqualTo(city);
assertThat(result.get(1).getCity()).isEqualTo(city);
}
@Test
public void testFindByPostalCodeAndCityIn() {
final String city = TEST_ADDRESS1_PARTITION1.getCity();
final List<String> postalCodes = Lists.newArrayList(TEST_ADDRESS1_PARTITION1.getPostalCode(),
TEST_ADDRESS2_PARTITION1.getPostalCode());
final List<Address> result = TestUtils.toList(repository.findByPostalCodeInAndCity(postalCodes, city));
assertThat(result.size()).isEqualTo(2);
assertThat(result).isEqualTo(Lists.newArrayList(TEST_ADDRESS1_PARTITION1, TEST_ADDRESS2_PARTITION1));
}
@Test
public void testFindByStreetOrCity() {
final String city = TEST_ADDRESS1_PARTITION1.getCity();
final String street = TEST_ADDRESS1_PARTITION2.getStreet();
final List<Address> result = TestUtils.toList(repository.findByStreetOrCity(street, city));
final List<Address> reference = Arrays.asList(
TEST_ADDRESS1_PARTITION1, TEST_ADDRESS1_PARTITION2, TEST_ADDRESS2_PARTITION1);
result.sort(Comparator.comparing(Address::getPostalCode));
reference.sort(Comparator.comparing(Address::getPostalCode));
Assert.assertEquals(reference.size(), result.size());
Assert.assertEquals(reference, result);
}
@Test
public void testCount() {
final long count = repository.count();
assertThat(count).isEqualTo(4);
repository.deleteByCity(TestConstants.CITY);
final long newCount = repository.count();
assertThat(newCount).isEqualTo(2);
}
@Test
public void deleteWithoutPartitionedColumnShouldFail() {
expectedException.expect(Exception.class);
repository.deleteById(TEST_ADDRESS1_PARTITION1.getPostalCode());
}
@Test
public void canDeleteByIdAndPartitionedCity() {
final long count = repository.count();
assertThat(count).isEqualTo(4);
repository.deleteByPostalCodeAndCity(
TEST_ADDRESS1_PARTITION1.getPostalCode(), TEST_ADDRESS1_PARTITION1.getCity());
final List<Address> result = TestUtils.toList(repository.findAll());
assertThat(result.size()).isEqualTo(3);
}
@Test
public void canDeleteByPartitionedCity() {
final long count = repository.count();
assertThat(count).isEqualTo(4);
repository.deleteByCity(TEST_ADDRESS1_PARTITION1.getCity());
final List<Address> result = TestUtils.toList(repository.findAll());
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity());
}
@Test
public void testDeleteByIdAndPartitionKey() {
final long count = repository.count();
assertThat(count).isEqualTo(4);
Optional<Address> addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
assertThat(addressById.isPresent()).isTrue();
repository.deleteById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
final List<Address> result = TestUtils.toList(repository.findAll());
assertThat(result.size()).isEqualTo(3);
addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
assertThat(addressById.isPresent()).isFalse();
}
@Test
public void testFindAllByPartitionKey() {
List<Address> findAll =
TestUtils.toList(repository.findAll(new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity())));
// Since there are two addresses with partition1
assertThat(findAll.size()).isEqualTo(2);
assertThat(findAll.containsAll(Lists.newArrayList(TEST_ADDRESS1_PARTITION1,
TEST_ADDRESS2_PARTITION1))).isTrue();
findAll = TestUtils.toList(repository.findAll(new PartitionKey(TEST_ADDRESS1_PARTITION2.getCity())));
// Since there is one address with partition2
assertThat(findAll.size()).isEqualTo(1);
assertThat(findAll.contains(TEST_ADDRESS1_PARTITION2)).isTrue();
findAll = TestUtils.toList(repository.findAll(new PartitionKey(TEST_ADDRESS4_PARTITION3.getCity())));
// Since there is one address with partition3
assertThat(findAll.size()).isEqualTo(1);
assertThat(findAll.contains(TEST_ADDRESS4_PARTITION3)).isTrue();
}
@Test
public void testUpdateEntity() {
final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), TestConstants.NEW_STREET,
TEST_ADDRESS1_PARTITION1.getCity());
repository.save(updatedAddress);
final List<Address> results =
TestUtils.toList(repository.findByPostalCodeAndCity(updatedAddress.getPostalCode(),
updatedAddress.getCity()));
assertThat(results.size()).isEqualTo(1);
assertThat(results.get(0).getStreet()).isEqualTo(updatedAddress.getStreet());
assertThat(results.get(0).getPostalCode()).isEqualTo(updatedAddress.getPostalCode());
}
@Test
public void testPatchEntitySet() {
Address patchedAddress = repository.save(TestConstants.POSTAL_CODE, new PartitionKey(CITY), Address.class, patchSetOperation);
assertThat(patchedAddress.getStreet()).isEqualTo(TestConstants.NEW_STREET);
}
@Test
public void testPatchEntityReplace() {
Address patchedAddress = repository.save(TestConstants.POSTAL_CODE, new PartitionKey(CITY), Address.class, patchReplaceOperation);
assertThat(patchedAddress.getStreet()).isEqualTo(TestConstants.NEW_STREET);
}
@Test
public void testPatchEntityRemove() {
Address patchedAddress = repository.save(TestConstants.POSTAL_CODE, new PartitionKey(CITY), Address.class, patchRemoveOperation);
assertNull(patchedAddress.getStreet());
}
@Test
public void testPatchPreConditionSuccess() {
options.setFilterPredicate("FROM address a WHERE a.city = '"+CITY+"'");
Address patchedAddress = repository.save(TestConstants.POSTAL_CODE, new PartitionKey(CITY), Address.class, patchSetOperation, options);
assertThat(patchedAddress.getStreet()).isEqualTo(TestConstants.NEW_STREET);
}
@Test
public void testPatchPreConditionFail() {
try {
options.setFilterPredicate("FROM address a WHERE a.city = 'dummy'");
Address patchedAddress = repository.save(TestConstants.POSTAL_CODE, new PartitionKey(CITY), Address.class, patchSetOperation, options);
assertThat(patchedAddress.getStreet()).isEqualTo(TestConstants.NEW_STREET);
Assert.fail();
} catch (CosmosAccessException ex) {
assertThat(ex.getCosmosException().getStatusCode()).isEqualTo(TestConstants.PRECONDITION_FAILED_STATUS_CODE);
}
}
}