|
| 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; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.eclipse.sirius.components.diagrams.tests.DiagramEventPayloadConsumer.assertRefreshedDiagramThat; |
| 17 | + |
| 18 | +import com.jayway.jsonpath.JsonPath; |
| 19 | + |
| 20 | +import java.text.MessageFormat; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.concurrent.atomic.AtomicReference; |
| 26 | +import java.util.function.Consumer; |
| 27 | + |
| 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.collaborative.diagrams.dto.DropNodesInput; |
| 31 | +import org.eclipse.sirius.components.core.api.IFeedbackMessageService; |
| 32 | +import org.eclipse.sirius.components.core.api.SuccessPayload; |
| 33 | +import org.eclipse.sirius.components.diagrams.Diagram; |
| 34 | +import org.eclipse.sirius.components.diagrams.layoutdata.Position; |
| 35 | +import org.eclipse.sirius.components.representations.Message; |
| 36 | +import org.eclipse.sirius.components.representations.MessageLevel; |
| 37 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 38 | +import org.eclipse.syson.application.controllers.diagrams.testers.DropNodesWithMessageMutationRunner; |
| 39 | +import org.eclipse.syson.application.data.GeneralViewFlowUsageProjectData; |
| 40 | +import org.eclipse.syson.services.api.IDefaultSysMLMoveElementService; |
| 41 | +import org.eclipse.syson.services.api.ISysMLMoveElementServiceDelegate; |
| 42 | +import org.eclipse.syson.services.api.MoveStatus; |
| 43 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 44 | +import org.eclipse.syson.sysml.Element; |
| 45 | +import org.junit.jupiter.api.BeforeEach; |
| 46 | +import org.junit.jupiter.api.DisplayName; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | +import org.springframework.beans.factory.annotation.Autowired; |
| 49 | +import org.springframework.boot.test.context.SpringBootTest; |
| 50 | +import org.springframework.boot.test.context.TestConfiguration; |
| 51 | +import org.springframework.context.annotation.Bean; |
| 52 | +import org.springframework.context.annotation.Import; |
| 53 | +import org.springframework.transaction.annotation.Transactional; |
| 54 | + |
| 55 | +import reactor.core.publisher.Flux; |
| 56 | +import reactor.test.StepVerifier; |
| 57 | + |
| 58 | +/** |
| 59 | + * Integration tests for {@link ISysMLMoveElementServiceDelegate}. |
| 60 | + * |
| 61 | + * @author Arthur Daussy |
| 62 | + */ |
| 63 | +@Transactional |
| 64 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 65 | +@Import(SysMLMoveElementServiceDelegateIntegrationTests.SysMLMoveElementServiceDelegateTestConfiguration.class) |
| 66 | +public class SysMLMoveElementServiceDelegateIntegrationTests extends AbstractIntegrationTests { |
| 67 | + |
| 68 | + private static final String DROP_NODES_TYPENAME = "$.data.dropNodes.__typename"; |
| 69 | + |
| 70 | + private static final String DROP_NODES_MESSAGE_BODY = "$.data.dropNodes.messages[0].body"; |
| 71 | + |
| 72 | + private static final String DROP_NODES_MESSAGE_LEVEL = "$.data.dropNodes.messages[0].level"; |
| 73 | + |
| 74 | + private static final String EXPECTED_FEEDBACK_MESSAGE = "Moved screen to Package1"; |
| 75 | + |
| 76 | + @Autowired |
| 77 | + private IGivenInitialServerState givenInitialServerState; |
| 78 | + |
| 79 | + @Autowired |
| 80 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 81 | + |
| 82 | + @Autowired |
| 83 | + private DropNodesWithMessageMutationRunner dropNodeRunner; |
| 84 | + |
| 85 | + private Flux<DiagramRefreshedEventPayload> givenSubscriptionToDiagram() { |
| 86 | + var diagramEventInput = new DiagramEventInput(UUID.randomUUID(), |
| 87 | + GeneralViewFlowUsageProjectData.EDITING_CONTEXT_ID, |
| 88 | + GeneralViewFlowUsageProjectData.GraphicalIds.DIAGRAM_ID); |
| 89 | + return this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeEach |
| 93 | + public void beforeEach() { |
| 94 | + this.givenInitialServerState.initialize(); |
| 95 | + } |
| 96 | + |
| 97 | + @DisplayName("GIVEN a move delegate, WHEN Screen is dropped into Package 1, THEN the delegate moves it and returns a feedback message") |
| 98 | + @GivenSysONServer({ GeneralViewFlowUsageProjectData.SCRIPT_PATH }) |
| 99 | + @Test |
| 100 | + public void moveScreenPartWithDelegate() { |
| 101 | + var flux = this.givenSubscriptionToDiagram(); |
| 102 | + |
| 103 | + AtomicReference<Diagram> diagram = new AtomicReference<>(); |
| 104 | + |
| 105 | + Consumer<Object> initialDiagramContentConsumer = assertRefreshedDiagramThat(diagram::set); |
| 106 | + |
| 107 | + Runnable dropScreenPartFromDiagramToPackage = () -> { |
| 108 | + var input = new DropNodesInput( |
| 109 | + UUID.randomUUID(), |
| 110 | + GeneralViewFlowUsageProjectData.EDITING_CONTEXT_ID, |
| 111 | + GeneralViewFlowUsageProjectData.GraphicalIds.DIAGRAM_ID, |
| 112 | + List.of(GeneralViewFlowUsageProjectData.GraphicalIds.SCREEN_PART_ID), |
| 113 | + null, |
| 114 | + List.of(new Position(0, 0))); |
| 115 | + var result = this.dropNodeRunner.run(input); |
| 116 | + String typename = JsonPath.read(result.data(), DROP_NODES_TYPENAME); |
| 117 | + assertThat(typename).isEqualTo(SuccessPayload.class.getSimpleName()); |
| 118 | + String messageBody = JsonPath.read(result.data(), DROP_NODES_MESSAGE_BODY); |
| 119 | + assertThat(messageBody).isEqualTo(EXPECTED_FEEDBACK_MESSAGE); |
| 120 | + String messageLevel = JsonPath.read(result.data(), DROP_NODES_MESSAGE_LEVEL); |
| 121 | + assertThat(messageLevel).isEqualTo(MessageLevel.INFO.toString()); |
| 122 | + }; |
| 123 | + |
| 124 | + Consumer<Object> updatedDiagramContentConsumer = assertRefreshedDiagramThat(newDiagram -> { |
| 125 | + assertThat(newDiagram.getNodes()) |
| 126 | + .extracting(org.eclipse.sirius.components.diagrams.Node::getTargetObjectId) |
| 127 | + .contains(GeneralViewFlowUsageProjectData.SemanticIds.SCREEN_PART); |
| 128 | + }); |
| 129 | + |
| 130 | + StepVerifier.create(flux) |
| 131 | + .consumeNextWith(initialDiagramContentConsumer) |
| 132 | + .then(dropScreenPartFromDiagramToPackage) |
| 133 | + .consumeNextWith(updatedDiagramContentConsumer) |
| 134 | + .thenCancel() |
| 135 | + .verify(Duration.ofSeconds(10)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Test configuration used to activate the move delegate only for this integration test. |
| 140 | + */ |
| 141 | + @TestConfiguration |
| 142 | + static class SysMLMoveElementServiceDelegateTestConfiguration { |
| 143 | + |
| 144 | + @Bean |
| 145 | + ISysMLMoveElementServiceDelegate feedbackMoveElementServiceDelegate(IDefaultSysMLMoveElementService defaultMoveElementService, IFeedbackMessageService feedbackMessageService) { |
| 146 | + return new FeedbackMoveElementServiceDelegate(defaultMoveElementService, feedbackMessageService); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test move delegate which behaves like the default implementation and emits a feedback message. |
| 152 | + */ |
| 153 | + private static final class FeedbackMoveElementServiceDelegate implements ISysMLMoveElementServiceDelegate { |
| 154 | + |
| 155 | + private final IDefaultSysMLMoveElementService defaultMoveElementService; |
| 156 | + |
| 157 | + private final IFeedbackMessageService feedbackMessageService; |
| 158 | + |
| 159 | + FeedbackMoveElementServiceDelegate(IDefaultSysMLMoveElementService defaultMoveElementService, IFeedbackMessageService feedbackMessageService) { |
| 160 | + this.defaultMoveElementService = Objects.requireNonNull(defaultMoveElementService); |
| 161 | + this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean canHandle(Element element, Element newParent) { |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public MoveStatus moveSemanticElement(Element element, Element newParent) { |
| 171 | + MoveStatus moveStatus = this.defaultMoveElementService.moveSemanticElement(element, newParent); |
| 172 | + if (moveStatus.isSuccess() && element != newParent) { |
| 173 | + String elementLabel = element.getDeclaredName(); |
| 174 | + String parentLabel = newParent.getDeclaredName(); |
| 175 | + this.feedbackMessageService.addFeedbackMessage(new Message(MessageFormat.format("Moved {0} to {1}", elementLabel, parentLabel), MessageLevel.INFO)); |
| 176 | + } |
| 177 | + return moveStatus; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments