use of org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput in project sirius-web by eclipse-sirius.
the class RenameDocumentEventHandlerTests method testRenameDocument.
@Test
public void testRenameDocument() {
IDocumentService noOpDocumentService = new IDocumentService.NoOp() {
@Override
public Optional<Document> rename(UUID documentId, String newName) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return Optional.of(new Document(documentId, new Project(UUID.randomUUID(), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), newName, "noContent"));
}
};
RenameDocumentEventHandler handler = new RenameDocumentEventHandler(noOpDocumentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
UUID documentId = UUID.randomUUID();
IInput input = new RenameDocumentInput(UUID.randomUUID(), documentId, NEW_NAME);
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
DocumentMetadataAdapter adapter = new DocumentMetadataAdapter(OLD_NAME);
Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(documentId.toString()));
resource.eAdapters().add(adapter);
editingDomain.getResourceSet().getResources().add(resource);
assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
assertThat(adapter.getName()).isEqualTo(OLD_NAME);
IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
One<IPayload> payloadSink = Sinks.one();
assertThat(handler.canHandle(editingContext, input)).isTrue();
handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
assertThat(adapter.getName()).isEqualTo(NEW_NAME);
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(RenameDocumentSuccessPayload.class);
}
use of org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput in project sirius-web by eclipse-sirius.
the class RenameDocumentEventHandler method handle.
@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
this.counter.increment();
String message = this.messageService.invalidInput(input.getClass().getSimpleName(), RenameDocumentInput.class.getSimpleName());
IPayload payload = new ErrorPayload(input.getId(), message);
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
// @formatter:off
Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
if (input instanceof RenameDocumentInput) {
RenameDocumentInput renameDocumentInput = (RenameDocumentInput) input;
UUID documentId = renameDocumentInput.getDocumentId();
String newName = renameDocumentInput.getNewName();
Optional<Document> optionalDocument = this.documentService.rename(documentId, newName);
if (optionalEditingDomain.isPresent() && optionalDocument.isPresent()) {
Document document = optionalDocument.get();
AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
// @formatter:off
resourceSet.getResources().stream().filter(resource -> document.getId().toString().equals(resource.getURI().toString())).findFirst().ifPresent(resource -> {
resource.eAdapters().stream().filter(DocumentMetadataAdapter.class::isInstance).map(DocumentMetadataAdapter.class::cast).findFirst().ifPresent(adapter -> adapter.setName(newName));
});
// @formatter:on
payload = new RenameDocumentSuccessPayload(input.getId());
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
}
}
payloadSink.tryEmitValue(payload);
changeDescriptionSink.tryEmitNext(changeDescription);
}
Aggregations