|
| 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 | +import static org.assertj.core.api.Assertions.fail; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import com.jayway.jsonpath.JsonPath; |
| 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.managevisibility.InvokeManageVisibilityActionInput; |
| 29 | +import org.eclipse.sirius.components.core.api.SuccessPayload; |
| 30 | +import org.eclipse.sirius.components.diagrams.ViewModifier; |
| 31 | +import org.eclipse.sirius.components.diagrams.tests.graphql.GetManageVisibilityActionsQueryRunner; |
| 32 | +import org.eclipse.sirius.components.diagrams.tests.graphql.InvokeManageVisibilityActionMutationRunner; |
| 33 | +import org.eclipse.sirius.web.application.nodeaction.managevisibility.ManageVisibilityHideAllAction; |
| 34 | +import org.eclipse.sirius.web.application.nodeaction.managevisibility.ManageVisibilityRevealAllAction; |
| 35 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 36 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 37 | +import org.eclipse.syson.application.data.GeneralViewManageVisibilityTestsProjectData; |
| 38 | +import org.eclipse.syson.diagram.general.view.services.nodeactions.managevisibility.GeneralViewManageVisibilityRevealValuedContentAction; |
| 39 | +import org.eclipse.syson.services.diagrams.api.IGivenDiagramSubscription; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.DisplayName; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.springframework.beans.factory.annotation.Autowired; |
| 44 | +import org.springframework.boot.test.context.SpringBootTest; |
| 45 | +import org.springframework.test.context.jdbc.Sql; |
| 46 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 47 | +import org.springframework.transaction.annotation.Transactional; |
| 48 | + |
| 49 | +import reactor.core.publisher.Flux; |
| 50 | +import reactor.test.StepVerifier; |
| 51 | + |
| 52 | +/** |
| 53 | + * Tests the manage visibility node action. |
| 54 | + * |
| 55 | + * @author mcharfadi |
| 56 | + */ |
| 57 | +@Transactional |
| 58 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 59 | +public class GVManageVisibilityTests extends AbstractIntegrationTests { |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private IGivenInitialServerState givenInitialServerState; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private IGivenDiagramSubscription givenDiagramSubscription; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private GetManageVisibilityActionsQueryRunner getActionsQueryRunner; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private InvokeManageVisibilityActionMutationRunner invokeActionMutationRunner; |
| 72 | + |
| 73 | + @BeforeEach |
| 74 | + public void setUp() { |
| 75 | + this.givenInitialServerState.initialize(); |
| 76 | + } |
| 77 | + |
| 78 | + private Flux<DiagramRefreshedEventPayload> givenSubscriptionToGeneralViewDiagram() { |
| 79 | + var diagramEventInput = new DiagramEventInput( |
| 80 | + UUID.randomUUID(), |
| 81 | + GeneralViewManageVisibilityTestsProjectData.EDITING_CONTEXT_ID, |
| 82 | + GeneralViewManageVisibilityTestsProjectData.GraphicalIds.DIAGRAM_ID); |
| 83 | + return this.givenDiagramSubscription.subscribe(diagramEventInput); |
| 84 | + } |
| 85 | + |
| 86 | + @Sql(scripts = { GeneralViewManageVisibilityTestsProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 87 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 88 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 89 | + @DisplayName("Given a graphical node with some children hidden and some revealed, when invoking the show valued content only, then only the children that have children are visible") |
| 90 | + @Test |
| 91 | + public void invokeShowOnlyValuedContent() { |
| 92 | + var flux = this.givenSubscriptionToGeneralViewDiagram(); |
| 93 | + var nodeId = new AtomicReference<String>(); |
| 94 | + Consumer<DiagramRefreshedEventPayload> initialDiagramContentConsumer = payload -> Optional.of(payload) |
| 95 | + .ifPresentOrElse(diagramRefreshedEventPayload -> { |
| 96 | + var diagram = diagramRefreshedEventPayload.diagram(); |
| 97 | + assertThat(diagram.getNodes()).hasSize(1); |
| 98 | + nodeId.set(diagram.getNodes().get(0).getId()); |
| 99 | + assertThat(diagram.getNodes().get(0).getChildNodes()).hasSize(5); |
| 100 | + var children = diagram.getNodes().get(0).getChildNodes(); |
| 101 | + assertThat(children.stream().filter(node -> node.getState().equals(ViewModifier.Hidden))).hasSize(5); |
| 102 | + assertThat(children.stream().filter(node -> node.getState().equals(ViewModifier.Normal))).hasSize(0); |
| 103 | + }, () -> fail("Missing diagram")); |
| 104 | + |
| 105 | + Runnable getActions = () -> { |
| 106 | + Map<String, Object> variables = Map.of( |
| 107 | + "editingContextId", GeneralViewManageVisibilityTestsProjectData.EDITING_CONTEXT_ID, |
| 108 | + "diagramId", GeneralViewManageVisibilityTestsProjectData.GraphicalIds.DIAGRAM_ID, |
| 109 | + "diagramElementId", nodeId.get() |
| 110 | + ); |
| 111 | + var result = this.getActionsQueryRunner.run(variables); |
| 112 | + List<String> actionsIds = JsonPath.read(result, "$.data.viewer.editingContext.representation.description.manageVisibilityActions[*].id"); |
| 113 | + List<String> actionsLabels = JsonPath.read(result, "$.data.viewer.editingContext.representation.description.manageVisibilityActions[*].label"); |
| 114 | + |
| 115 | + assertThat(actionsIds) |
| 116 | + .isNotEmpty() |
| 117 | + .contains(ManageVisibilityRevealAllAction.ACTION_ID) |
| 118 | + .contains(ManageVisibilityHideAllAction.ACTION_ID) |
| 119 | + .contains(GeneralViewManageVisibilityRevealValuedContentAction.ACTION_ID); |
| 120 | + |
| 121 | + assertThat(actionsLabels) |
| 122 | + .isNotEmpty() |
| 123 | + .contains("Hide all") |
| 124 | + .contains("Reveal all") |
| 125 | + .contains("Reveal valued content only"); |
| 126 | + }; |
| 127 | + |
| 128 | + Runnable invokeRevealValuedContentAction = () -> { |
| 129 | + var input = new InvokeManageVisibilityActionInput(UUID.randomUUID(), GeneralViewManageVisibilityTestsProjectData.EDITING_CONTEXT_ID, GeneralViewManageVisibilityTestsProjectData.GraphicalIds.DIAGRAM_ID, nodeId.get(), GeneralViewManageVisibilityRevealValuedContentAction.ACTION_ID); |
| 130 | + var result = this.invokeActionMutationRunner.run(input); |
| 131 | + String typename = JsonPath.read(result, "$.data.invokeManageVisibilityAction.__typename"); |
| 132 | + assertThat(typename).isEqualTo(SuccessPayload.class.getSimpleName()); |
| 133 | + }; |
| 134 | + |
| 135 | + Consumer<DiagramRefreshedEventPayload> updatedAfterRevealDiagramContentMatcher = payload -> Optional.of(payload) |
| 136 | + .ifPresentOrElse(diagramRefreshedEventPayload -> { |
| 137 | + var diagram = diagramRefreshedEventPayload.diagram(); |
| 138 | + assertThat(diagram.getNodes()).hasSize(1); |
| 139 | + nodeId.set(diagram.getNodes().get(0).getId()); |
| 140 | + assertThat(diagram.getNodes().get(0).getChildNodes()).hasSize(5); |
| 141 | + var children = diagram.getNodes().get(0).getChildNodes(); |
| 142 | + assertThat(children.stream().filter(node -> node.getState().equals(ViewModifier.Hidden))).hasSize(3); |
| 143 | + assertThat(children.stream().filter(node -> node.getState().equals(ViewModifier.Normal))).hasSize(1); |
| 144 | + assertThat(children.stream().filter(node -> node.getState().equals(ViewModifier.Faded))).hasSize(1); |
| 145 | + }, () -> fail("Missing diagram")); |
| 146 | + |
| 147 | + StepVerifier.create(flux) |
| 148 | + .consumeNextWith(initialDiagramContentConsumer) |
| 149 | + .then(getActions) |
| 150 | + .then(invokeRevealValuedContentAction) |
| 151 | + .consumeNextWith(updatedAfterRevealDiagramContentMatcher) |
| 152 | + .thenCancel() |
| 153 | + .verify(Duration.ofSeconds(10)); |
| 154 | + } |
| 155 | +} |
0 commit comments