DATAMONGO-1685 - Polishing.

Migrate assertions to AssertJ. Fix Javadoc.

Original pull request: #460.
This commit is contained in:
Christoph Strobl
2017-05-05 15:08:06 +02:00
committed by Mark Paluch
parent af85b46e7d
commit bb84b92d1d
3 changed files with 37 additions and 79 deletions

View File

@@ -109,7 +109,7 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) * @see org.springframework.data.repository.CrudRepository#findById(java.io.Serializable)
*/ */
@Override @Override
public Optional<T> findById(ID id) { public Optional<T> findById(ID id) {

View File

@@ -15,8 +15,7 @@
*/ */
package org.springframework.data.mongodb.repository; package org.springframework.data.mongodb.repository;
import static org.hamcrest.Matchers.*; import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -50,7 +49,7 @@ public class ContactRepositoryIntegrationTests {
Person person = new Person("Oliver", "Gierke"); Person person = new Person("Oliver", "Gierke");
Contact result = repository.save(person); 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 @Test // DATAMONGO-1245
@@ -58,6 +57,6 @@ public class ContactRepositoryIntegrationTests {
Person person = repository.save(new Person("Oliver", "Gierke")); 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);
} }
} }

View File

@@ -15,8 +15,7 @@
*/ */
package org.springframework.data.mongodb.repository.support; package org.springframework.data.mongodb.repository.support;
import static org.hamcrest.Matchers.*; import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.ExampleMatcher.*; import static org.springframework.data.domain.ExampleMatcher.*;
import java.util.ArrayList; import java.util.ArrayList;
@@ -29,15 +28,14 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.assertj.core.api.Assertions;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.ExampleMatcher.*;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
@@ -87,32 +85,28 @@ public class SimpleMongoRepositoryTests {
@Test @Test
public void findALlFromCustomCollectionName() { public void findALlFromCustomCollectionName() {
List<Person> result = repository.findAll(); assertThat(repository.findAll()).hasSize(all.size());
assertThat(result, hasSize(all.size()));
} }
@Test @Test
public void findOneFromCustomCollectionName() { public void findOneFromCustomCollectionName() {
Person result = repository.findById(dave.getId()).get(); assertThat(repository.findById(dave.getId()).get()).isEqualTo(dave);
assertThat(result, is(dave));
} }
@Test @Test
public void deleteFromCustomCollectionName() { public void deleteFromCustomCollectionName() {
repository.delete(dave);
List<Person> result = repository.findAll();
assertThat(result, hasSize(all.size() - 1)); repository.delete(dave);
assertThat(result, not(hasItem(dave)));
assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave);
} }
@Test @Test
public void deleteByIdFromCustomCollectionName() { public void deleteByIdFromCustomCollectionName() {
repository.deleteById(dave.getId());
List<Person> result = repository.findAll();
assertThat(result, hasSize(all.size() - 1)); repository.deleteById(dave.getId());
assertThat(result, not(hasItem(dave)));
assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave);
} }
@Test // DATAMONGO-1054 @Test // DATAMONGO-1054
@@ -123,9 +117,7 @@ public class SimpleMongoRepositoryTests {
Person person1 = new Person("First1" + randomId, "Last2" + randomId, 42); Person person1 = new Person("First1" + randomId, "Last2" + randomId, 42);
person1 = repository.insert(person1); person1 = repository.insert(person1);
Person saved = repository.findById(person1.getId()).get(); assertThat(repository.findById(person1.getId())).contains(person1);
assertThat(saved, is(equalTo(person1)));
} }
@Test // DATAMONGO-1054 @Test // DATAMONGO-1054
@@ -143,7 +135,7 @@ public class SimpleMongoRepositoryTests {
List<Person> saved = repository.insert(persons); List<Person> saved = repository.insert(persons);
assertThat(saved, hasSize(persons.size())); assertThat(saved).hasSize(persons.size());
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
} }
@@ -162,7 +154,7 @@ public class SimpleMongoRepositoryTests {
List<Person> saved = repository.insert(persons); List<Person> saved = repository.insert(persons);
assertThat(saved, hasSize(persons.size())); assertThat(saved).hasSize(persons.size());
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
} }
@@ -175,9 +167,8 @@ public class SimpleMongoRepositoryTests {
Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 10)); Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 10));
assertThat(result.getContent(), hasItems(dave, oliver)); assertThat(result.getContent()).hasSize(2).contains(dave, oliver);
assertThat(result.getContent(), hasSize(2)); assertThat(result.getTotalPages()).isEqualTo(1);
assertThat(result.getTotalPages(), is(1));
} }
@Test // DATAMONGO-1464 @Test // DATAMONGO-1464
@@ -189,8 +180,8 @@ public class SimpleMongoRepositoryTests {
Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 1)); Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 1));
assertThat(result.getContent(), hasSize(1)); assertThat(result.getContent()).hasSize(1);
assertThat(result.getTotalPages(), is(2)); assertThat(result.getTotalPages()).isEqualTo(2);
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -200,10 +191,7 @@ public class SimpleMongoRepositoryTests {
sample.setLastname("Matthews"); sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver);
assertThat(result, containsInAnyOrder(dave, oliver));
assertThat(result, hasSize(2));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -219,10 +207,7 @@ public class SimpleMongoRepositoryTests {
sample.setAddress(dave.getAddress()); sample.setAddress(dave.getAddress());
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(dave);
assertThat(result, hasItem(dave));
assertThat(result, hasSize(1));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -238,10 +223,7 @@ public class SimpleMongoRepositoryTests {
sample.setAddress(new Address(null, null, "Washington")); sample.setAddress(new Address(null, null, "Washington"));
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver);
assertThat(result, hasItems(dave, oliver));
assertThat(result, hasSize(2));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -255,9 +237,8 @@ public class SimpleMongoRepositoryTests {
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
Example<Person> example = Example.of(sample, matching().withIncludeNullValues()); Example<Person> example = Example.of(sample, matching().withIncludeNullValues());
List<Person> result = repository.findAll(example);
assertThat(result, empty()); assertThat(repository.findAll(example)).isEmpty();
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -271,10 +252,8 @@ public class SimpleMongoRepositoryTests {
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
Example<Person> example = Example.of(sample, matching().withIncludeNullValues()); Example<Person> example = Example.of(sample, matching().withIncludeNullValues());
List<Person> result = repository.findAll(example);
assertThat(result, hasItem(dave)); assertThat(repository.findAll(example)).hasSize(1).contains(dave);
assertThat(result, hasSize(1));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -285,10 +264,8 @@ public class SimpleMongoRepositoryTests {
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
Example<Person> example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING)); Example<Person> example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING));
List<Person> result = repository.findAll(example);
assertThat(result, hasItems(dave, oliver)); assertThat(repository.findAll(example)).hasSize(2).contains(dave, oliver);
assertThat(result, hasSize(2));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -308,10 +285,7 @@ public class SimpleMongoRepositoryTests {
sample.setCreator(user); sample.setCreator(user);
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
assertThat(result, hasItem(megan));
assertThat(result, hasSize(1));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -326,10 +300,7 @@ public class SimpleMongoRepositoryTests {
sample.setLocation(megan.getLocation()); sample.setLocation(megan.getLocation());
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
assertThat(result, hasItem(megan));
assertThat(result, hasSize(1));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -344,10 +315,7 @@ public class SimpleMongoRepositoryTests {
sample.setLocation(megan.getLocation()); sample.setLocation(megan.getLocation());
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<Person> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
assertThat(result, hasItem(megan));
assertThat(result, hasSize(1));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -363,10 +331,7 @@ public class SimpleMongoRepositoryTests {
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
List<PersonExtended> result = repository.findAll(Example.of(sample)); assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(reference);
assertThat(result, hasSize(1));
assertThat(result, hasItem(reference));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -377,9 +342,7 @@ public class SimpleMongoRepositoryTests {
sample.setLastname("Matthews"); sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
Optional<Person> result = repository.findOne(Example.of(sample)); assertThat(repository.findOne(Example.of(sample))).isPresent().contains(dave);
Assertions.assertThat(result).isPresent().contains(dave);
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -390,9 +353,7 @@ public class SimpleMongoRepositoryTests {
sample.setLastname("Matthews"); sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
boolean result = repository.exists(Example.of(sample)); assertThat(repository.exists(Example.of(sample))).isTrue();
assertThat(result, is(true));
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
@@ -402,16 +363,14 @@ public class SimpleMongoRepositoryTests {
sample.setLastname("Matthews"); sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email"); trimDomainType(sample, "id", "createdAt", "email");
long result = repository.count(Example.of(sample)); assertThat(repository.count(Example.of(sample))).isEqualTo(2L);
assertThat(result, is(equalTo(2L)));
} }
private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) { private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {
for (Person person : saved) { for (Person person : saved) {
Person reference = references.get(person.getId()); Person reference = references.get(person.getId());
assertThat(person, is(equalTo(reference))); assertThat(person).isEqualTo(reference);
} }
} }