Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1354] Fix rename FormDescriptionEditor from the project explorer
Bug: #1354 Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
- Loading branch information
1 parent
d92d999
commit 7228a3a
Showing
5 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...components/collaborative/formdescriptioneditors/dto/RenameFormDescriptionEditorInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.dto; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorInput; | ||
|
||
/** | ||
* The input of the rename diagram mutation. | ||
* | ||
* @author arichard | ||
*/ | ||
public final class RenameFormDescriptionEditorInput implements IFormDescriptionEditorInput { | ||
|
||
private UUID id; | ||
|
||
private String editingContextId; | ||
|
||
private String formDescriptionEditorId; | ||
|
||
private String newLabel; | ||
|
||
public RenameFormDescriptionEditorInput() { | ||
// Used by Jackson | ||
} | ||
|
||
public RenameFormDescriptionEditorInput(UUID id, String editingContextId, String formDescriptionEditorId, String newLabel) { | ||
this.id = Objects.requireNonNull(id); | ||
this.editingContextId = Objects.requireNonNull(editingContextId); | ||
this.formDescriptionEditorId = Objects.requireNonNull(formDescriptionEditorId); | ||
this.newLabel = Objects.requireNonNull(newLabel); | ||
} | ||
|
||
@Override | ||
public UUID getId() { | ||
return this.id; | ||
} | ||
|
||
public String getEditingContextId() { | ||
return this.editingContextId; | ||
} | ||
|
||
@Override | ||
public String getRepresentationId() { | ||
return this.formDescriptionEditorId; | ||
} | ||
|
||
public String getNewLabel() { | ||
return this.newLabel; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String pattern = "{0} '{'id: {1}, editingContextId: {2}, formDescriptionEditorId: {3}, newLabel: {4}'}'"; //$NON-NLS-1$ | ||
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId, this.formDescriptionEditorId, this.newLabel); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...ollaborative/formdescriptioneditors/handlers/RenameFormDescriptionEditorEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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 java.util.Objects; | ||
|
||
import org.eclipse.sirius.components.collaborative.api.ChangeDescription; | ||
import org.eclipse.sirius.components.collaborative.api.ChangeKind; | ||
import org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService; | ||
import org.eclipse.sirius.components.collaborative.api.Monitoring; | ||
import org.eclipse.sirius.components.collaborative.dto.RenameRepresentationSuccessPayload; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorContext; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorEventHandler; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorInput; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.dto.RenameFormDescriptionEditorInput; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.messages.ICollaborativeFormDescriptionEditorMessageService; | ||
import org.eclipse.sirius.components.core.api.ErrorPayload; | ||
import org.eclipse.sirius.components.core.api.IEditingContext; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
import org.eclipse.sirius.components.formdescriptioneditors.FormDescriptionEditor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import reactor.core.publisher.Sinks.Many; | ||
import reactor.core.publisher.Sinks.One; | ||
|
||
/** | ||
* Handler used to rename a form. | ||
* | ||
* @author arichard | ||
*/ | ||
@Service | ||
public class RenameFormDescriptionEditorEventHandler implements IFormDescriptionEditorEventHandler { | ||
|
||
private final IRepresentationPersistenceService representationPersistenceService; | ||
|
||
private final ICollaborativeFormDescriptionEditorMessageService messageService; | ||
|
||
private final Counter counter; | ||
|
||
public RenameFormDescriptionEditorEventHandler(IRepresentationPersistenceService representationPersistenceService, ICollaborativeFormDescriptionEditorMessageService messageService, | ||
MeterRegistry meterRegistry) { | ||
this.representationPersistenceService = Objects.requireNonNull(representationPersistenceService); | ||
this.messageService = Objects.requireNonNull(messageService); | ||
|
||
// @formatter:off | ||
this.counter = Counter.builder(Monitoring.EVENT_HANDLER) | ||
.tag(Monitoring.NAME, this.getClass().getSimpleName()) | ||
.register(meterRegistry); | ||
// @formatter:on | ||
} | ||
|
||
@Override | ||
public boolean canHandle(IFormDescriptionEditorInput formDescriptionEditorInput) { | ||
return formDescriptionEditorInput instanceof RenameFormDescriptionEditorInput; | ||
} | ||
|
||
@Override | ||
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IFormDescriptionEditorContext formDescriptionEditorContext, | ||
IFormDescriptionEditorInput formDescriptionEditorInput) { | ||
this.counter.increment(); | ||
|
||
String message = this.messageService.invalidInput(formDescriptionEditorInput.getClass().getSimpleName(), RenameFormDescriptionEditorInput.class.getSimpleName()); | ||
IPayload payload = new ErrorPayload(formDescriptionEditorInput.getId(), message); | ||
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, formDescriptionEditorInput.getRepresentationId(), formDescriptionEditorInput); | ||
|
||
if (formDescriptionEditorInput instanceof RenameFormDescriptionEditorInput) { | ||
RenameFormDescriptionEditorInput renameRepresentationInput = (RenameFormDescriptionEditorInput) formDescriptionEditorInput; | ||
String newLabel = renameRepresentationInput.getNewLabel(); | ||
|
||
FormDescriptionEditor renamedFormDescriptionEditor = FormDescriptionEditor.newFormDescriptionEditor(formDescriptionEditorContext.getFormDescriptionEditor()).label(newLabel).build(); | ||
this.representationPersistenceService.save(editingContext, renamedFormDescriptionEditor); | ||
|
||
payload = new RenameRepresentationSuccessPayload(formDescriptionEditorInput.getId(), renamedFormDescriptionEditor); | ||
changeDescription = new ChangeDescription(ChangeKind.REPRESENTATION_RENAMING, renameRepresentationInput.getRepresentationId(), formDescriptionEditorInput); | ||
} | ||
|
||
payloadSink.tryEmitValue(payload); | ||
changeDescriptionSink.tryEmitNext(changeDescription); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...orative/formdescriptioneditors/handlers/RenameFormDescriptionEditorEventHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.List; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.collaborative.api.ChangeDescription; | ||
import org.eclipse.sirius.components.collaborative.api.ChangeKind; | ||
import org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService; | ||
import org.eclipse.sirius.components.collaborative.dto.RenameRepresentationSuccessPayload; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.api.IFormDescriptionEditorContext; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.dto.RenameFormDescriptionEditorInput; | ||
import org.eclipse.sirius.components.collaborative.formdescriptioneditors.messages.ICollaborativeFormDescriptionEditorMessageService; | ||
import org.eclipse.sirius.components.core.api.IEditingContext; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
import org.eclipse.sirius.components.formdescriptioneditors.FormDescriptionEditor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import reactor.core.publisher.Sinks; | ||
import reactor.core.publisher.Sinks.Many; | ||
import reactor.core.publisher.Sinks.One; | ||
|
||
/** | ||
* Unit tests of the rename representation event handler. | ||
* | ||
* @author arichard | ||
*/ | ||
public class RenameFormDescriptionEditorEventHandlerTests { | ||
private static final String OLD_LABEL = "oldLabel"; //$NON-NLS-1$ | ||
|
||
private static final String NEW_LABEL = "newLabel"; //$NON-NLS-1$ | ||
|
||
@Test | ||
public void testRenameRepresentation() { | ||
String projectId = UUID.randomUUID().toString(); | ||
String formDescriptionEditorDescriptionId = UUID.randomUUID().toString(); | ||
String representationId = UUID.randomUUID().toString(); | ||
UUID targetObjectId = UUID.randomUUID(); | ||
|
||
// @formatter:off | ||
FormDescriptionEditor formDescriptionEditor = FormDescriptionEditor.newFormDescriptionEditor(representationId) | ||
.label(OLD_LABEL) | ||
.descriptionId(formDescriptionEditorDescriptionId) | ||
.targetObjectId(targetObjectId.toString()) | ||
.widgets(List.of()) | ||
.build(); | ||
// @formatter:on | ||
|
||
RenameFormDescriptionEditorEventHandler handler = new RenameFormDescriptionEditorEventHandler(new IRepresentationPersistenceService.NoOp(), | ||
new ICollaborativeFormDescriptionEditorMessageService.NoOp(), new SimpleMeterRegistry()); | ||
|
||
var input = new RenameFormDescriptionEditorInput(UUID.randomUUID(), projectId, representationId, NEW_LABEL); | ||
assertThat(handler.canHandle(input)).isTrue(); | ||
|
||
Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer(); | ||
One<IPayload> payloadSink = Sinks.one(); | ||
|
||
IFormDescriptionEditorContext formDescriptionEditorContext = new IFormDescriptionEditorContext() { | ||
|
||
@Override | ||
public void update(FormDescriptionEditor updatedFormDescriptionEditor) { | ||
} | ||
|
||
@Override | ||
public FormDescriptionEditor getFormDescriptionEditor() { | ||
return formDescriptionEditor; | ||
} | ||
}; | ||
handler.handle(payloadSink, changeDescriptionSink, new IEditingContext.NoOp(), formDescriptionEditorContext, input); | ||
|
||
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst(); | ||
assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.REPRESENTATION_RENAMING); | ||
|
||
IPayload payload = payloadSink.asMono().block(); | ||
assertThat(payload).isInstanceOf(RenameRepresentationSuccessPayload.class); | ||
assertThat(((RenameRepresentationSuccessPayload) payload).getRepresentation().getLabel()).isEqualTo(NEW_LABEL); | ||
} | ||
} |