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

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

Introduction

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

Prototype

public void setOverride(boolean override) 

Source Link

Document

Configures whether to override an existing Index having the same definition but different name as the Index that would be created by this IndexFactoryBean .

Usage

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

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

    when(mockQueryService.getIndexes()).thenReturn(Collections.emptyList());

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

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

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setIgnoreIfExists(true);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.KEY);

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

    try {/*  ww  w .  j  av a 2s . com*/
        indexFactoryBean.createIndex(mockQueryService, "TestIndex");
    } catch (GemfireIndexException expected) {

        assertThat(expected).hasMessageStartingWith(String.format(
                "An Index with a different name [unknown] having the same definition [%s] already exists;"
                        + " You may attempt to override the existing Index [unknown] 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"));
    }
}

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

@Test
public void createIndexThrowsIndexExistsExceptionAndIgnoresThisIndex() throws Exception {

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

    when(mockLog.isWarnEnabled()).thenReturn(true);

    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.setIgnoreIfExists(true);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.PRIMARY_KEY);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isTrue();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(mockIndex);

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

    verify(mockLog, times(1)).warn(eq(String.format(
            "WARNING! You are choosing to ignore this Index [TestIndex] and return the existing Index"
                    + " having the same basic definition [%s] but with a different name [MockIndex];"
                    + " Make sure no OQL Query Hints refer to this Index by name [TestIndex]",
            indexFactoryBean.toBasicIndexDefinition())));

    verify(mockQueryService, times(1)).getIndexes();
}

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

@Test
public void createIndexThrowsIndexExistsExceptionAndOverridesExistingIndex() throws Exception {

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

    Index testIndex = mockIndexWithDefinition("TestIndex", "id", "/Example", IndexType.PRIMARY_KEY);

    when(mockLog.isWarnEnabled()).thenReturn(true);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

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

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

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(testIndex);

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

    verify(mockLog, times(1)).warn(eq(String.format(
            "WARNING! You are attempting to 'override' an existing Index [MockIndex]"
                    + " having the same basic definition [%s] as the Index that will be created by this"
                    + " IndexFactoryBean [TestIndex]; 'Override' effectively 'renames' the existing Index [MockIndex]"
                    + " by removing it then recreating it under the new name [TestIndex] with the same definition;"
                    + " You should be careful to update any existing OQL Query Hints referring to the old"
                    + " Index name [MockIndex] to now use the new name [TestIndex]",
            indexFactoryBean.toBasicIndexDefinition())));

    verify(mockQueryService, times(1)).getIndexes();
}

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

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

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

    when(mockLog.isWarnEnabled()).thenReturn(true);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

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

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

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

    try {//ww w .j  a  v  a 2 s.co m
        indexFactoryBean.createIndex(mockQueryService, "TestIndex");
    } catch (GemfireIndexException expected) {

        assertThat(expected).hasMessageStartingWith(
                "Attempt to 'override' existing Index [MockIndex] with the Index that would be created by this"
                        + " IndexFactoryBean [TestIndex] failed; you should verify the state of your system"
                        + " and make sure the previously existing Index [MockIndex] still exits");

        assertThat(expected).hasCauseInstanceOf(GemfireIndexException.class);
        assertThat(expected.getCause()).hasMessageStartingWith(
                String.format("Failed to create Index [%s]", indexFactoryBean.toDetailedIndexDefinition()));
        assertThat(expected.getCause()).hasCauseInstanceOf(RuntimeException.class);
        assertThat(expected.getCause().getCause()).hasMessageStartingWith("RETRY");
        assertThat(expected.getCause().getCause()).hasNoCause();

        throw expected;
    } finally {

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

        verify(mockLog, times(1)).warn(eq(String.format(
                "WARNING! You are attempting to 'override' an existing Index [MockIndex]"
                        + " having the same basic definition [%s] as the Index that will be created by this"
                        + " IndexFactoryBean [TestIndex]; 'Override' effectively 'renames' the existing Index [MockIndex]"
                        + " by removing it then recreating it under the new name [TestIndex] with the same definition;"
                        + " You should be careful to update any existing OQL Query Hints referring to the old"
                        + " Index name [MockIndex] to now use the new name [TestIndex]",
                indexFactoryBean.toBasicIndexDefinition())));

        verify(mockQueryService, times(1)).getIndexes();
    }
}

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

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

    when(mockQueryService.getIndexes()).thenReturn(Collections.emptyList());

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).when(indexFactoryBean)
            .createFunctionalIndex(eq(mockQueryService), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setIgnoreIfExists(true);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.FUNCTIONAL);

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

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

        assertThat(expected).hasMessageStartingWith(String.format(
                "An Index with the same name [TestIndex] having possibly a different definition already exists;"
                        + " you may choose to ignore this Index definition [%s] and use the existing Index"
                        + " definition [unknown] by setting the 'ignoreIfExists' property to 'true'",
                indexFactoryBean.toDetailedIndexDefinition()));

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

        throw expected;
    } finally {
        verify(indexFactoryBean, times(1)).createFunctionalIndex(eq(mockQueryService), eq("TestIndex"),
                eq("id"), eq("/Example"), eq(null));

        verifyZeroInteractions(mockLog);

        verify(mockQueryService, times(1)).getIndexes();
    }
}

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

@Test
public void createIndexThrowsIndexNameConflictExceptionAndIgnoresThisIndex() throws Exception {

    Index mockIndex = mockIndexWithDefinition("TestIndex", "price", "/Orders", IndexType.FUNCTIONAL);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).when(indexFactoryBean)
            .createFunctionalIndex(eq(mockQueryService), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("price");
    indexFactoryBean.setFrom("/Orders");
    indexFactoryBean.setIgnoreIfExists(true);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.FUNCTIONAL);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isTrue();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(mockIndex);

    verify(indexFactoryBean, times(1)).createFunctionalIndex(eq(mockQueryService), eq("TestIndex"), eq("price"),
            eq("/Orders"), eq(null));

    verifyZeroInteractions(mockLog);/*from  w w  w.j ava2 s. co m*/

    verify(mockQueryService, times(1)).getIndexes();
}

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

@Test
public void createIndexThrowsIndexNameConflictExceptionAndIgnoresThisIndexWithWarning() throws Exception {

    Index mockIndex = mockIndexWithDefinition("TestIndex", "id", "/Orders", IndexType.PRIMARY_KEY);

    when(mockLog.isWarnEnabled()).thenReturn(true);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).when(indexFactoryBean)
            .createFunctionalIndex(eq(mockQueryService), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("price");
    indexFactoryBean.setFrom("/Orders");
    indexFactoryBean.setIgnoreIfExists(true);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.FUNCTIONAL);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isTrue();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(mockIndex);

    String existingIndexDefinition = String.format(IndexFactoryBean.BASIC_INDEX_DEFINITION, "id", "/Orders",
            IndexType.PRIMARY_KEY);/*from w  ww .j  av  a  2s.  c  om*/

    verify(indexFactoryBean, times(1)).createFunctionalIndex(eq(mockQueryService), eq("TestIndex"), eq("price"),
            eq("/Orders"), eq(null));

    verify(mockLog, times(1)).warn(String.format(
            "WARNING! Returning existing Index [TestIndex] having a definition [%1$s] that does not match"
                    + " the Index defined [%2$s] by this IndexFactoryBean [TestIndex]",
            existingIndexDefinition, indexFactoryBean.toBasicIndexDefinition()));

    verify(mockQueryService, times(1)).getIndexes();
}

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

@Test
public void createIndexThrowsIndexNameConflictExceptionAndOverridesExistingIndex() throws Exception {

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

    Index testIndex = mockIndexWithDefinition("TestIndex", "id", "/Example", IndexType.PRIMARY_KEY);

    assertThat(mockIndex).isNotSameAs(testIndex);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

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

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

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

    // NOTE: this test case is also testing the smart "override" handling logic, which just
    // returns the existing Index if both the name and definition are the same on when an
    // IndexNameConflictException is thrown.
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(mockIndex);

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

    verifyZeroInteractions(mockLog);//  w w w .  j  a  v a  2s  .  c om

    verify(mockQueryService, times(1)).getIndexes();
}

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

@Test
public void createIndexThrowsIndexNameConflictExceptionAndOverridesExistingIndexWithWarning() throws Exception {

    Index mockIndex = mockIndexWithDefinition("TestIndex", "price", "/Orders", IndexType.FUNCTIONAL);

    Index testIndex = mockIndexWithDefinition("TestIndex", "purchaseDate", "/Orders", IndexType.HASH);

    assertThat(mockIndex).isNotSameAs(testIndex);

    when(mockLog.isWarnEnabled()).thenReturn(true);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).doReturn(testIndex).when(indexFactoryBean)
            .createHashIndex(any(QueryService.class), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("purchaseDate");
    indexFactoryBean.setFrom("/Orders");
    indexFactoryBean.setIgnoreIfExists(false);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.HASH);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(testIndex);

    String existingIndexDefinition = String.format(IndexFactoryBean.BASIC_INDEX_DEFINITION,
            mockIndex.getIndexedExpression(), mockIndex.getFromClause(),
            IndexType.valueOf(mockIndex.getType()));

    verify(indexFactoryBean, times(2)).createHashIndex(eq(mockQueryService), eq("TestIndex"),
            eq("purchaseDate"), eq("/Orders"), eq(null));

    verify(mockLog, times(1)).warn(eq(String.format(
            "WARNING! Overriding existing Index [TestIndex] having a definition [%1$s] that does not match"
                    + " the Index defined [%2$s] by this IndexFactoryBean [TestIndex]",
            existingIndexDefinition, indexFactoryBean.toBasicIndexDefinition())));

    verify(mockQueryService, times(1)).getIndexes();
}

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

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

    Index mockIndex = mockIndexWithDefinition("MockIndex", "purchaseDate", "/Example", IndexType.HASH);

    when(mockLog.isWarnEnabled()).thenReturn(true);

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

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).doThrow(new RuntimeException("RETRY"))
            .when(indexFactoryBean)//from   w w w  .  j av  a2 s  .  co m
            .createKeyIndex(any(QueryService.class), anyString(), anyString(), anyString());

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setIgnoreIfExists(false);
    indexFactoryBean.setName("MockIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.PRIMARY_KEY);

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

    try {
        indexFactoryBean.createIndex(mockQueryService, "MockIndex");
    } catch (GemfireIndexException expected) {

        assertThat(expected).hasMessageStartingWith(
                "Attempt to 'override' existing Index [MockIndex] with the Index that would be created by this"
                        + " IndexFactoryBean [MockIndex] failed; you should verify the state of your system"
                        + " and make sure the previously existing Index [MockIndex] still exits");

        assertThat(expected).hasCauseInstanceOf(GemfireIndexException.class);
        assertThat(expected.getCause()).hasMessageStartingWith(
                String.format("Failed to create Index [%s]", indexFactoryBean.toDetailedIndexDefinition()));
        assertThat(expected.getCause()).hasCauseInstanceOf(RuntimeException.class);
        assertThat(expected.getCause().getCause()).hasMessageStartingWith("RETRY");
        assertThat(expected.getCause().getCause()).hasNoCause();

        throw expected;
    } finally {

        String existingIndexDefinition = String.format(IndexFactoryBean.BASIC_INDEX_DEFINITION,
                mockIndex.getIndexedExpression(), mockIndex.getFromClause(),
                IndexType.valueOf(mockIndex.getType()));

        verify(indexFactoryBean, times(2)).createKeyIndex(eq(mockQueryService), eq("MockIndex"), eq("id"),
                eq("/Example"));

        verify(mockLog, times(1)).warn(eq(String.format(
                "WARNING! Overriding existing Index [MockIndex] having a definition [%1$s] that does not match"
                        + " the Index defined [%2$s] by this IndexFactoryBean [MockIndex]",
                existingIndexDefinition, indexFactoryBean.toBasicIndexDefinition())));

        verify(mockQueryService, times(1)).getIndexes();
    }
}