Skip to content

Commit

Permalink
[test] Fix flaky backend coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
frouene committed Jul 24, 2024
1 parent 23b4970 commit 9c5941d
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Object> getObject(IEditingContext editingContext, String objectId) {
Optional<Object> 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<IPayload> payloadSink = Sinks.one();
Sinks.Many<ChangeDescription> 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();

}
}

0 comments on commit 9c5941d

Please sign in to comment.