From 71a180bcd41284f3b77e256b81ece3f6d8435b4a Mon Sep 17 00:00:00 2001 From: Michael Charfadi Date: Wed, 12 Jun 2024 09:38:47 +0200 Subject: [PATCH] [3614] Add the ability to contribute additional payloads to representation subscriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: https://github.com/eclipse-sirius/sirius-web/issues/3614 Signed-off-by: Michaƫl Charfadi --- CHANGELOG.adoc | 2 +- ...sentationEventProcessorFluxCustomizer.java | 31 ++++ .../tests/assertions/CheckboxAssert.java | 37 ++++ .../tests/assertions/FormAssertions.java | 5 + .../forms/tests/assertions/LinkAssert.java | 96 ++++++++++ .../graphql/EditCheckboxMutationRunner.java | 48 +++++ .../SiriusWebStarterConfiguration.java | 22 ++- .../forms/CheckboxControllerTests.java | 164 ++++++++++++++++++ .../forms/LinkControllerTests.java | 106 +++++++++++ ...tionEventProcessorFluxCustomizerTests.java | 86 +++++++++ .../FormWithCheckboxDescriptionProvider.java | 135 ++++++++++++++ .../FormWithLinkDescriptionProvider.java | 123 +++++++++++++ ...ustomizedRepresentationEventProcessor.java | 45 +++++ .../services/representations/TestPayload.java | 25 +++ ...sentationEventProcessorFluxCustomizer.java | 45 +++++ 15 files changed, 968 insertions(+), 2 deletions(-) create mode 100644 packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IRepresentationEventProcessorFluxCustomizer.java create mode 100644 packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/LinkAssert.java create mode 100644 packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/graphql/EditCheckboxMutationRunner.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/CheckboxControllerTests.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/LinkControllerTests.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/representations/RepresentationEventProcessorFluxCustomizerTests.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithCheckboxDescriptionProvider.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithLinkDescriptionProvider.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/OnCustomizedRepresentationEventProcessor.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestPayload.java create mode 100644 packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestRepresentationEventProcessorFluxCustomizer.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e69f16dfc4..43892fad1d 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -68,7 +68,7 @@ More existing APIs will be migrated to this new common pattern. - https://github.com/eclipse-sirius/sirius-web/issues/3344[#3344] [core] Add support for reloading representations from the database - https://github.com/eclipse-sirius/sirius-web/issues/3553[#3553] [core] Add RepresentationFactory extension point - https://github.com/eclipse-sirius/sirius-web/issues/3587[#3587] [sirius-web] Add an extension point to contribute new project cards - +- https://github.com/eclipse-sirius/sirius-web/issues/3614[#3614] [core] Add the ability to contribute additional payloads to representation subscriptions === Improvements diff --git a/packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IRepresentationEventProcessorFluxCustomizer.java b/packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IRepresentationEventProcessorFluxCustomizer.java new file mode 100644 index 0000000000..73c99962de --- /dev/null +++ b/packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IRepresentationEventProcessorFluxCustomizer.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.collaborative.api; + +import org.eclipse.sirius.components.core.api.IInput; +import org.eclipse.sirius.components.core.api.IPayload; + +import reactor.core.publisher.Flux; + +/** + * Can be used to customize outputEvents of IRepresentationEventProcessor. + * + * @author mcharfadi + */ +public interface IRepresentationEventProcessorFluxCustomizer { + + boolean canHandle(String editingContextId, IRepresentationConfiguration configuration, IInput input, IRepresentationEventProcessor representationEventProcessor); + + Flux customize(String editingContextId, IRepresentationConfiguration configuration, IInput input, IRepresentationEventProcessor representationEventProcessor, Flux outputEvents); + +} diff --git a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/CheckboxAssert.java b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/CheckboxAssert.java index 3faffedb56..76221249c6 100644 --- a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/CheckboxAssert.java +++ b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/CheckboxAssert.java @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.sirius.components.forms.tests.assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.assertj.core.api.AbstractAssert; import org.eclipse.sirius.components.forms.Checkbox; @@ -25,4 +27,39 @@ public class CheckboxAssert extends AbstractAssert { public CheckboxAssert(Checkbox checkbox) { super(checkbox, CheckboxAssert.class); } + + public CheckboxAssert hasLabel(String label) { + assertThat(this.actual.getLabel()).isEqualTo(label); + + return this; + } + + public CheckboxAssert hasValue(boolean value) { + assertThat(this.actual.isValue()).isEqualTo(value); + + return this; + } + + public CheckboxAssert hasHelp(String help) { + assertThat(this.actual.getHelpTextProvider().get()).isEqualTo(help); + + return this; + } + + public CheckboxAssert isReadOnly() { + assertThat(this.actual.isReadOnly()) + .withFailMessage("Expecting the checkbox to be read only but was not read only instead") + .isTrue(); + + return this; + } + + public CheckboxAssert isNotReadOnly() { + assertThat(this.actual.isReadOnly()) + .withFailMessage("Expecting the checkbox not to be read only but was read only instead") + .isFalse(); + + return this; + } + } diff --git a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/FormAssertions.java b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/FormAssertions.java index c9907d125c..9db7f45063 100644 --- a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/FormAssertions.java +++ b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/FormAssertions.java @@ -17,6 +17,7 @@ import org.eclipse.sirius.components.forms.DateTime; import org.eclipse.sirius.components.forms.Form; import org.eclipse.sirius.components.forms.Group; +import org.eclipse.sirius.components.forms.Link; import org.eclipse.sirius.components.forms.MultiSelect; import org.eclipse.sirius.components.forms.Page; import org.eclipse.sirius.components.forms.RichText; @@ -49,6 +50,10 @@ public static CheckboxAssert assertThat(Checkbox checkbox) { return new CheckboxAssert(checkbox); } + public static LinkAssert assertThat(Link link) { + return new LinkAssert(link); + } + public static MultiSelectAssert assertThat(MultiSelect multiSelect) { return new MultiSelectAssert(multiSelect); } diff --git a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/LinkAssert.java b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/LinkAssert.java new file mode 100644 index 0000000000..7a30bc1ae2 --- /dev/null +++ b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/assertions/LinkAssert.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.forms.tests.assertions; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.assertj.core.api.AbstractAssert; +import org.eclipse.sirius.components.forms.Link; + +/** + * Custom assertion class used to perform some tests on a link widget. + * + * @author sbegaudeau + */ +public class LinkAssert extends AbstractAssert { + + public LinkAssert(Link link) { + super(link, LinkAssert.class); + } + + public LinkAssert hasLabel(String label) { + assertThat(this.actual.getLabel()).isEqualTo(label); + + return this; + } + + public LinkAssert hasUrl(String url) { + assertThat(this.actual.getUrl()).isEqualTo(url); + + return this; + } + + public LinkAssert hasHelp(String help) { + assertThat(this.actual.getHelpTextProvider().get()).isEqualTo(help); + + return this; + } + + public LinkAssert isReadOnly() { + assertThat(this.actual.isReadOnly()) + .withFailMessage("Expecting the link to be read only but was not read only instead") + .isTrue(); + + return this; + } + + public LinkAssert isNotReadOnly() { + assertThat(this.actual.isReadOnly()) + .withFailMessage("Expecting the link not to be read only but was read only instead") + .isFalse(); + + return this; + } + + public LinkAssert isBold() { + assertThat(this.actual.getStyle().isBold()) + .withFailMessage("Expecting the link to be bold but was not bold instead") + .isTrue(); + + return this; + } + + public LinkAssert isNotBold() { + assertThat(this.actual.getStyle().isBold()) + .withFailMessage("Expecting the link not to be bold but was bold instead") + .isFalse(); + + return this; + } + + public LinkAssert isItalic() { + assertThat(this.actual.getStyle().isItalic()) + .withFailMessage("Expecting the link to be italic but was not italic instead") + .isTrue(); + + return this; + } + + public LinkAssert isNotItalic() { + assertThat(this.actual.getStyle().isItalic()) + .withFailMessage("Expecting the link not to be italic but was italic instead") + .isFalse(); + + return this; + } +} diff --git a/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/graphql/EditCheckboxMutationRunner.java b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/graphql/EditCheckboxMutationRunner.java new file mode 100644 index 0000000000..99ec9e77d6 --- /dev/null +++ b/packages/forms/backend/sirius-components-forms-tests/src/main/java/org/eclipse/sirius/components/forms/tests/graphql/EditCheckboxMutationRunner.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.forms.tests.graphql; + +import java.util.Objects; + +import org.eclipse.sirius.components.collaborative.forms.dto.EditCheckboxInput; +import org.eclipse.sirius.components.graphql.tests.api.IGraphQLRequestor; +import org.eclipse.sirius.components.graphql.tests.api.IMutationRunner; +import org.springframework.stereotype.Service; + +/** + * Used to edit a checkbox with the GraphQL API. + * + * @author sbegaudeau + */ +@Service +public class EditCheckboxMutationRunner implements IMutationRunner { + + private static final String EDIT_CHECKBOX_MUTATION = """ + mutation editCheckbox($input: EditCheckboxInput!) { + editCheckbox(input: $input) { + __typename + } + } + """; + + private final IGraphQLRequestor graphQLRequestor; + + public EditCheckboxMutationRunner(IGraphQLRequestor graphQLRequestor) { + this.graphQLRequestor = Objects.requireNonNull(graphQLRequestor); + } + + @Override + public String run(EditCheckboxInput input) { + return this.graphQLRequestor.execute(EDIT_CHECKBOX_MUTATION, input); + } +} diff --git a/packages/sirius-web/backend/sirius-web-starter/src/main/java/org/eclipse/sirius/web/starter/SiriusWebStarterConfiguration.java b/packages/sirius-web/backend/sirius-web-starter/src/main/java/org/eclipse/sirius/web/starter/SiriusWebStarterConfiguration.java index d47025ad4d..456ad7af54 100644 --- a/packages/sirius-web/backend/sirius-web-starter/src/main/java/org/eclipse/sirius/web/starter/SiriusWebStarterConfiguration.java +++ b/packages/sirius-web/backend/sirius-web-starter/src/main/java/org/eclipse/sirius/web/starter/SiriusWebStarterConfiguration.java @@ -12,10 +12,14 @@ *******************************************************************************/ package org.eclipse.sirius.web.starter; +import java.util.List; +import java.util.Objects; import java.util.concurrent.Executors; import org.eclipse.sirius.components.collaborative.api.IEditingContextEventProcessorRegistry; import org.eclipse.sirius.components.collaborative.api.IRepresentationConfiguration; +import org.eclipse.sirius.components.collaborative.api.IRepresentationEventProcessor; +import org.eclipse.sirius.components.collaborative.api.IRepresentationEventProcessorFluxCustomizer; import org.eclipse.sirius.components.collaborative.api.ISubscriptionManagerFactory; import org.eclipse.sirius.components.collaborative.editingcontext.api.IEditingContextEventProcessorExecutorServiceProvider; import org.eclipse.sirius.components.collaborative.representations.SubscriptionManager; @@ -68,6 +72,12 @@ }) public class SiriusWebStarterConfiguration { + private final List representationEventProcessorFluxCustomizers; + + public SiriusWebStarterConfiguration(List representationEventProcessorFluxCustomizers) { + this.representationEventProcessorFluxCustomizers = Objects.requireNonNull(representationEventProcessorFluxCustomizers); + } + @Bean @ConditionalOnMissingBean(IEditingContextEventProcessorExecutorServiceProvider.class) public IEditingContextEventProcessorExecutorServiceProvider editingContextEventProcessorExecutorServiceProvider() { @@ -123,12 +133,22 @@ public IEventProcessorSubscriptionProvider eventProcessorSubscriptionProvider(IE public Flux getSubscription(String editingContextId, IRepresentationConfiguration representationConfiguration, IInput input) { return editingContextEventProcessorRegistry.getOrCreateEditingContextEventProcessor(editingContextId) .flatMap(processor -> processor.acquireRepresentationEventProcessor(representationConfiguration, input)) - .map(representationEventProcessor -> representationEventProcessor.getOutputEvents(input)) + .map(representationEventProcessor -> customizeFlux(editingContextId, representationConfiguration, input, representationEventProcessor)) .orElse(Flux.empty()); } }; } + private Flux customizeFlux(String editingContextId, IRepresentationConfiguration representationConfiguration, IInput input, IRepresentationEventProcessor representationEventProcessor) { + Flux flux = representationEventProcessor.getOutputEvents(input); + for (IRepresentationEventProcessorFluxCustomizer representationEventProcessorFluxCustomizer : this.representationEventProcessorFluxCustomizers) { + if (representationEventProcessorFluxCustomizer.canHandle(editingContextId, representationConfiguration, input, representationEventProcessor)) { + flux = representationEventProcessorFluxCustomizer.customize(editingContextId, representationConfiguration, input, representationEventProcessor, flux); + } + } + return flux; + } + @Bean @ConditionalOnMissingBean public IEditingContextDispatcher editingContextDispatcher(IEditingContextEventProcessorRegistry editingContextEventProcessorRegistry, IMessageService messageService) { diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/CheckboxControllerTests.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/CheckboxControllerTests.java new file mode 100644 index 0000000000..54bfe7549e --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/CheckboxControllerTests.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.application.controllers.forms; + +import static org.assertj.core.api.Assertions.fail; +import static org.eclipse.sirius.components.forms.tests.assertions.FormAssertions.assertThat; + +import com.jayway.jsonpath.JsonPath; + +import java.time.Duration; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import org.assertj.core.api.Assertions; +import org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput; +import org.eclipse.sirius.components.collaborative.forms.dto.EditCheckboxInput; +import org.eclipse.sirius.components.collaborative.forms.dto.FormRefreshedEventPayload; +import org.eclipse.sirius.components.core.api.SuccessPayload; +import org.eclipse.sirius.components.forms.Checkbox; +import org.eclipse.sirius.components.forms.tests.graphql.EditCheckboxMutationRunner; +import org.eclipse.sirius.components.forms.tests.navigation.FormNavigator; +import org.eclipse.sirius.web.AbstractIntegrationTests; +import org.eclipse.sirius.web.data.StudioIdentifiers; +import org.eclipse.sirius.web.services.forms.FormWithCheckboxDescriptionProvider; +import org.eclipse.sirius.web.tests.services.api.IGivenCreatedFormSubscription; +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.transaction.annotation.Transactional; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +/** + * Integration tests of the checkbox widget. + * + * @author sbegaudeau + */ +@Transactional +@SuppressWarnings("checkstyle:MultipleStringLiterals") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "sirius.web.test.enabled=studio" }) +public class CheckboxControllerTests extends AbstractIntegrationTests { + + @Autowired + private IGivenInitialServerState givenInitialServerState; + + @Autowired + private IGivenCreatedFormSubscription givenCreatedFormSubscription; + + @Autowired + private FormWithCheckboxDescriptionProvider formWithCheckboxDescriptionProvider; + + @Autowired + private EditCheckboxMutationRunner editCheckboxMutationRunner; + + @BeforeEach + public void beforeEach() { + this.givenInitialServerState.initialize(); + } + + private Flux givenSubscriptionToCheckboxForm() { + var input = new CreateRepresentationInput( + UUID.randomUUID(), + StudioIdentifiers.SAMPLE_STUDIO_PROJECT.toString(), + this.formWithCheckboxDescriptionProvider.getRepresentationDescriptionId(), + StudioIdentifiers.DOMAIN_OBJECT.toString(), + "FormWithCheckbox" + ); + return this.givenCreatedFormSubscription.createAndSubscribe(input); + } + + @Test + @DisplayName("Given a checkbox widget, when it is displayed, then it is properly initialized") + @Sql(scripts = {"/scripts/studio.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + @Sql(scripts = {"/scripts/cleanup.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) + public void givenCheckboxWidgetWhenItIsDisplayedThenItIsProperlyInitialized() { + var flux = this.givenSubscriptionToCheckboxForm(); + + Consumer initialFormContentConsumer = payload -> Optional.of(payload) + .map(FormRefreshedEventPayload::form) + .ifPresentOrElse(form -> { + var groupNavigator = new FormNavigator(form).page("Page").group("Group"); + var checkbox = groupNavigator.findWidget("Name", Checkbox.class); + + assertThat(checkbox) + .hasLabel("Name") + .hasValue(true) + .hasHelp("Does the object have a name?") + .isNotReadOnly(); + }, () -> fail("Missing form")); + + StepVerifier.create(flux) + .consumeNextWith(initialFormContentConsumer) + .thenCancel() + .verify(Duration.ofSeconds(10)); + } + + @Test + @DisplayName("Given a checkbox widget, when it is edited, then its value is updated") + @Sql(scripts = {"/scripts/studio.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + @Sql(scripts = {"/scripts/cleanup.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) + public void givenCheckboxWidgetWhenItIsEditedThenItsValueIsUpdated() { + var flux = this.givenSubscriptionToCheckboxForm(); + + var formId = new AtomicReference(); + var checkboxId = new AtomicReference(); + + Consumer initialFormContentConsumer = payload -> Optional.of(payload) + .map(FormRefreshedEventPayload::form) + .ifPresentOrElse(form -> { + formId.set(form.getId()); + + var groupNavigator = new FormNavigator(form).page("Page").group("Group"); + var checkbox = groupNavigator.findWidget("Name", Checkbox.class); + + checkboxId.set(checkbox.getId()); + }, () -> fail("Missing form")); + + Runnable editCheckbox = () -> { + var input = new EditCheckboxInput(UUID.randomUUID(), StudioIdentifiers.SAMPLE_STUDIO_PROJECT.toString(), formId.get(), checkboxId.get(), false); + var result = this.editCheckboxMutationRunner.run(input); + + String typename = JsonPath.read(result, "$.data.editCheckbox.__typename"); + Assertions.assertThat(typename).isEqualTo(SuccessPayload.class.getSimpleName()); + }; + + Consumer updatedFormContentConsumer = payload -> Optional.of(payload) + .map(FormRefreshedEventPayload::form) + .ifPresentOrElse(form -> { + var groupNavigator = new FormNavigator(form).page("Page").group("Group"); + var checkbox = groupNavigator.findWidget("Name", Checkbox.class); + + assertThat(checkbox) + .hasLabel("Name") + .hasValue(false) + .hasHelp("Does the object have a name?") + .isNotReadOnly(); + }, () -> fail("Missing form")); + + StepVerifier.create(flux) + .consumeNextWith(initialFormContentConsumer) + .then(editCheckbox) + .consumeNextWith(updatedFormContentConsumer) + .thenCancel() + .verify(Duration.ofSeconds(10)); + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/LinkControllerTests.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/LinkControllerTests.java new file mode 100644 index 0000000000..d581cf1815 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/forms/LinkControllerTests.java @@ -0,0 +1,106 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.application.controllers.forms; + +import static org.assertj.core.api.Assertions.fail; +import static org.eclipse.sirius.components.forms.tests.assertions.FormAssertions.assertThat; + +import java.time.Duration; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Consumer; + +import org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput; +import org.eclipse.sirius.components.collaborative.forms.dto.FormRefreshedEventPayload; +import org.eclipse.sirius.components.forms.Link; +import org.eclipse.sirius.components.forms.tests.navigation.FormNavigator; +import org.eclipse.sirius.web.AbstractIntegrationTests; +import org.eclipse.sirius.web.data.StudioIdentifiers; +import org.eclipse.sirius.web.services.forms.FormWithLinkDescriptionProvider; +import org.eclipse.sirius.web.tests.services.api.IGivenCreatedFormSubscription; +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.transaction.annotation.Transactional; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +/** + * Integration tests of the link widget. + * + * @author sbegaudeau + */ +@Transactional +@SuppressWarnings("checkstyle:MultipleStringLiterals") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "sirius.web.test.enabled=studio" }) +public class LinkControllerTests extends AbstractIntegrationTests { + + @Autowired + private IGivenInitialServerState givenInitialServerState; + + @Autowired + private IGivenCreatedFormSubscription givenCreatedFormSubscription; + + @Autowired + private FormWithLinkDescriptionProvider formWithLinkDescriptionProvider; + + @BeforeEach + public void beforeEach() { + this.givenInitialServerState.initialize(); + } + + private Flux givenSubscriptionToLinkForm() { + var input = new CreateRepresentationInput( + UUID.randomUUID(), + StudioIdentifiers.SAMPLE_STUDIO_PROJECT.toString(), + this.formWithLinkDescriptionProvider.getRepresentationDescriptionId(), + StudioIdentifiers.DOMAIN_OBJECT.toString(), + "FormWithLink" + ); + return this.givenCreatedFormSubscription.createAndSubscribe(input); + } + + @Test + @DisplayName("Given a link widget, when it is displayed, then it is properly initialized") + @Sql(scripts = {"/scripts/studio.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + @Sql(scripts = {"/scripts/cleanup.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) + public void givenLinkWidgetWhenItIsDisplayedThenItIsProperlyInitialized() { + var flux = this.givenSubscriptionToLinkForm(); + + Consumer initialFormContentConsumer = payload -> Optional.of(payload) + .map(FormRefreshedEventPayload::form) + .ifPresentOrElse(form -> { + var groupNavigator = new FormNavigator(form).page("Page").group("Group"); + var link = groupNavigator.findWidget("Link", Link.class); + + assertThat(link) + .hasLabel("Link") + .hasUrl("https://www.google.com") + .hasHelp("Links to outside documentation") + .isReadOnly() + .isBold() + .isNotItalic(); + }, () -> fail("Missing form")); + + StepVerifier.create(flux) + .consumeNextWith(initialFormContentConsumer) + .thenCancel() + .verify(Duration.ofSeconds(10)); + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/representations/RepresentationEventProcessorFluxCustomizerTests.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/representations/RepresentationEventProcessorFluxCustomizerTests.java new file mode 100644 index 0000000000..cc3b3ce382 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/application/controllers/representations/RepresentationEventProcessorFluxCustomizerTests.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.application.controllers.representations; + +import java.time.Duration; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Predicate; + +import org.eclipse.sirius.components.collaborative.forms.dto.FormRefreshedEventPayload; +import org.eclipse.sirius.components.collaborative.forms.dto.PropertiesEventInput; +import org.eclipse.sirius.components.forms.tests.graphql.PropertiesEventSubscriptionRunner; +import org.eclipse.sirius.web.AbstractIntegrationTests; +import org.eclipse.sirius.web.data.TestIdentifiers; +import org.eclipse.sirius.web.services.representations.TestPayload; +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; + +import graphql.execution.DataFetcherResult; +import reactor.test.StepVerifier; + +/** + * Used to test the representation event processor flux customizer. + * + * @author sbegaudeau + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "sirius.web.test.enabled=representationeventprocessorfluxcustomizer" }) +public class RepresentationEventProcessorFluxCustomizerTests extends AbstractIntegrationTests { + + @Autowired + private IGivenInitialServerState givenInitialServerState; + + @Autowired + private PropertiesEventSubscriptionRunner propertiesEventSubscriptionRunner; + + @BeforeEach + public void beforeEach() { + this.givenInitialServerState.initialize(); + } + + @Test + @DisplayName("Given a form representation, when we customize its output events, then additional payloads are received") + @Sql(scripts = {"/scripts/initialize.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + @Sql(scripts = {"/scripts/cleanup.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)) + public void givenFormRepresentationWhenWeCustomizeItsOuputEventsThenAdditionalPayloadsAreReceived() { + var input = new PropertiesEventInput(UUID.randomUUID(), TestIdentifiers.ECORE_SAMPLE_PROJECT.toString(), List.of(TestIdentifiers.EPACKAGE_OBJECT.toString())); + var flux = this.propertiesEventSubscriptionRunner.run(input); + + Predicate formContentMatcher = object -> Optional.of(object) + .filter(DataFetcherResult.class::isInstance) + .map(DataFetcherResult.class::cast) + .map(DataFetcherResult::getData) + .filter(FormRefreshedEventPayload.class::isInstance) + .isPresent(); + + Predicate testPayloadMatcher = object -> Optional.of(object) + .filter(DataFetcherResult.class::isInstance) + .map(DataFetcherResult.class::cast) + .map(DataFetcherResult::getData) + .filter(TestPayload.class::isInstance) + .isPresent(); + + StepVerifier.create(flux) + .expectNextMatches(formContentMatcher) + .expectNextMatches(testPayloadMatcher) + .thenCancel() + .verify(Duration.ofSeconds(10)); + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithCheckboxDescriptionProvider.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithCheckboxDescriptionProvider.java new file mode 100644 index 0000000000..c5dd6ea8ee --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithCheckboxDescriptionProvider.java @@ -0,0 +1,135 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.services.forms; + +import java.util.Objects; +import java.util.UUID; + +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.core.api.IEditingContextProcessor; +import org.eclipse.sirius.components.emf.ResourceMetadataAdapter; +import org.eclipse.sirius.components.emf.services.IDAdapter; +import org.eclipse.sirius.components.emf.services.JSONResourceFactory; +import org.eclipse.sirius.components.view.View; +import org.eclipse.sirius.components.view.builder.generated.ChangeContextBuilder; +import org.eclipse.sirius.components.view.builder.generated.FormBuilders; +import org.eclipse.sirius.components.view.builder.generated.FormDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.GroupDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.PageDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.SetValueBuilder; +import org.eclipse.sirius.components.view.builder.generated.ViewBuilder; +import org.eclipse.sirius.components.view.emf.form.IFormIdProvider; +import org.eclipse.sirius.components.view.form.FormDescription; +import org.eclipse.sirius.emfjson.resource.JsonResource; +import org.eclipse.sirius.web.application.editingcontext.EditingContext; +import org.eclipse.sirius.web.services.OnStudioTests; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +/** + * Used to provide a view based form description to tests checkboxes. + * + * @author sbegaudeau + */ +@Service +@Conditional(OnStudioTests.class) +@SuppressWarnings("checkstyle:MultipleStringLiterals") +public class FormWithCheckboxDescriptionProvider implements IEditingContextProcessor { + private final IFormIdProvider formIdProvider; + + private final View view; + + private FormDescription formDescription; + + public FormWithCheckboxDescriptionProvider(IFormIdProvider formIdProvider) { + this.formIdProvider = Objects.requireNonNull(formIdProvider); + this.view = this.createView(); + } + + @Override + public void preProcess(IEditingContext editingContext) { + if (editingContext instanceof EditingContext siriusWebEditingContext) { + siriusWebEditingContext.getViews().add(this.view); + } + } + + public String getRepresentationDescriptionId() { + return this.formIdProvider.getId(this.formDescription); + } + + private View createView() { + ViewBuilder viewBuilder = new ViewBuilder(); + View textfieldFormView = viewBuilder.build(); + textfieldFormView.getDescriptions().add(this.createFormDescription()); + + textfieldFormView.eAllContents().forEachRemaining(eObject -> { + eObject.eAdapters().add(new IDAdapter(UUID.nameUUIDFromBytes(EcoreUtil.getURI(eObject).toString().getBytes()))); + }); + + String resourcePath = UUID.nameUUIDFromBytes("FormWithCheckboxDescription".getBytes()).toString(); + JsonResource resource = new JSONResourceFactory().createResourceFromPath(resourcePath); + resource.eAdapters().add(new ResourceMetadataAdapter("FormWithCheckboxDescription")); + resource.getContents().add(textfieldFormView); + + return textfieldFormView; + } + + private FormDescription createFormDescription() { + var defaultStyle = new FormBuilders().newCheckboxDescriptionStyle() + .build(); + + var editCheckbox = new ChangeContextBuilder() + .expression("aql:self") + .children( + new SetValueBuilder() + .featureName("name") + .valueExpression("aql:''") + .build() + ) + .build(); + + var checkboxDescription = new FormBuilders().newCheckboxDescription() + .name("Checkbox") + .labelExpression("aql:'Name'") + .helpExpression("Does the object have a name?") + .valueExpression("aql:self.name.size() > 0") + .style(defaultStyle) + .body(editCheckbox) + .build(); + + var groupDescription = new GroupDescriptionBuilder() + .name("Group") + .labelExpression("Group") + .semanticCandidatesExpression("aql:self") + .children(checkboxDescription) + .build(); + + var pageDescription = new PageDescriptionBuilder() + .name("Page") + .labelExpression("Page") + .domainType("domain:Domain") + .semanticCandidatesExpression("aql:self") + .groups(groupDescription) + .build(); + + this.formDescription = new FormDescriptionBuilder() + .name("Form") + .titleExpression("aql:'FormWithCheckbox'") + .domainType("domain:Domain") + .pages(pageDescription) + .build(); + + return this.formDescription; + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithLinkDescriptionProvider.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithLinkDescriptionProvider.java new file mode 100644 index 0000000000..52f44f7538 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/forms/FormWithLinkDescriptionProvider.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.services.forms; + +import java.util.Objects; +import java.util.UUID; + +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.core.api.IEditingContextProcessor; +import org.eclipse.sirius.components.emf.ResourceMetadataAdapter; +import org.eclipse.sirius.components.emf.services.IDAdapter; +import org.eclipse.sirius.components.emf.services.JSONResourceFactory; +import org.eclipse.sirius.components.view.View; +import org.eclipse.sirius.components.view.builder.generated.FormBuilders; +import org.eclipse.sirius.components.view.builder.generated.FormDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.GroupDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.PageDescriptionBuilder; +import org.eclipse.sirius.components.view.builder.generated.ViewBuilder; +import org.eclipse.sirius.components.view.emf.form.IFormIdProvider; +import org.eclipse.sirius.components.view.form.FormDescription; +import org.eclipse.sirius.emfjson.resource.JsonResource; +import org.eclipse.sirius.web.application.editingcontext.EditingContext; +import org.eclipse.sirius.web.services.OnStudioTests; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +/** + * Used to provide a view based form description to tests links. + * + * @author sbegaudeau + */ +@Service +@Conditional(OnStudioTests.class) +@SuppressWarnings("checkstyle:MultipleStringLiterals") +public class FormWithLinkDescriptionProvider implements IEditingContextProcessor { + private final IFormIdProvider formIdProvider; + + private final View view; + + private FormDescription formDescription; + + public FormWithLinkDescriptionProvider(IFormIdProvider formIdProvider) { + this.formIdProvider = Objects.requireNonNull(formIdProvider); + this.view = this.createView(); + } + + @Override + public void preProcess(IEditingContext editingContext) { + if (editingContext instanceof EditingContext siriusWebEditingContext) { + siriusWebEditingContext.getViews().add(this.view); + } + } + + public String getRepresentationDescriptionId() { + return this.formIdProvider.getId(this.formDescription); + } + + private View createView() { + ViewBuilder viewBuilder = new ViewBuilder(); + View linkFormView = viewBuilder.build(); + linkFormView.getDescriptions().add(this.createFormDescription()); + + linkFormView.eAllContents().forEachRemaining(eObject -> { + eObject.eAdapters().add(new IDAdapter(UUID.nameUUIDFromBytes(EcoreUtil.getURI(eObject).toString().getBytes()))); + }); + + String resourcePath = UUID.nameUUIDFromBytes("FormWithLinkDescription".getBytes()).toString(); + JsonResource resource = new JSONResourceFactory().createResourceFromPath(resourcePath); + resource.eAdapters().add(new ResourceMetadataAdapter("FormWithLinkDescription")); + resource.getContents().add(linkFormView); + + return linkFormView; + } + + private FormDescription createFormDescription() { + var defaultTextStyle = new FormBuilders().newLinkDescriptionStyle() + .bold(true) + .build(); + + var linkDescription = new FormBuilders().newLinkDescription() + .name("Link") + .labelExpression("aql:'Link'") + .helpExpression("Links to outside documentation") + .valueExpression("aql:'https://www.google.com'") + .style(defaultTextStyle) + .build(); + + var groupDescription = new GroupDescriptionBuilder() + .name("Group") + .labelExpression("Group") + .semanticCandidatesExpression("aql:self") + .children(linkDescription) + .build(); + + var pageDescription = new PageDescriptionBuilder() + .name("Page") + .labelExpression("Page") + .domainType("domain:Domain") + .semanticCandidatesExpression("aql:self") + .groups(groupDescription) + .build(); + + this.formDescription = new FormDescriptionBuilder() + .name("Form") + .titleExpression("aql:'FormWithLink'") + .domainType("domain:Domain") + .pages(pageDescription) + .build(); + + return this.formDescription; + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/OnCustomizedRepresentationEventProcessor.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/OnCustomizedRepresentationEventProcessor.java new file mode 100644 index 0000000000..95e8275242 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/OnCustomizedRepresentationEventProcessor.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.services.representations; + +import java.util.Arrays; + +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +/** + * Used to trigger the registration of the test representation event processor flux customizer. + * + * @author sbegaudeau + */ +public class OnCustomizedRepresentationEventProcessor extends SpringBootCondition { + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + var message = ConditionMessage.forCondition(this.getClass().getSimpleName()).notAvailable("sirius.web.test.enabled"); + ConditionOutcome outcome = ConditionOutcome.noMatch(message); + + var siriusWebTestEnabled = context.getEnvironment().getProperty("sirius.web.test.enabled", ""); + var siriusWebTestEnabledFeatures = Arrays.stream(siriusWebTestEnabled.split(",")).map(String::trim).toList(); + + if (siriusWebTestEnabledFeatures.contains("representationeventprocessorfluxcustomizer")) { + message = ConditionMessage.forCondition(this.getClass().getSimpleName()).available("sirius.web.test.enabled=representationeventprocessorfluxcustomizer"); + outcome = ConditionOutcome.match(message); + } + + return outcome; + } +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestPayload.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestPayload.java new file mode 100644 index 0000000000..4efd3c5867 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestPayload.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.services.representations; + +import java.util.UUID; + +import org.eclipse.sirius.components.core.api.IPayload; + +/** + * Test payload. + * + * @author sbegaudeau + */ +public record TestPayload(UUID id) implements IPayload { +} diff --git a/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestRepresentationEventProcessorFluxCustomizer.java b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestRepresentationEventProcessorFluxCustomizer.java new file mode 100644 index 0000000000..23141985c0 --- /dev/null +++ b/packages/sirius-web/backend/sirius-web/src/test/java/org/eclipse/sirius/web/services/representations/TestRepresentationEventProcessorFluxCustomizer.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2024, 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.services.representations; + +import org.eclipse.sirius.components.collaborative.api.IRepresentationConfiguration; +import org.eclipse.sirius.components.collaborative.api.IRepresentationEventProcessor; +import org.eclipse.sirius.components.collaborative.api.IRepresentationEventProcessorFluxCustomizer; +import org.eclipse.sirius.components.core.api.IInput; +import org.eclipse.sirius.components.core.api.IPayload; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import reactor.core.publisher.Flux; + +/** + * Used to add new payloads to a representation event processor during tests. + * + * @author sbegaudeau + */ +@Service +@Conditional(OnCustomizedRepresentationEventProcessor.class) +public class TestRepresentationEventProcessorFluxCustomizer implements IRepresentationEventProcessorFluxCustomizer { + @Override + public boolean canHandle(String editingContextId, IRepresentationConfiguration configuration, IInput input, IRepresentationEventProcessor representationEventProcessor) { + return true; + } + + @Override + public Flux customize(String editingContextId, IRepresentationConfiguration configuration, IInput input, IRepresentationEventProcessor representationEventProcessor, Flux outputEvents) { + return Flux.merge( + outputEvents, + Flux.just(new TestPayload(input.id())) + ); + } +}