GfshExecutionStrategy implements ExecutionStrategy { @Override public Object execute(ParseResult parseResult) { Method method = parseResult.getMethod(); if (isShellOnly(method)) { Assert.notNull(parseResult, "Parse result required"); synchronized (mutex) { Assert.isTrue(isReadyForCommands(), "Not yet ready for commands"); Object exeuctionResult = new CommandExecutor(null).execute((GfshParseResult) parseResult); if (exeuctionResult instanceof ResultModel) { return new CommandResult((ResultModel) exeuctionResult); } return exeuctionResult; } } if (!GfshParseResult.class.isInstance(parseResult)) { throw new IllegalStateException("Configuration error!"); } ResultModel resultModel = executeOnRemote((GfshParseResult) parseResult); if (resultModel == null) { return null; } return new CommandResult(resultModel); } GfshExecutionStrategy(Gfsh shell); @Override Object execute(ParseResult parseResult); @Override boolean isReadyForCommands(); @Override void terminate();  }
@Test public void testOfflineCommand() throws Exception { when(parsedCommand.getMethod()).thenReturn(Commands.class.getDeclaredMethod("offlineCommand")); when(parsedCommand.getInstance()).thenReturn(new Commands()); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result.nextLine().trim()).isEqualTo(COMMAND1_SUCCESS); }
@Test public void testOfflineCommandThatReturnsResultModel() throws NoSuchMethodException { when(parsedCommand.getMethod()).thenReturn(Commands.class.getDeclaredMethod("offlineCommand2")); when(parsedCommand.getInstance()).thenReturn(new Commands()); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result.nextLine().trim()).isEqualTo(COMMAND3_SUCCESS); }
@Test public void testOnLineCommandWhenGfshisOffLine() throws Exception { when(parsedCommand.getMethod()).thenReturn(Commands.class.getDeclaredMethod("onlineCommand")); when(parsedCommand.getInstance()).thenReturn(new Commands()); when(gfsh.isConnectedAndReady()).thenReturn(false); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result).isNull(); }
@Test public void testOnLineCommandWhenGfshisOnLine() throws Exception { when(parsedCommand.getMethod()).thenReturn(Commands.class.getDeclaredMethod("onlineCommand")); when(parsedCommand.getInstance()).thenReturn(new Commands()); when(gfsh.isConnectedAndReady()).thenReturn(true); OperationInvoker invoker = mock(OperationInvoker.class); ResultModel offLineResult = new Commands().onlineCommand(); String jsonResult = offLineResult.toJson(); when(invoker.processCommand(any(CommandRequest.class))).thenReturn(jsonResult); when(gfsh.getOperationInvoker()).thenReturn(invoker); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result.nextLine().trim()).isEqualTo(COMMAND2_SUCCESS); }
@Test public void testOnLineCommandWhenGfshReceivesInvalidJson() throws Exception { when(parsedCommand.getMethod()).thenReturn(Commands.class.getDeclaredMethod("onlineCommand")); when(parsedCommand.getInstance()).thenReturn(new Commands()); when(gfsh.isConnectedAndReady()).thenReturn(true); OperationInvoker invoker = mock(OperationInvoker.class); when(invoker.processCommand(any(CommandRequest.class))).thenReturn("invalid-json"); when(gfsh.getOperationInvoker()).thenReturn(invoker); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result.getStatus()).isEqualTo(Result.Status.ERROR); assertThat(result.nextLine().trim()).contains("Unable to parse the remote response."); }
@Test public void resolveInterceptorClassName() throws Exception { when(parsedCommand.getMethod()) .thenReturn(Commands.class.getDeclaredMethod("interceptedCommand")); when(parsedCommand.getInstance()).thenReturn(new Commands()); when(gfsh.isConnectedAndReady()).thenReturn(true); OperationInvoker invoker = mock(OperationInvoker.class); ResultModel interceptedResult = new Commands().interceptedCommand(); String jsonResult = interceptedResult.toJson(); when(invoker.processCommand(any(CommandRequest.class))).thenReturn(jsonResult); when(gfsh.getOperationInvoker()).thenReturn(invoker); Result result = (Result) gfshExecutionStrategy.execute(parsedCommand); assertThat(result.nextLine().trim()).isEqualTo(COMMAND4_SUCCESS); Mockito.verify(parsedCommand, times(1)).setUserInput(AFTER_INTERCEPTION_MESSAGE); }