|
| 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.assertj.core.api.InstanceOfAssertFactories.type; |
| 17 | +import static org.eclipse.sirius.components.diagrams.tests.DiagramEventPayloadConsumer.assertRefreshedDiagramThat; |
| 18 | +import static org.eclipse.sirius.components.diagrams.tests.assertions.DiagramInstanceOfAssertFactories.EDGE; |
| 19 | +import static org.eclipse.sirius.components.diagrams.tests.assertions.DiagramInstanceOfAssertFactories.EDGE_STYLE; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.List; |
| 23 | +import java.util.UUID; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import org.eclipse.emf.ecore.EClass; |
| 28 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramEventInput; |
| 29 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload; |
| 30 | +import org.eclipse.sirius.components.core.api.IObjectSearchService; |
| 31 | +import org.eclipse.sirius.components.diagrams.ArrowStyle; |
| 32 | +import org.eclipse.sirius.components.diagrams.Diagram; |
| 33 | +import org.eclipse.sirius.components.diagrams.Edge; |
| 34 | +import org.eclipse.sirius.components.diagrams.Label; |
| 35 | +import org.eclipse.sirius.components.diagrams.ViewModifier; |
| 36 | +import org.eclipse.sirius.components.view.emf.diagram.IDiagramIdProvider; |
| 37 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 38 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 39 | +import org.eclipse.syson.GivenSysONServer; |
| 40 | +import org.eclipse.syson.application.controller.editingcontext.checkers.SemanticCheckerService; |
| 41 | +import org.eclipse.syson.application.controllers.diagrams.testers.EdgeCreationTester; |
| 42 | +import org.eclipse.syson.application.data.GeneralViewWithTopNodesTestProjectData; |
| 43 | +import org.eclipse.syson.services.SemanticRunnableFactory; |
| 44 | +import org.eclipse.syson.services.diagrams.DiagramComparator; |
| 45 | +import org.eclipse.syson.services.diagrams.DiagramDescriptionIdProvider; |
| 46 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramDescription; |
| 47 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 48 | +import org.eclipse.syson.standard.diagrams.view.SDVDescriptionNameGenerator; |
| 49 | +import org.eclipse.syson.sysml.FramedConcernMembership; |
| 50 | +import org.eclipse.syson.sysml.SysmlPackage; |
| 51 | +import org.eclipse.syson.sysml.helper.LabelConstants; |
| 52 | +import org.eclipse.syson.util.IDescriptionNameGenerator; |
| 53 | +import org.eclipse.syson.util.SysONRepresentationDescriptionIdentifiers; |
| 54 | +import org.junit.jupiter.api.BeforeEach; |
| 55 | +import org.junit.jupiter.api.Test; |
| 56 | +import org.springframework.beans.factory.annotation.Autowired; |
| 57 | +import org.springframework.boot.test.context.SpringBootTest; |
| 58 | +import org.springframework.transaction.annotation.Transactional; |
| 59 | + |
| 60 | +import reactor.core.publisher.Flux; |
| 61 | +import reactor.test.StepVerifier; |
| 62 | + |
| 63 | +/** |
| 64 | + * {@link org.eclipse.syson.sysml.FramedConcernMembership} related test in General View. |
| 65 | + * |
| 66 | + * @author gcoutable |
| 67 | + */ |
| 68 | +@Transactional |
| 69 | +@GivenSysONServer({ GeneralViewWithTopNodesTestProjectData.SCRIPT_PATH }) |
| 70 | +@SuppressWarnings("checkstyle:MultipleStringLiterals") |
| 71 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 72 | +public class GVFramedConcernTests extends AbstractIntegrationTests { |
| 73 | + |
| 74 | + @Autowired |
| 75 | + private IGivenInitialServerState givenInitialServerState; |
| 76 | + |
| 77 | + @Autowired |
| 78 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 79 | + |
| 80 | + @Autowired |
| 81 | + private IGivenDiagramDescription givenDiagramDescription; |
| 82 | + |
| 83 | + @Autowired |
| 84 | + private IDiagramIdProvider diagramIdProvider; |
| 85 | + |
| 86 | + @Autowired |
| 87 | + private DiagramComparator diagramComparator; |
| 88 | + |
| 89 | + @Autowired |
| 90 | + private SemanticRunnableFactory semanticRunnableFactory; |
| 91 | + |
| 92 | + @Autowired |
| 93 | + private IObjectSearchService objectSearchService; |
| 94 | + |
| 95 | + @Autowired |
| 96 | + private EdgeCreationTester edgeCreationTester; |
| 97 | + |
| 98 | + private final IDescriptionNameGenerator descriptionNameGenerator = new SDVDescriptionNameGenerator(); |
| 99 | + |
| 100 | + private SemanticCheckerService semanticCheckerService; |
| 101 | + |
| 102 | + private Flux<DiagramRefreshedEventPayload> givenSubscriptionToDiagram() { |
| 103 | + var diagramEventInput = new DiagramEventInput(UUID.randomUUID(), GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, GeneralViewWithTopNodesTestProjectData.GraphicalIds.DIAGRAM_ID); |
| 104 | + return this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 105 | + } |
| 106 | + |
| 107 | + @BeforeEach |
| 108 | + public void setUp() { |
| 109 | + this.givenInitialServerState.initialize(); |
| 110 | + this.semanticCheckerService = new SemanticCheckerService(this.semanticRunnableFactory, this.objectSearchService, GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, |
| 111 | + GeneralViewWithTopNodesTestProjectData.SemanticIds.PACKAGE_1_ID); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testCreateFramedConcernWithEdgeBetweenRequirementUsageAndConcernUsage() { |
| 116 | + this.createFramedConcernWithEdge(SysmlPackage.eINSTANCE.getRequirementUsage(), GeneralViewWithTopNodesTestProjectData.GraphicalIds.REQUIREMENT_USAGE_ID, "requirement"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testCreateFramedConcernWithEdgeBetweenRequirementDefinitionAndConcernUsage() { |
| 121 | + this.createFramedConcernWithEdge(SysmlPackage.eINSTANCE.getRequirementDefinition(), GeneralViewWithTopNodesTestProjectData.GraphicalIds.REQUIREMENT_DEFINITION_ID, "RequirementDefinition"); |
| 122 | + } |
| 123 | + |
| 124 | + private void createFramedConcernWithEdge(EClass parentEClass, String graphicalSourceNodeId, String parentLabel) { |
| 125 | + var flux = this.givenSubscriptionToDiagram(); |
| 126 | + |
| 127 | + var diagramDescription = this.givenDiagramDescription.getDiagramDescription(GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, |
| 128 | + SysONRepresentationDescriptionIdentifiers.GENERAL_VIEW_DIAGRAM_DESCRIPTION_ID); |
| 129 | + var diagramDescriptionIdProvider = new DiagramDescriptionIdProvider(diagramDescription, this.diagramIdProvider); |
| 130 | + |
| 131 | + var edgeCreationToolId = diagramDescriptionIdProvider.getEdgeCreationToolId( |
| 132 | + this.descriptionNameGenerator.getNodeName(parentEClass), |
| 133 | + "New framed Concern"); |
| 134 | + |
| 135 | + AtomicReference<Diagram> diagram = new AtomicReference<>(); |
| 136 | + Consumer<Object> initialDiagramContentConsumer = assertRefreshedDiagramThat(diagram::set); |
| 137 | + |
| 138 | + Runnable createEdgeRunnable = () -> this.edgeCreationTester.createEdgeUsingNodeId(GeneralViewWithTopNodesTestProjectData.EDITING_CONTEXT_ID, |
| 139 | + diagram, |
| 140 | + graphicalSourceNodeId, |
| 141 | + GeneralViewWithTopNodesTestProjectData.GraphicalIds.CONCERN_USAGE_ID, |
| 142 | + edgeCreationToolId); |
| 143 | + |
| 144 | + Consumer<Object> diagramContentConsumerAfterNewEdge = assertRefreshedDiagramThat(newDiagram -> { |
| 145 | + var newEdges = this.diagramComparator.newEdges(diagram.get(), newDiagram); |
| 146 | + var newVisibleEdges = newEdges.stream().filter(edge -> edge.getState() != ViewModifier.Hidden).toList(); |
| 147 | + |
| 148 | + assertThat(newVisibleEdges).hasSize(1).first(EDGE) |
| 149 | + .hasSourceId(graphicalSourceNodeId) |
| 150 | + .hasTargetId(GeneralViewWithTopNodesTestProjectData.GraphicalIds.CONCERN_USAGE_ID) |
| 151 | + .extracting(Edge::getCenterLabel) |
| 152 | + .extracting(Label::text) |
| 153 | + .hasToString(LabelConstants.OPEN_QUOTE + LabelConstants.FRAME + LabelConstants.CLOSE_QUOTE); |
| 154 | + |
| 155 | + assertThat(newVisibleEdges).hasSize(1).first(EDGE) |
| 156 | + .extracting(Edge::getStyle, EDGE_STYLE) |
| 157 | + .hasSourceArrow(ArrowStyle.None) |
| 158 | + .hasTargetArrow(ArrowStyle.InputArrow); |
| 159 | + }); |
| 160 | + |
| 161 | + Consumer<Object> additionalCheck = object -> { |
| 162 | + assertThat(object).isInstanceOf(List.class) |
| 163 | + .asInstanceOf(type(List.class)) |
| 164 | + .satisfies(framedConcerns -> { |
| 165 | + assertThat((List<?>) framedConcerns).size().isEqualTo(1); |
| 166 | + assertThat(framedConcerns.getFirst()) |
| 167 | + .isInstanceOf(FramedConcernMembership.class) |
| 168 | + .asInstanceOf(type(FramedConcernMembership.class)) |
| 169 | + .satisfies(framedConcernMembership -> { |
| 170 | + var ownedConcern = framedConcernMembership.getOwnedConcern(); |
| 171 | + var ownedSubsetting = ownedConcern.getOwnedSubsetting(); |
| 172 | + assertThat(ownedSubsetting).isNotEmpty(); |
| 173 | + assertThat(ownedSubsetting.getFirst().getSubsettedFeature()).isEqualTo(framedConcernMembership.getReferencedConcern()); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }; |
| 177 | + |
| 178 | + Runnable semanticCheck = this.semanticCheckerService.checkEditingContext(this.semanticCheckerService.getElementInParentSemanticChecker(parentLabel, SysmlPackage.eINSTANCE.getElement_OwnedRelationship(), SysmlPackage.eINSTANCE.getFramedConcernMembership(), additionalCheck)); |
| 179 | + |
| 180 | + StepVerifier.create(flux) |
| 181 | + .consumeNextWith(initialDiagramContentConsumer) |
| 182 | + .then(createEdgeRunnable) |
| 183 | + .consumeNextWith(diagramContentConsumerAfterNewEdge) |
| 184 | + .then(semanticCheck) |
| 185 | + .thenCancel() |
| 186 | + .verify(Duration.ofSeconds(10)); |
| 187 | + } |
| 188 | +} |
0 commit comments