Example usage for org.springframework.data.gemfire IndexFactoryBean setCache

List of usage examples for org.springframework.data.gemfire IndexFactoryBean setCache

Introduction

In this page you can find the example usage for org.springframework.data.gemfire IndexFactoryBean setCache.

Prototype

public void setCache(RegionService cache) 

Source Link

Document

Sets a reference to the RegionService .

Usage

From source file:example.app.config.gemfire.GemFireConfiguration.java

@Bean
@DependsOn("Contacts")
public IndexFactoryBean emailIndex(GemFireCache gemfireCache) {
    IndexFactoryBean lastNameIndex = new IndexFactoryBean();

    lastNameIndex.setCache(gemfireCache);
    lastNameIndex.setExpression("email");
    lastNameIndex.setFrom("/Contacts");
    lastNameIndex.setName("EmailIdx");
    lastNameIndex.setType(IndexType.HASH);

    return lastNameIndex;
}

From source file:example.app.config.gemfire.GemFireConfiguration.java

@Bean
@DependsOn("Contacts")
public IndexFactoryBean lastNameIndex(GemFireCache gemfireCache) {
    IndexFactoryBean lastNameIndex = new IndexFactoryBean();

    lastNameIndex.setCache(gemfireCache);
    lastNameIndex.setExpression("person.lastName");
    lastNameIndex.setFrom("/Contacts");
    lastNameIndex.setName("PersonLastNameIdx");
    lastNameIndex.setType(IndexType.HASH);

    return lastNameIndex;
}

From source file:org.spring.data.gemfire.app.context.config.PeerCacheConfiguration.java

@Bean
public IndexFactoryBean exampleIndex(Cache gemfireCache) {
    IndexFactoryBean customerId = new IndexFactoryBean();

    customerId.setCache(gemfireCache);
    customerId.setName("CustomerIdIdx");
    customerId.setExpression("id");
    customerId.setFrom("/Customers");
    customerId.setType(IndexType.PRIMARY_KEY);

    return customerId;
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

private IndexFactoryBean newIndexFactoryBean() {

    IndexFactoryBean indexFactoryBean = spy(new IndexFactoryBean() {
        @Override/*from w ww .j  a va2s .  co m*/
        protected Log newLog() {
            return mockLog;
        }
    });

    indexFactoryBean.setBeanFactory(mockBeanFactory);
    indexFactoryBean.setCache(mockCache);
    indexFactoryBean.setQueryService(mockQueryService);

    return indexFactoryBean;
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test(expected = IllegalStateException.class)
public void afterPropertiesSetWithNullQueryService() throws Exception {

    IndexFactoryBean indexFactoryBean = spy(new IndexFactoryBean());

    doReturn(null).when(indexFactoryBean).lookupQueryService();

    try {/*from  w w  w  . ja v a  2  s.c o  m*/
        indexFactoryBean.setCache(mockCache);
        indexFactoryBean.setName("TestIndex");
        indexFactoryBean.afterPropertiesSet();
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("QueryService is required to create an Index");
        assertThat(expected).hasNoCause();

        throw expected;
    } finally {
        verify(indexFactoryBean, times(1)).lookupQueryService();
    }
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test(expected = IllegalStateException.class)
public void resolveCacheThrowsExceptionForUnresolvableCache() {
    try {/*w  w w  . j  a va2  s  .co  m*/
        IndexFactoryBean indexFactoryBean = newIndexFactoryBean();
        indexFactoryBean.setCache(null);
        indexFactoryBean.resolveCache();
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Cache is required");
        assertThat(expected).hasNoCause();

        throw expected;
    }
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void doLookupQueryServiceOnClientCache() {

    ClientCache mockClientCache = mock(ClientCache.class);
    QueryService mockQueryService = mockQueryService("testDoLookupQueryServiceOnClientCache.MockQueryService");

    when(mockClientCache.getLocalQueryService()).thenReturn(mockQueryService);

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setCache(mockClientCache);
    indexFactoryBean.setQueryService(null);

    assertThat(indexFactoryBean.doLookupQueryService()).isSameAs(mockQueryService);

    verify(mockClientCache, times(1)).getLocalQueryService();
    verify(mockClientCache, never()).getQueryService();
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void doLookupQueryServiceOnPeerCache() {

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setCache(mockCache);
    indexFactoryBean.setQueryService(null);

    assertThat(indexFactoryBean.doLookupQueryService()).isSameAs(mockQueryService);

    verify(mockCache, times(1)).getQueryService();
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void defineMultipleIndexesWithSeparateIndexFactoryBeansSameSpringContext() throws Exception {

    ConfigurableBeanFactory mockBeanFactory = mock(ConfigurableBeanFactory.class,
            "testDefineMultipleIndexesWithSeparateIndexFactoryBeansSameSpringContext.MockBeanFactory");

    Cache mockCacheOne = mock(Cache.class,
            "testDefineMultipleIndexesWithSeparateIndexFactoryBeansSameSpringContext.MockCacheOne");

    Cache mockCacheTwo = mock(Cache.class,
            "testDefineMultipleIndexesWithSeparateIndexFactoryBeansSameSpringContext.MockCacheTwo");

    AtomicReference<QueryService> queryServiceReference = new AtomicReference<>(null);

    doAnswer(invocation -> (queryServiceReference.get() != null)).when(mockBeanFactory)
            .containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE));

    doAnswer(invocation -> queryServiceReference.get()).when(mockBeanFactory).getBean(
            eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE), eq(QueryService.class));

    doAnswer(invocation -> {/*  ww  w.j  a  v a2 s .  c o m*/

        assertEquals(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE,
                invocation.getArgument(0));

        queryServiceReference.compareAndSet(null, invocation.getArgument(1));

        return null;
    }).when(mockBeanFactory).registerSingleton(
            eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE), any(QueryService.class));

    when(mockCacheOne.getQueryService()).thenReturn(mockQueryService);

    IndexFactoryBean indexFactoryBeanOne = new IndexFactoryBean();

    indexFactoryBeanOne.setBeanFactory(mockBeanFactory);
    indexFactoryBeanOne.setCache(mockCacheOne);
    indexFactoryBeanOne.setDefine(true);
    indexFactoryBeanOne.setExpression("id");
    indexFactoryBeanOne.setFrom("/People");
    indexFactoryBeanOne.setName("PersonIdIndex");
    indexFactoryBeanOne.setType("Key");
    indexFactoryBeanOne.afterPropertiesSet();

    IndexFactoryBean indexFactoryBeanTwo = new IndexFactoryBean();

    indexFactoryBeanTwo.setBeanFactory(mockBeanFactory);
    indexFactoryBeanTwo.setCache(mockCacheTwo);
    indexFactoryBeanTwo.setDefine(true);
    indexFactoryBeanTwo.setExpression("purchaseDate");
    indexFactoryBeanTwo.setFrom("/Orders");
    indexFactoryBeanTwo.setImports("org.example.Order");
    indexFactoryBeanTwo.setName("PurchaseDateIndex");
    indexFactoryBeanTwo.setType("HASH");
    indexFactoryBeanTwo.afterPropertiesSet();

    verify(mockBeanFactory, times(2))
            .containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE));

    verify(mockBeanFactory, times(1)).getBean(
            eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE), eq(QueryService.class));

    verify(mockBeanFactory, times(1)).registerSingleton(
            eq(GemfireConstants.DEFAULT_GEMFIRE_INDEX_DEFINITION_QUERY_SERVICE), same(mockQueryService));

    verify(mockCacheOne, times(1)).getQueryService();
    verify(mockCacheTwo, never()).getQueryService();

    verify(mockQueryService, times(1)).defineKeyIndex(eq("PersonIdIndex"), eq("id"), eq("/People"));

    verify(mockQueryService, times(1)).defineHashIndex(eq("PurchaseDateIndex"), eq("purchaseDate"),
            eq("/Orders"), eq("org.example.Order"));
}