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

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

Introduction

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

Prototype

public void setExpression(String expression) 

Source Link

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);/*w w  w  .  j  av  a2s. co m*/
    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

@Test
@SuppressWarnings("all")
public void afterPropertiesSetIsSuccessful() throws Exception {

    ConfigurableBeanFactory mockConfigurableBeanFactory = mock(ConfigurableBeanFactory.class);

    Index mockIndex = mock(Index.class, "testAfterPropertiesSetIsSuccessful.MockIndex");

    when(mockQueryService.createKeyIndex(eq("TestKeyIndex"), eq("id"), eq("/Example"))).thenReturn(mockIndex);

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setBeanFactory(mockConfigurableBeanFactory);
    indexFactoryBean.setBeanName("KeyIndexBean");
    indexFactoryBean.setDefine(false);/*from  w  ww  . ja va 2s . c  o m*/
    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setName("TestKeyIndex");
    indexFactoryBean.setType("key");
    indexFactoryBean.afterPropertiesSet();

    assertThat(indexFactoryBean.getIndex()).isEqualTo(mockIndex);

    Index actualIndex = indexFactoryBean.getObject();

    assertThat(actualIndex).isEqualTo(mockIndex);
    assertThat(indexFactoryBean.getObject()).isSameAs(actualIndex);
    assertThat(Index.class).isAssignableFrom(indexFactoryBean.getObjectType());
    assertThat(indexFactoryBean.isSingleton()).isTrue();

    verify(indexFactoryBean, times(1)).getBeanName();
    verify(indexFactoryBean, never()).lookupQueryService();
    verify(mockConfigurableBeanFactory, times(1)).registerAlias(eq("KeyIndexBean"), eq("TestKeyIndex"));
    verify(mockQueryService, times(1)).createKeyIndex(eq("TestKeyIndex"), eq("id"), eq("/Example"));
    verifyZeroInteractions(mockCache);
}

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

@Test(expected = IllegalArgumentException.class)
public void afterPropertiesSetWithUnspecifiedFromClause() throws Exception {
    try {/*from  w ww.j  a v  a 2  s  .c  o m*/
        IndexFactoryBean indexFactoryBean = newIndexFactoryBean();
        indexFactoryBean.setName("TestIndex");
        indexFactoryBean.setExpression("id");
        indexFactoryBean.afterPropertiesSet();
    } catch (Exception expected) {
        assertThat(expected).hasMessage("Index from clause is required");
        assertThat(expected).hasNoCause();

        throw expected;
    }
}

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

@Test(expected = IllegalArgumentException.class)
public void afterPropertiesSetUsingImportsWithInvalidIndexKeyType() throws Exception {
    try {/*from   w w  w.j  av  a 2 s .  co m*/
        IndexFactoryBean indexFactoryBean = newIndexFactoryBean();
        indexFactoryBean.setName("TestIndex");
        indexFactoryBean.setExpression("id");
        indexFactoryBean.setFrom("/Example");
        indexFactoryBean.setImports("org.example.DomainType");
        indexFactoryBean.setType("PriMary_Key");
        indexFactoryBean.afterPropertiesSet();
    } catch (IllegalArgumentException expected) {
        assertThat(expected).hasMessage("Imports are not supported with a KEY Index");
        assertThat(expected).hasNoCause();

        throw expected;
    }
}

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

@Test
public void createIndexReturnsNewKeyIndex() throws Exception {

    Index mockIndex = mockIndex("testCreateIndexReturnsNewKeyIndex.MockIndex");

    when(mockQueryService.createKeyIndex(eq("KeyIndex"), eq("id"), eq("/Example"))).thenReturn(mockIndex);

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setName("KeyIndex");
    indexFactoryBean.setType("key");

    Index actualIndex = indexFactoryBean.createIndex(mockQueryService, "KeyIndex");

    assertThat(actualIndex).isSameAs(mockIndex);

    verify(mockQueryService, times(1)).createKeyIndex(eq("KeyIndex"), eq("id"), eq("/Example"));
}

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

@Test
public void createIndexReturnsNewHashIndex() throws Exception {

    Index mockIndex = mockIndex("testCreateIndexReturnsNewHashIndex.MockIndex");

    when(mockQueryService.createHashIndex(eq("HashIndex"), eq("name"), eq("/Animals"), eq("org.example.Dog")))
            .thenReturn(mockIndex);//from  w  w  w . j  a va  2  s .  c  om

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setExpression("name");
    indexFactoryBean.setFrom("/Animals");
    indexFactoryBean.setImports("org.example.Dog");
    indexFactoryBean.setName("HashIndex");
    indexFactoryBean.setType("HasH");

    Index actualIndex = indexFactoryBean.createIndex(mockQueryService, "HashIndex");

    assertThat(actualIndex).isSameAs(mockIndex);

    verify(mockQueryService, times(1)).createHashIndex(eq("HashIndex"), eq("name"), eq("/Animals"),
            eq("org.example.Dog"));
}

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

@Test
public void createIndexReturnsNewFunctionalIndex() throws Exception {

    Index mockIndex = mockIndex("testCreateIndexReturnsNewFunctionalIndex.MockIndex");

    when(mockQueryService.createIndex(eq("FunctionalIndex"), eq("someField"), eq("/Example")))
            .thenReturn(mockIndex);//from www  . ja v  a 2s.  c  o m

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    indexFactoryBean.setExpression("someField");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setImports("  ");
    indexFactoryBean.setName("FunctionalIndex");
    indexFactoryBean.setType((String) null);

    Index actualIndex = indexFactoryBean.createIndex(mockQueryService, "FunctionalIndex");

    assertThat(actualIndex).isSameAs(mockIndex);

    verify(mockQueryService, times(1)).createIndex(eq("FunctionalIndex"), eq("someField"), eq("/Example"));
}

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

@Test(expected = GemfireIndexException.class)
public void createIndexThrowsIndexExistsException() throws Exception {

    Index mockIndex = mockIndexWithDefinition("MockIndex", "id", "/Example", IndexType.PRIMARY_KEY);

    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex));

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexExistsException("TEST")).when(indexFactoryBean).createKeyIndex(any(QueryService.class),
            anyString(), anyString(), anyString());

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setType(IndexType.PRIMARY_KEY);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isFalse();

    try {//from w  w  w  . java 2  s  .  com
        indexFactoryBean.createIndex(mockQueryService, "TestIndex");
    } catch (GemfireIndexException expected) {

        assertThat(expected).hasMessageStartingWith(String.format(
                "An Index with a different name [MockIndex] having the same definition [%s] already exists;"
                        + " You may attempt to override the existing Index [MockIndex] with the new name [TestIndex]"
                        + " by setting the 'override' property to 'true'",
                indexFactoryBean.toBasicIndexDefinition()));

        assertThat(expected).hasCauseInstanceOf(IndexExistsException.class);
        assertThat(expected.getCause()).hasMessage("TEST");
        assertThat(expected.getCause()).hasNoCause();

        throw expected;
    } finally {
        verify(mockQueryService, times(1)).getIndexes();

        verify(indexFactoryBean, times(1)).createKeyIndex(eq(mockQueryService), eq("TestIndex"), eq("id"),
                eq("/Example"));
    }
}