Skip to content

DATAMONGO-1685 - Adapt to QueryByExampleExecutor API changes. #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -28,7 +28,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.0.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>2.0.0.DATACMNS-1058-SNAPSHOT</springdata.commons>
<mongo>3.4.2</mongo>
<mongo.reactivestreams>1.3.0</mongo.reactivestreams>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1685-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,13 @@ public <S extends T> List<S> findAll(Example<S> example) {
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> S findOne(Example<S> example) {
public <S extends T> Optional<S> findOne(Example<S> 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()));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +30,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("config/MongoNamespaceIntegrationTests-context.xml")
Expand All @@ -49,14 +49,14 @@ 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
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))).containsInstanceOf(Person.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,9 +33,9 @@
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;
Expand Down Expand Up @@ -86,32 +85,28 @@ public void setUp() {

@Test
public void findALlFromCustomCollectionName() {
List<Person> 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<Person> 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<Person> 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
Expand All @@ -122,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
Expand All @@ -142,7 +135,7 @@ public void shouldInsertMultipleFromList() {

List<Person> saved = repository.insert(persons);

assertThat(saved, hasSize(persons.size()));
assertThat(saved).hasSize(persons.size());
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
}

Expand All @@ -161,7 +154,7 @@ public void shouldInsertMutlipleFromSet() {

List<Person> saved = repository.insert(persons);

assertThat(saved, hasSize(persons.size()));
assertThat(saved).hasSize(persons.size());
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
}

Expand All @@ -174,9 +167,8 @@ public void findByExampleShouldLookUpEntriesCorrectly() {

Page<Person> 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
Expand All @@ -188,8 +180,8 @@ public void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() {

Page<Person> 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
Expand All @@ -199,10 +191,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectly() {
sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -218,10 +207,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject()
sample.setAddress(dave.getAddress());
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -237,10 +223,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedOb
sample.setAddress(new Address(null, null, "Washington"));
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -254,9 +237,8 @@ public void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInSt
trimDomainType(sample, "id", "createdAt", "email");

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
Expand All @@ -270,10 +252,8 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInS
trimDomainType(sample, "id", "createdAt", "email");

Example<Person> example = Example.of(sample, matching().withIncludeNullValues());
List<Person> result = repository.findAll(example);

assertThat(result, hasItem(dave));
assertThat(result, hasSize(1));
assertThat(repository.findAll(example)).hasSize(1).contains(dave);
}

@Test // DATAMONGO-1245
Expand All @@ -284,10 +264,8 @@ public void findAllByExampleShouldRespectStringMatchMode() {
trimDomainType(sample, "id", "createdAt", "email");

Example<Person> example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING));
List<Person> 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
Expand All @@ -307,10 +285,7 @@ public void findAllByExampleShouldResolveDbRefCorrectly() {
sample.setCreator(user);
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -325,10 +300,7 @@ public void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() {
sample.setLocation(megan.getLocation());
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -343,10 +315,7 @@ public void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() {
sample.setLocation(megan.getLocation());
trimDomainType(sample, "id", "createdAt", "email");

List<Person> 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
Expand All @@ -362,10 +331,7 @@ public void findAllByExampleShouldProcessInheritanceCorrectly() {

trimDomainType(sample, "id", "createdAt", "email");

List<PersonExtended> 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
Expand All @@ -376,9 +342,7 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() {
sample.setLastname("Matthews");
trimDomainType(sample, "id", "createdAt", "email");

Person result = repository.findOne(Example.of(sample));

assertThat(result, is(equalTo(dave)));
assertThat(repository.findOne(Example.of(sample))).isPresent().contains(dave);
}

@Test // DATAMONGO-1245
Expand All @@ -389,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
Expand All @@ -401,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<String, Person> references, List<Person> saved) {

for (Person person : saved) {
Person reference = references.get(person.getId());
assertThat(person, is(equalTo(reference)));
assertThat(person).isEqualTo(reference);
}
}

Expand Down