|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Obeo. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Obeo - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.syson.application.controllers.diagrams.general.view; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.junit.jupiter.api.Assertions.fail; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramEventInput; |
| 27 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload; |
| 28 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.ToolVariable; |
| 29 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.ToolVariableType; |
| 30 | +import org.eclipse.sirius.components.diagrams.Diagram; |
| 31 | +import org.eclipse.sirius.components.diagrams.Node; |
| 32 | +import org.eclipse.sirius.components.diagrams.ViewModifier; |
| 33 | +import org.eclipse.sirius.components.diagrams.tests.navigation.DiagramNavigator; |
| 34 | +import org.eclipse.sirius.components.view.emf.diagram.IDiagramIdProvider; |
| 35 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 36 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 37 | +import org.eclipse.syson.application.controllers.diagrams.testers.ToolTester; |
| 38 | +import org.eclipse.syson.application.data.GeneralViewEmptyTestProjectData; |
| 39 | +import org.eclipse.syson.services.diagrams.DiagramDescriptionIdProvider; |
| 40 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramDescription; |
| 41 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 42 | +import org.eclipse.syson.standard.diagrams.view.SDVDescriptionNameGenerator; |
| 43 | +import org.eclipse.syson.sysml.SysmlPackage; |
| 44 | +import org.eclipse.syson.util.IDescriptionNameGenerator; |
| 45 | +import org.eclipse.syson.util.SysONRepresentationDescriptionIdentifiers; |
| 46 | +import org.junit.jupiter.api.BeforeEach; |
| 47 | +import org.junit.jupiter.api.DisplayName; |
| 48 | +import org.junit.jupiter.api.Test; |
| 49 | +import org.springframework.beans.factory.annotation.Autowired; |
| 50 | +import org.springframework.boot.test.context.SpringBootTest; |
| 51 | +import org.springframework.test.context.jdbc.Sql; |
| 52 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 53 | +import org.springframework.transaction.annotation.Transactional; |
| 54 | + |
| 55 | +import reactor.core.publisher.Flux; |
| 56 | +import reactor.test.StepVerifier; |
| 57 | + |
| 58 | +/** |
| 59 | + * Test the creation of a do action with referenced action in the General View diagram.. |
| 60 | + * |
| 61 | + * @author pcdavid |
| 62 | + */ |
| 63 | +@Transactional |
| 64 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 65 | +public class GVStateDoActionWithReferencedActionTests extends AbstractIntegrationTests { |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private IGivenInitialServerState givenInitialServerState; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 72 | + |
| 73 | + @Autowired |
| 74 | + private IGivenDiagramDescription givenDiagramDescription; |
| 75 | + |
| 76 | + @Autowired |
| 77 | + private IDiagramIdProvider diagramIdProvider; |
| 78 | + |
| 79 | + @Autowired |
| 80 | + private ToolTester toolTester; |
| 81 | + |
| 82 | + private final IDescriptionNameGenerator descriptionNameGenerator = new SDVDescriptionNameGenerator(); |
| 83 | + |
| 84 | + private Flux<DiagramRefreshedEventPayload> givenSubscriptionToDiagram() { |
| 85 | + var diagramEventInput = new DiagramEventInput(UUID.randomUUID(), |
| 86 | + GeneralViewEmptyTestProjectData.EDITING_CONTEXT, |
| 87 | + GeneralViewEmptyTestProjectData.GraphicalIds.DIAGRAM_ID); |
| 88 | + var flux = this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 89 | + return flux; |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeEach |
| 93 | + public void beforeEach() { |
| 94 | + this.givenInitialServerState.initialize(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @Sql(scripts = { GeneralViewEmptyTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 99 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 100 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 101 | + @DisplayName("GIVEN a General View with a state and action, WHEN invoking do action with referenced action, THEN the PerformActionUsage is visible in the state") |
| 102 | + public void checkPerformActionRevealInState() { |
| 103 | + var flux = this.givenSubscriptionToDiagram(); |
| 104 | + |
| 105 | + var diagramDescription = this.givenDiagramDescription.getDiagramDescription(GeneralViewEmptyTestProjectData.EDITING_CONTEXT, |
| 106 | + SysONRepresentationDescriptionIdentifiers.GENERAL_VIEW_DIAGRAM_DESCRIPTION_ID); |
| 107 | + |
| 108 | + var diagramDescriptionIdProvider = new DiagramDescriptionIdProvider(diagramDescription, this.diagramIdProvider); |
| 109 | + var newStateToolId = diagramDescriptionIdProvider.getDiagramCreationToolId("New State"); |
| 110 | + assertThat(newStateToolId).as("The tool 'New State' should exist on the diagram").isNotNull(); |
| 111 | + var newActionToolId = diagramDescriptionIdProvider.getDiagramCreationToolId("New Action"); |
| 112 | + assertThat(newActionToolId).as("The tool 'New Action' should exist on the diagram").isNotNull(); |
| 113 | + var newDoActionWithReferencedActionToolId = diagramDescriptionIdProvider.getNodeToolId(this.descriptionNameGenerator.getNodeName(SysmlPackage.eINSTANCE.getStateUsage()), |
| 114 | + "New Do Action with referenced Action"); |
| 115 | + assertThat(newDoActionWithReferencedActionToolId).as("The tool 'New Do Action with referenced Action' should exist on State").isNotNull(); |
| 116 | + |
| 117 | + var diagramReference = new AtomicReference<Diagram>(null); |
| 118 | + var stateNodeId = new AtomicReference<String>(null); |
| 119 | + var actionId = new AtomicReference<String>(null); |
| 120 | + |
| 121 | + Consumer<DiagramRefreshedEventPayload> initialDiagramContentConsumer = payload -> Optional.of(payload) |
| 122 | + .map(DiagramRefreshedEventPayload::diagram) |
| 123 | + .ifPresentOrElse(diagram -> { |
| 124 | + diagramReference.set(diagram); |
| 125 | + }, () -> fail("Missing diagram")); |
| 126 | + |
| 127 | + Runnable createState = () -> this.toolTester.invokeTool(GeneralViewEmptyTestProjectData.EDITING_CONTEXT, diagramReference, newStateToolId); |
| 128 | + |
| 129 | + Consumer<DiagramRefreshedEventPayload> diagramWithStateConsumer = payload -> Optional.of(payload) |
| 130 | + .map(DiagramRefreshedEventPayload::diagram) |
| 131 | + .ifPresentOrElse(diagram -> { |
| 132 | + assertThat(diagram.getNodes()).hasSize(1); |
| 133 | + diagram.getNodes().stream() |
| 134 | + .filter(node -> node.getTargetObjectLabel().equals("state1")) |
| 135 | + .findFirst() |
| 136 | + .ifPresentOrElse(node -> stateNodeId.set(node.getId()), () -> fail("Node 'state1' not found")); |
| 137 | + }, () -> fail("Missing diagram")); |
| 138 | + |
| 139 | + Runnable createAction = () -> this.toolTester.invokeTool(GeneralViewEmptyTestProjectData.EDITING_CONTEXT, diagramReference, newActionToolId); |
| 140 | + |
| 141 | + Consumer<DiagramRefreshedEventPayload> diagramWithStateAndActionConsumer = payload -> Optional.of(payload) |
| 142 | + .map(DiagramRefreshedEventPayload::diagram) |
| 143 | + .ifPresentOrElse(diagram -> { |
| 144 | + assertThat(diagram.getNodes()).hasSize(2); |
| 145 | + diagram.getNodes().stream() |
| 146 | + .filter(node -> node.getTargetObjectLabel().equals("action1")) |
| 147 | + .findFirst() |
| 148 | + .ifPresentOrElse(node -> actionId.set(node.getTargetObjectId()), () -> fail("Node 'action1' not found")); |
| 149 | + }, () -> fail("Missing diagram")); |
| 150 | + |
| 151 | + Runnable createDoActionWithReference = () -> { |
| 152 | + this.toolTester.invokeTool(GeneralViewEmptyTestProjectData.EDITING_CONTEXT, diagramReference.get().getId(), stateNodeId.get(), newDoActionWithReferencedActionToolId, |
| 153 | + List.of(new ToolVariable("selectedObject", actionId.get(), ToolVariableType.OBJECT_ID))); |
| 154 | + }; |
| 155 | + |
| 156 | + Consumer<DiagramRefreshedEventPayload> diagramWithDoActionConsumer = payload -> Optional.of(payload) |
| 157 | + .map(DiagramRefreshedEventPayload::diagram) |
| 158 | + .ifPresentOrElse(diagram -> { |
| 159 | + assertThat(diagram.getNodes()).hasSize(2); |
| 160 | + assertThat(diagram.getEdges()).isEmpty(); |
| 161 | + DiagramNavigator navigator = new DiagramNavigator(diagram); |
| 162 | + Collection<Node> visibleNodes = navigator.findAllNodes().stream().filter(node -> node.getState() == ViewModifier.Normal).toList(); |
| 163 | + assertThat(visibleNodes.stream().filter(node -> node.getInsideLabel().getText().equals("actions"))).hasSize(1).allMatch(node -> node.getChildNodes().size() == 1); |
| 164 | + assertThat(visibleNodes.stream().filter(node -> node.getInsideLabel().getText().equals("perform actions"))).hasSize(1).allMatch(node -> node.getChildNodes().size() == 1); |
| 165 | + assertThat(visibleNodes.stream().filter(node -> node.getInsideLabel().getText().equals("ref do ::> action1"))).hasSize(2); |
| 166 | + }, () -> fail("Missing diagram")); |
| 167 | + |
| 168 | + StepVerifier.create(flux) |
| 169 | + .consumeNextWith(initialDiagramContentConsumer) |
| 170 | + .then(createState) |
| 171 | + .consumeNextWith(diagramWithStateConsumer) |
| 172 | + .then(createAction) |
| 173 | + .consumeNextWith(diagramWithStateAndActionConsumer) |
| 174 | + .then(createDoActionWithReference) |
| 175 | + .consumeNextWith(diagramWithDoActionConsumer) |
| 176 | + .thenCancel() |
| 177 | + .verify(Duration.ofSeconds(10)); |
| 178 | + } |
| 179 | +} |
0 commit comments