-
Notifications
You must be signed in to change notification settings - Fork 880
Expand file tree
/
Copy pathAdHocCommandIntegrationTest.java
More file actions
380 lines (302 loc) · 16.2 KB
/
Copy pathAdHocCommandIntegrationTest.java
File metadata and controls
380 lines (302 loc) · 16.2 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
*
* Copyright 2023-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.igniterealtime.smack.inttest.annotations.AfterClass;
import org.igniterealtime.smack.inttest.annotations.BeforeClass;
import org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
import org.jivesoftware.smackx.commands.packet.AdHocCommandDataBuilder;
import org.jivesoftware.smackx.commands.packet.AdHocCommandDataBuilder.NextStage;
import org.jivesoftware.smackx.commands.packet.AdHocCommandDataBuilder.PreviousStage;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.form.FillableForm;
import org.jivesoftware.smackx.xdata.form.SubmitForm;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.annotations.SpecificationReference;
@SpecificationReference(document = "XEP-0050", version = "1.3.0")
public class AdHocCommandIntegrationTest extends AbstractSmackIntegrationTest {
public AdHocCommandIntegrationTest(SmackIntegrationTestEnvironment environment) {
super(environment);
}
@BeforeClass
public void subscribe() throws Exception {
// RFC6120 10.5.4 and RFC 6121 8.5.3.1 are at odds with each-other in regard to full-JID IQ delivery. Best possible chance of that happening is with mutual subscription.
IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
}
@AfterClass
public void unsubscribe() throws SmackException.NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
IntegrationTestRosterUtil.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
}
@SmackIntegrationTest
public void singleStageAdHocCommandTest() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
AdHocCommandManager manOne = AdHocCommandManager.getInstance(conOne);
AdHocCommandManager manTwo = AdHocCommandManager.getInstance(conTwo);
String commandNode = "test-list";
String commandName = "Return a list for testing purposes";
AdHocCommandHandlerFactory factory = (String node, String name, String sessionId) -> {
return new AdHocCommandHandler.SingleStage(node, name, sessionId) {
@Override
public AdHocCommandData executeSingleStage(AdHocCommandDataBuilder response) {
FormField field = FormField.textPrivateBuilder("my-field").build();
DataForm form = DataForm.builder(DataForm.Type.result).addField(field).build();
response.setForm(form);
return response.build();
}
};
};
manOne.registerCommand(commandNode, commandName, factory);
try {
AdHocCommand command = manTwo.getRemoteCommand(conOne.getUser(), commandNode);
AdHocCommandResult result = command.execute();
AdHocCommandData response = result.getResponse();
DataForm form = response.getForm();
FormField field = form.getField("my-field");
assertNotNull(field, "Expected a field named 'my-field' to exist in the form that " +
conTwo.getUser() + " obtained from " + conOne.getUser() + "'s command node '" + commandNode +
"' (but it did not).");
} finally {
manOne.unregisterCommand(commandNode);
}
}
private static class MyMultiStageAdHocCommandServer extends AdHocCommandHandler {
private Integer a;
private Integer b;
private static DataForm createDataForm(String variableName) {
FormField field = FormField.textSingleBuilder(variableName).setRequired().build();
return DataForm.builder(DataForm.Type.form)
.setTitle("Variable " + variableName)
.setInstructions("Please provide an integer variable " + variableName)
.addField(field)
.build();
}
private static DataForm createDataFormOp() {
FormField field = FormField.listSingleBuilder("op")
.setLabel("Arithmetic Operation")
.setRequired()
.addOption("+")
.addOption("-")
.build();
return DataForm.builder(DataForm.Type.form)
.setTitle("Operation")
.setInstructions("Please select the arithmetic operation to be performed with a and b")
.addField(field)
.build();
}
private static final DataForm dataFormAskingForA = createDataForm("a");
private static final DataForm dataFormAskingForB = createDataForm("b");
private static final DataForm dataFormAskingForOp = createDataFormOp();
MyMultiStageAdHocCommandServer(String node, String name, String sessionId) {
super(node, name, sessionId);
}
@Override
protected AdHocCommandData execute(AdHocCommandDataBuilder response) throws XMPPErrorException {
return response.setForm(dataFormAskingForA).setStatusExecuting(PreviousStage.none,
NextStage.nonFinal).build();
}
private static Integer extractIntegerField(SubmitForm form, String fieldName) throws XMPPErrorException {
FormField field = form.getField(fieldName);
if (field == null)
throw newBadRequestException("Submitted form does not contain a field of name " + fieldName);
String fieldValue = field.getFirstValue();
if (fieldValue == null)
throw newBadRequestException("Submitted form contains field of name " + fieldName + " without value");
try {
return Integer.parseInt(fieldValue);
} catch (NumberFormatException e) {
throw newBadRequestException("Submitted form contains field of name " + fieldName + " with value " + fieldValue + " that is not an integer");
}
}
@Override
protected AdHocCommandData next(AdHocCommandDataBuilder response, SubmitForm submittedForm)
throws XMPPErrorException {
DataForm form;
switch (getCurrentStage()) {
case 2:
a = extractIntegerField(submittedForm, "a");
form = dataFormAskingForB;
response.setStatusExecuting(PreviousStage.exists, NextStage.nonFinal);
break;
case 3:
b = extractIntegerField(submittedForm, "b");
form = dataFormAskingForOp;
response.setStatusExecuting(PreviousStage.exists, NextStage.isFinal);
break;
case 4:
// Ad-Hoc Commands particularity: Can get to 'complete' via 'next'.
return complete(response, submittedForm);
default:
throw new IllegalStateException();
}
return response.setForm(form).build();
}
@Override
protected AdHocCommandData complete(AdHocCommandDataBuilder response, SubmitForm submittedForm)
throws XMPPErrorException {
if (getCurrentStage() != 4) {
throw new IllegalStateException();
}
if (a == null || b == null) {
throw new IllegalStateException();
}
String op = submittedForm.getField("op").getFirstValue();
int result;
switch (op) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
default:
throw newBadRequestException("Submitted operation " + op + " is neither + nor -");
}
response.setStatusCompleted();
FormField field = FormField.textSingleBuilder("result").setValue(result).build();
DataForm form = DataForm.builder(DataForm.Type.result).setTitle("Result").addField(field).build();
return response.setForm(form).build();
}
@Override
protected AdHocCommandData prev(AdHocCommandDataBuilder response) throws XMPPErrorException {
switch (getCurrentStage()) {
case 1:
return execute(response);
case 2:
return response.setForm(dataFormAskingForA)
.setStatusExecuting(PreviousStage.exists, NextStage.nonFinal)
.build();
case 3:
return response.setForm(dataFormAskingForB)
.setStatusExecuting(PreviousStage.exists, NextStage.isFinal)
.build();
default:
throw new IllegalStateException();
}
}
@Override
public void cancel() {
}
}
@SmackIntegrationTest
public void multiStageAdHocCommandTest() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
AdHocCommandManager manOne = AdHocCommandManager.getInstance(conOne);
AdHocCommandManager manTwo = AdHocCommandManager.getInstance(conTwo);
String commandNode = "my-multi-stage-command";
String commandName = "An example multi-sage ad-hoc command";
AdHocCommandHandlerFactory factory = (String node, String name, String sessionId) -> {
return new MyMultiStageAdHocCommandServer(node, name, sessionId);
};
manOne.registerCommand(commandNode, commandName, factory);
try {
AdHocCommand command = manTwo.getRemoteCommand(conOne.getUser(), commandNode);
AdHocCommandResult.StatusExecuting result = command.execute().asExecutingOrThrow();
FillableForm form = result.getFillableForm();
form.setAnswer("a", 42);
SubmitForm submitForm = form.getSubmitForm();
result = command.next(submitForm).asExecutingOrThrow();
form = result.getFillableForm();
form.setAnswer("b", 23);
submitForm = form.getSubmitForm();
result = command.next(submitForm).asExecutingOrThrow();
form = result.getFillableForm();
form.setAnswer("op", "+");
submitForm = form.getSubmitForm();
AdHocCommandResult.StatusCompleted completed = command.complete(submitForm).asCompletedOrThrow();
String operationResult = completed.getResponse().getForm().getField("result").getFirstValue();
assertEquals("65", operationResult,
"Unexpected value in the field 'result' from the command result that " + conTwo.getUser() +
" received from " + conOne.getUser() + " after completing a multi-staged ad-hoc command on node '" +
commandNode + "'.");
} finally {
manTwo.unregisterCommand(commandNode);
}
}
@SmackIntegrationTest
public void multiStageWithPrevAdHocCommandTest() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
AdHocCommandManager manOne = AdHocCommandManager.getInstance(conOne);
AdHocCommandManager manTwo = AdHocCommandManager.getInstance(conTwo);
String commandNode = "my-multi-stage-with-prev-command";
String commandName = "An example multi-sage ad-hoc command";
AdHocCommandHandlerFactory factory = (String node, String name, String sessionId) -> {
return new MyMultiStageAdHocCommandServer(node, name, sessionId);
};
manOne.registerCommand(commandNode, commandName, factory);
try {
AdHocCommand command = manTwo.getRemoteCommand(conOne.getUser(), commandNode);
AdHocCommandResult.StatusExecuting result = command.execute().asExecutingOrThrow();
FillableForm form = result.getFillableForm();
form.setAnswer("a", 42);
SubmitForm submitForm = form.getSubmitForm();
command.next(submitForm).asExecutingOrThrow();
// Ups, I wanted a different value for 'a', lets execute 'prev' to get back to the previous stage.
result = command.prev().asExecutingOrThrow();
form = result.getFillableForm();
form.setAnswer("a", 77);
submitForm = form.getSubmitForm();
result = command.next(submitForm).asExecutingOrThrow();
form = result.getFillableForm();
form.setAnswer("b", 23);
submitForm = form.getSubmitForm();
result = command.next(submitForm).asExecutingOrThrow();
form = result.getFillableForm();
form.setAnswer("op", "+");
submitForm = form.getSubmitForm();
AdHocCommandResult.StatusCompleted completed = command.complete(submitForm).asCompletedOrThrow();
String operationResult = completed.getResponse().getForm().getField("result").getFirstValue();
assertEquals("100", operationResult,
"Unexpected value in the field 'result' from the command result that " + conTwo.getUser() +
" received from " + conOne.getUser() + " after completing a multi-staged ad-hoc command on node '" +
commandNode + "'.");
} finally {
manTwo.unregisterCommand(commandNode);
}
}
@SmackIntegrationTest
public void multiStageInvalidArgAdHocCommandTest() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
AdHocCommandManager manOne = AdHocCommandManager.getInstance(conOne);
AdHocCommandManager manTwo = AdHocCommandManager.getInstance(conTwo);
String commandNode = "my-multi-stage-invalid-arg-command";
String commandName = "An example multi-sage ad-hoc command";
AdHocCommandHandlerFactory factory = (String node, String name, String sessionId) -> {
return new MyMultiStageAdHocCommandServer(node, name, sessionId);
};
manOne.registerCommand(commandNode, commandName, factory);
try {
AdHocCommand command = manTwo.getRemoteCommand(conOne.getUser(), commandNode);
AdHocCommandResult.StatusExecuting result = command.execute().asExecutingOrThrow();
FillableForm form = result.getFillableForm();
form.setAnswer("a", "forty-two");
SubmitForm submitForm = form.getSubmitForm();
XMPPErrorException exception = assertThrows(XMPPErrorException.class, () -> command.next(submitForm));
assertEquals(StanzaError.Condition.bad_request, exception.getStanzaError().getCondition(),
"Unexpected error condition received after " + conTwo.getUser() + " supplied an invalid argument " +
"to the command node '" + commandNode + "' of " + conOne.getUser());
} finally {
manTwo.unregisterCommand(commandNode);
}
}
}