|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 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.eclipse.sirius.components.diagrams.tests.DiagramEventPayloadConsumer.assertRefreshedDiagramThat; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.List; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramEventInput; |
| 25 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload; |
| 26 | +import org.eclipse.sirius.components.diagrams.tests.navigation.DiagramNavigator; |
| 27 | +import org.eclipse.sirius.components.view.emf.diagram.IDiagramIdProvider; |
| 28 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 29 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 30 | +import org.eclipse.syson.application.controllers.diagrams.testers.ToolTester; |
| 31 | +import org.eclipse.syson.application.data.GeneralViewWithTopNodesTestProjectData; |
| 32 | +import org.eclipse.syson.services.diagrams.DiagramDescriptionIdProvider; |
| 33 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramDescription; |
| 34 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 35 | +import org.eclipse.syson.standard.diagrams.view.SDVDescriptionNameGenerator; |
| 36 | +import org.eclipse.syson.sysml.SysmlPackage; |
| 37 | +import org.eclipse.syson.util.IDescriptionNameGenerator; |
| 38 | +import org.eclipse.syson.util.SysONRepresentationDescriptionIdentifiers; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.DisplayName; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.springframework.beans.factory.annotation.Autowired; |
| 43 | +import org.springframework.boot.test.context.SpringBootTest; |
| 44 | +import org.springframework.test.context.jdbc.Sql; |
| 45 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 46 | +import org.springframework.transaction.annotation.Transactional; |
| 47 | + |
| 48 | +import reactor.core.publisher.Flux; |
| 49 | +import reactor.test.StepVerifier; |
| 50 | + |
| 51 | +/** |
| 52 | + * Tests the nodes inside the a PAckage nodes in the General View diagram. |
| 53 | + * |
| 54 | + * @author arichard |
| 55 | + */ |
| 56 | +@Transactional |
| 57 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 58 | +public class GVPackageTests extends AbstractIntegrationTests { |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private IGivenInitialServerState givenInitialServerState; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private IGivenDiagramDescription givenDiagramDescription; |
| 68 | + |
| 69 | + @Autowired |
| 70 | + private IDiagramIdProvider diagramIdProvider; |
| 71 | + |
| 72 | + @Autowired |
| 73 | + private ToolTester toolTester; |
| 74 | + |
| 75 | + private final IDescriptionNameGenerator descriptionNameGenerator = new SDVDescriptionNameGenerator(); |
| 76 | + |
| 77 | + private Flux<DiagramRefreshedEventPayload> givenSubscriptionToDiagram() { |
| 78 | + var diagramEventInput = new DiagramEventInput(UUID.randomUUID(), |
| 79 | + GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, |
| 80 | + GeneralViewWithTopNodesTestProjectData.GraphicalIds.DIAGRAM_ID); |
| 81 | + var flux = this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 82 | + return flux; |
| 83 | + } |
| 84 | + |
| 85 | + @BeforeEach |
| 86 | + public void beforeEach() { |
| 87 | + this.givenInitialServerState.initialize(); |
| 88 | + } |
| 89 | + |
| 90 | + @DisplayName("GIVEN a diagram with a Package node, WHEN a Part node and a Sub-Part node are created, THEN the Part node and the Sub-Part node are visible inside the Package, on the same level, with an edge between them") |
| 91 | + @Sql(scripts = { GeneralViewWithTopNodesTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 92 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 93 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 94 | + @Test |
| 95 | + public void testCreateSubPartInPackage() { |
| 96 | + var flux = this.givenSubscriptionToDiagram(); |
| 97 | + |
| 98 | + var diagramDescription = this.givenDiagramDescription.getDiagramDescription(GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, |
| 99 | + SysONRepresentationDescriptionIdentifiers.GENERAL_VIEW_DIAGRAM_DESCRIPTION_ID); |
| 100 | + var diagramDescriptionIdProvider = new DiagramDescriptionIdProvider(diagramDescription, this.diagramIdProvider); |
| 101 | + |
| 102 | + var newPartToolId = diagramDescriptionIdProvider.getNodeCreationToolId(this.descriptionNameGenerator.getNodeName(SysmlPackage.eINSTANCE.getPackage()), "New Part"); |
| 103 | + assertThat(newPartToolId).as("The tool 'New Part' should exist on the Package").isNotNull(); |
| 104 | + |
| 105 | + var newSubPartToolId = diagramDescriptionIdProvider.getNodeCreationToolId(this.descriptionNameGenerator.getNodeName(SysmlPackage.eINSTANCE.getPartUsage()), "New Part"); |
| 106 | + assertThat(newPartToolId).as("The tool 'New Part' should exist on the PartUsage").isNotNull(); |
| 107 | + |
| 108 | + var diagramId = new AtomicReference<String>(); |
| 109 | + var packageNodeId = new AtomicReference<String>(); |
| 110 | + var partNodeId = new AtomicReference<String>(); |
| 111 | + |
| 112 | + Consumer<Object> initialDiagramContentConsumer = assertRefreshedDiagramThat(diag -> { |
| 113 | + diagramId.set(diag.getId()); |
| 114 | + |
| 115 | + var packageNode = new DiagramNavigator(diag).nodeWithLabel("Package").getNode(); |
| 116 | + packageNodeId.set(packageNode.getId()); |
| 117 | + |
| 118 | + assertThat(packageNode.getChildNodes()).hasSize(0); |
| 119 | + |
| 120 | + assertThat(new DiagramNavigator(diag).findDiagramEdgeCount()).isEqualTo(3); |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | + Runnable newPartUsageTool = () -> this.toolTester.invokeTool(GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, diagramId.get(), packageNodeId.get(), newPartToolId, |
| 125 | + List.of()); |
| 126 | + |
| 127 | + Consumer<Object> updatedDiagramContentConsumerAfterNewPart = assertRefreshedDiagramThat(diag -> { |
| 128 | + var packageNode = new DiagramNavigator(diag).nodeWithLabel("Package").getNode(); |
| 129 | + |
| 130 | + var partNode = new DiagramNavigator(diag).nodeWithLabel("Package").childNodeWithLabel("\u00ABpart\u00BB\npart1").getNode(); |
| 131 | + partNodeId.set(partNode.getId()); |
| 132 | + |
| 133 | + assertThat(packageNode.getChildNodes()).hasSize(1); |
| 134 | + assertThat(packageNode.getChildNodes().get(0)).isEqualTo(partNode); |
| 135 | + |
| 136 | + assertThat(new DiagramNavigator(diag).findDiagramEdgeCount()).isEqualTo(3); |
| 137 | + |
| 138 | + }); |
| 139 | + |
| 140 | + Runnable newSubPartUsageTool = () -> this.toolTester.invokeTool(GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, diagramId.get(), partNodeId.get(), newSubPartToolId, |
| 141 | + List.of()); |
| 142 | + |
| 143 | + Consumer<Object> updatedDiagramContentConsumerAfterNewSubPart = assertRefreshedDiagramThat(diag -> { |
| 144 | + var packageNode = new DiagramNavigator(diag).nodeWithLabel("Package").getNode(); |
| 145 | + |
| 146 | + assertThat(packageNode.getChildNodes()).hasSize(2); |
| 147 | + |
| 148 | + // a new edge exists between the Part and the sub-Part inside the Package node |
| 149 | + assertThat(new DiagramNavigator(diag).findDiagramEdgeCount()).isEqualTo(4); |
| 150 | + }); |
| 151 | + |
| 152 | + StepVerifier.create(flux) |
| 153 | + .consumeNextWith(initialDiagramContentConsumer) |
| 154 | + .then(newPartUsageTool) |
| 155 | + .consumeNextWith(updatedDiagramContentConsumerAfterNewPart) |
| 156 | + .then(newSubPartUsageTool) |
| 157 | + .consumeNextWith(updatedDiagramContentConsumerAfterNewSubPart) |
| 158 | + .thenCancel() |
| 159 | + .verify(Duration.ofSeconds(10)); |
| 160 | + } |
| 161 | +} |
0 commit comments