Search in sources :

Example 1 with AddConnectorCommand

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

the class GeneralCreateNodeActionTest method testExecuteAction.

@Test
public void testExecuteAction() {
    final AbstractCanvasHandler canvasHandler = mock(AbstractCanvasHandler.class);
    final String sourceNodeId = "src-id";
    final String targetNodeId = "dest-id";
    final String connectorId = "edge-id";
    final AbstractCanvas canvas = mock(AbstractCanvas.class);
    doReturn(canvas).when(canvasHandler).getCanvas();
    doReturn(canvas).when(canvasHandler).getAbstractCanvas();
    final Diagram diagram = mock(Diagram.class);
    doReturn(diagram).when(canvasHandler).getDiagram();
    final Metadata metadata = mock(Metadata.class);
    doReturn(metadata).when(diagram).getMetadata();
    final String shapeSetId = "shape-set-id";
    doReturn(shapeSetId).when(metadata).getShapeSetId();
    final Index graphIndex = mock(Index.class);
    doReturn(graphIndex).when(canvasHandler).getGraphIndex();
    final Element sourceElement = mock(Element.class);
    doReturn(sourceElement).when(graphIndex).get(sourceNodeId);
    final Node sourceNode = mock(Node.class);
    final View sourceElementContent = mock(View.class);
    doReturn(sourceElementContent).when(sourceNode).getContent();
    doReturn(Bounds.create(10d, 0d, 200d, 100d)).when(sourceElementContent).getBounds();
    doReturn(sourceNode).when(sourceElement).asNode();
    doReturn(Collections.emptyList()).when(sourceNode).getInEdges();
    final Element targetNodeElement = mock(Element.class);
    doReturn(targetNodeElement).when(clientFactoryManager).newElement(anyString(), eq(targetNodeId));
    final Node targetNode = mock(Node.class);
    final View targetElementContent = mock(View.class);
    doReturn(targetElementContent).when(targetNode).getContent();
    doReturn(Bounds.create(-100d, 0d, 0d, 100d)).when(targetElementContent).getBounds();
    doReturn(targetNode).when(targetNodeElement).asNode();
    final String targetNodeUuid = "target-uuid";
    doReturn(targetNodeUuid).when(targetNode).getUUID();
    final Element connectorElement = mock(Element.class);
    doReturn(connectorElement).when(clientFactoryManager).newElement(anyString(), eq(connectorId));
    final Edge connectorEdge = mock(Edge.class);
    doReturn(connectorEdge).when(connectorElement).asEdge();
    when(canvasLayoutUtils.getNext(eq(canvasHandler), eq(sourceNode), eq(targetNode))).thenReturn(new Point2D(100d, 500d));
    createNodeAction.executeAction(canvasHandler, sourceNodeId, targetNodeId, connectorId);
    final ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
    verify(sessionCommandManager).execute(eq(canvasHandler), commandArgumentCaptor.capture());
    final DeferredCompositeCommand command = (DeferredCompositeCommand) commandArgumentCaptor.getValue();
    final DeferredCommand c0 = (DeferredCommand) command.getCommands().get(0);
    final DeferredCommand c1 = (DeferredCommand) command.getCommands().get(1);
    final DeferredCommand c2 = (DeferredCommand) command.getCommands().get(2);
    final DeferredCommand c3 = (DeferredCommand) command.getCommands().get(3);
    final AddNodeCommand addNodeCommand = (AddNodeCommand) c0.getCommand();
    final UpdateElementPositionCommand updateElementPositionCommand = (UpdateElementPositionCommand) c1.getCommand();
    final AddConnectorCommand addConnectorCommand = (AddConnectorCommand) c2.getCommand();
    final SetConnectionTargetNodeCommand setTargetNodeCommand = (SetConnectionTargetNodeCommand) c3.getCommand();
    Assertions.assertThat(targetNode).isEqualTo(addNodeCommand.getCandidate());
    Assertions.assertThat(shapeSetId).isEqualTo(addNodeCommand.getShapeSetId());
    Assertions.assertThat(connectorEdge).isEqualTo(addConnectorCommand.getCandidate());
    Assertions.assertThat(sourceNode).isEqualTo(addConnectorCommand.getSource());
    Assertions.assertThat(shapeSetId).isEqualTo(addConnectorCommand.getShapeSetId());
    Assertions.assertThat(connectorEdge).isEqualTo(setTargetNodeCommand.getEdge());
    Assertions.assertThat(targetNode).isEqualTo(setTargetNodeCommand.getNode());
    Assertions.assertThat(targetNode).isEqualTo(updateElementPositionCommand.getElement());
    Assertions.assertThat(new Point2D(100d, 500d)).isEqualTo(updateElementPositionCommand.getLocation());
    final ArgumentCaptor<CanvasSelectionEvent> eventArgumentCaptor = ArgumentCaptor.forClass(CanvasSelectionEvent.class);
    verify(selectionEvent).fire(eventArgumentCaptor.capture());
    final CanvasSelectionEvent eCaptured = eventArgumentCaptor.getValue();
    Assertions.assertThat(targetNodeUuid).isEqualTo(eCaptured.getIdentifiers().iterator().next());
    Assertions.assertThat(addConnectorCommand.getConnection()).isInstanceOf(MagnetConnection.class);
    Assertions.assertThat(((MagnetConnection) addConnectorCommand.getConnection()).getMagnetIndex().getAsInt()).isEqualTo(MagnetConnection.MAGNET_LEFT);
    Assertions.assertThat(setTargetNodeCommand.getConnection()).isInstanceOf(MagnetConnection.class);
    Assertions.assertThat(((MagnetConnection) setTargetNodeCommand.getConnection()).getMagnetIndex().getAsInt()).isEqualTo(MagnetConnection.MAGNET_RIGHT);
}
Also used : DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) Element(org.kie.workbench.common.stunner.core.graph.Element) Node(org.kie.workbench.common.stunner.core.graph.Node) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Index(org.kie.workbench.common.stunner.core.graph.processing.index.Index) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) Command(org.kie.workbench.common.stunner.core.command.Command) DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Edge(org.kie.workbench.common.stunner.core.graph.Edge) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) Test(org.junit.Test)

Example 2 with AddConnectorCommand

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

the class CreateNodeActionTest method testAction.

@Test
@SuppressWarnings("unchecked")
public void testAction() {
    when(canvasLayoutUtils.getNext(eq(canvasHandler), eq(element), eq(targetNode))).thenReturn(new Point2D(100d, 500d));
    final MouseClickEvent event = mock(MouseClickEvent.class);
    when(event.getX()).thenReturn(100d);
    when(event.getY()).thenReturn(500d);
    ToolboxAction<AbstractCanvasHandler> cascade = tested.onMouseClick(canvasHandler, NODE_UUID, event);
    assertEquals(tested, cascade);
    ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
    verify(sessionCommandManager, times(1)).execute(eq(canvasHandler), commandArgumentCaptor.capture());
    DeferredCompositeCommand command = (DeferredCompositeCommand) commandArgumentCaptor.getValue();
    DeferredCommand c0 = (DeferredCommand) command.getCommands().get(0);
    DeferredCommand c1 = (DeferredCommand) command.getCommands().get(1);
    DeferredCommand c2 = (DeferredCommand) command.getCommands().get(2);
    DeferredCommand c3 = (DeferredCommand) command.getCommands().get(3);
    AddNodeCommand addNodeCommand = (AddNodeCommand) c0.getCommand();
    UpdateElementPositionCommand updateElementPositionCommand = (UpdateElementPositionCommand) c1.getCommand();
    AddConnectorCommand addConnectorCommand = (AddConnectorCommand) c2.getCommand();
    SetConnectionTargetNodeCommand setTargetNodeCommand = (SetConnectionTargetNodeCommand) c3.getCommand();
    assertEquals(targetNode, addNodeCommand.getCandidate());
    assertEquals("ss1", addNodeCommand.getShapeSetId());
    assertEquals(edge, addConnectorCommand.getCandidate());
    assertEquals(element, addConnectorCommand.getSource());
    assertEquals("ss1", addConnectorCommand.getShapeSetId());
    assertEquals(edge, setTargetNodeCommand.getEdge());
    assertEquals(targetNode, setTargetNodeCommand.getNode());
    assertEquals(targetNode, updateElementPositionCommand.getElement());
    assertEquals(new Point2D(100d, 500d), updateElementPositionCommand.getLocation());
    final ArgumentCaptor<CanvasSelectionEvent> eventArgumentCaptor = ArgumentCaptor.forClass(CanvasSelectionEvent.class);
    verify(canvasElementSelectedEvent, times(1)).fire(eventArgumentCaptor.capture());
    final CanvasSelectionEvent eCaptured = eventArgumentCaptor.getValue();
    assertEquals(TARGET_NODE_UUID, eCaptured.getIdentifiers().iterator().next());
}
Also used : DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) Command(org.kie.workbench.common.stunner.core.command.Command) DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) MouseClickEvent(org.kie.workbench.common.stunner.core.client.shape.view.event.MouseClickEvent) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)2 AddConnectorCommand (org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand)2 AddNodeCommand (org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand)2 SetConnectionTargetNodeCommand (org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand)2 UpdateElementPositionCommand (org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand)2 CanvasSelectionEvent (org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent)2 Command (org.kie.workbench.common.stunner.core.command.Command)2 DeferredCommand (org.kie.workbench.common.stunner.core.command.impl.DeferredCommand)2 DeferredCompositeCommand (org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand)2 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)2 AbstractCanvas (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas)1 MouseClickEvent (org.kie.workbench.common.stunner.core.client.shape.view.event.MouseClickEvent)1 Diagram (org.kie.workbench.common.stunner.core.diagram.Diagram)1 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)1 Edge (org.kie.workbench.common.stunner.core.graph.Edge)1 Element (org.kie.workbench.common.stunner.core.graph.Element)1 Node (org.kie.workbench.common.stunner.core.graph.Node)1 View (org.kie.workbench.common.stunner.core.graph.content.view.View)1 Index (org.kie.workbench.common.stunner.core.graph.processing.index.Index)1