UsernamePasswordInterceptor extends AbstractCliAroundInterceptor { @Override public ResultModel preExecution(GfshParseResult parseResult) { if (gfsh == null || !gfsh.isConnectedAndReady()) { return new ResultModel(); } String userInput = parseResult.getUserInput(); String username = parseResult.getParamValueAsString("username"); String password = parseResult.getParamValueAsString("password"); if (!StringUtils.isBlank(password) && StringUtils.isBlank(username)) { username = gfsh.readText(CliStrings.INTERCEPTOR_USERNAME); userInput = userInput + " --username=" + username; parseResult.setUserInput(userInput); } else if (!StringUtils.isBlank(username) && StringUtils.isBlank(password)) { password = gfsh.readPassword(CliStrings.INTERCEPTOR_PASSWORD); userInput = userInput + " --password=" + password; parseResult.setUserInput(userInput); } return new ResultModel(); } UsernamePasswordInterceptor();  UsernamePasswordInterceptor(Gfsh gfsh); @Override ResultModel preExecution(GfshParseResult parseResult);  }
@Test public void WithConnectedGfshWithNoUsernameAndNoPasswordWillPrompt_interactive() throws Exception { when(gfsh.readText("Username: ")).thenReturn("user"); when(gfsh.readPassword("Password: ")).thenReturn("pass"); when(gfsh.isConnectedAndReady()).thenReturn(true); when(parseResult.getUserInput()).thenReturn("command"); when(parseResult.getParamValueAsString("username")).thenReturn(""); when(parseResult.getParamValueAsString("password")).thenReturn(""); interceptor.preExecution(parseResult); verify(gfsh, times(0)).readText(any()); verify(gfsh, times(0)).readPassword(any()); verify(parseResult, times(0)).setUserInput("command"); }
@Test public void WithConnectedGfshWithUsernameButNoPasswordWillPrompt_interactive() throws Exception { when(gfsh.readPassword("Password: ")).thenReturn("pass"); when(gfsh.isConnectedAndReady()).thenReturn(true); when(parseResult.getUserInput()).thenReturn("command --username=user"); when(parseResult.getParamValueAsString("username")).thenReturn("user"); when(parseResult.getParamValueAsString("password")).thenReturn(""); interceptor.preExecution(parseResult); verify(gfsh, times(0)).readText(any()); verify(gfsh, times(1)).readPassword(any()); verify(parseResult, times(1)).setUserInput("command --username=user --password=pass"); }
@Test public void WithConnectedGfshWithUsernameAndPasswordWillNotPrompt_interactive() throws Exception { when(gfsh.isConnectedAndReady()).thenReturn(true); when(parseResult.getUserInput()).thenReturn("command --username=user --password=pass"); when(parseResult.getParamValueAsString("username")).thenReturn("user"); when(parseResult.getParamValueAsString("password")).thenReturn("pass"); interceptor.preExecution(parseResult); verify(gfsh, times(0)).readText(any()); verify(gfsh, times(0)).readPassword(any()); verify(parseResult, times(0)).setUserInput("command --username=user --password=pass"); }
@Test public void WithConnectedGfshWithPasswordButNoUsernameWillPrompt_interactive() throws Exception { when(gfsh.readText("Username: ")).thenReturn("user"); when(gfsh.isConnectedAndReady()).thenReturn(true); when(parseResult.getUserInput()).thenReturn("command --password=pass"); when(parseResult.getParamValueAsString("username")).thenReturn(""); when(parseResult.getParamValueAsString("password")).thenReturn("pass"); interceptor.preExecution(parseResult); verify(gfsh, times(1)).readText(any()); verify(gfsh, times(0)).readPassword(any()); verify(parseResult, times(1)).setUserInput("command --password=pass --username=user"); }
@Test public void WithNonConnectedGfshWithoutUsernameAndPasswordWillNotPrompt_interactive() throws Exception { when(gfsh.isConnectedAndReady()).thenReturn(false); when(parseResult.getUserInput()).thenReturn("command"); when(parseResult.getParamValueAsString("username")).thenReturn(""); when(parseResult.getParamValueAsString("password")).thenReturn(""); interceptor.preExecution(parseResult); verify(gfsh, times(0)).readText(any()); verify(gfsh, times(0)).readPassword(any()); verify(parseResult, times(0)).setUserInput(any()); }