|
| 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.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
| 22 | + |
| 23 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramEventInput; |
| 24 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload; |
| 25 | +import org.eclipse.sirius.components.core.api.IObjectSearchService; |
| 26 | +import org.eclipse.sirius.components.diagrams.Diagram; |
| 27 | +import org.eclipse.sirius.components.graphql.tests.ExecuteEditingContextFunctionSuccessPayload; |
| 28 | +import org.eclipse.sirius.components.view.diagram.DiagramDescription; |
| 29 | +import org.eclipse.sirius.components.view.emf.diagram.IDiagramIdProvider; |
| 30 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 31 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 32 | +import org.eclipse.syson.application.controllers.diagrams.testers.NodeCreationTester; |
| 33 | +import org.eclipse.syson.application.data.ViewUsageExposedElementsTestProjectData; |
| 34 | +import org.eclipse.syson.diagram.general.view.GVDescriptionNameGenerator; |
| 35 | +import org.eclipse.syson.services.SemanticRunnableFactory; |
| 36 | +import org.eclipse.syson.services.diagrams.DiagramDescriptionIdProvider; |
| 37 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramDescription; |
| 38 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramReference; |
| 39 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 40 | +import org.eclipse.syson.sysml.Element; |
| 41 | +import org.eclipse.syson.sysml.PartUsage; |
| 42 | +import org.eclipse.syson.sysml.SysmlPackage; |
| 43 | +import org.eclipse.syson.sysml.ViewUsage; |
| 44 | +import org.eclipse.syson.util.IDescriptionNameGenerator; |
| 45 | +import org.eclipse.syson.util.SysONRepresentationDescriptionIdentifiers; |
| 46 | +import org.junit.jupiter.api.AfterEach; |
| 47 | +import org.junit.jupiter.api.BeforeEach; |
| 48 | +import org.junit.jupiter.api.DisplayName; |
| 49 | +import org.junit.jupiter.api.Test; |
| 50 | +import org.springframework.beans.factory.annotation.Autowired; |
| 51 | +import org.springframework.boot.test.context.SpringBootTest; |
| 52 | +import org.springframework.test.context.jdbc.Sql; |
| 53 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 54 | +import org.springframework.transaction.annotation.Transactional; |
| 55 | + |
| 56 | +import reactor.test.StepVerifier; |
| 57 | +import reactor.test.StepVerifier.Step; |
| 58 | + |
| 59 | +/** |
| 60 | + * Tests the synchronization between the diagram nodes and the ViewUsage#exposedElements reference. |
| 61 | + * |
| 62 | + * @author arichard |
| 63 | + */ |
| 64 | +@Transactional |
| 65 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 66 | +public class GVViewUsageExposedElements extends AbstractIntegrationTests { |
| 67 | + |
| 68 | + private final IDescriptionNameGenerator descriptionNameGenerator = new GVDescriptionNameGenerator(); |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private IGivenInitialServerState givenInitialServerState; |
| 72 | + |
| 73 | + @Autowired |
| 74 | + private IGivenDiagramReference givenDiagram; |
| 75 | + |
| 76 | + @Autowired |
| 77 | + private IGivenDiagramDescription givenDiagramDescription; |
| 78 | + |
| 79 | + @Autowired |
| 80 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 81 | + |
| 82 | + @Autowired |
| 83 | + private IDiagramIdProvider diagramIdProvider; |
| 84 | + |
| 85 | + @Autowired |
| 86 | + private NodeCreationTester nodeCreationTester; |
| 87 | + |
| 88 | + @Autowired |
| 89 | + private SemanticRunnableFactory semanticRunnableFactory; |
| 90 | + |
| 91 | + @Autowired |
| 92 | + private IObjectSearchService objectSearchService; |
| 93 | + |
| 94 | + private Step<DiagramRefreshedEventPayload> verifier; |
| 95 | + |
| 96 | + private AtomicReference<Diagram> diagram; |
| 97 | + |
| 98 | + private DiagramDescription diagramDescription; |
| 99 | + |
| 100 | + private DiagramDescriptionIdProvider diagramDescriptionIdProvider; |
| 101 | + |
| 102 | + @BeforeEach |
| 103 | + public void setUp() { |
| 104 | + this.givenInitialServerState.initialize(); |
| 105 | + var diagramEventInput = new DiagramEventInput(UUID.randomUUID(), |
| 106 | + ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 107 | + ViewUsageExposedElementsTestProjectData.GraphicalIds.DIAGRAM_ID); |
| 108 | + var flux = this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 109 | + this.verifier = StepVerifier.create(flux); |
| 110 | + this.diagram = this.givenDiagram.getDiagram(this.verifier); |
| 111 | + this.diagramDescription = this.givenDiagramDescription.getDiagramDescription(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 112 | + SysONRepresentationDescriptionIdentifiers.GENERAL_VIEW_DIAGRAM_DESCRIPTION_ID); |
| 113 | + this.diagramDescriptionIdProvider = new DiagramDescriptionIdProvider(this.diagramDescription, this.diagramIdProvider); |
| 114 | + } |
| 115 | + |
| 116 | + @AfterEach |
| 117 | + public void tearDown() { |
| 118 | + if (this.verifier != null) { |
| 119 | + this.verifier.thenCancel() |
| 120 | + .verify(Duration.ofSeconds(10)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @DisplayName("GIVEN a GV diagram on a ViewUsage, WHEN New Part tool is executed, THEN a the ViewUsage#exposedElements is updated with the new Part") |
| 125 | + @Sql(scripts = { ViewUsageExposedElementsTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 126 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 127 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 128 | + @Test |
| 129 | + public void newPartToolShouldUpdateExposedElements() { |
| 130 | + String creationToolId = this.diagramDescriptionIdProvider.getDiagramCreationToolId(this.descriptionNameGenerator.getCreationToolName(SysmlPackage.eINSTANCE.getPartUsage())); |
| 131 | + assertThat(creationToolId).as("The tool 'New Part' should exist on the diagram").isNotNull(); |
| 132 | + this.verifier.then(() -> this.nodeCreationTester.createNodeOnDiagram(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 133 | + this.diagram, |
| 134 | + creationToolId)); |
| 135 | + |
| 136 | + this.verifier.consumeNextWith(payload -> Optional.of(payload)); |
| 137 | + |
| 138 | + Runnable semanticChecker = this.semanticRunnableFactory.createRunnable(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 139 | + (editingContext, executeEditingContextFunctionInput) -> { |
| 140 | + Object viewUsageObject = this.objectSearchService.getObject(editingContext, ViewUsageExposedElementsTestProjectData.SemanticIds.VIEW_USAGE_GV_ID).orElse(null); |
| 141 | + assertThat(viewUsageObject).isInstanceOf(ViewUsage.class); |
| 142 | + ViewUsage viewUsage = (ViewUsage) viewUsageObject; |
| 143 | + |
| 144 | + assertEquals(1, viewUsage.getExposedElement().size()); |
| 145 | + assertThat(viewUsage.getExposedElement().get(0)).isInstanceOf(PartUsage.class); |
| 146 | + assertThat(viewUsage.getExposedElement().get(0)).extracting(Element::getDeclaredName).isEqualTo("part2"); |
| 147 | + |
| 148 | + return new ExecuteEditingContextFunctionSuccessPayload(executeEditingContextFunctionInput.id(), true); |
| 149 | + }); |
| 150 | + |
| 151 | + this.verifier.then(semanticChecker); |
| 152 | + } |
| 153 | + |
| 154 | + @DisplayName("GIVEN a GV diagram on a ViewUsage, WHEN Add existing element(s) tool is executed, THEN a the ViewUsage#exposedElements is updated with partA") |
| 155 | + @Sql(scripts = { ViewUsageExposedElementsTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 156 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 157 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 158 | + @Test |
| 159 | + public void addExistingElementsToolShouldUpdateExposedElements() { |
| 160 | + String creationToolId = this.diagramDescriptionIdProvider.getDiagramCreationToolId("Add existing elements"); |
| 161 | + assertThat(creationToolId).as("The tool 'Add existing elements' should exist on the diagram").isNotNull(); |
| 162 | + this.verifier.then(() -> this.nodeCreationTester.createNodeOnDiagram(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 163 | + this.diagram, |
| 164 | + creationToolId)); |
| 165 | + |
| 166 | + this.verifier.consumeNextWith(payload -> Optional.of(payload)); |
| 167 | + |
| 168 | + Runnable semanticChecker = this.semanticRunnableFactory.createRunnable(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 169 | + (editingContext, executeEditingContextFunctionInput) -> { |
| 170 | + Object viewUsageObject = this.objectSearchService.getObject(editingContext, ViewUsageExposedElementsTestProjectData.SemanticIds.VIEW_USAGE_GV_ID).orElse(null); |
| 171 | + assertThat(viewUsageObject).isInstanceOf(ViewUsage.class); |
| 172 | + ViewUsage viewUsage = (ViewUsage) viewUsageObject; |
| 173 | + |
| 174 | + assertEquals(1, viewUsage.getExposedElement().size()); |
| 175 | + assertThat(viewUsage.getExposedElement().get(0)).isInstanceOf(PartUsage.class); |
| 176 | + assertThat(viewUsage.getExposedElement().get(0)).extracting(Element::getElementId).isEqualTo(ViewUsageExposedElementsTestProjectData.SemanticIds.PART_A_ID); |
| 177 | + |
| 178 | + return new ExecuteEditingContextFunctionSuccessPayload(executeEditingContextFunctionInput.id(), true); |
| 179 | + }); |
| 180 | + |
| 181 | + this.verifier.then(semanticChecker); |
| 182 | + } |
| 183 | + |
| 184 | + @DisplayName("GIVEN a GV diagram on a ViewUsage, WHEN Add existing element(s) (recursive) tool is executed, THEN a the ViewUsage#exposedElements is updated with partA and partB") |
| 185 | + @Sql(scripts = { ViewUsageExposedElementsTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 186 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 187 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 188 | + @Test |
| 189 | + public void addExistingElementsRecursivelyToolShouldUpdateExposedElements() { |
| 190 | + String creationToolId = this.diagramDescriptionIdProvider.getDiagramCreationToolId("Add existing elements (recursive)"); |
| 191 | + assertThat(creationToolId).as("The tool 'Add existing elements (recursive)' should exist on the diagram").isNotNull(); |
| 192 | + this.verifier.then(() -> this.nodeCreationTester.createNodeOnDiagram(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 193 | + this.diagram, |
| 194 | + creationToolId)); |
| 195 | + |
| 196 | + this.verifier.consumeNextWith(payload -> Optional.of(payload)); |
| 197 | + |
| 198 | + Runnable semanticChecker = this.semanticRunnableFactory.createRunnable(ViewUsageExposedElementsTestProjectData.EDITING_CONTEXT_ID, |
| 199 | + (editingContext, executeEditingContextFunctionInput) -> { |
| 200 | + Object viewUsageObject = this.objectSearchService.getObject(editingContext, ViewUsageExposedElementsTestProjectData.SemanticIds.VIEW_USAGE_GV_ID).orElse(null); |
| 201 | + assertThat(viewUsageObject).isInstanceOf(ViewUsage.class); |
| 202 | + ViewUsage viewUsage = (ViewUsage) viewUsageObject; |
| 203 | + |
| 204 | + assertEquals(2, viewUsage.getExposedElement().size()); |
| 205 | + assertThat(viewUsage.getExposedElement().get(0)).isInstanceOf(PartUsage.class); |
| 206 | + assertThat(viewUsage.getExposedElement().get(0)).extracting(Element::getElementId).isEqualTo(ViewUsageExposedElementsTestProjectData.SemanticIds.PART_A_ID); |
| 207 | + assertThat(viewUsage.getExposedElement().get(1)).isInstanceOf(PartUsage.class); |
| 208 | + assertThat(viewUsage.getExposedElement().get(1)).extracting(Element::getElementId).isEqualTo(ViewUsageExposedElementsTestProjectData.SemanticIds.PART_B_ID); |
| 209 | + |
| 210 | + return new ExecuteEditingContextFunctionSuccessPayload(executeEditingContextFunctionInput.id(), true); |
| 211 | + }); |
| 212 | + |
| 213 | + this.verifier.then(semanticChecker); |
| 214 | + } |
| 215 | +} |
0 commit comments