Skip to content

Commit af85b46

Browse files
christophstroblmp911de
authored andcommittedMay 11, 2017
DATAMONGO-1685 - Adapt to QueryByExampleExecutor API changes.
Use Optional as return type for findOne(Example example). Related ticket: DATACMNS-1058. Original pull request: spring-projects#460.
1 parent 96fbe49 commit af85b46

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed
 

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,13 @@ public <S extends T> List<S> findAll(Example<S> example) {
312312
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
313313
*/
314314
@Override
315-
public <S extends T> S findOne(Example<S> example) {
315+
public <S extends T> Optional<S> findOne(Example<S> example) {
316316

317317
Assert.notNull(example, "Sample must not be null!");
318318

319319
Query q = new Query(new Criteria().alike(example));
320-
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
320+
return Optional
321+
.ofNullable(mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()));
321322
}
322323

323324
/*

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @author Oliver Gierke
3333
* @author Mark Paluch
34+
* @author Christoph Strobl
3435
*/
3536
@RunWith(SpringJUnit4ClassRunner.class)
3637
@ContextConfiguration("config/MongoNamespaceIntegrationTests-context.xml")
@@ -57,6 +58,6 @@ public void findsContactByTypedExample() {
5758

5859
Person person = repository.save(new Person("Oliver", "Gierke"));
5960

60-
assertThat(repository.findOne(Example.of(person)), instanceOf(Person.class));
61+
assertThat(repository.findOne(Example.of(person)).get(), instanceOf(Person.class));
6162
}
6263
}

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Set;
3030
import java.util.UUID;
3131

32+
import org.assertj.core.api.Assertions;
3233
import org.junit.Before;
3334
import org.junit.Test;
3435
import org.junit.runner.RunWith;
@@ -376,9 +377,9 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() {
376377
sample.setLastname("Matthews");
377378
trimDomainType(sample, "id", "createdAt", "email");
378379

379-
Person result = repository.findOne(Example.of(sample));
380+
Optional<Person> result = repository.findOne(Example.of(sample));
380381

381-
assertThat(result, is(equalTo(dave)));
382+
Assertions.assertThat(result).isPresent().contains(dave);
382383
}
383384

384385
@Test // DATAMONGO-1245

0 commit comments

Comments
 (0)
Please sign in to comment.