|
1 | 1 | package com.redis.om.spring.annotations.document;
|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat;
|
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
4 | 5 |
|
5 | 6 | import java.util.*;
|
6 | 7 | import java.util.stream.Collectors;
|
@@ -286,4 +287,57 @@ void testExtremeReferencesWithFindAll() {
|
286 | 287 | assertThat(tmr.getRef10()).isNotNull();
|
287 | 288 | }
|
288 | 289 | }
|
| 290 | + |
| 291 | + @Test |
| 292 | + void testNPEWithMissingReference() { |
| 293 | + // First create a city with a valid state reference |
| 294 | + var usa = countryRepository.save(Country.of("USA")); |
| 295 | + var fl = stateRepository.save(State.of("FL", "Florida", usa)); |
| 296 | + var miami = cityRepository.save(City.of("Miami", fl)); |
| 297 | + |
| 298 | + // Verify the city was saved correctly |
| 299 | + var savedCity = cityRepository.findById("Miami"); |
| 300 | + assertThat(savedCity).isPresent(); |
| 301 | + assertThat(savedCity.get().getState().getId()).isEqualTo("FL"); |
| 302 | + |
| 303 | + // Now delete the state that the city references |
| 304 | + stateRepository.deleteById("FL"); |
| 305 | + |
| 306 | + // Verify the state is gone |
| 307 | + var deletedState = stateRepository.findById("FL"); |
| 308 | + assertThat(deletedState).isEmpty(); |
| 309 | + |
| 310 | + // After the fix, this should no longer throw NPE |
| 311 | + // Instead, it should return the city with null state reference |
| 312 | + var cityWithMissingRef = cityRepository.findById("Miami"); |
| 313 | + assertThat(cityWithMissingRef).isPresent(); |
| 314 | + assertThat(cityWithMissingRef.get().getId()).isEqualTo("Miami"); |
| 315 | + assertThat(cityWithMissingRef.get().getState()).isNull(); |
| 316 | + } |
| 317 | + |
| 318 | + @Test |
| 319 | + void testCollectionWithMissingReferences() { |
| 320 | + // Create states and a collection referencing them |
| 321 | + var usa = countryRepository.save(Country.of("USA")); |
| 322 | + var ny = stateRepository.save(State.of("NY", "New York", usa)); |
| 323 | + var nj = stateRepository.save(State.of("NJ", "New Jersey", usa)); |
| 324 | + var ct = stateRepository.save(State.of("CT", "Connecticut", usa)); |
| 325 | + |
| 326 | + var statesCollection = statesRepository.save(States.of("Tri-State Area", Set.of(ny, nj, ct))); |
| 327 | + |
| 328 | + // Verify the collection was saved correctly |
| 329 | + var saved = statesRepository.findById("Tri-State Area"); |
| 330 | + assertThat(saved).isPresent(); |
| 331 | + assertThat(saved.get().getStates()).hasSize(3); |
| 332 | + |
| 333 | + // Delete one of the referenced states |
| 334 | + stateRepository.deleteById("NJ"); |
| 335 | + |
| 336 | + // Retrieving the collection should not throw NPE |
| 337 | + // It should only contain the existing states |
| 338 | + var collectionWithMissingRef = statesRepository.findById("Tri-State Area"); |
| 339 | + assertThat(collectionWithMissingRef).isPresent(); |
| 340 | + assertThat(collectionWithMissingRef.get().getStates()).hasSize(2); |
| 341 | + assertThat(collectionWithMissingRef.get().getStates()).extracting("id").containsExactlyInAnyOrder("NY", "CT"); |
| 342 | + } |
289 | 343 | }
|
0 commit comments