GetClusterConfigurationFunction implements InternalFunction { @Override public void execute(FunctionContext context) { Set<String> groups = (Set<String>) context.getArguments(); InternalLocator internalLocator = InternalLocator.getLocator(); logger.info("Received request for configuration: {}", groups); if (!internalLocator.isSharedConfigurationEnabled()) { String errorMessage = "The cluster configuration service is not enabled on this member."; logger.warn(errorMessage); context.getResultSender().lastResult(new IllegalStateException(errorMessage)); return; } if (internalLocator.isSharedConfigurationRunning()) { InternalConfigurationPersistenceService clusterConfigurationService = internalLocator.getConfigurationPersistenceService(); try { ConfigurationResponse response = clusterConfigurationService.createConfigurationResponse(groups); context.getResultSender().lastResult(response); } catch (Exception exception) { logger.warn("Unable to retrieve the cluster configuration", exception); context.getResultSender().lastResult(exception); } } else { context.getResultSender().lastResult(null); } }  @Override void execute(FunctionContext context);  }
@Test public void executeShouldReturnIllegalStateExceptionWhenClusterConfigurationServiceIsDisabled() { when(mockedLocator.isSharedConfigurationEnabled()).thenReturn(false); ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class); assertThatCode(() -> getClusterConfigurationFunction.execute(mockedFunctionContext)) .doesNotThrowAnyException(); verify(mockedResultSender).lastResult(argumentCaptor.capture()); Exception exceptionThrown = argumentCaptor.getValue(); assertThat(exceptionThrown).isInstanceOf(IllegalStateException.class) .hasMessage("The cluster configuration service is not enabled on this member."); }
@Test public void executeShouldReturnExceptionWhenClusterConfigurationServiceIsEnabledButFailuresOccurWhileRetrievingIt() { when(mockedConfigurationService.createConfigurationResponse(any())) .thenThrow(new RuntimeException("Mocked Exception.")); when(mockedLocator.getConfigurationPersistenceService()).thenReturn(mockedConfigurationService); ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class); assertThatCode(() -> getClusterConfigurationFunction.execute(mockedFunctionContext)) .doesNotThrowAnyException(); verify(mockedResultSender).lastResult(argumentCaptor.capture()); Exception exceptionThrown = argumentCaptor.getValue(); assertThat(exceptionThrown).isInstanceOf(RuntimeException.class) .hasMessage("Mocked Exception."); }
@Test public void executeShouldReturnNullWhenClusterConfigurationServiceIsEnabledButNotRunning() { when(mockedLocator.isSharedConfigurationRunning()).thenReturn(false); assertThatCode(() -> getClusterConfigurationFunction.execute(mockedFunctionContext)) .doesNotThrowAnyException(); verify(mockedResultSender, times(1)).lastResult(null); }
@Test public void executeShouldReturnTheRequestConfigurationWhenClusterConfigurationServiceIsEnabled() { Set<String> requestedGroups = new HashSet<>(Arrays.asList("group1", "group2")); when(mockedFunctionContext.getArguments()).thenReturn(requestedGroups); when(mockedLocator.getConfigurationPersistenceService()).thenReturn(mockedConfigurationService); ConfigurationResponse mockedResponse = new ConfigurationResponse(); when(mockedConfigurationService.createConfigurationResponse(any())).thenReturn(mockedResponse); ArgumentCaptor<ConfigurationResponse> argumentCaptor = ArgumentCaptor.forClass(ConfigurationResponse.class); assertThatCode(() -> getClusterConfigurationFunction.execute(mockedFunctionContext)) .doesNotThrowAnyException(); verify(mockedResultSender).lastResult(argumentCaptor.capture()); verify(mockedConfigurationService, times(1)).createConfigurationResponse(requestedGroups); verify(mockedResultSender, times(1)).lastResult(mockedResponse); }