-
Notifications
You must be signed in to change notification settings - Fork 45
/
CreateElementEventHandlerTests.java
210 lines (173 loc) · 9.01 KB
/
CreateElementEventHandlerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
* Copyright (c) 2023 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.widget.reference.handlers;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.List;
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.forms.api.IFormQueryService;
import org.eclipse.sirius.components.collaborative.widget.reference.dto.CreateElementInReferenceSuccessPayload;
import org.eclipse.sirius.components.collaborative.widget.reference.dto.CreateElementInput;
import org.eclipse.sirius.components.collaborative.widget.reference.messages.IReferenceMessageService;
import org.eclipse.sirius.components.core.api.ErrorPayload;
import org.eclipse.sirius.components.core.api.IEditService;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IFeedbackMessageService;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.forms.AbstractWidget;
import org.eclipse.sirius.components.forms.Form;
import org.eclipse.sirius.components.forms.Group;
import org.eclipse.sirius.components.forms.Page;
import org.eclipse.sirius.components.widget.reference.IReferenceWidgetCreateElementHandler;
import org.eclipse.sirius.components.widget.reference.ReferenceValue;
import org.eclipse.sirius.components.widget.reference.ReferenceWidget;
import org.junit.jupiter.api.Test;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import reactor.core.publisher.Sinks;
/**
* Unit tests of the widget reference create element event handler.
*
* @author Jerome Gout
*/
public class CreateElementEventHandlerTests {
private static final UUID FORM_ID = UUID.randomUUID();
private static final String REF_WIDGET_ID = "RefWidget id";
@Test
public void testCreateElementInReference() {
String referenceValueId = "ReferenceValue Id";
String changeKind = ChangeKind.SEMANTIC_CHANGE;
var input = new CreateElementInput(UUID.randomUUID(), FORM_ID.toString(), UUID.randomUUID()
.toString(), REF_WIDGET_ID, "864d9074-86a6-451a-871f-8fbe1cae1ae2", "domainId", "creationDescriptionId", "descriptionId");
AtomicBoolean hasBeenExecuted = new AtomicBoolean();
ReferenceValue referenceValue = ReferenceValue.newReferenceValue(referenceValueId)
.label("")
.kind("")
.build();
IObjectService objectService = new IObjectService.NoOp() {
@Override
public Optional<Object> getObject(IEditingContext editingContext, String objectId) {
return Optional.of(referenceValue);
}
};
ReferenceWidget referenceWidget = ReferenceWidget.newReferenceWidget(REF_WIDGET_ID)
.diagnostics(Collections.emptyList())
.referenceValues(List.of(referenceValue))
.referenceOptionsProvider(List::of)
.descriptionId("")
.label("")
.readOnly(false)
.ownerId("")
.ownerKind("")
.referenceKind("")
.many(false)
.containment(false)
.build();
Group group = Group.newGroup("groupId")
.label("group label")
.widgets(Collections.singletonList(referenceWidget))
.build();
Page page = Page.newPage("pageId")
.label("page label")
.groups(Collections.singletonList(group))
.build();
Form form = Form.newForm(FORM_ID.toString())
.targetObjectId("targetObjectId")
.descriptionId(UUID.randomUUID().toString())
.label("form label")
.pages(Collections.singletonList(page))
.build();
IFormQueryService formQueryService = new IFormQueryService.NoOp() {
@Override
public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
return Optional.of(referenceWidget);
}
};
IReferenceWidgetCreateElementHandler referenceWidgetCreateElementHandler = new IReferenceWidgetCreateElementHandler.NoOp() {
@Override
public Optional<Object> createRootObject(IEditingContext editingContext, UUID documentId, String domainId, String rootObjectCreationDescriptionId, String descriptionId) {
hasBeenExecuted.set(true);
return Optional.of(referenceValue);
}
};
CreateElementEventHandler handler = new CreateElementEventHandler(formQueryService, new IReferenceMessageService.NoOp(), objectService, List.of(referenceWidgetCreateElementHandler),
new IEditService.NoOp(), new SimpleMeterRegistry(), new IFeedbackMessageService.NoOp());
assertThat(handler.canHandle(input)).isTrue();
Sinks.Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
Sinks.One<IPayload> payloadSink = Sinks.one();
handler.handle(payloadSink, changeDescriptionSink, new IEditingContext.NoOp(), form, input);
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
assertThat(changeDescription.getKind()).isEqualTo(changeKind);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(CreateElementInReferenceSuccessPayload.class);
assertThat(hasBeenExecuted.get()).isTrue();
}
@Test
public void testCreateElementInReferenceReadOnly() {
var input = new CreateElementInput(UUID.randomUUID(), FORM_ID.toString(), UUID.randomUUID()
.toString(), REF_WIDGET_ID, "864d9074-86a6-451a-871f-8fbe1cae1ae2", "domainId", "creationDescriptionId", "descriptionId");
AtomicBoolean hasBeenExecuted = new AtomicBoolean();
ReferenceWidget referenceWidget = ReferenceWidget.newReferenceWidget(REF_WIDGET_ID)
.diagnostics(Collections.emptyList())
.referenceValues(List.of())
.referenceOptionsProvider(List::of)
.descriptionId("")
.label("")
.readOnly(true)
.ownerId("")
.ownerKind("")
.referenceKind("")
.many(false)
.containment(false)
.build();
Group group = Group.newGroup("groupId")
.label("group label")
.widgets(Collections.singletonList(referenceWidget))
.build();
Page page = Page.newPage("pageId")
.label("page label")
.groups(Collections.singletonList(group))
.build();
Form form = Form.newForm(FORM_ID.toString())
.targetObjectId("targetObjectId")
.descriptionId(UUID.randomUUID().toString())
.label("form label")
.pages(Collections.singletonList(page))
.build();
IFormQueryService formQueryService = new IFormQueryService.NoOp() {
@Override
public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
return Optional.of(referenceWidget);
}
};
IReferenceMessageService messageService = new IReferenceMessageService.NoOp() {
@Override
public String unableToEditReadOnlyWidget() {
return "Read-only widget can not be edited";
}
};
CreateElementEventHandler handler = new CreateElementEventHandler(formQueryService, messageService, new IObjectService.NoOp(), List.of(), new IEditService.NoOp(), new SimpleMeterRegistry(), new IFeedbackMessageService.NoOp());
assertThat(handler.canHandle(input)).isTrue();
Sinks.Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
Sinks.One<IPayload> payloadSink = Sinks.one();
handler.handle(payloadSink, changeDescriptionSink, new IEditingContext.NoOp(), form, input);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(ErrorPayload.class);
assertThat(((ErrorPayload) payload).message()).isEqualTo("Read-only widget can not be edited");
assertThat(hasBeenExecuted.get()).isFalse();
}
}