Mock the Map/Region.values() operation.

This commit is contained in:
John Blum
2020-09-16 15:25:37 -07:00
parent efed3af760
commit f144f98271
2 changed files with 52 additions and 0 deletions

View File

@@ -2551,6 +2551,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
.when(mockRegion).containsValue(any());
// Region.containsValueForKey(key)
// NOTE: This containsValueForKey(..) operation is not atomic
doAnswer(invocation -> {
K key = invocation.getArgument(0);
@@ -2608,6 +2609,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
}).when(mockRegion).get(ArgumentMatchers.<K>any());
// Region.getAll(:Collection<K>)
// NOTE: This getAll(..) operation is not atomic
doAnswer(invocation -> {
Collection<K> keys = invocation.getArgument(0);
@@ -2752,6 +2754,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
}).when(mockRegion).put(any(), any());
// Map.putAll(:Map<K, V>) / Region.putAll(:Map<K, V>)
// NOTE: This putAll(..) operation is not atomic
doAnswer(invocation -> {
Map<K, V> map = invocation.getArgument(0);
@@ -2763,6 +2766,8 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
}).when(mockRegion).putAll(any(Map.class));
// TODO Map.putIfAbsent(key, value) / Region.putIfAbsent(key, value) ???
// Map.remove(key) / Region.remove(key)
doAnswer(invocation -> {
@@ -2798,7 +2803,10 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
}).when(mockRegion).remove(any());
// TODO Map.remove(key, value) / Region.remove(key, value) ???
// Region.removeAll(:Collection<K>)
// NOTE: This removeAll(..) implementation is not atomic
doAnswer(invocation -> {
Collection<K> keys = invocation.getArgument(0);
@@ -2811,8 +2819,15 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
}).when(mockRegion).removeAll(any(Collection.class));
// TODO Map.replace(key, value) / Region.replace(key, value) ???
// TODO Map.replace(key, oldValue, newValue) / Region.replace(key, oldValue, newValue) ???
// TODO Map.replaceAll(:BiFunction<K, V) ???
// Region.size()
doAnswer(invocation -> data.size()).when(mockRegion).size();
// Map.values() / Region.values()
doAnswer(invocation -> Collections.unmodifiableCollection(data.values())).when(mockRegion).values();
}
public static <K, V> Region<K, V> mockSubRegion(Region<K, V> parent, String name,

View File

@@ -34,6 +34,7 @@ import static org.mockito.Mockito.when;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -748,4 +749,40 @@ public class MockRegionDataAccessOperationsAndEventsUnitTests {
verify(this.mockRegion, times(1)).remove(eq(2));
verify(this.mockRegion, never()).remove(eq(3));
}
@Test
public void mapRegionValuesIsCorrect() {
this.mockRegion.put(1, "TEST");
this.mockRegion.put(2, "MOCK");
this.mockRegion.put(3, "NULL");
Collection<Object> values = this.mockRegion.values();
assertThat(values).isNotNull();
assertThat(values).hasSize(3);
assertThat(values).containsExactlyInAnyOrder("TEST", "MOCK", "NULL");
this.mockRegion.put(3, "THREE");
this.mockRegion.put(4, "FOUR");
values = this.mockRegion.values();
assertThat(values).isNotNull();
assertThat(values).hasSize(4);
assertThat(values).containsExactlyInAnyOrder("TEST", "MOCK", "THREE", "FOUR");
this.mockRegion.removeAll(Arrays.asList(3, 4));
values = this.mockRegion.values();
assertThat(values).isNotNull();
assertThat(values).hasSize(2);
assertThat(values).containsExactlyInAnyOrder("TEST", "MOCK");
}
@Test(expected = UnsupportedOperationException.class)
public void mapRegionValuesIsUnmodifiable() {
this.mockRegion.values().add("TEST");
}
}