Search in sources :

Example 1 with EditSelectInput

use of org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput in project sirius-components by eclipse-sirius.

the class EditSelectEventHandlerTests method testSelectEdition.

@Test
public void testSelectEdition() {
    // $NON-NLS-1$
    String id = "Select id";
    // $NON-NLS-1$
    var input = new EditSelectInput(UUID.randomUUID(), UUID.randomUUID().toString(), FORM_ID, id, "false");
    AtomicBoolean hasBeenExecuted = new AtomicBoolean();
    Function<String, IStatus> newValueHandler = newValue -> {
        hasBeenExecuted.set(true);
        return new Success();
    };
    // @formatter:off
    SelectOption trueOption = // $NON-NLS-1$
    SelectOption.newSelectOption("true").label(// $NON-NLS-1$
    "True").build();
    SelectOption falseOption = // $NON-NLS-1$
    SelectOption.newSelectOption("false").label(// $NON-NLS-1$
    "False").build();
    Select select = Select.newSelect(id).label(// $NON-NLS-1$
    "label").value(// $NON-NLS-1$
    "true").newValueHandler(newValueHandler).options(List.of(trueOption, falseOption)).diagnostics(List.of()).build();
    Group group = // $NON-NLS-1$
    Group.newGroup("groupId").label(// $NON-NLS-1$
    "group label").widgets(List.of(select)).build();
    Page page = // $NON-NLS-1$
    Page.newPage("pageId").label(// $NON-NLS-1$
    "page label").groups(List.of(group)).build();
    Form form = Form.newForm(FORM_ID).targetObjectId(// $NON-NLS-1$
    "targetObjectId").descriptionId(UUID.randomUUID()).label(// $NON-NLS-1$
    "form label").pages(List.of(page)).build();
    // @formatter:on
    IFormQueryService formQueryService = new IFormQueryService.NoOp() {

        @Override
        public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
            return Optional.of(select);
        }
    };
    EditSelectEventHandler handler = new EditSelectEventHandler(formQueryService, new ICollaborativeFormMessageService.NoOp(), new SimpleMeterRegistry());
    assertThat(handler.canHandle(input)).isTrue();
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    handler.handle(payloadSink, changeDescriptionSink, form, input);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(EditSelectSuccessPayload.class);
    assertThat(hasBeenExecuted.get()).isTrue();
}
Also used : Sinks(reactor.core.publisher.Sinks) Form(org.eclipse.sirius.components.forms.Form) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SelectOption(org.eclipse.sirius.components.forms.SelectOption) One(reactor.core.publisher.Sinks.One) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(java.util.function.Function) AbstractWidget(org.eclipse.sirius.components.forms.AbstractWidget) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) IStatus(org.eclipse.sirius.components.representations.IStatus) Page(org.eclipse.sirius.components.forms.Page) EditSelectInput(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) EditSelectSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectSuccessPayload) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Select(org.eclipse.sirius.components.forms.Select) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) IPayload(org.eclipse.sirius.components.core.api.IPayload) Group(org.eclipse.sirius.components.forms.Group) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) Group(org.eclipse.sirius.components.forms.Group) IStatus(org.eclipse.sirius.components.representations.IStatus) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) Form(org.eclipse.sirius.components.forms.Form) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Page(org.eclipse.sirius.components.forms.Page) Success(org.eclipse.sirius.components.representations.Success) EditSelectInput(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SelectOption(org.eclipse.sirius.components.forms.SelectOption) Select(org.eclipse.sirius.components.forms.Select) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) AbstractWidget(org.eclipse.sirius.components.forms.AbstractWidget) Test(org.junit.jupiter.api.Test)

Example 2 with EditSelectInput

use of org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput in project sirius-components by eclipse-sirius.

the class EditSelectEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, Form form, IFormInput formInput) {
    this.counter.increment();
    String message = this.messageService.invalidInput(formInput.getClass().getSimpleName(), EditSelectInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(formInput.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, formInput.getRepresentationId(), formInput);
    if (formInput instanceof EditSelectInput) {
        EditSelectInput input = (EditSelectInput) formInput;
        // @formatter:off
        Optional<Select> optionalSelect = this.formQueryService.findWidget(form, input.getSelectId()).filter(Select.class::isInstance).map(Select.class::cast);
        IStatus status = optionalSelect.map(Select::getNewValueHandler).map(handler -> handler.apply(input.getNewValue())).orElse(// $NON-NLS-1$
        new Failure(""));
        if (status instanceof Success) {
            payload = new EditSelectSuccessPayload(formInput.getId());
            changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, formInput.getRepresentationId(), formInput);
        } else if (status instanceof Failure) {
            payload = new ErrorPayload(formInput.getId(), ((Failure) status).getMessage());
        }
    }
    changeDescriptionSink.tryEmitNext(changeDescription);
    payloadSink.tryEmitValue(payload);
}
Also used : ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) Counter(io.micrometer.core.instrument.Counter) Form(org.eclipse.sirius.components.forms.Form) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) EditSelectSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectSuccessPayload) Failure(org.eclipse.sirius.components.representations.Failure) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Select(org.eclipse.sirius.components.forms.Select) One(reactor.core.publisher.Sinks.One) Objects(java.util.Objects) IFormEventHandler(org.eclipse.sirius.components.collaborative.forms.api.IFormEventHandler) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) IPayload(org.eclipse.sirius.components.core.api.IPayload) IStatus(org.eclipse.sirius.components.representations.IStatus) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) IFormInput(org.eclipse.sirius.components.collaborative.forms.api.IFormInput) EditSelectInput(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IStatus(org.eclipse.sirius.components.representations.IStatus) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Select(org.eclipse.sirius.components.forms.Select) EditSelectSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectSuccessPayload) Failure(org.eclipse.sirius.components.representations.Failure) Success(org.eclipse.sirius.components.representations.Success) EditSelectInput(org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload)

Aggregations

Optional (java.util.Optional)2 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)2 IFormQueryService (org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService)2 EditSelectInput (org.eclipse.sirius.components.collaborative.forms.dto.EditSelectInput)2 EditSelectSuccessPayload (org.eclipse.sirius.components.collaborative.forms.dto.EditSelectSuccessPayload)2 ICollaborativeFormMessageService (org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService)2 IPayload (org.eclipse.sirius.components.core.api.IPayload)2 Form (org.eclipse.sirius.components.forms.Form)2 Select (org.eclipse.sirius.components.forms.Select)2 IStatus (org.eclipse.sirius.components.representations.IStatus)2 Success (org.eclipse.sirius.components.representations.Success)2 Many (reactor.core.publisher.Sinks.Many)2 One (reactor.core.publisher.Sinks.One)2 Counter (io.micrometer.core.instrument.Counter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 List (java.util.List)1 Objects (java.util.Objects)1 UUID (java.util.UUID)1