|
| 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.explorer.view; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.eclipse.sirius.components.trees.tests.TreeEventPayloadConsumer.assertRefreshedTreeThat; |
| 17 | + |
| 18 | +import com.jayway.jsonpath.JsonPath; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.function.Predicate; |
| 28 | + |
| 29 | +import org.eclipse.emf.ecore.EObject; |
| 30 | +import org.eclipse.emf.ecore.resource.Resource; |
| 31 | +import org.eclipse.sirius.components.core.api.IEditingContextSearchService; |
| 32 | +import org.eclipse.sirius.components.core.api.IIdentityService; |
| 33 | +import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext; |
| 34 | +import org.eclipse.sirius.components.trees.Tree; |
| 35 | +import org.eclipse.sirius.components.trees.TreeItem; |
| 36 | +import org.eclipse.sirius.components.trees.tests.graphql.InitialDirectEditTreeItemLabelQueryRunner; |
| 37 | +import org.eclipse.sirius.web.application.views.explorer.ExplorerEventInput; |
| 38 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 39 | +import org.eclipse.sirius.web.tests.services.explorer.ExplorerEventSubscriptionRunner; |
| 40 | +import org.eclipse.sirius.web.tests.services.representation.RepresentationIdBuilder; |
| 41 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 42 | +import org.eclipse.syson.application.data.ExplorerViewDirectEditTestProjectData; |
| 43 | +import org.eclipse.syson.tree.explorer.view.SysONTreeViewDescriptionProvider; |
| 44 | +import org.eclipse.syson.tree.explorer.view.filters.SysONTreeFilterProvider; |
| 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.test.context.jdbc.Sql; |
| 51 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 52 | +import org.springframework.transaction.annotation.Transactional; |
| 53 | + |
| 54 | +import reactor.test.StepVerifier; |
| 55 | + |
| 56 | +/** |
| 57 | + * Integration tests of the Explorer view. |
| 58 | + * |
| 59 | + * @author arichard |
| 60 | + */ |
| 61 | +@Transactional |
| 62 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 63 | +public class ExplorerViewControllerIntegrationTests extends AbstractIntegrationTests { |
| 64 | + |
| 65 | + /** |
| 66 | + * Record used to contain both a way to find a tree item and some predicate to validate on said tree item in order |
| 67 | + * to simplify the design of some advanced tests. |
| 68 | + * |
| 69 | + * @author arichard |
| 70 | + */ |
| 71 | + private record TreeItemMatcher(Function<Tree, TreeItem> treeItemFinder, Predicate<TreeItem> treeItemPredicate) { |
| 72 | + } |
| 73 | + |
| 74 | + private final TreeItemMatcher view1GV = new TreeItemMatcher( |
| 75 | + tree -> tree.getChildren().get(0).getChildren().get(0).getChildren().get(2), |
| 76 | + treeItem -> treeItem.getLabel().toString().equals("view1 [GeneralView]")); |
| 77 | + |
| 78 | + private final TreeItemMatcher view2AFV = new TreeItemMatcher( |
| 79 | + tree -> tree.getChildren().get(0).getChildren().get(0).getChildren().get(3), |
| 80 | + treeItem -> treeItem.getLabel().toString().equals("view2 [ActionFlowView]")); |
| 81 | + |
| 82 | + private final TreeItemMatcher view3STV = new TreeItemMatcher( |
| 83 | + tree -> tree.getChildren().get(0).getChildren().get(0).getChildren().get(4), |
| 84 | + treeItem -> treeItem.getLabel().toString().equals("view3 [StateTransitionView]")); |
| 85 | + |
| 86 | + private final TreeItemMatcher view4IV = new TreeItemMatcher( |
| 87 | + tree -> tree.getChildren().get(0).getChildren().get(0).getChildren().get(1).getChildren().get(1), |
| 88 | + treeItem -> treeItem.getLabel().toString().equals("view4 [InterconnectionView]")); |
| 89 | + |
| 90 | + @Autowired |
| 91 | + private IGivenInitialServerState givenInitialServerState; |
| 92 | + |
| 93 | + @Autowired |
| 94 | + private IEditingContextSearchService editingContextSearchService; |
| 95 | + |
| 96 | + @Autowired |
| 97 | + private IIdentityService identityService; |
| 98 | + |
| 99 | + @Autowired |
| 100 | + private ExplorerEventSubscriptionRunner treeEventSubscriptionRunner; |
| 101 | + |
| 102 | + @Autowired |
| 103 | + private InitialDirectEditTreeItemLabelQueryRunner initialDirectEditTreeItemLabelQueryRunner; |
| 104 | + |
| 105 | + @Autowired |
| 106 | + private RepresentationIdBuilder representationIdBuilder; |
| 107 | + |
| 108 | + @Autowired |
| 109 | + private SysONTreeViewDescriptionProvider sysONTreeViewDescriptionProvider; |
| 110 | + |
| 111 | + @BeforeEach |
| 112 | + public void beforeEach() { |
| 113 | + this.givenInitialServerState.initialize(); |
| 114 | + } |
| 115 | + |
| 116 | + @DisplayName("GIVEN the SysON Explorer View, WHEN we direct edit a ViewUsage typed with a standard diagram, THEN the type of ViewUsage is not part of the initial value of the direct edit") |
| 117 | + @Sql(scripts = { ExplorerViewDirectEditTestProjectData.SCRIPT_PATH }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, |
| 118 | + config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 119 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 120 | + @Test |
| 121 | + public void testDirectEditOnViewUsage() { |
| 122 | + var expandedIds = this.getAllTreeItemIds(); |
| 123 | + var activatedFilters = List.of(SysONTreeFilterProvider.HIDE_ROOT_NAMESPACES_ID, SysONTreeFilterProvider.HIDE_MEMBERSHIPS_TREE_ITEM_FILTER_ID); |
| 124 | + var treeRepresentationId = this.representationIdBuilder.buildExplorerRepresentationId(this.sysONTreeViewDescriptionProvider.getDescriptionId(), expandedIds, activatedFilters); |
| 125 | + |
| 126 | + var treeEventInput = new ExplorerEventInput(UUID.randomUUID(), ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID, treeRepresentationId); |
| 127 | + var treeFlux = this.treeEventSubscriptionRunner.run(treeEventInput); |
| 128 | + |
| 129 | + var hasProjectContent = this.getTreeRefreshedEventPayloadMatcher(List.of(this.view1GV, this.view2AFV, this.view3STV, this.view4IV)); |
| 130 | + |
| 131 | + StepVerifier.create(treeFlux) |
| 132 | + .consumeNextWith(hasProjectContent) |
| 133 | + .then(this.triggerDirectEditTreeItemLabel(ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID, treeRepresentationId, UUID.fromString(ExplorerViewDirectEditTestProjectData.SemanticIds.VIEW_1_GV_ID), "view1")) |
| 134 | + .then(this.triggerDirectEditTreeItemLabel(ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID, treeRepresentationId, |
| 135 | + UUID.fromString(ExplorerViewDirectEditTestProjectData.SemanticIds.VIEW_2_AFV_ID), "view2")) |
| 136 | + .then(this.triggerDirectEditTreeItemLabel(ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID, treeRepresentationId, |
| 137 | + UUID.fromString(ExplorerViewDirectEditTestProjectData.SemanticIds.VIEW_3_STV_ID), "view3")) |
| 138 | + .then(this.triggerDirectEditTreeItemLabel(ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID, treeRepresentationId, |
| 139 | + UUID.fromString(ExplorerViewDirectEditTestProjectData.SemanticIds.VIEW_4_IV_ID), "view4")) |
| 140 | + .thenCancel() |
| 141 | + .verify(Duration.ofSeconds(100)); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + private List<String> getAllTreeItemIds() { |
| 146 | + var optionalEditingContext = this.editingContextSearchService.findById(ExplorerViewDirectEditTestProjectData.EDITING_CONTEXT_ID) |
| 147 | + .filter(IEMFEditingContext.class::isInstance) |
| 148 | + .map(IEMFEditingContext.class::cast); |
| 149 | + assertThat(optionalEditingContext).isPresent(); |
| 150 | + |
| 151 | + var editingContext = optionalEditingContext.get(); |
| 152 | + var expandedIds = new ArrayList<String>(); |
| 153 | + editingContext.getDomain().getResourceSet().getAllContents().forEachRemaining(notifier -> { |
| 154 | + if (notifier instanceof Resource || notifier instanceof EObject) { |
| 155 | + expandedIds.add(this.identityService.getId(notifier)); |
| 156 | + } |
| 157 | + }); |
| 158 | + return expandedIds; |
| 159 | + } |
| 160 | + |
| 161 | + private Consumer<Object> getTreeRefreshedEventPayloadMatcher(List<TreeItemMatcher> treeItemMatchers) { |
| 162 | + return assertRefreshedTreeThat(tree -> { |
| 163 | + assertThat(treeItemMatchers).allMatch(treeItemMatcher -> { |
| 164 | + var treeItem = treeItemMatcher.treeItemFinder.apply(tree); |
| 165 | + return treeItemMatcher.treeItemPredicate.test(treeItem); |
| 166 | + }); |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + private Runnable triggerDirectEditTreeItemLabel(String editingContextId, String treeId, UUID treeItemId, String expectedLabel) { |
| 171 | + return () -> { |
| 172 | + Map<String, Object> variables = Map.of( |
| 173 | + "editingContextId", editingContextId, |
| 174 | + "representationId", treeId, |
| 175 | + "treeItemId", treeItemId); |
| 176 | + var result = this.initialDirectEditTreeItemLabelQueryRunner.run(variables); |
| 177 | + |
| 178 | + String initialDirectEditLabel = JsonPath.read(result, "$.data.viewer.editingContext.representation.description.initialDirectEditTreeItemLabel"); |
| 179 | + assertThat(initialDirectEditLabel).isEqualTo(expectedLabel); |
| 180 | + }; |
| 181 | + } |
| 182 | +} |
0 commit comments