Search in sources :

Example 6 with Person

use of org.immutables.criteria.personmodel.Person in project immutables by immutables.

the class GetAllOptimizationTest method withoutIdResolver.

/**
 * {@link Region#getAll(Collection)} should not be called without
 * known key metadata
 */
@Test
void withoutIdResolver() {
    // custom extractor based on generic function
    KeyExtractor.Factory extractor = KeyExtractor.generic(obj -> {
        Person person = (Person) obj;
        return person.id();
    });
    PersonRepository repository = createRepository(builder -> builder.keyExtractorFactory(extractor));
    repository.find(person.id.is("id1")).fetch();
    check(calls).isEmpty();
    repository.find(person.id.in("id1", "id2")).fetch();
    check(calls).isEmpty();
    // negatives
    repository.find(person.id.isNot("id1")).fetch();
    check(calls.isEmpty());
    repository.find(person.id.notIn("id1", "id2")).fetch();
    check(calls.isEmpty());
}
Also used : PersonRepository(org.immutables.criteria.personmodel.PersonRepository) KeyExtractor(org.immutables.criteria.backend.KeyExtractor) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 7 with Person

use of org.immutables.criteria.personmodel.Person in project immutables by immutables.

the class UpdatableTest method name.

@Test
@Disabled("compile-time only")
void name() {
    PersonCriteria person = PersonCriteria.person;
    Updater<Person, Single<WriteResult>> updater = null;
    updater.set(person.nickName, Optional.of("aaa")).set(person, // instead of replace ?
    ImmutablePerson.builder().build()).set(person.fullName, "John").set(person.isActive, true).execute();
    updater.replace(ImmutablePerson.builder().build()).execute();
}
Also used : Single(io.reactivex.Single) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Person(org.immutables.criteria.personmodel.Person) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 8 with Person

use of org.immutables.criteria.personmodel.Person in project immutables by immutables.

the class ChangeStreamsTest method multiFilter.

@Test
void multiFilter() throws InterruptedException {
    PersonCriteria filter = person.age.atLeast(21).isActive.isTrue();
    Flowable<WatchEvent<Person>> flowable = Flowable.fromPublisher(repository.watcher(filter).watch());
    TestSubscriber<WatchEvent<Person>> test = flowable.test();
    repository.insert(generator.next().withId("id1").withAge(19));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id2").withAge(21).withIsActive(false));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id3").withAge(21).withIsActive(true));
    test.awaitCount(1).assertValueCount(1);
    Person person = test.values().get(0).newValue().get();
    check(person.id()).is("id3");
}
Also used : PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) WatchEvent(org.immutables.criteria.backend.WatchEvent) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 9 with Person

use of org.immutables.criteria.personmodel.Person in project immutables by immutables.

the class ExpressionInterpreterTest method reflection.

@Test
public void reflection() {
    final PersonCriteria crit = PersonCriteria.person;
    final ImmutablePerson person = example.withFullName("John");
    check(!evaluate(crit.age.is(11), person));
    check(evaluate(crit.age.isNot(11), person));
    check(evaluate(crit.age.is(11), person.withAge(11)));
    check(evaluate(crit.age.is(22).fullName.is("John"), person));
    check(!evaluate(crit.age.is(22).fullName.is("Mary"), person));
    check(!evaluate(crit.age.in(1, 2, 3), person));
    check(evaluate(crit.age.notIn(1, 2, 3), person));
    check(evaluate(crit.age.in(22, 23, 24), person));
    check(!evaluate(crit.age.notIn(22, 23, 24), person));
    check(!evaluate(crit.isActive.isTrue(), person));
    check(evaluate(crit.isActive.isFalse(), person));
    check(evaluate(crit.isActive.isTrue().or().isActive.isFalse(), person));
    check(evaluate(crit.isActive.isFalse().or().isActive.isTrue().nickName.isAbsent(), person));
    check(!evaluate(crit.age.atLeast(23), person));
    check(evaluate(crit.age.atMost(22), person));
    check(!evaluate(crit.age.lessThan(22), person));
    check(!evaluate(crit.age.greaterThan(22), person));
    check(!evaluate(crit.age.atLeast(23), person));
    // optionals
    check(evaluate(crit.nickName.isPresent(), person));
    check(!evaluate(crit.nickName.isAbsent(), person));
    check(!evaluate(crit.nickName.isPresent(), person.withNickName(Optional.empty())));
    check(evaluate(crit.nickName.isAbsent(), person.withNickName(Optional.empty())));
    // == value().$expr
    check(!evaluate(crit.nickName.isNot("Smith"), person.withNickName("Smith")));
    check(evaluate(crit.nickName.in("Smith", "Nobody"), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.in("Nobody", "Sky"), person.withNickName("Smith")));
    check(evaluate(crit.nickName.notIn("Nobody", "Sky"), person.withNickName("Smith")));
    // == value($expr)
    check(evaluate(crit.nickName.with(v -> v.is("Smith")), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.with(v -> v.isNot("Smith")), person.withNickName("Smith")));
    check(evaluate(crit.nickName.with(v -> v.in("Smith", "Nobody")), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.with(v -> v.in("Nobody", "Sky")), person.withNickName("Smith")));
    check(evaluate(crit.nickName.with(v -> v.notIn("Nobody", "Sky")), person.withNickName("Smith")));
}
Also used : Test(org.junit.jupiter.api.Test) ImmutableAddress(org.immutables.criteria.personmodel.ImmutableAddress) Person(org.immutables.criteria.personmodel.Person) Checkers.check(org.immutables.check.Checkers.check) Address(org.immutables.criteria.personmodel.Address) LocalDate(java.time.LocalDate) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Criterias(org.immutables.criteria.Criterias) Optional(java.util.Optional) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) Test(org.junit.jupiter.api.Test)

Example 10 with Person

use of org.immutables.criteria.personmodel.Person in project immutables by immutables.

the class AggregateQueryBuilderTest method agg1.

@Test
void agg1() {
    PersonCriteria person = PersonCriteria.person;
    // select nickName, sum(age), max(dateOfBirth) from person
    // where age >= 30
    // group by nickName
    // order by nickName
    // limit 11
    Query query = Query.of(Person.class).addGroupBy(Matchers.toExpression(person.nickName)).addCollations(Collections.singleton(Collation.of(Matchers.toExpression(person.nickName)))).addProjections(Matchers.toExpression(person.nickName), Matchers.toExpression(person.age.sum()), Matchers.toExpression(person.dateOfBirth.max())).withFilter(Criterias.toQuery(person.age.atLeast(30)).filter().get()).withLimit(11);
    AggregateQueryBuilder builder = new AggregateQueryBuilder(query, MAPPER, mapping, pathNaming, idPredicate);
    ObjectNode json = builder.jsonQuery();
    JsonChecker.of(json).is("{'_source':false,", "size:0,", "'stored_fields':'_none_',", "'query.constant_score.filter.range.age.gte' : 30,", "aggregations:{expr0:{terms:{field:'nickName',missing:'__MISSING__',size:11," + " order:{'_key':'asc'}},", "aggregations:{expr1:{sum:{field:'age'}},expr2:{max:{field:'dateOfBirth'}}}}}}");
}
Also used : Query(org.immutables.criteria.expression.Query) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Aggregations

Person (org.immutables.criteria.personmodel.Person)10 Test (org.junit.jupiter.api.Test)8 PersonCriteria (org.immutables.criteria.personmodel.PersonCriteria)6 WatchEvent (org.immutables.criteria.backend.WatchEvent)3 ImmutablePerson (org.immutables.criteria.personmodel.ImmutablePerson)3 PersonGenerator (org.immutables.criteria.personmodel.PersonGenerator)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)2 ReflectionBasedAutoSerializer (org.apache.geode.pdx.ReflectionBasedAutoSerializer)2 Checkers.check (org.immutables.check.Checkers.check)2 PersonRepository (org.immutables.criteria.personmodel.PersonRepository)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Disabled (org.junit.jupiter.api.Disabled)2 Flowable (io.reactivex.Flowable)1 Single (io.reactivex.Single)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1 LocalDate (java.time.LocalDate)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Region (org.apache.geode.cache.Region)1