diff --git a/packages/formdescriptioneditors/backend/sirius-components-collaborative-formdescriptioneditors/src/test/java/org/eclipse/sirius/components/collaborative/formdescriptioneditors/handlers/DeleteGroupEventHandlerTests.java b/packages/formdescriptioneditors/backend/sirius-components-collaborative-formdescriptioneditors/src/test/java/org/eclipse/sirius/components/collaborative/formdescriptioneditors/handlers/DeleteGroupEventHandlerTests.java new file mode 100644 index 0000000000..2a6c3f5d61 --- /dev/null +++ b/packages/formdescriptioneditors/backend/sirius-components-collaborative-formdescriptioneditors/src/test/java/org/eclipse/sirius/components/collaborative/formdescriptioneditors/handlers/DeleteGroupEventHandlerTests.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * 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.formdescriptioneditors.handlers; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.eclipse.sirius.components.collaborative.api.ChangeDescription; +import org.eclipse.sirius.components.collaborative.api.ChangeKind; +import org.eclipse.sirius.components.collaborative.formdescriptioneditors.FormDescriptionEditorContext; +import org.eclipse.sirius.components.collaborative.formdescriptioneditors.TestFormDescriptionEditorBuilder; +import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorContext; +import org.eclipse.sirius.components.collaborative.formdescriptioneditors.dto.DeleteGroupInput; +import org.eclipse.sirius.components.collaborative.formdescriptioneditors.messages.ICollaborativeFormDescriptionEditorMessageService; +import org.eclipse.sirius.components.core.api.IEditService; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.core.api.IObjectService; +import org.eclipse.sirius.components.core.api.IPayload; +import org.eclipse.sirius.components.core.api.SuccessPayload; +import org.eclipse.sirius.components.formdescriptioneditors.FormDescriptionEditor; +import org.eclipse.sirius.components.view.form.FormDescription; +import org.eclipse.sirius.components.view.form.FormFactory; +import org.eclipse.sirius.components.view.form.GroupDescription; +import org.eclipse.sirius.components.view.form.PageDescription; +import org.junit.jupiter.api.Test; + +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import reactor.core.publisher.Sinks; + +/** + * Tests of the delete group event handler. + * + * @author frouene + */ +public class DeleteGroupEventHandlerTests { + + private static final String GROUP_ID = "groupId"; + + @Test + public void testDeleteGroup() { + + FormDescription formDescription = FormFactory.eINSTANCE.createFormDescription(); + PageDescription pageDescription = FormFactory.eINSTANCE.createPageDescription(); + formDescription.getPages().add(pageDescription); + GroupDescription groupDescription = FormFactory.eINSTANCE.createGroupDescription(); + pageDescription.getGroups().add(groupDescription); + + FormDescriptionEditor formDescriptionEditor = new TestFormDescriptionEditorBuilder().getFormDescriptionEditor(UUID.randomUUID().toString()); + + var objectService = new IObjectService.NoOp() { + @Override + public Optional getObject(IEditingContext editingContext, String objectId) { + Optional result = Optional.empty(); + if (GROUP_ID.equals(objectId)) { + result = Optional.of(groupDescription); + } + return result; + } + }; + AtomicBoolean groupDeleted = new AtomicBoolean(false); + IEditService.NoOp editService = new IEditService.NoOp() { + @Override + public void delete(Object object) { + if (object == groupDescription) { + groupDeleted.set(true); + } + } + }; + + var handler = new DeleteGroupEventHandler(objectService, editService, new ICollaborativeFormDescriptionEditorMessageService.NoOp(), new SimpleMeterRegistry()); + var input = new DeleteGroupInput(UUID.randomUUID(), "editingContextId", "representationId", GROUP_ID); + + assertThat(handler.canHandle(input)).isTrue(); + + Sinks.One payloadSink = Sinks.one(); + Sinks.Many changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer(); + IFormDescriptionEditorContext formDescriptionEditorContext = new FormDescriptionEditorContext(formDescriptionEditor); + + handler.handle(payloadSink, changeDescriptionSink, new IEditingContext.NoOp(), formDescriptionEditorContext, input); + + ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst(); + assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE); + + IPayload payload = payloadSink.asMono().block(); + assertThat(payload).isInstanceOf(SuccessPayload.class); + assertThat(groupDeleted).isTrue(); + + } +}