AbstractScenarioGridCommand extends AbstractScenarioSimulationCommand { protected CommandResult<ScenarioSimulationViolation> setCurrentContext(ScenarioSimulationContext context) { try { final Simulation simulationToRestore = restorableStatus.getSimulation(); final Background backgroundToRestore = restorableStatus.getBackground(); if (simulationToRestore == null) { throw new IllegalStateException("Simulation is null in restorable status"); } if (backgroundToRestore == null) { throw new IllegalStateException("Background is null in restorable status"); } final ScenarioSimulationContext.Status originalStatus = context.getStatus().cloneStatus(); context.getSimulationGrid().getModel().clearSelections(); context.getBackgroundGrid().getModel().clearSelections(); context.getSimulationGrid().setContent(simulationToRestore, context.getSettings().getType()); context.getScenarioSimulationEditorPresenter().getModel().setSimulation(simulationToRestore); context.getBackgroundGrid().setContent(backgroundToRestore, context.getSettings().getType()); context.getScenarioSimulationEditorPresenter().getModel().setBackground(backgroundToRestore); context.getScenarioSimulationEditorPresenter().reloadTestTools(true); context.setStatus(restorableStatus); restorableStatus = originalStatus; return commonExecution(context); } catch (Exception e) { return new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singleton(new ScenarioSimulationViolation(e.getMessage()))); } } protected  AbstractScenarioGridCommand(final GridWidget gridWidget); protected  AbstractScenarioGridCommand(); @Override CommandResult<ScenarioSimulationViolation> execute(ScenarioSimulationContext context); @Override CommandResult<ScenarioSimulationViolation> undo(ScenarioSimulationContext context); CommandResult<ScenarioSimulationViolation> redo(ScenarioSimulationContext context); Optional<CommandResult<ScenarioSimulationViolation>> commonUndoRedoPreexecution(final ScenarioSimulationContext context);  }
@Test public void setCurrentContextNoSimulationNoBackground() { final ScenarioSimulationContext.Status status = scenarioSimulationContextLocal.getStatus(); commandSpy.restorableStatus = status; commandSpy.restorableStatus.setSimulation(null); commandSpy.restorableStatus.setBackground(null); final CommandResult<ScenarioSimulationViolation> retrieved = commandSpy.setCurrentContext(scenarioSimulationContextLocal); assertEquals(CommandResult.Type.ERROR, retrieved.getType()); verify(scenarioGridModelMock, never()).clearSelections(); verify(backgroundGridModelMock, never()).clearSelections(); verify(scenarioGridMock, never()).setContent(eq(simulationMock), eq(scenarioSimulationContextLocal.getSettings().getType())); verify(scenarioSimulationModelMock, never()).setSimulation(eq(simulationMock)); verify(backgroundGridMock, never()).setContent(eq(simulationMock), eq(scenarioSimulationContextLocal.getSettings().getType())); verify(scenarioSimulationModelMock, never()).setBackground(eq(backgroundMock)); verify(scenarioSimulationEditorPresenterMock, never()).reloadTestTools(eq(true)); verify(commandSpy, never()).commonExecution(eq(scenarioSimulationContextLocal)); assertEquals(status, commandSpy.restorableStatus); }
@Test public void setCurrentContextSimulationBackground() { final ScenarioSimulationContext.Status status = scenarioSimulationContextLocal.getStatus(); commandSpy.restorableStatus = status; final CommandResult<ScenarioSimulationViolation> retrieved = commandSpy.setCurrentContext(scenarioSimulationContextLocal); assertEquals(CommandResult.Type.INFO, retrieved.getType()); verify(scenarioGridModelMock, times(1)).clearSelections(); verify(backgroundGridModelMock, times(1)).clearSelections(); verify(scenarioGridMock, times(1)).setContent(eq(simulationMock), eq(scenarioSimulationContextLocal.getSettings().getType())); verify(scenarioSimulationModelMock, times(1)).setSimulation(eq(simulationMock)); verify(backgroundGridMock, times(1)).setContent(eq(backgroundMock), eq(scenarioSimulationContextLocal.getSettings().getType())); verify(scenarioSimulationModelMock, times(1)).setBackground(eq(backgroundMock)); verify(scenarioSimulationEditorPresenterMock, times(1)).reloadTestTools(eq(true)); verify(commandSpy, times(1)).commonExecution(eq(scenarioSimulationContextLocal)); assertNotEquals(status, commandSpy.restorableStatus); }