|
|
|
|
@@ -16,11 +16,17 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.data.gemfire;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
|
import static org.hamcrest.Matchers.*;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.*;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
|
|
import static org.mockito.Mockito.atLeastOnce;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
@@ -37,9 +43,7 @@ import org.apache.geode.cache.query.Query;
|
|
|
|
|
import org.apache.geode.cache.query.QueryService;
|
|
|
|
|
import org.apache.geode.cache.query.SelectResults;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.rules.ExpectedException;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
@@ -55,19 +59,16 @@ import org.springframework.data.gemfire.test.support.AbstractUnitAndIntegrationT
|
|
|
|
|
* @see org.junit.Test
|
|
|
|
|
* @see org.mockito.Mock
|
|
|
|
|
* @see org.mockito.Mockito
|
|
|
|
|
* @see org.mockito.runners.MockitoJUnitRunner
|
|
|
|
|
* @see org.mockito.junit.MockitoJUnitRunner
|
|
|
|
|
* @see org.springframework.data.gemfire.GemfireTemplate
|
|
|
|
|
* @see AbstractUnitAndIntegrationTestsWithMockSupport
|
|
|
|
|
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
|
|
|
|
|
* @see org.springframework.test.context.ContextConfiguration
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
public class GemfireTemplateUnitTests extends AbstractUnitAndIntegrationTestsWithMockSupport {
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public ExpectedException exception = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
private GemfireTemplate template;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
@@ -100,13 +101,19 @@ public class GemfireTemplateUnitTests extends AbstractUnitAndIntegrationTestsWit
|
|
|
|
|
assertThat(localTemplate.isExposeNativeRegion()).isFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Test(expected = IllegalArgumentException.class)
|
|
|
|
|
public void constructWithNullRegionThrowsIllegalArgumentException() {
|
|
|
|
|
exception.expect(IllegalArgumentException.class);
|
|
|
|
|
exception.expectCause(is(nullValue(Throwable.class)));
|
|
|
|
|
exception.expectMessage("Region is required");
|
|
|
|
|
|
|
|
|
|
new GemfireTemplate(null);
|
|
|
|
|
try {
|
|
|
|
|
new GemfireTemplate(null);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException expected) {
|
|
|
|
|
|
|
|
|
|
assertThat(expected).hasMessage("Region is required");
|
|
|
|
|
assertThat(expected).hasNoCause();
|
|
|
|
|
|
|
|
|
|
throw expected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@@ -159,28 +166,32 @@ public class GemfireTemplateUnitTests extends AbstractUnitAndIntegrationTestsWit
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void findIsSuccessful() throws Exception {
|
|
|
|
|
|
|
|
|
|
Object[] expectedParams = { "arg" };
|
|
|
|
|
|
|
|
|
|
String expectedQuery = "SELECT * FROM /Example";
|
|
|
|
|
|
|
|
|
|
SelectResults mockSelectResults = mock(SelectResults.class);
|
|
|
|
|
|
|
|
|
|
when(mockQuery.execute(any(Object[].class))).thenReturn(mockSelectResults);
|
|
|
|
|
when(mockQuery.execute(any(Object.class))).thenReturn(mockSelectResults);
|
|
|
|
|
|
|
|
|
|
assertThat(template.find(expectedQuery, expectedParams)).isEqualTo(mockSelectResults);
|
|
|
|
|
|
|
|
|
|
verify(mockRegion, atLeastOnce()).getRegionService();
|
|
|
|
|
verify(mockRegionService, times(1)).getQueryService();
|
|
|
|
|
verify(mockQueryService, times(1)).newQuery(eq(expectedQuery));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq(expectedParams));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq("arg"));
|
|
|
|
|
verifyZeroInteractions(mockSelectResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected = InvalidDataAccessApiUsageException.class)
|
|
|
|
|
public void findWithSingleResultQueryThrowsInvalidDataAccessApiUsageException() throws Exception {
|
|
|
|
|
|
|
|
|
|
Object[] expectedParams = { "arg" };
|
|
|
|
|
|
|
|
|
|
String expectedQuery = "SELECT 1 FROM /Example";
|
|
|
|
|
|
|
|
|
|
when(mockQuery.execute(any(Object[].class))).thenReturn(1);
|
|
|
|
|
when(mockQuery.execute(any(Object.class))).thenReturn(1);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
template.find(expectedQuery, expectedParams);
|
|
|
|
|
@@ -189,62 +200,68 @@ public class GemfireTemplateUnitTests extends AbstractUnitAndIntegrationTestsWit
|
|
|
|
|
verify(mockRegion, atLeastOnce()).getRegionService();
|
|
|
|
|
verify(mockRegionService, times(1)).getQueryService();
|
|
|
|
|
verify(mockQueryService, times(1)).newQuery(eq(expectedQuery));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq(expectedParams));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq("arg"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void findUniqueReturnsSelectResultsIsSuccessful() throws Exception {
|
|
|
|
|
|
|
|
|
|
Object[] expectedParams = { "arg" };
|
|
|
|
|
|
|
|
|
|
String expectedQuery = "SELECT 1 FROM /Example";
|
|
|
|
|
|
|
|
|
|
SelectResults mockSelectResults = mock(SelectResults.class);
|
|
|
|
|
|
|
|
|
|
when(mockQuery.execute(eq(expectedParams))).thenReturn(mockSelectResults);
|
|
|
|
|
when(mockQuery.execute(any(Object.class))).thenReturn(mockSelectResults);
|
|
|
|
|
when(mockSelectResults.asList()).thenReturn(Collections.singletonList(1));
|
|
|
|
|
|
|
|
|
|
assertThat(template.<Integer>findUnique(expectedQuery, expectedParams)).isEqualTo(1);
|
|
|
|
|
assertThat((Object) template.findUnique(expectedQuery, expectedParams)).isEqualTo(1);
|
|
|
|
|
|
|
|
|
|
verify(mockRegion, atLeastOnce()).getRegionService();
|
|
|
|
|
verify(mockRegionService, times(1)).getQueryService();
|
|
|
|
|
verify(mockQueryService, times(1)).newQuery(eq(expectedQuery));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq(expectedParams));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq("arg"));
|
|
|
|
|
verify(mockSelectResults, times(1)).asList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void findUniqueReturnsObjectIsSuccessful() throws Exception {
|
|
|
|
|
|
|
|
|
|
Object[] expectedParams = { "arg" };
|
|
|
|
|
|
|
|
|
|
String expectedQuery = "SELECT 1 FROM /Example";
|
|
|
|
|
|
|
|
|
|
when(mockQuery.execute(eq(expectedParams))).thenReturn("test");
|
|
|
|
|
when(mockQuery.execute(any(Object.class))).thenReturn("test");
|
|
|
|
|
|
|
|
|
|
assertThat(template.<String>findUnique(expectedQuery, expectedParams)).isEqualTo("test");
|
|
|
|
|
assertThat((Object) template.findUnique(expectedQuery, expectedParams)).isEqualTo("test");
|
|
|
|
|
|
|
|
|
|
verify(mockRegion, atLeastOnce()).getRegionService();
|
|
|
|
|
verify(mockRegionService, times(1)).getQueryService();
|
|
|
|
|
verify(mockQueryService, times(1)).newQuery(eq(expectedQuery));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq(expectedParams));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq("arg"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected = InvalidDataAccessApiUsageException.class)
|
|
|
|
|
public void findUniqueWithMultiResultQueryThrowsInvalidDataAccessApiUsageException() throws Exception {
|
|
|
|
|
|
|
|
|
|
Object[] expectedParams = { "arg" };
|
|
|
|
|
|
|
|
|
|
String expectedQuery = "SELECT 1 FROM /Example";
|
|
|
|
|
|
|
|
|
|
SelectResults mockSelectResults = mock(SelectResults.class);
|
|
|
|
|
|
|
|
|
|
when(mockQuery.execute(eq(expectedParams))).thenReturn(mockSelectResults);
|
|
|
|
|
when(mockQuery.execute(any(Object.class))).thenReturn(mockSelectResults);
|
|
|
|
|
when(mockSelectResults.asList()).thenReturn(Arrays.asList(1, 2));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
assertThat(template.<Integer>findUnique(expectedQuery, expectedParams)).isEqualTo(1);
|
|
|
|
|
assertThat((Object) template.findUnique(expectedQuery, expectedParams)).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
verify(mockRegion, atLeastOnce()).getRegionService();
|
|
|
|
|
verify(mockRegionService, times(1)).getQueryService();
|
|
|
|
|
verify(mockQueryService, times(1)).newQuery(eq(expectedQuery));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq(expectedParams));
|
|
|
|
|
verify(mockQuery, times(1)).execute(eq("arg"));
|
|
|
|
|
verify(mockSelectResults, times(1)).asList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|