RestoreRedundancyFunction implements InternalFunction<Object[]> { @Override public void execute(FunctionContext<Object[]> context) { Object[] arguments = context.getArguments(); RestoreRedundancyRequest request = (RestoreRedundancyRequest) arguments[0]; boolean isStatusCommand = (boolean) arguments[1]; RestoreRedundancyOperation redundancyOperation = context.getCache().getResourceManager().createRestoreRedundancyOperation(); Set<String> includeRegionsSet = null; if (request.getIncludeRegions() != null) { includeRegionsSet = new HashSet<>(request.getIncludeRegions()); } Set<String> excludeRegionsSet = null; if (request.getExcludeRegions() != null) { excludeRegionsSet = new HashSet<>(request.getExcludeRegions()); } redundancyOperation.includeRegions(includeRegionsSet); redundancyOperation.excludeRegions(excludeRegionsSet); RestoreRedundancyResultsImpl results; try { if (isStatusCommand) { results = (RestoreRedundancyResultsImpl) redundancyOperation.redundancyStatus(); } else { redundancyOperation.shouldReassignPrimaries(request.getReassignPrimaries()); results = (RestoreRedundancyResultsImpl) redundancyOperation.start().join(); } results.setSuccess(true); results.setStatusMessage("Success"); } catch (Exception e) { results = new SerializableRestoreRedundancyResultsImpl(); results.setSuccess(false); results.setStatusMessage(e.getMessage()); } context.getResultSender().lastResult(results); }  @Override // this would return the RestoreRedundancyResults if successful, // it will return an exception to the caller if status is failure or any exception happens void execute(FunctionContext<Object[]> context); @Override String getId(); @Override boolean hasResult(); @Override boolean optimizeForWrite(); @Override boolean isHA(); static final String ID; }
@Test public void executeFunctionSetsFieldsOnRestoreRedundancyOperation() { String[] includeRegions = {"includedRegion1", "includedRegion2"}; String[] excludeRegions = {"excludedRegion1", "excludedRegion2"}; request.setExcludeRegions(Arrays.asList(excludeRegions)); request.setIncludeRegions(Arrays.asList(includeRegions)); function.execute(mockContext); verify(mockOperation).includeRegions(new HashSet<>(request.getIncludeRegions())); verify(mockOperation).excludeRegions(new HashSet<>(request.getExcludeRegions())); verify(mockOperation).shouldReassignPrimaries(request.getReassignPrimaries()); }
@Test public void executeFunctionSetsIncludedAndExcludedRegionsOnRestoreRedundancyOperationWhenNull() { function.execute(mockContext); verify(mockOperation).includeRegions(null); verify(mockOperation).excludeRegions(null); verify(mockOperation).shouldReassignPrimaries(true); }
@Test public void executeFunctionUsesStatusMethodWhenIsStatusCommandIsTrue() { when(mockOperation.redundancyStatus()).thenReturn(mockResults); when(mockResults.getRegionOperationStatus()) .thenReturn(RestoreRedundancyResults.Status.SUCCESS); when(mockContext.getArguments()).thenReturn(new Object[] {request, true}); function.execute(mockContext); verify(mockOperation, times(1)).redundancyStatus(); verify(mockOperation, times(0)).start(); }
@Test public void executeFunctionReturnsOkWhenResultStatusIsFailure() { when(mockResults.getRegionOperationStatus()) .thenReturn(RestoreRedundancyResults.Status.FAILURE); function.execute(mockContext); verify(resultSender).lastResult(argumentCaptor.capture()); SerializableRestoreRedundancyResultsImpl result = argumentCaptor.getValue(); verify(result).setSuccess(true); assertThat(result.getRegionOperationStatus()) .isEqualTo(RestoreRedundancyResults.Status.FAILURE); assertThat(result).isSameAs(mockResults); }
@Test public void executeFunctionReturnsOkWhenResultStatusIsSuccess() { when(mockResults.getRegionOperationStatus()) .thenReturn(RestoreRedundancyResults.Status.SUCCESS); function.execute(mockContext); verify(resultSender).lastResult(argumentCaptor.capture()); SerializableRestoreRedundancyResultsImpl result = argumentCaptor.getValue(); verify(result).setSuccess(true); assertThat(result.getRegionOperationStatus()) .isEqualTo(RestoreRedundancyResults.Status.SUCCESS); assertThat(result).isSameAs(mockResults); }
@Test public void executeFunctionReturnsFailureWhenExceptionThrownDuringOperation() { when(mockOperation.start()).thenThrow(new RuntimeException("Any exception")); function.execute(mockContext); verify(resultSender).lastResult(argumentCaptor.capture()); RestoreRedundancyResults result = argumentCaptor.getValue(); assertThat(result.getSuccess()).isFalse(); assertThat(result.getStatusMessage()).isEqualTo("Any exception"); }