Search in sources :

Example 1 with CommandResult

use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.

the class CommandRegistryListenerTest method testUndoFailed.

@Test
@SuppressWarnings("unchecked")
public void testUndoFailed() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.ERROR);
    tested.onUndo(context, command, result);
    verify(commandRegistry, times(0)).pop();
    verify(commandRegistry, times(0)).peek();
    verify(commandRegistry, times(0)).register(any(Command.class));
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 2 with CommandResult

use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.

the class CommandRegistryListenerTest method testExecuteSuccess.

@Test
@SuppressWarnings("unchecked")
public void testExecuteSuccess() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.INFO);
    tested.onExecute(context, command, result);
    verify(commandRegistry, times(1)).register(eq(command));
    verify(commandRegistry, times(0)).pop();
    verify(commandRegistry, times(0)).peek();
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 3 with CommandResult

use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.

the class CommandRegistryListenerTest method testUndoSuccess.

@Test
@SuppressWarnings("unchecked")
public void testUndoSuccess() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.INFO);
    tested.onUndo(context, command, result);
    verify(commandRegistry, times(1)).pop();
    verify(commandRegistry, times(0)).peek();
    verify(commandRegistry, times(0)).register(any(Command.class));
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 4 with CommandResult

use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.

the class CompositeCommandTest method testExecuteFailedThenUndo.

@Test
@SuppressWarnings("unchecked")
public void testExecuteFailedThenUndo() {
    Command c1 = mock(Command.class);
    when(c1.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c1.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    Command c2 = mock(Command.class);
    when(c2.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c2.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    CommandResult<RuleViolation> failed = new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singletonList(new RuleViolationImpl("failed")));
    Command c3 = mock(Command.class);
    when(c3.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c3.execute(eq(commandExecutionContext))).thenReturn(failed);
    CompositeCommand composite = new CompositeCommand.Builder<>().addCommand(c1).addCommand(c2).addCommand(c3).build();
    CommandResult result = composite.execute(commandExecutionContext);
    assertEquals(CommandResult.Type.ERROR, result.getType());
    verify(c1, times(1)).undo(eq(commandExecutionContext));
    verify(c2, times(1)).undo(eq(commandExecutionContext));
    verify(c3, times(1)).undo(eq(commandExecutionContext));
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 5 with CommandResult

use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.

the class PasteSelectionSessionCommandTest method executeWithMultiSelection.

@Test
@SuppressWarnings("unchecked")
public void executeWithMultiSelection() {
    pasteSelectionSessionCommand.bind(session);
    // Mock the callback of CloneNodeCommand
    ArgumentCaptor<Consumer> consumerNode = ArgumentCaptor.forClass(Consumer.class);
    ArgumentCaptor<Consumer> consumerNode2 = ArgumentCaptor.forClass(Consumer.class);
    when(cloneNodeCommand.getCandidate()).thenReturn(node);
    when(cloneNodeCommand2.getCandidate()).thenReturn(node2);
    when(canvasCommandFactory.cloneNode(eq(node), any(), any(), consumerNode.capture())).thenReturn(cloneNodeCommand);
    when(canvasCommandFactory.cloneNode(eq(node2), any(), any(), consumerNode2.capture())).thenReturn(cloneNodeCommand2);
    Map<Node, ArgumentCaptor<Consumer>> consumerMap = new HashMap() {

        {
            put(node, consumerNode);
            put(node2, consumerNode2);
        }
    };
    // Mock the callback of CloneConnectorCommand
    ArgumentCaptor<Consumer> consumerEdge = ArgumentCaptor.forClass(Consumer.class);
    when(canvasCommandFactory.cloneConnector(any(), anyString(), anyString(), anyString(), consumerEdge.capture())).thenReturn(cloneConnectorCommand);
    // apply callbacks mocks
    when(sessionCommandManager.execute(eq(canvasHandler), any())).thenAnswer(param -> {
        CompositeCommand argument = param.getArgumentAt(1, CompositeCommand.class);
        // callback to nodes
        argument.getCommands().stream().filter(c -> c instanceof CloneNodeCommand).forEach(c -> {
            CloneNodeCommand cloneNodeCommand = (CloneNodeCommand) c;
            Node candidate = cloneNodeCommand.getCandidate();
            consumerMap.get(candidate).getValue().accept(cloneMap.get(candidate));
        });
        // callback to connectors
        argument.getCommands().stream().filter(c -> c instanceof CloneConnectorCommand).forEach(c -> consumerEdge.getValue().accept(cloneEdge));
        return commandResult;
    });
    // Executing the command
    clipboardControl.set(graphInstance.startNode, graphInstance.edge1, graphInstance.intermNode);
    when(selectionControl.getSelectedItems()).thenReturn(Arrays.asList(graphInstance.startNode.getUUID(), graphInstance.edge1.getUUID(), graphInstance.intermNode.getUUID()));
    pasteSelectionSessionCommand.execute(callback);
    verify(canvasCommandFactory, times(1)).cloneNode(eq(graphInstance.startNode), eq(graphInstance.parentNode.getUUID()), eq(new Point2D(X, DEFAULT_PADDING + Y + NODE_SIZE)), any());
    verify(canvasCommandFactory, times(1)).cloneConnector(eq(graphInstance.edge1), anyString(), anyString(), anyString(), any());
    // check command registry update after execution to allow a single undo/redo
    verify(commandRegistry, times(2)).pop();
    ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
    verify(commandRegistry, times(1)).register(commandArgumentCaptor.capture());
    assertTrue(commandArgumentCaptor.getValue() instanceof CompositeCommand);
    assertEquals(((CompositeCommand) commandArgumentCaptor.getValue()).size(), 2);
}
Also used : Arrays(java.util.Arrays) Edge(org.kie.workbench.common.stunner.core.graph.Edge) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) UUID(org.kie.workbench.common.stunner.core.util.UUID) DEFAULT_PADDING(org.kie.workbench.common.stunner.core.client.session.command.impl.PasteSelectionSessionCommand.DEFAULT_PADDING) CloneNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneNodeCommand) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) CommandRegistry(org.kie.workbench.common.stunner.core.registry.command.CommandRegistry) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) CanvasCommandFactory(org.kie.workbench.common.stunner.core.client.command.CanvasCommandFactory) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) ClientSession(org.kie.workbench.common.stunner.core.client.session.ClientSession) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Objects(java.util.Objects) Matchers.any(org.mockito.Matchers.any) Node(org.kie.workbench.common.stunner.core.graph.Node) Mockito.mock(org.mockito.Mockito.mock) LocalClipboardControl(org.kie.workbench.common.stunner.core.client.canvas.controls.clipboard.LocalClipboardControl) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Matchers.anyString(org.mockito.Matchers.anyString) ArgumentCaptor(org.mockito.ArgumentCaptor) ClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.ClientSessionCommand) Element(org.kie.workbench.common.stunner.core.graph.Element) ClipboardControl(org.kie.workbench.common.stunner.core.client.canvas.controls.clipboard.ClipboardControl) KeyboardEvent(org.kie.workbench.common.stunner.core.client.event.keyboard.KeyboardEvent) Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Before(org.junit.Before) Event(javax.enterprise.event.Event) CloneConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneConnectorCommand) TestingGraphInstanceBuilder(org.kie.workbench.common.stunner.core.TestingGraphInstanceBuilder) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) SessionCommandManager(org.kie.workbench.common.stunner.core.client.command.SessionCommandManager) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) TestingGraphMockHandler(org.kie.workbench.common.stunner.core.TestingGraphMockHandler) ArgumentCaptor(org.mockito.ArgumentCaptor) HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) CloneNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneNodeCommand) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) CloneConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneConnectorCommand) Consumer(java.util.function.Consumer) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) CloneNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneNodeCommand) ClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.ClientSessionCommand) Command(org.kie.workbench.common.stunner.core.command.Command) CloneConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.CloneConnectorCommand) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Test(org.junit.Test)

Aggregations

CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)18 Command (org.kie.workbench.common.stunner.core.command.Command)11 Test (org.junit.Test)7 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)7 Edge (org.kie.workbench.common.stunner.core.graph.Edge)7 Node (org.kie.workbench.common.stunner.core.graph.Node)7 View (org.kie.workbench.common.stunner.core.graph.content.view.View)7 Collectors (java.util.stream.Collectors)6 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)6 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)5 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)5 Objects (java.util.Objects)4 Event (javax.enterprise.event.Event)4 CanvasSelectionEvent (org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent)4 CanvasCommandFactory (org.kie.workbench.common.stunner.core.client.command.CanvasCommandFactory)4 CanvasCommandResultBuilder (org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder)4 SessionCommandManager (org.kie.workbench.common.stunner.core.client.command.SessionCommandManager)4 CommandUtils (org.kie.workbench.common.stunner.core.command.util.CommandUtils)4 Element (org.kie.workbench.common.stunner.core.graph.Element)4 ArrayList (java.util.ArrayList)3