Search in sources :

Example 21 with ExampleMatcher

use of org.springframework.data.domain.ExampleMatcher in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleSupportsPropertyTransforms.

// GH-929
@Test
void queryByExampleSupportsPropertyTransforms() {
    Person person = new Person();
    person.setFirstname("Frodo");
    person.setLastname("Baggins");
    person.setSecret("I have the ring!");
    ExampleMatcher matcher = // 
    matching().withTransformer("firstname", o -> {
        if (o.isPresent()) {
            return o.map(o1 -> ((String) o1).toUpperCase());
        }
        return o;
    }).withTransformer("lastname", o -> {
        if (o.isPresent()) {
            return o.map(o1 -> ((String) o1).toLowerCase());
        }
        return o;
    });
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria().map(Object::toString).get()).contains(// 
    "(firstname = 'FRODO')", // 
    " AND ", // 
    "(lastname = 'baggins')", "(secret = 'I have the ring!')");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StringMatcher(org.springframework.data.domain.ExampleMatcher.StringMatcher) RelationalMappingContext(org.springframework.data.relational.core.mapping.RelationalMappingContext) GenericPropertyMatchers(org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers) Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Example(org.springframework.data.domain.Example) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Data(lombok.Data) Assertions(org.assertj.core.api.Assertions) AllArgsConstructor(lombok.AllArgsConstructor) Id(org.springframework.data.annotation.Id) NoArgsConstructor(lombok.NoArgsConstructor) Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 22 with ExampleMatcher

use of org.springframework.data.domain.ExampleMatcher in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithNullMatchingFirstnameAndLastname.

// GH-929
@Test
void queryByExampleWithNullMatchingFirstnameAndLastname() {
    Person person = new Person();
    person.setFirstname("Bilbo");
    person.setLastname("Baggins");
    ExampleMatcher matcher = matching().withIncludeNullValues();
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria().map(Object::toString).get()).contains(// 
    "(firstname IS NULL OR firstname = 'Bilbo')", // 
    " AND ", "(lastname IS NULL OR lastname = 'Baggins')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 23 with ExampleMatcher

use of org.springframework.data.domain.ExampleMatcher in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithFirstnameWithStringMatchingOnTheEnding.

// GH-929
@Test
void queryByExampleWithFirstnameWithStringMatchingOnTheEnding() {
    Person person = new Person();
    person.setFirstname("do");
    ExampleMatcher matcher = matching().withStringMatcher(ENDING);
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria()).map(// 
    Object::toString).hasValue("(firstname LIKE '%do')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 24 with ExampleMatcher

use of org.springframework.data.domain.ExampleMatcher in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithFirstnameWithFieldSpecificStringMatcherStartsWith.

// GH-929
@Test
void queryByExampleWithFirstnameWithFieldSpecificStringMatcherStartsWith() {
    Person person = new Person();
    person.setFirstname("Fro");
    ExampleMatcher matcher = matching().withMatcher("firstname", startsWith());
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria()).map(// 
    Object::toString).hasValue("(firstname LIKE 'Fro%')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 25 with ExampleMatcher

use of org.springframework.data.domain.ExampleMatcher in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithFirstnameIgnoreCaseFieldLevel.

// GH-929
@Test
void queryByExampleWithFirstnameIgnoreCaseFieldLevel() {
    Person person = new Person();
    person.setFirstname("fro");
    ExampleMatcher matcher = matching().withMatcher("firstname", startsWith().ignoreCase());
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria()).map(// 
    Object::toString).hasValue("(firstname LIKE 'fro%')");
    assertThat(example.getMatcher().getPropertySpecifiers().getForPath("firstname").getIgnoreCase()).isTrue();
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Aggregations

ExampleMatcher (org.springframework.data.domain.ExampleMatcher)33 Test (org.junit.jupiter.api.Test)20 Query (org.springframework.data.relational.core.query.Query)17 NoSuchElementException (java.util.NoSuchElementException)9 OperationFailedException (me.kadarh.mecaworks.service.exceptions.OperationFailedException)9 ResourceNotFoundException (me.kadarh.mecaworks.service.exceptions.ResourceNotFoundException)9 Objects (java.util.Objects)2 Classe (me.kadarh.mecaworks.domain.others.Classe)2 Marque (me.kadarh.mecaworks.domain.others.Marque)2 SousFamille (me.kadarh.mecaworks.domain.others.SousFamille)2 Assertions (org.assertj.core.api.Assertions)2 Test (org.junit.Test)2 Example (org.springframework.data.domain.Example)2 BaseEntity (com.google.cloud.datastore.BaseEntity)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Cursor (com.google.cloud.datastore.Cursor)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)1 Entity (com.google.cloud.datastore.Entity)1 Builder (com.google.cloud.datastore.Entity.Builder)1