AlterQueryServiceFunction extends CliFunction<Object[]> { @Override @SuppressWarnings({"unchecked", "deprecation"}) public CliFunctionResult executeFunction(FunctionContext<Object[]> context) { Set<String> parameterSet; boolean forceUpdate = (boolean) context.getArguments()[0]; String authorizerName = (String) context.getArguments()[1]; if (context.getArguments()[2] != null) { parameterSet = (Set<String>) context.getArguments()[2]; } else { parameterSet = Collections.emptySet(); } if (StringUtils.isEmpty(authorizerName)) { return new CliFunctionResult(context.getMemberName(), CliFunctionResult.StatusState.ERROR, EMPTY_AUTHORIZER_ERROR); } if (!isSecurityEnabled()) { return new CliFunctionResult(context.getMemberName(), CliFunctionResult.StatusState.ERROR, SECURITY_NOT_ENABLED_MESSAGE); } if (Boolean .parseBoolean(System.getProperty(ALLOW_UNTRUSTED_METHOD_INVOCATION_SYSTEM_PROPERTY))) { return new CliFunctionResult(context.getMemberName(), CliFunctionResult.StatusState.ERROR, DEPRECATED_PROPERTY_ERROR); } try { Cache cache = context.getCache(); ((InternalCache) cache).getService(QueryConfigurationService.class) .updateMethodAuthorizer(cache, forceUpdate, authorizerName, parameterSet); } catch (Exception ex) { return new CliFunctionResult(context.getMemberName(), CliFunctionResult.StatusState.ERROR, ex.getMessage()); } String message = AUTHORIZER_UPDATED_MESSAGE + authorizerName + (parameterSet.size() > 0 ? AUTHORIZER_PARAMETERS_MESSAGE + String.join(", ", parameterSet) : ""); return new CliFunctionResult(context.getMemberName(), CliFunctionResult.StatusState.OK, message); }  @Override @SuppressWarnings({"unchecked", "deprecation"}) CliFunctionResult executeFunction(FunctionContext<Object[]> context); static final String AUTHORIZER_UPDATED_MESSAGE; static final String EMPTY_AUTHORIZER_ERROR; static final String SECURITY_NOT_ENABLED_MESSAGE; @SuppressWarnings("deprecation")
  static final String DEPRECATED_PROPERTY_ERROR; }
@Test public void executeFunctionUpdatesMethodAuthorizerAndReturnsMessageWhenAuthorizerNameIsNotNull() { CliFunctionResult result = function.executeFunction(mockContext); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.OK.toString()); assertThat(result.getStatusMessage()) .contains(AUTHORIZER_UPDATED_MESSAGE + AUTHORIZER_NAME + AUTHORIZER_PARAMETERS_MESSAGE) .contains(PARAMETER1).contains(PARAMETER2); verify(mockQueryConfigService).updateMethodAuthorizer(mockCache, true, AUTHORIZER_NAME, parameterSet); }
@Test public void executeFunctionUpdatesMethodAuthorizerAndReturnsMessageWhenAuthorizerNameIsNotNullAndThereAreNoParameters() { when(mockContext.getArguments()).thenReturn(new Object[] {false, AUTHORIZER_NAME, null}); CliFunctionResult result = function.executeFunction(mockContext); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.OK.toString()); assertThat(result.getStatusMessage()).isEqualTo(AUTHORIZER_UPDATED_MESSAGE + AUTHORIZER_NAME); verify(mockQueryConfigService).updateMethodAuthorizer(mockCache, false, AUTHORIZER_NAME, Collections.emptySet()); }
@Test public void executeFunctionReturnsErrorWhenAuthorizerNameIsNull() { when(mockContext.getArguments()).thenReturn(new Object[] {false, null, Collections.emptySet()}); CliFunctionResult result = function.executeFunction(mockContext); verifyNoMoreInteractions(mockQueryConfigService); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.ERROR.toString()); assertThat(result.getStatusMessage()).isEqualTo(EMPTY_AUTHORIZER_ERROR); }
@Test public void executeFunctionReturnsErrorWhenDeprecatedSystemPropertyIsSet() { setAllowUntrustedMethodInvocationSystemProperty(); CliFunctionResult result = function.executeFunction(mockContext); verifyNoMoreInteractions(mockQueryConfigService); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.ERROR.toString()); assertThat(result.getStatusMessage()).isEqualTo(DEPRECATED_PROPERTY_ERROR); }
@Test public void executeFunctionReturnsErrorWhenCacheIsNull() { when(mockContext.getCache()).thenReturn(null); CliFunctionResult result = function.executeFunction(mockContext); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.ERROR.toString()); }
@Test public void executeFunctionReturnsErrorWhenUpdateMethodAuthorizerThrowsException() { doThrow(new RuntimeException()).when(mockQueryConfigService).updateMethodAuthorizer(any(), anyBoolean(), any(), any()); CliFunctionResult result = function.executeFunction(mockContext); assertThat(result.getStatus()).isEqualTo(CliFunctionResult.StatusState.ERROR.toString()); }