From dab55a2096c8cda85f7722c4c12c800b89c633f7 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 5 May 2017 14:30:15 +0200 Subject: [PATCH 1/3] DATAMONGO-1685 - Prepare issue branch. --- pom.xml | 4 ++-- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index eea61f492c..1bfe09b834 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT pom Spring Data MongoDB @@ -28,7 +28,7 @@ multi spring-data-mongodb - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATACMNS-1058-SNAPSHOT 3.4.2 1.3.0 diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 4a49168713..e875aa4dcf 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 750ed23aa8..33c8488634 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index 50d0a6454a..b610700d90 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 886bcca7b6..d5a019bf71 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1685-SNAPSHOT ../pom.xml From f15a3a80d3aca1fda9aa9a0d64241c3bde188c45 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 5 May 2017 14:41:47 +0200 Subject: [PATCH 2/3] DATAMONGO-1685 - Adapt to QueryByExampleExecuter API changes. Use Optional as return type for findOne(Example example). See DATACMNS-1058 for details. --- .../mongodb/repository/support/SimpleMongoRepository.java | 5 +++-- .../repository/ContactRepositoryIntegrationTests.java | 3 ++- .../repository/support/SimpleMongoRepositoryTests.java | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index 1e0830d71b..3ea1c45737 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -312,12 +312,13 @@ public List findAll(Example example) { * @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example) */ @Override - public S findOne(Example example) { + public Optional findOne(Example example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); - return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()); + return Optional + .ofNullable(mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName())); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java index 035050de37..5918016bc8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java @@ -31,6 +31,7 @@ * * @author Oliver Gierke * @author Mark Paluch + * @author Christoph Strobl */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("config/MongoNamespaceIntegrationTests-context.xml") @@ -57,6 +58,6 @@ public void findsContactByTypedExample() { Person person = repository.save(new Person("Oliver", "Gierke")); - assertThat(repository.findOne(Example.of(person)), instanceOf(Person.class)); + assertThat(repository.findOne(Example.of(person)).get(), instanceOf(Person.class)); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java index 044df3a874..88b2a02f85 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java @@ -29,6 +29,7 @@ import java.util.Set; import java.util.UUID; +import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -376,9 +377,9 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() { sample.setLastname("Matthews"); trimDomainType(sample, "id", "createdAt", "email"); - Person result = repository.findOne(Example.of(sample)); + Optional result = repository.findOne(Example.of(sample)); - assertThat(result, is(equalTo(dave))); + Assertions.assertThat(result).isPresent().contains(dave); } @Test // DATAMONGO-1245 From e787f2cc06a5e6c526e525375215e315acba077b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 5 May 2017 15:08:06 +0200 Subject: [PATCH 3/3] DATAMONGO-1685 - Polishing. Migrate assertions to AssertJ. --- .../ContactRepositoryIntegrationTests.java | 7 +- .../support/SimpleMongoRepositoryTests.java | 99 ++++++------------- 2 files changed, 32 insertions(+), 74 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java index 5918016bc8..ebb9dd4385 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java @@ -15,8 +15,7 @@ */ package org.springframework.data.mongodb.repository; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; import org.junit.Before; import org.junit.Test; @@ -50,7 +49,7 @@ public void readsAndWritesContactCorrectly() { Person person = new Person("Oliver", "Gierke"); Contact result = repository.save(person); - assertTrue(repository.findById(result.getId().toString()).get() instanceof Person); + assertThat(repository.findById(result.getId().toString())).containsInstanceOf(Person.class); } @Test // DATAMONGO-1245 @@ -58,6 +57,6 @@ public void findsContactByTypedExample() { Person person = repository.save(new Person("Oliver", "Gierke")); - assertThat(repository.findOne(Example.of(person)).get(), instanceOf(Person.class)); + assertThat(repository.findOne(Example.of(person))).containsInstanceOf(Person.class); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java index 88b2a02f85..b7322abb17 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java @@ -15,8 +15,7 @@ */ package org.springframework.data.mongodb.repository.support; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; import static org.springframework.data.domain.ExampleMatcher.*; import java.util.ArrayList; @@ -29,15 +28,14 @@ import java.util.Set; import java.util.UUID; -import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleMatcher.StringMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.ExampleMatcher.*; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.geo.GeoJsonPoint; @@ -87,32 +85,28 @@ public void setUp() { @Test public void findALlFromCustomCollectionName() { - List result = repository.findAll(); - assertThat(result, hasSize(all.size())); + assertThat(repository.findAll()).hasSize(all.size()); } @Test public void findOneFromCustomCollectionName() { - Person result = repository.findById(dave.getId()).get(); - assertThat(result, is(dave)); + assertThat(repository.findById(dave.getId()).get()).isEqualTo(dave); } @Test public void deleteFromCustomCollectionName() { + repository.delete(dave); - List result = repository.findAll(); - assertThat(result, hasSize(all.size() - 1)); - assertThat(result, not(hasItem(dave))); + assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave); } @Test public void deleteByIdFromCustomCollectionName() { + repository.deleteById(dave.getId()); - List result = repository.findAll(); - assertThat(result, hasSize(all.size() - 1)); - assertThat(result, not(hasItem(dave))); + assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave); } @Test // DATAMONGO-1054 @@ -123,9 +117,7 @@ public void shouldInsertSingle() { Person person1 = new Person("First1" + randomId, "Last2" + randomId, 42); person1 = repository.insert(person1); - Person saved = repository.findById(person1.getId()).get(); - - assertThat(saved, is(equalTo(person1))); + assertThat(repository.findById(person1.getId())).contains(person1); } @Test // DATAMONGO-1054 @@ -143,7 +135,7 @@ public void shouldInsertMultipleFromList() { List saved = repository.insert(persons); - assertThat(saved, hasSize(persons.size())); + assertThat(saved).hasSize(persons.size()); assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); } @@ -162,7 +154,7 @@ public void shouldInsertMutlipleFromSet() { List saved = repository.insert(persons); - assertThat(saved, hasSize(persons.size())); + assertThat(saved).hasSize(persons.size()); assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); } @@ -175,9 +167,8 @@ public void findByExampleShouldLookUpEntriesCorrectly() { Page result = repository.findAll(Example.of(sample), PageRequest.of(0, 10)); - assertThat(result.getContent(), hasItems(dave, oliver)); - assertThat(result.getContent(), hasSize(2)); - assertThat(result.getTotalPages(), is(1)); + assertThat(result.getContent()).hasSize(2).contains(dave, oliver); + assertThat(result.getTotalPages()).isEqualTo(1); } @Test // DATAMONGO-1464 @@ -189,8 +180,8 @@ public void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() { Page result = repository.findAll(Example.of(sample), PageRequest.of(0, 1)); - assertThat(result.getContent(), hasSize(1)); - assertThat(result.getTotalPages(), is(2)); + assertThat(result.getContent()).hasSize(1); + assertThat(result.getTotalPages()).isEqualTo(2); } @Test // DATAMONGO-1245 @@ -200,10 +191,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectly() { sample.setLastname("Matthews"); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, containsInAnyOrder(dave, oliver)); - assertThat(result, hasSize(2)); + assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver); } @Test // DATAMONGO-1245 @@ -219,10 +207,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject() sample.setAddress(dave.getAddress()); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasItem(dave)); - assertThat(result, hasSize(1)); + assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(dave); } @Test // DATAMONGO-1245 @@ -238,10 +223,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedOb sample.setAddress(new Address(null, null, "Washington")); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasItems(dave, oliver)); - assertThat(result, hasSize(2)); + assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver); } @Test // DATAMONGO-1245 @@ -255,9 +237,8 @@ public void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInSt trimDomainType(sample, "id", "createdAt", "email"); Example example = Example.of(sample, matching().withIncludeNullValues()); - List result = repository.findAll(example); - assertThat(result, empty()); + assertThat(repository.findAll(example)).isEmpty(); } @Test // DATAMONGO-1245 @@ -271,10 +252,8 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInS trimDomainType(sample, "id", "createdAt", "email"); Example example = Example.of(sample, matching().withIncludeNullValues()); - List result = repository.findAll(example); - assertThat(result, hasItem(dave)); - assertThat(result, hasSize(1)); + assertThat(repository.findAll(example)).hasSize(1).contains(dave); } @Test // DATAMONGO-1245 @@ -285,10 +264,8 @@ public void findAllByExampleShouldRespectStringMatchMode() { trimDomainType(sample, "id", "createdAt", "email"); Example example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING)); - List result = repository.findAll(example); - assertThat(result, hasItems(dave, oliver)); - assertThat(result, hasSize(2)); + assertThat(repository.findAll(example)).hasSize(2).contains(dave, oliver); } @Test // DATAMONGO-1245 @@ -308,10 +285,7 @@ public void findAllByExampleShouldResolveDbRefCorrectly() { sample.setCreator(user); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasItem(megan)); - assertThat(result, hasSize(1)); + assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan); } @Test // DATAMONGO-1245 @@ -326,10 +300,7 @@ public void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() { sample.setLocation(megan.getLocation()); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasItem(megan)); - assertThat(result, hasSize(1)); + assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan); } @Test // DATAMONGO-1245 @@ -344,10 +315,7 @@ public void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() { sample.setLocation(megan.getLocation()); trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasItem(megan)); - assertThat(result, hasSize(1)); + assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan); } @Test // DATAMONGO-1245 @@ -363,10 +331,7 @@ public void findAllByExampleShouldProcessInheritanceCorrectly() { trimDomainType(sample, "id", "createdAt", "email"); - List result = repository.findAll(Example.of(sample)); - - assertThat(result, hasSize(1)); - assertThat(result, hasItem(reference)); + assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(reference); } @Test // DATAMONGO-1245 @@ -377,9 +342,7 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() { sample.setLastname("Matthews"); trimDomainType(sample, "id", "createdAt", "email"); - Optional result = repository.findOne(Example.of(sample)); - - Assertions.assertThat(result).isPresent().contains(dave); + assertThat(repository.findOne(Example.of(sample))).isPresent().contains(dave); } @Test // DATAMONGO-1245 @@ -390,9 +353,7 @@ public void existsByExampleShouldLookUpEntriesCorrectly() { sample.setLastname("Matthews"); trimDomainType(sample, "id", "createdAt", "email"); - boolean result = repository.exists(Example.of(sample)); - - assertThat(result, is(true)); + assertThat(repository.exists(Example.of(sample))).isTrue(); } @Test // DATAMONGO-1245 @@ -402,16 +363,14 @@ public void countByExampleShouldLookUpEntriesCorrectly() { sample.setLastname("Matthews"); trimDomainType(sample, "id", "createdAt", "email"); - long result = repository.count(Example.of(sample)); - - assertThat(result, is(equalTo(2L))); + assertThat(repository.count(Example.of(sample))).isEqualTo(2L); } private void assertThatAllReferencePersonsWereStoredCorrectly(Map references, List saved) { for (Person person : saved) { Person reference = references.get(person.getId()); - assertThat(person, is(equalTo(reference))); + assertThat(person).isEqualTo(reference); } }