Search in sources :

Example 1 with PersonRepository

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

the class GeodeCqTest method pubsub.

@Test
public void pubsub() throws Exception {
    PersonRepository repository = new PersonRepository(new GeodeBackend(GeodeSetup.of(x -> region)));
    TestSubscriber<WatchEvent<Person>> events = Flowable.fromPublisher(repository.watcher(PersonCriteria.person).watch()).test();
    final PersonGenerator generator = new PersonGenerator();
    final int count = 4;
    for (int i = 0; i < count; i++) {
        repository.insert(generator.next().withId("id" + i));
    }
    check(region.keySet()).notEmpty();
    // ensure (de)serialization is successful
    check(region.query("true")).hasSize(count);
    events.awaitCount(count);
    events.assertNoErrors();
    events.assertValueCount(count);
    check(events.values().stream().map(e -> e.newValue().get().id()).collect(Collectors.toList())).hasContentInAnyOrder("id0", "id1", "id2", "id3");
}
Also used : WatchEvent(org.immutables.criteria.backend.WatchEvent) PersonRepository(org.immutables.criteria.personmodel.PersonRepository) BeforeEach(org.junit.jupiter.api.BeforeEach) Person(org.immutables.criteria.personmodel.Person) ClientRegionShortcut(org.apache.geode.cache.client.ClientRegionShortcut) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) ReflectionBasedAutoSerializer(org.apache.geode.pdx.ReflectionBasedAutoSerializer) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) Flowable(io.reactivex.Flowable) Checkers.check(org.immutables.check.Checkers.check) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) Region(org.apache.geode.cache.Region) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) TestSubscriber(io.reactivex.subscribers.TestSubscriber) ClientCache(org.apache.geode.cache.client.ClientCache) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) PersonRepository(org.immutables.criteria.personmodel.PersonRepository) WatchEvent(org.immutables.criteria.backend.WatchEvent) Test(org.junit.jupiter.api.Test)

Example 2 with PersonRepository

use of org.immutables.criteria.personmodel.PersonRepository 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 3 with PersonRepository

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

the class GetAllOptimizationTest method optimization_getAll.

/**
 * With ID resolver {@link Region#getAll(Collection)} should be called
 */
@SuppressWarnings("unchecked")
@Test
void optimization_getAll() {
    PersonRepository repository = createRepository(builder -> builder);
    repository.find(person.id.is("id1")).fetch();
    // expect some calls to be intercepted
    if (calls.isEmpty()) {
        Assertions.fail("Region.getAll(...) was not called. Check that this optimization is enabled");
    }
    check(calls).hasSize(1);
    check(calls.get(0).method.getName()).isIn("get", "getAll");
    check((Iterable<Object>) calls.get(0).args[0]).hasAll("id1");
    calls.clear();
    repository.find(person.id.in("id1", "id2")).fetch();
    check(calls).hasSize(1);
    check(calls.get(0).method.getName()).is("getAll");
    check((Iterable<Object>) calls.get(0).args[0]).hasAll("id1", "id2");
    calls.clear();
    // negatives should not use getAll
    repository.find(person.id.isNot("id1")).fetch();
    check(calls.isEmpty());
    repository.find(person.id.notIn("id1", "id2")).fetch();
    check(calls.isEmpty());
    // any other composite expression should not trigger getAll
    repository.find(person.id.is("id1").age.is(1)).fetch();
    repository.find(person.age.is(1).id.in(Arrays.asList("id1", "id2"))).fetch();
    check(calls.isEmpty());
}
Also used : PersonRepository(org.immutables.criteria.personmodel.PersonRepository) Test(org.junit.jupiter.api.Test)

Example 4 with PersonRepository

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

the class GetAllOptimizationTest method createRepository.

/**
 * Create repository using lambda for customization
 */
private PersonRepository createRepository(Function<GeodeSetup.Builder, GeodeSetup.Builder> fn) {
    RegionResolver resolver = new LocalResolver(RegionResolver.defaultResolver(cache));
    GeodeSetup.Builder setup = fn.apply(GeodeSetup.builder().regionResolver(resolver));
    AutocreateRegion autocreate = new AutocreateRegion(cache);
    Backend backend = WithSessionCallback.wrap(new GeodeBackend(setup.build()), autocreate);
    return new PersonRepository(backend);
}
Also used : Backend(org.immutables.criteria.backend.Backend) PersonRepository(org.immutables.criteria.personmodel.PersonRepository)

Aggregations

PersonRepository (org.immutables.criteria.personmodel.PersonRepository)4 Test (org.junit.jupiter.api.Test)3 Person (org.immutables.criteria.personmodel.Person)2 Flowable (io.reactivex.Flowable)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1 Collectors (java.util.stream.Collectors)1 Region (org.apache.geode.cache.Region)1 ClientCache (org.apache.geode.cache.client.ClientCache)1 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)1 ClientRegionShortcut (org.apache.geode.cache.client.ClientRegionShortcut)1 ReflectionBasedAutoSerializer (org.apache.geode.pdx.ReflectionBasedAutoSerializer)1 Checkers.check (org.immutables.check.Checkers.check)1 Backend (org.immutables.criteria.backend.Backend)1 KeyExtractor (org.immutables.criteria.backend.KeyExtractor)1 WatchEvent (org.immutables.criteria.backend.WatchEvent)1 PersonCriteria (org.immutables.criteria.personmodel.PersonCriteria)1 PersonGenerator (org.immutables.criteria.personmodel.PersonGenerator)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Disabled (org.junit.jupiter.api.Disabled)1