|
| 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.tree.explorer.view.handlers; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.junit.jupiter.api.Assertions.fail; |
| 17 | + |
| 18 | +import com.jayway.jsonpath.JsonPath; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.UUID; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +import org.eclipse.sirius.components.collaborative.trees.dto.DropTreeItemInput; |
| 27 | +import org.eclipse.sirius.components.collaborative.trees.dto.TreeRefreshedEventPayload; |
| 28 | +import org.eclipse.sirius.components.core.api.ErrorPayload; |
| 29 | +import org.eclipse.sirius.components.core.api.SuccessPayload; |
| 30 | +import org.eclipse.sirius.components.trees.tests.graphql.DropTreeItemMutationRunner; |
| 31 | +import org.eclipse.sirius.web.application.views.explorer.ExplorerEventInput; |
| 32 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 33 | +import org.eclipse.sirius.web.tests.services.explorer.ExplorerEventSubscriptionRunner; |
| 34 | +import org.eclipse.sirius.web.tests.services.representation.RepresentationIdBuilder; |
| 35 | +import org.eclipse.syson.AbstractIntegrationTests; |
| 36 | +import org.eclipse.syson.application.data.SysMLv2Identifiers; |
| 37 | +import org.eclipse.syson.tree.explorer.view.SysONTreeViewDescriptionProvider; |
| 38 | +import org.eclipse.syson.tree.explorer.view.filters.SysONTreeFilterProvider; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.DisplayName; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.springframework.beans.factory.annotation.Autowired; |
| 43 | +import org.springframework.boot.test.context.SpringBootTest; |
| 44 | +import org.springframework.test.context.jdbc.Sql; |
| 45 | +import org.springframework.test.context.jdbc.SqlConfig; |
| 46 | +import org.springframework.test.context.transaction.TestTransaction; |
| 47 | +import org.springframework.transaction.annotation.Transactional; |
| 48 | + |
| 49 | +import graphql.execution.DataFetcherResult; |
| 50 | +import reactor.test.StepVerifier; |
| 51 | + |
| 52 | +/** |
| 53 | + * Integration tests for Explorer DnD. |
| 54 | + * |
| 55 | + * @author Arthur Daussy |
| 56 | + */ |
| 57 | +@Transactional |
| 58 | +@SuppressWarnings("checkstyle:MultipleStringLiterals") |
| 59 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 60 | +public class DropTreeItemHandlerTest extends AbstractIntegrationTests { |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private IGivenInitialServerState givenInitialServerState; |
| 64 | + |
| 65 | + @Autowired |
| 66 | + private ExplorerEventSubscriptionRunner treeEventSubscriptionRunner; |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private DropTreeItemMutationRunner dropTreeItemMutationRunner; |
| 70 | + |
| 71 | + @Autowired |
| 72 | + private RepresentationIdBuilder representationIdBuilder; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + private SysONTreeViewDescriptionProvider treeProvider; |
| 76 | + |
| 77 | + @BeforeEach |
| 78 | + public void beforeEach() { |
| 79 | + this.givenInitialServerState.initialize(); |
| 80 | + } |
| 81 | + |
| 82 | + @DisplayName("Given the simple project, when drag an dropping a part definition on a package, then the part definition should be moved under the Package.") |
| 83 | + @Sql(scripts = { "/scripts/syson-test-database.sql" }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 84 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 85 | + @Test |
| 86 | + public void checkDnDPartDefinitionOnPackage() { |
| 87 | + |
| 88 | + var expandedIds = List.of( |
| 89 | + SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE1.toString(), |
| 90 | + SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE2.toString(), |
| 91 | + SysMLv2Identifiers.SIMPLE_PROJECT.toString(), |
| 92 | + SysMLv2Identifiers.SIMPLE_PROJECT_DOCUMENT.toString()); |
| 93 | + |
| 94 | + var explorerRepresentationId = this.representationIdBuilder.buildExplorerRepresentationId(this.treeProvider.getDescriptionId(), expandedIds, |
| 95 | + List.of(SysONTreeFilterProvider.HIDE_MEMBERSHIPS_TREE_ITEM_FILTER_ID, SysONTreeFilterProvider.HIDE_ROOT_NAMESPACES_ID)); |
| 96 | + var input = new ExplorerEventInput(UUID.randomUUID(), SysMLv2Identifiers.SIMPLE_PROJECT.toString(), explorerRepresentationId); |
| 97 | + var flux = this.treeEventSubscriptionRunner.run(input); |
| 98 | + |
| 99 | + Consumer<Object> initialTreeContentConsumer = object -> Optional.of(object) |
| 100 | + .filter(DataFetcherResult.class::isInstance) |
| 101 | + .map(DataFetcherResult.class::cast) |
| 102 | + .map(DataFetcherResult::getData) |
| 103 | + .filter(TreeRefreshedEventPayload.class::isInstance) |
| 104 | + .map(TreeRefreshedEventPayload.class::cast) |
| 105 | + .map(TreeRefreshedEventPayload::tree) |
| 106 | + .ifPresentOrElse(tree -> { |
| 107 | + assertThat(tree).isNotNull(); |
| 108 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).hasSize(2); |
| 109 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).anyMatch(treeItem -> treeItem.getId() |
| 110 | + .equals(SysMLv2Identifiers.SIMPLE_PROJECT_PART.toString())); |
| 111 | + assertThat(tree.getChildren().get(0).getChildren().get(1).getChildren()).hasSize(1); |
| 112 | + assertThat(tree.getChildren().get(0).getChildren().get(1).getChildren()).anyMatch(treeItem -> treeItem.getId() |
| 113 | + .equals(SysMLv2Identifiers.SIMPLE_PROJECT_PART_DEF.toString())); |
| 114 | + }, () -> fail("Missing tree")); |
| 115 | + |
| 116 | + Runnable dropItemMutation = () -> { |
| 117 | + DropTreeItemInput dropTreeItemInput = new DropTreeItemInput( |
| 118 | + UUID.randomUUID(), SysMLv2Identifiers.SIMPLE_PROJECT.toString(), |
| 119 | + explorerRepresentationId, |
| 120 | + List.of(SysMLv2Identifiers.SIMPLE_PROJECT_PART_DEF.toString()), |
| 121 | + SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE1.toString(), |
| 122 | + -1); |
| 123 | + var result = this.dropTreeItemMutationRunner.run(dropTreeItemInput); |
| 124 | + String typename = JsonPath.read(result, "$.data.dropTreeItem.__typename"); |
| 125 | + assertThat(typename).isEqualTo(SuccessPayload.class.getSimpleName()); |
| 126 | + |
| 127 | + TestTransaction.flagForCommit(); |
| 128 | + TestTransaction.end(); |
| 129 | + TestTransaction.start(); |
| 130 | + }; |
| 131 | + |
| 132 | + Consumer<Object> updateTreeContentConsumer = object -> Optional.of(object) |
| 133 | + .filter(DataFetcherResult.class::isInstance) |
| 134 | + .map(DataFetcherResult.class::cast) |
| 135 | + .map(DataFetcherResult::getData) |
| 136 | + .filter(TreeRefreshedEventPayload.class::isInstance) |
| 137 | + .map(TreeRefreshedEventPayload.class::cast) |
| 138 | + .map(TreeRefreshedEventPayload::tree) |
| 139 | + .ifPresentOrElse(tree -> { |
| 140 | + assertThat(tree).isNotNull(); |
| 141 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).hasSize(3); |
| 142 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).anyMatch(treeItem -> treeItem.getId() |
| 143 | + .equals(SysMLv2Identifiers.SIMPLE_PROJECT_PART_DEF.toString())); |
| 144 | + }, () -> fail("Missing tree")); |
| 145 | + |
| 146 | + StepVerifier.create(flux) |
| 147 | + .consumeNextWith(initialTreeContentConsumer) |
| 148 | + .then(dropItemMutation) |
| 149 | + .consumeNextWith(updateTreeContentConsumer) |
| 150 | + .thenCancel() |
| 151 | + .verify(Duration.ofSeconds(10)); |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + @DisplayName("Given a SySML project, when drag an dropping an element into one of its descendant, then the selected element should not be moved") |
| 156 | + @Sql(scripts = { "/scripts/syson-test-database.sql" }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 157 | + @Sql(scripts = { "/scripts/cleanup.sql" }, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) |
| 158 | + @Test |
| 159 | + public void checkForbiddenDropOnDescendant() { |
| 160 | + |
| 161 | + var expandedIds = List.of( |
| 162 | + SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE1.toString(), |
| 163 | + SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE2.toString(), |
| 164 | + SysMLv2Identifiers.SIMPLE_PROJECT.toString(), |
| 165 | + SysMLv2Identifiers.SIMPLE_PROJECT_DOCUMENT.toString()); |
| 166 | + |
| 167 | + var explorerRepresentationId = this.representationIdBuilder.buildExplorerRepresentationId(this.treeProvider.getDescriptionId(), expandedIds, |
| 168 | + List.of(SysONTreeFilterProvider.HIDE_MEMBERSHIPS_TREE_ITEM_FILTER_ID, SysONTreeFilterProvider.HIDE_ROOT_NAMESPACES_ID)); |
| 169 | + var input = new ExplorerEventInput(UUID.randomUUID(), SysMLv2Identifiers.SIMPLE_PROJECT.toString(), explorerRepresentationId); |
| 170 | + var flux = this.treeEventSubscriptionRunner.run(input); |
| 171 | + |
| 172 | + Consumer<Object> initialTreeContentConsumer = object -> Optional.of(object) |
| 173 | + .filter(DataFetcherResult.class::isInstance) |
| 174 | + .map(DataFetcherResult.class::cast) |
| 175 | + .map(DataFetcherResult::getData) |
| 176 | + .filter(TreeRefreshedEventPayload.class::isInstance) |
| 177 | + .map(TreeRefreshedEventPayload.class::cast) |
| 178 | + .map(TreeRefreshedEventPayload::tree) |
| 179 | + .ifPresentOrElse(tree -> { |
| 180 | + assertThat(tree).isNotNull(); |
| 181 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).hasSize(2); |
| 182 | + assertThat(tree.getChildren().get(0).getChildren().get(0).getChildren()).anyMatch(treeItem -> treeItem.getId() |
| 183 | + .equals(SysMLv2Identifiers.SIMPLE_PROJECT_PART.toString())); |
| 184 | + assertThat(tree.getChildren().get(0).getChildren().get(1).getChildren()).hasSize(1); |
| 185 | + assertThat(tree.getChildren().get(0).getChildren().get(1).getChildren()).anyMatch(treeItem -> treeItem.getId() |
| 186 | + .equals(SysMLv2Identifiers.SIMPLE_PROJECT_PART_DEF.toString())); |
| 187 | + }, () -> fail("Missing tree")); |
| 188 | + |
| 189 | + Runnable dropItemMutation = () -> { |
| 190 | + DropTreeItemInput dropTreeItemInput = new DropTreeItemInput( |
| 191 | + UUID.randomUUID(), SysMLv2Identifiers.SIMPLE_PROJECT.toString(), |
| 192 | + explorerRepresentationId, |
| 193 | + List.of(SysMLv2Identifiers.SIMPLE_PROJECT_PACKAGE1.toString()), |
| 194 | + SysMLv2Identifiers.SIMPLE_PROJECT_PART.toString(), |
| 195 | + -1); |
| 196 | + var result = this.dropTreeItemMutationRunner.run(dropTreeItemInput); |
| 197 | + String typename = JsonPath.read(result, "$.data.dropTreeItem.__typename"); |
| 198 | + assertThat(typename).isEqualTo(ErrorPayload.class.getSimpleName()); |
| 199 | + |
| 200 | + TestTransaction.flagForCommit(); |
| 201 | + TestTransaction.end(); |
| 202 | + TestTransaction.start(); |
| 203 | + }; |
| 204 | + |
| 205 | + |
| 206 | + StepVerifier.create(flux) |
| 207 | + .consumeNextWith(initialTreeContentConsumer) |
| 208 | + .then(dropItemMutation) |
| 209 | + .thenCancel() |
| 210 | + .verify(Duration.ofSeconds(10)); |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments