use of org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput in project sirius-components by eclipse-sirius.
the class DeleteListItemEventHandlerTests method testListItemDeletion.
@Test
public void testListItemDeletion() {
// $NON-NLS-1$
String listId = "List id";
// $NON-NLS-1$
String listItemId = "element id to delete";
// $NON-NLS-1$
String changeKind = "delete something";
// $NON-NLS-1$
String changeDescriptionParameterKey = "change_description_parameter_key";
Map<String, Object> parameters = new HashMap<>();
parameters.put(changeDescriptionParameterKey, listItemId);
var input = new DeleteListItemInput(UUID.randomUUID(), FORM_ID.toString(), UUID.randomUUID().toString(), listId, listItemId);
AtomicBoolean hasBeenExecuted = new AtomicBoolean();
Supplier<IStatus> deleteHandler = () -> {
hasBeenExecuted.set(true);
return new Success(changeKind, parameters);
};
// @formatter:off
ListItem listItem = ListItem.newListItem(listItemId).deletable(true).deleteHandler(deleteHandler).imageURL(// $NON-NLS-1$
"").kind(// $NON-NLS-1$
"Diagram").label(// $NON-NLS-1$
"empty representation").build();
List list = List.newList(listId).diagnostics(Collections.emptyList()).items(Collections.singletonList(listItem)).label(// $NON-NLS-1$
"").build();
Group group = // $NON-NLS-1$
Group.newGroup("groupId").label(// $NON-NLS-1$
"group label").widgets(Collections.singletonList(list)).build();
Page page = // $NON-NLS-1$
Page.newPage("pageId").label(// $NON-NLS-1$
"page label").groups(Collections.singletonList(group)).build();
Form form = Form.newForm(FORM_ID.toString()).targetObjectId(// $NON-NLS-1$
"targetObjectId").descriptionId(UUID.randomUUID()).label(// $NON-NLS-1$
"form label").pages(Collections.singletonList(page)).build();
// @formatter:on
IFormQueryService formQueryService = new IFormQueryService.NoOp() {
@Override
public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
return Optional.of(list);
}
};
DeleteListItemEventHandler handler = new DeleteListItemEventHandler(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);
Map<String, Object> changeDescriptionParameters = changeDescription.getParameters();
assertThat(changeDescriptionParameters.get(changeDescriptionParameterKey)).isEqualTo(listItemId);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(DeleteListItemSuccessPayload.class);
assertThat(hasBeenExecuted.get()).isTrue();
}
use of org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput in project sirius-components by eclipse-sirius.
the class DeleteListItemEventHandler 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(), DeleteListItemInput.class.getSimpleName());
IPayload payload = new ErrorPayload(formInput.getId(), message);
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, formInput.getRepresentationId(), formInput);
if (formInput instanceof DeleteListItemInput) {
DeleteListItemInput input = (DeleteListItemInput) formInput;
// @formatter:off
var optionalListItem = this.formQueryService.findWidget(form, input.getListId()).filter(List.class::isInstance).map(List.class::cast).stream().map(List::getItems).flatMap(Collection::stream).filter(item -> item.getId().toString().equals(input.getListItemId())).findFirst();
var status = optionalListItem.map(ListItem::getDeleteHandler).map(handler -> handler.get()).orElse(// $NON-NLS-1$
new Failure(""));
if (status instanceof Success) {
Success success = (Success) status;
changeDescription = new ChangeDescription(success.getChangeKind(), formInput.getRepresentationId(), formInput, success.getParameters());
payload = new DeleteListItemSuccessPayload(formInput.getId());
} else if (status instanceof Failure) {
payload = new ErrorPayload(formInput.getId(), ((Failure) status).getMessage());
}
}
changeDescriptionSink.tryEmitNext(changeDescription);
payloadSink.tryEmitValue(payload);
}
Aggregations