GetRegionsFunction implements InternalFunction<Void> { @Override public void execute(FunctionContext<Void> functionContext) { try { Cache cache = functionContext.getCache(); Set<Region<?, ?>> regions = cache.rootRegions(); if (regions == null || regions.isEmpty()) { functionContext.getResultSender().lastResult(null); } else { Set<RegionInformation> regionInformationSet = new HashSet<>(); for (Region<?, ?> region : regions) { RegionInformation regInfo = new RegionInformation(region, true); regionInformationSet.add(regInfo); } functionContext.getResultSender() .lastResult(regionInformationSet.toArray(new RegionInformation[0])); } } catch (Exception e) { functionContext.getResultSender().sendException(e); } }  @Override void execute(FunctionContext<Void> functionContext);  }
@Test public void lastResultIsNullWhenThereAreNoRegions() { Cache cacheFromFunctionContext = mock(Cache.class); when(cacheFromFunctionContext.rootRegions()).thenReturn(Collections.emptySet()); when(functionContext.getCache()).thenReturn(cacheFromFunctionContext); getRegionsFunction.execute(functionContext); verify(resultSender).lastResult(isNull()); }
@Test public void lastResultHasRegionInformationForRegion() { String regionNameInCacheFromFunctionContext = "MyRegion"; Cache cacheFromFunctionContext = cacheWithOneRootRegion(regionNameInCacheFromFunctionContext); when(functionContext.getCache()).thenReturn(cacheFromFunctionContext); getRegionsFunction.execute(functionContext); assertThat(lastResultFrom(resultSender).getValue()) .extracting(r -> r.getPath()) .containsExactly(regionNameInCacheFromFunctionContext); }
@Test public void getsCacheFromFunctionContext() { Cache cacheFromFunctionContext = mock(Cache.class); when(cacheFromFunctionContext.rootRegions()).thenReturn(Collections.emptySet()); when(functionContext.getCache()).thenReturn(cacheFromFunctionContext); getRegionsFunction.execute(functionContext); verify(functionContext).getCache(); }