RegionDestroyFunction implements InternalFunction<String> { @Override @SuppressWarnings("deprecation") public void execute(FunctionContext<String> context) { String regionPath = context.getArguments(); String memberName = context.getMemberName(); try { String functionId = context.getFunctionId(); if (!getId().equals(functionId) || regionPath == null) { context.getResultSender().lastResult(new CliFunctionResult("", false, "Function Id mismatch or arguments is not available.")); return; } Cache cache = ((InternalCache) context.getCache()).getCacheForProcessingClientRequests(); Region<?, ?> region = cache.getRegion(regionPath); if (region == null) { context.getResultSender().lastResult(new CliFunctionResult(memberName, true, String.format("Region '%s' already destroyed", regionPath))); return; } region.destroyRegion(); String regionName = regionPath.startsWith(SEPARATOR) ? regionPath.substring(1) : regionPath; XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", regionName); context.getResultSender().lastResult(new CliFunctionResult(memberName, xmlEntity, String.format("Region '%s' destroyed successfully", regionPath))); } catch (IllegalStateException ex) { context.getResultSender() .lastResult(new CliFunctionResult(memberName, false, ex.getMessage())); } catch (RegionDestroyedException ex) { context.getResultSender().lastResult(new CliFunctionResult(memberName, true, String.format("Region '%s' already destroyed", regionPath))); } catch (Exception ex) { LogService.getLogger().error(ex.getMessage(), ex); context.getResultSender().lastResult(new CliFunctionResult(memberName, ex, ex.getMessage())); } }  @Override boolean hasResult(); @Override @SuppressWarnings("deprecation") void execute(FunctionContext<String> context); @Override String getId(); @Override boolean optimizeForWrite(); @Override boolean isHA(); @Immutable
  static final RegionDestroyFunction INSTANCE; }
@Test @SuppressWarnings("deprecation") public void functionContextIsWrong() { function.execute(context); verify(resultSender).lastResult(resultCaptor.capture()); CliFunctionResult result = resultCaptor.getValue(); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getMessage()).contains("Function Id mismatch or arguments is not available"); }
@Test @SuppressWarnings("deprecation") public void regionAlreadyDestroyed() { when(context.getFunctionId()).thenReturn(RegionDestroyFunction.class.getName()); when(filterCache.getRegion(any())).thenReturn(null); function.execute(context); verify(resultSender).lastResult(resultCaptor.capture()); CliFunctionResult result = resultCaptor.getValue(); assertThat(result.isSuccessful()).isTrue(); assertThat(result.getThrowable()).isNull(); assertThat(result.getMessage()).contains("Region 'testRegion' already destroyed"); }
@Test @SuppressWarnings("deprecation") public void regionAlreadyDestroyed_throwException() { when(context.getFunctionId()).thenReturn(RegionDestroyFunction.class.getName()); @SuppressWarnings("unchecked") Region<Object, Object> region = mock(Region.class); when(filterCache.getRegion(any())).thenReturn(region); doThrow(mock(RegionDestroyedException.class)).when(region).destroyRegion(); function.execute(context); verify(resultSender).lastResult(resultCaptor.capture()); CliFunctionResult result = resultCaptor.getValue(); assertThat(result.isSuccessful()).isTrue(); assertThat(result.getThrowable()).isNull(); assertThat(result.getMessage()).contains("Region 'testRegion' already destroyed"); }
@Test @SuppressWarnings("deprecation") public void illegalStateExceptionWillNotThrowExceptionToCommand() { when(context.getFunctionId()).thenReturn(RegionDestroyFunction.class.getName()); @SuppressWarnings("unchecked") Region<Object, Object> region = mock(Region.class); when(filterCache.getRegion(any())).thenReturn(region); doThrow(new IllegalStateException("message")).when(region).destroyRegion(); function.execute(context); verify(resultSender).lastResult(resultCaptor.capture()); CliFunctionResult result = resultCaptor.getValue(); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getThrowable()).isNull(); assertThat(result.getMessage()).isEqualTo("message"); }