Skip to content

Commit 512e9ea

Browse files
michael-wirthodrotbohm
authored andcommitted
DATAES-363 - Fixed CrudRepository.existsById(…) implementation.
Properly use Optional.isPresent() over a null check. Original pull request: spring-projects#183.
1 parent b8400d8 commit 512e9ea

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Kevin Leturc
5656
* @author Mark Paluch
5757
* @author Christoph Strobl
58+
* @author Michael Wirth
5859
*/
5960
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable>
6061
implements ElasticsearchRepository<T, ID> {
@@ -188,7 +189,7 @@ public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
188189

189190
@Override
190191
public boolean existsById(ID id) {
191-
return findById(id) != null;
192+
return findById(id).isPresent();
192193
}
193194

194195
@Override

src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepositoryTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* @author Mohsin Husen
4848
* @author Mark Paluch
4949
* @author Christoph Strobl
50+
* @author Michael Wirth
5051
*/
5152
@RunWith(SpringJUnit4ClassRunner.class)
5253
@ContextConfiguration("classpath:/simple-repository-test.xml")
@@ -274,6 +275,18 @@ public void shouldReturnTrueGivenDocumentWithIdExists() {
274275
assertEquals(exist, true);
275276
}
276277

278+
@Test // DATAES-363
279+
public void shouldReturnFalseGivenDocumentWithIdDoesNotExist() {
280+
// given
281+
String documentId = randomNumeric(5);
282+
283+
// when
284+
boolean exist = repository.existsById(documentId);
285+
286+
// then
287+
assertEquals(exist, false);
288+
}
289+
277290
@Test
278291
public void shouldReturnResultsForGivenSearchQuery() {
279292
// given

src/test/java/org/springframework/data/elasticsearch/repository/support/UUIDElasticsearchRepositoryTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @author Mohsin Husen
4646
* @author Mark Paluch
4747
* @author Christoph Strobl
48+
* @author Michael Wirth
4849
*/
4950
@RunWith(SpringJUnit4ClassRunner.class)
5051
@ContextConfiguration("classpath:/simple-repository-test.xml")
@@ -262,6 +263,18 @@ public void shouldReturnTrueGivenDocumentWithIdExists() {
262263
assertEquals(exist, true);
263264
}
264265

266+
@Test // DATAES-363
267+
public void shouldReturnFalseGivenDocumentWithIdDoesNotExist() {
268+
// given
269+
UUID documentId = UUID.randomUUID();
270+
271+
// when
272+
boolean exist = repository.existsById(documentId);
273+
274+
// then
275+
assertEquals(exist, false);
276+
}
277+
265278
@Test
266279
public void shouldDeleteAll() {
267280
// when

0 commit comments

Comments
 (0)