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
|
package tools.refinery.language.web.xtext.servlet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.eclipse.xtext.web.server.model.DocumentStateResult;
import org.eclipse.xtext.web.server.syntaxcoloring.HighlightingResult;
import org.eclipse.xtext.web.server.validation.ValidationResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.jupiter.MockitoExtension;
import com.google.inject.Inject;
import tools.refinery.language.web.tests.AwaitTerminationExecutorServiceProvider;
import tools.refinery.language.web.tests.ProblemWebInjectorProvider;
import tools.refinery.language.web.xtext.server.ResponseHandler;
import tools.refinery.language.web.xtext.server.ResponseHandlerException;
import tools.refinery.language.web.xtext.server.TransactionExecutor;
import tools.refinery.language.web.xtext.server.message.XtextWebOkResponse;
import tools.refinery.language.web.xtext.server.message.XtextWebRequest;
import tools.refinery.language.web.xtext.server.message.XtextWebResponse;
@ExtendWith(MockitoExtension.class)
@ExtendWith(InjectionExtension.class)
@InjectWith(ProblemWebInjectorProvider.class)
class TransactionExecutorTest {
private static final String RESOURCE_NAME = "test.problem";
private static final String PROBLEM_CONTENT_TYPE = "application/x-tools.refinery.problem";
private static final String TEST_PROBLEM = """
class Person {
Person[0..*] friend opposite friend
}
friend(a, b).
""";
private static final Map<String, String> UPDATE_FULL_TEXT_PARAMS = Map.of("resource", RESOURCE_NAME, "serviceType",
"update", "fullText", TEST_PROBLEM);
@Inject
private IResourceServiceProvider.Registry resourceServiceProviderRegistry;
@Inject
private AwaitTerminationExecutorServiceProvider executorServices;
private TransactionExecutor transactionExecutor;
@BeforeEach
void beforeEach() {
transactionExecutor = new TransactionExecutor(new SimpleSession(), resourceServiceProviderRegistry);
}
@Test
void updateFullTextTest() throws ResponseHandlerException {
var captor = newCaptor();
var stateId = updateFullText(captor);
assertThatPrecomputedMessagesAreReceived(stateId, captor.getAllValues());
}
@Test
void updateDeltaTextHighlightAndValidationChange() throws ResponseHandlerException {
var stateId = updateFullText();
var responseHandler = sendRequestAndWaitForAllResponses(
new XtextWebRequest("bar", Map.of("resource", RESOURCE_NAME, "serviceType", "update", "requiredStateId",
stateId, "deltaText", "individual q.\nnode(q).\n<invalid text>\n", "deltaOffset", "0", "deltaReplaceLength", "0")));
var captor = newCaptor();
verify(responseHandler, times(3)).onResponse(captor.capture());
var newStateId = getStateId("bar", captor.getAllValues().get(0));
assertThatPrecomputedMessagesAreReceived(newStateId, captor.getAllValues());
}
@Test
void updateDeltaTextHighlightChangeOnly() throws ResponseHandlerException {
var stateId = updateFullText();
var responseHandler = sendRequestAndWaitForAllResponses(
new XtextWebRequest("bar", Map.of("resource", RESOURCE_NAME, "serviceType", "update", "requiredStateId",
stateId, "deltaText", "individual q.\nnode(q).\n", "deltaOffset", "0", "deltaReplaceLength", "0")));
var captor = newCaptor();
verify(responseHandler, times(2)).onResponse(captor.capture());
var newStateId = getStateId("bar", captor.getAllValues().get(0));
assertHighlightingResponse(newStateId, captor.getAllValues().get(1));
}
@Test
void fullTextWithoutResourceTest() throws ResponseHandlerException {
var resourceServiceProvider = resourceServiceProviderRegistry
.getResourceServiceProvider(URI.createFileURI(RESOURCE_NAME));
resourceServiceProviderRegistry.getContentTypeToFactoryMap().put(PROBLEM_CONTENT_TYPE, resourceServiceProvider);
var responseHandler = sendRequestAndWaitForAllResponses(new XtextWebRequest("foo",
Map.of("contentType", PROBLEM_CONTENT_TYPE, "fullText", TEST_PROBLEM, "serviceType", "validate")));
var captor = newCaptor();
verify(responseHandler).onResponse(captor.capture());
var response = captor.getValue();
assertThat(response, hasProperty("id", equalTo("foo")));
assertThat(response, hasProperty("responseData", instanceOf(ValidationResult.class)));
}
private ArgumentCaptor<XtextWebResponse> newCaptor() {
return ArgumentCaptor.forClass(XtextWebResponse.class);
}
private String updateFullText() throws ResponseHandlerException {
return updateFullText(newCaptor());
}
private String updateFullText(ArgumentCaptor<XtextWebResponse> captor) throws ResponseHandlerException {
var responseHandler = sendRequestAndWaitForAllResponses(new XtextWebRequest("foo", UPDATE_FULL_TEXT_PARAMS));
verify(responseHandler, times(3)).onResponse(captor.capture());
return getStateId("foo", captor.getAllValues().get(0));
}
private ResponseHandler sendRequestAndWaitForAllResponses(XtextWebRequest request) throws ResponseHandlerException {
var responseHandler = mock(ResponseHandler.class);
transactionExecutor.setResponseHandler(responseHandler);
transactionExecutor.handleRequest(request);
executorServices.waitForAllTasksToFinish();
return responseHandler;
}
private String getStateId(String requestId, XtextWebResponse okResponse) {
assertThat(okResponse, hasProperty("id", equalTo(requestId)));
assertThat(okResponse, hasProperty("responseData", instanceOf(DocumentStateResult.class)));
return ((DocumentStateResult) ((XtextWebOkResponse) okResponse).getResponseData()).getStateId();
}
private void assertThatPrecomputedMessagesAreReceived(String stateId, List<XtextWebResponse> responses) {
assertHighlightingResponse(stateId, responses.get(1));
assertValidationResponse(stateId, responses.get(2));
}
private void assertHighlightingResponse(String stateId, XtextWebResponse highlightingResponse) {
assertThat(highlightingResponse, hasProperty("resourceId", equalTo(RESOURCE_NAME)));
assertThat(highlightingResponse, hasProperty("stateId", equalTo(stateId)));
assertThat(highlightingResponse, hasProperty("service", equalTo("highlight")));
assertThat(highlightingResponse, hasProperty("pushData", instanceOf(HighlightingResult.class)));
}
private void assertValidationResponse(String stateId, XtextWebResponse validationResponse) {
assertThat(validationResponse, hasProperty("resourceId", equalTo(RESOURCE_NAME)));
assertThat(validationResponse, hasProperty("stateId", equalTo(stateId)));
assertThat(validationResponse, hasProperty("service", equalTo("validate")));
assertThat(validationResponse, hasProperty("pushData", instanceOf(ValidationResult.class)));
}
}
|