Corrected TestFlow and TestAdaptor to match dotnet.
This commit is contained in:
Родитель
6ff738e765
Коммит
2284e85b5a
|
@ -8,6 +8,7 @@ import java.util.concurrent.CompletableFuture;
|
|||
/**
|
||||
* Represents a bot that can operate on incoming activities.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Bot {
|
||||
/**
|
||||
* When implemented in a bot, handles an incoming activity.
|
||||
|
@ -16,5 +17,5 @@ public interface Bot {
|
|||
* incoming activity, and other data needed to process the activity.
|
||||
* @return A task that represents the work queued to execute.
|
||||
*/
|
||||
CompletableFuture onTurn(TurnContext turnContext);
|
||||
CompletableFuture<Void> onTurn(TurnContext turnContext);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ package com.microsoft.bot.builder;
|
|||
|
||||
import com.microsoft.bot.builder.adapters.TestAdapter;
|
||||
import com.microsoft.bot.builder.adapters.TestFlow;
|
||||
import com.microsoft.bot.connector.Channels;
|
||||
import com.microsoft.bot.schema.ActivityTypes;
|
||||
import com.microsoft.bot.schema.ChannelAccount;
|
||||
import com.microsoft.bot.schema.ConversationAccount;
|
||||
|
@ -25,7 +26,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
|
||||
// setup convState
|
||||
ConversationState convState = new ConversationState(storage);
|
||||
StatePropertyAccessor<Integer> convProperty = userState.createProperty("convCount");
|
||||
StatePropertyAccessor<Integer> convProperty = convState.createProperty("convCount");
|
||||
|
||||
TestAdapter adapter = new TestAdapter().use(new AutoSaveStateMiddleware(userState, convState));
|
||||
|
||||
|
@ -38,20 +39,20 @@ public class AutoSaveStateMiddlewareTests {
|
|||
|
||||
if(turnContext.getActivity().isType(ActivityTypes.MESSAGE)) {
|
||||
if (StringUtils.equals(turnContext.getActivity().getText(), "get userCount")) {
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(userCount.toString()));
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(userCount.toString())).join();
|
||||
}
|
||||
else if (StringUtils.equals(turnContext.getActivity().getText(), "get convCount")) {
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(convCount.toString()));
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(convCount.toString())).join();
|
||||
}
|
||||
}
|
||||
|
||||
// increment userCount and set property using accessor. To be saved later by AutoSaveStateMiddleware
|
||||
userCount++;
|
||||
userProperty.set(turnContext, userCount);
|
||||
userProperty.set(turnContext, userCount).join();
|
||||
|
||||
// increment convCount and set property using accessor. To be saved later by AutoSaveStateMiddleware
|
||||
convCount++;
|
||||
convProperty.set(turnContext, convCount);
|
||||
convProperty.set(turnContext, convCount).join();
|
||||
|
||||
return CompletableFuture.completedFuture(null);
|
||||
});
|
||||
|
@ -63,8 +64,8 @@ public class AutoSaveStateMiddlewareTests {
|
|||
.send("get userCount")
|
||||
.assertReply(String.format("%d", USER_INITIAL_COUNT + 2))
|
||||
.send("get convCount")
|
||||
.assertReply(String.format("%d", CONVERSATION_INITIAL_COUNT + 1))
|
||||
.startTest();
|
||||
.assertReply(String.format("%d", CONVERSATION_INITIAL_COUNT + 3))
|
||||
.startTest().join();
|
||||
|
||||
adapter = new TestAdapter(new ConversationReference(){{
|
||||
setChannelId("test");
|
||||
|
@ -79,7 +80,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
.assertReply(String.format("%d", USER_INITIAL_COUNT + 4))
|
||||
.send("get convCount")
|
||||
.assertReply(String.format("%d", CONVERSATION_INITIAL_COUNT + 1))
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -92,7 +93,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
|
||||
// setup convState
|
||||
ConversationState convState = new ConversationState(storage);
|
||||
StatePropertyAccessor<Integer> convProperty = userState.createProperty("convCount");
|
||||
StatePropertyAccessor<Integer> convProperty = convState.createProperty("convCount");
|
||||
|
||||
AutoSaveStateMiddleware bss = new AutoSaveStateMiddleware(){{
|
||||
add(userState);
|
||||
|
@ -110,20 +111,20 @@ public class AutoSaveStateMiddlewareTests {
|
|||
|
||||
if(turnContext.getActivity().isType(ActivityTypes.MESSAGE)) {
|
||||
if (StringUtils.equals(turnContext.getActivity().getText(), "get userCount")) {
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(userCount.toString()));
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(userCount.toString())).join();
|
||||
}
|
||||
else if (StringUtils.equals(turnContext.getActivity().getText(), "get convCount")) {
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(convCount.toString()));
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply(convCount.toString())).join();
|
||||
}
|
||||
}
|
||||
|
||||
// increment userCount and set property using accessor. To be saved later by AutoSaveStateMiddleware
|
||||
userCount++;
|
||||
userProperty.set(turnContext, userCount);
|
||||
userProperty.set(turnContext, userCount).join();
|
||||
|
||||
// increment convCount and set property using accessor. To be saved later by AutoSaveStateMiddleware
|
||||
convCount++;
|
||||
convProperty.set(turnContext, convCount);
|
||||
convProperty.set(turnContext, convCount).join();
|
||||
|
||||
return CompletableFuture.completedFuture(null);
|
||||
});
|
||||
|
@ -136,7 +137,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
.assertReply(String.format("%d", USER_INITIAL_COUNT + 2))
|
||||
.send("get convCount")
|
||||
.assertReply(String.format("%d", CONVERSATION_INITIAL_COUNT + 3))
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// new adapter on new conversation
|
||||
AutoSaveStateMiddleware bss2 = new AutoSaveStateMiddleware(){{
|
||||
|
@ -145,7 +146,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
}};
|
||||
|
||||
adapter = new TestAdapter(new ConversationReference(){{
|
||||
setChannelId("test");
|
||||
setChannelId(Channels.TEST);
|
||||
setServiceUrl("https://test.com");
|
||||
setUser(new ChannelAccount("user1", "User1"));
|
||||
setBot(new ChannelAccount("bot", "Bot"));
|
||||
|
@ -157,7 +158,7 @@ public class AutoSaveStateMiddlewareTests {
|
|||
.assertReply(String.format("%d", USER_INITIAL_COUNT + 4))
|
||||
.send("get convCount")
|
||||
.assertReply(String.format("%d", CONVERSATION_INITIAL_COUNT + 1))
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class BotAdapterBracketingTest {
|
|||
.assertReply("BEFORE")
|
||||
.assertReply("ECHO:test")
|
||||
.assertReply("AFTER")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -40,8 +40,10 @@ public class BotAdapterBracketingTest {
|
|||
BotCallbackHandler echoWithException = (turnContext -> {
|
||||
String toEcho = "ECHO:" + turnContext.getActivity().getText();
|
||||
return turnContext.sendActivity(turnContext.getActivity().createReply(toEcho))
|
||||
.thenApply(resourceResponse -> {
|
||||
throw new RuntimeException(uniqueId);
|
||||
.thenCompose(resourceResponse -> {
|
||||
CompletableFuture<Void> result = new CompletableFuture();
|
||||
result.completeExceptionally(new RuntimeException(uniqueId));
|
||||
return result;
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -49,9 +51,9 @@ public class BotAdapterBracketingTest {
|
|||
.send("test")
|
||||
.assertReply("BEFORE")
|
||||
.assertReply("ECHO:test")
|
||||
.assertReply("CAUGHT:" + uniqueId)
|
||||
.assertReply("CAUGHT: " + uniqueId)
|
||||
.assertReply("AFTER")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
private static class CatchExceptionMiddleware implements Middleware {
|
||||
|
@ -60,7 +62,7 @@ public class BotAdapterBracketingTest {
|
|||
return turnContext.sendActivity(turnContext.getActivity().createReply("BEFORE"))
|
||||
.thenCompose(resourceResponse -> next.next())
|
||||
.exceptionally(exception -> {
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply("CAUGHT:" + exception.getMessage())).join();
|
||||
turnContext.sendActivity(turnContext.getActivity().createReply("CAUGHT: " + exception.getCause().getMessage())).join();
|
||||
return null;
|
||||
})
|
||||
.thenCompose(result -> turnContext.sendActivity(turnContext.getActivity().createReply("AFTER"))
|
||||
|
|
|
@ -322,7 +322,7 @@ public class BotStateTests {
|
|||
return CompletableFuture.completedFuture(null);
|
||||
}))
|
||||
.send("set value")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -352,7 +352,7 @@ public class BotStateTests {
|
|||
new TestFlow(adapter, callback)
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -380,7 +380,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -408,7 +408,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -436,7 +436,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -464,7 +464,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -495,7 +495,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", testGuid)
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -523,7 +523,7 @@ public class BotStateTests {
|
|||
}))
|
||||
.test("set value", "value saved")
|
||||
.test("get value", "TypedObject")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -559,7 +559,7 @@ public class BotStateTests {
|
|||
return CompletableFuture.completedFuture(null);
|
||||
})
|
||||
.send(Activity.createConversationUpdateActivity())
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
|
|
@ -39,6 +39,6 @@ public class OnTurnErrorTests {
|
|||
.assertReply("foo", "passthrough")
|
||||
.send("NotImplementedException")
|
||||
.assertReply("Test")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,16 +54,14 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:foo")
|
||||
.send("bar")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:bar")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 6 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(6)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -130,16 +128,14 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:foo")
|
||||
.send("bar")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:bar")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 6 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(6)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -210,7 +206,7 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.send("update")
|
||||
.assertReply("new response")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 4 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(4)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -251,7 +247,7 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.assertReply("response")
|
||||
.send("deleteIt")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 4 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(4)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -284,16 +280,14 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:foo")
|
||||
.send("bar")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:bar")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 8 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(8)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -356,16 +350,14 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:foo")
|
||||
.send("bar")
|
||||
.assertReply(activity -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply("echo:bar")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 10 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(10)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -411,7 +403,7 @@ public class TelemetryMiddlewareTests {
|
|||
conversationId[0] = turnContext.getActivity().getConversation().getId();
|
||||
|
||||
if (StringUtils.equals(turnContext.getActivity().getText(), "update")) {
|
||||
activityToUpdate[0].setText("update");
|
||||
activityToUpdate[0].setText("new response");
|
||||
turnContext.updateActivity(activityToUpdate[0]).join();
|
||||
turnContext.deleteActivity(turnContext.getActivity().getId()).join();
|
||||
} else {
|
||||
|
@ -426,8 +418,8 @@ public class TelemetryMiddlewareTests {
|
|||
}))
|
||||
.send("foo")
|
||||
.send("update")
|
||||
.assertReply("response")
|
||||
.startTest();
|
||||
.assertReply("new response")
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 5 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(5)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
@ -477,7 +469,7 @@ public class TelemetryMiddlewareTests {
|
|||
.send("foo")
|
||||
.send("update")
|
||||
.assertReply("new response")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
// verify BotTelemetryClient was invoked 5 times, and capture arguments.
|
||||
verify(mockTelemetryClient, times(5)).trackEvent(eventNameCaptor.capture(), propertiesCaptor.capture());
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
package com.microsoft.bot.builder;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.microsoft.bot.builder.adapters.TestAdapter;
|
||||
import com.microsoft.bot.builder.adapters.TestFlow;
|
||||
import com.microsoft.bot.schema.*;
|
||||
|
@ -54,36 +53,30 @@ public class TranscriptMiddlewareTest {
|
|||
final String[] conversationId = {null};
|
||||
|
||||
new TestFlow(adapter, (context) -> {
|
||||
conversationId[0] = context.getActivity().getConversation().getId();
|
||||
Activity typingActivity = new Activity(ActivityTypes.TYPING) {{
|
||||
setRelatesTo(context.getActivity().getRelatesTo());
|
||||
}};
|
||||
conversationId[0] = context.getActivity().getConversation().getId();
|
||||
Activity typingActivity = new Activity(ActivityTypes.TYPING) {{
|
||||
setRelatesTo(context.getActivity().getRelatesTo());
|
||||
}};
|
||||
|
||||
context.sendActivity(typingActivity).join();
|
||||
context.sendActivity(typingActivity).join();
|
||||
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Assert.fail();
|
||||
}
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
context.sendActivity("echo:" + context.getActivity().getText()).join();
|
||||
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}).send("foo")
|
||||
.assertReply((activity) -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
context.sendActivity("echo:" + context.getActivity().getText()).join();
|
||||
return CompletableFuture.completedFuture(null);
|
||||
})
|
||||
.send("foo")
|
||||
.assertReply((activity) -> Assert.assertEquals(activity.getType(), ActivityTypes.TYPING))
|
||||
.assertReply("echo:foo")
|
||||
.send("bar")
|
||||
.assertReply((activity) -> {
|
||||
Assert.assertEquals(activity.getType(), ActivityTypes.TYPING);
|
||||
return null;
|
||||
})
|
||||
.assertReply((activity) -> Assert.assertEquals(activity.getType(), ActivityTypes.TYPING))
|
||||
.assertReply("echo:bar")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
PagedResult pagedResult = transcriptStore.getTranscriptActivities("test", conversationId[0]).join();
|
||||
Assert.assertEquals(6, pagedResult.getItems().size());
|
||||
|
@ -126,13 +119,16 @@ public class TranscriptMiddlewareTest {
|
|||
.send("foo")
|
||||
.send("update")
|
||||
.assertReply("new response")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
Thread.sleep(500);
|
||||
PagedResult pagedResult = transcriptStore.getTranscriptActivities("test", conversationId[0]).join();
|
||||
Assert.assertEquals(4, pagedResult.getItems().size());
|
||||
Assert.assertEquals("foo", ((Activity) pagedResult.getItems().get(0)).getText());
|
||||
Assert.assertEquals("response", ((Activity) pagedResult.getItems().get(1)).getText());
|
||||
if (!StringUtils.equals(((Activity)pagedResult.getItems().get(2)).getText(), "new response")) {
|
||||
Assert.fail("fail");
|
||||
}
|
||||
Assert.assertEquals("new response", ((Activity)pagedResult.getItems().get(2)).getText());
|
||||
Assert.assertEquals("update", ((Activity)pagedResult.getItems().get(3)).getText());
|
||||
Assert.assertEquals( ((Activity)pagedResult.getItems().get(1)).getId(), ((Activity) pagedResult.getItems().get(2)).getId());
|
||||
|
@ -159,7 +155,7 @@ public class TranscriptMiddlewareTest {
|
|||
.send("foo")
|
||||
.assertReply("response")
|
||||
.send("deleteIt")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
Thread.sleep(500);
|
||||
PagedResult pagedResult = transcriptStore.getTranscriptActivities("test", conversationId[0]).join();
|
||||
|
@ -206,7 +202,7 @@ public class TranscriptMiddlewareTest {
|
|||
.send("foo")
|
||||
.send("update")
|
||||
.assertReply("new response")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ public class TurnContextTests {
|
|||
return CompletableFuture.completedFuture(null);
|
||||
}))
|
||||
.send("TestResponded")
|
||||
.startTest();
|
||||
.startTest().join();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
package com.microsoft.bot.builder.adapters;
|
||||
|
||||
import com.microsoft.bot.builder.*;
|
||||
import com.microsoft.bot.connector.Channels;
|
||||
import com.microsoft.bot.schema.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
@ -27,22 +28,21 @@ public class TestAdapter extends BotAdapter {
|
|||
setConversationReference(reference);
|
||||
} else {
|
||||
setConversationReference(new ConversationReference() {{
|
||||
setChannelId("test");
|
||||
setChannelId(Channels.TEST);
|
||||
setServiceUrl("https://test.com");
|
||||
}});
|
||||
|
||||
conversationReference().setUser(new ChannelAccount() {{
|
||||
setId("user1");
|
||||
setName("User1");
|
||||
}});
|
||||
conversationReference().setBot(new ChannelAccount() {{
|
||||
setId("bot");
|
||||
setName("Bot");
|
||||
}});
|
||||
conversationReference().setConversation(new ConversationAccount() {{
|
||||
setIsGroup(Boolean.FALSE);
|
||||
setConversationType("convo1");
|
||||
setId("Conversation1");
|
||||
setUser(new ChannelAccount() {{
|
||||
setId("user1");
|
||||
setName("User1");
|
||||
}});
|
||||
setBot(new ChannelAccount() {{
|
||||
setId("bot");
|
||||
setName("Bot");
|
||||
}});
|
||||
setConversation(new ConversationAccount() {{
|
||||
setIsGroup(false);
|
||||
setConversationType("convo1");
|
||||
setId("Conversation1");
|
||||
}});
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
@ -57,27 +57,30 @@ public class TestAdapter extends BotAdapter {
|
|||
return this;
|
||||
}
|
||||
|
||||
public void processActivity(Activity activity,
|
||||
BotCallbackHandler callback) throws Exception {
|
||||
synchronized (conversationReference()) {
|
||||
// ready for next reply
|
||||
if (activity.getType() == null)
|
||||
activity.setType(ActivityTypes.MESSAGE);
|
||||
activity.setChannelId(conversationReference().getChannelId());
|
||||
activity.setFrom(conversationReference().getUser());
|
||||
activity.setRecipient(conversationReference().getBot());
|
||||
activity.setConversation(conversationReference().getConversation());
|
||||
activity.setServiceUrl(conversationReference().getServiceUrl());
|
||||
Integer next = nextId++;
|
||||
activity.setId(next.toString());
|
||||
}
|
||||
// Assume Default DateTime : DateTime(0)
|
||||
if (activity.getTimestamp() == null || activity.getTimestamp().toEpochSecond() == 0)
|
||||
activity.setTimestamp(OffsetDateTime.now(ZoneId.of("UTC")));
|
||||
public CompletableFuture<Void> processActivity(Activity activity,
|
||||
BotCallbackHandler callback) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
synchronized (conversationReference()) {
|
||||
// ready for next reply
|
||||
if (activity.getType() == null)
|
||||
activity.setType(ActivityTypes.MESSAGE);
|
||||
activity.setChannelId(conversationReference().getChannelId());
|
||||
activity.setFrom(conversationReference().getUser());
|
||||
activity.setRecipient(conversationReference().getBot());
|
||||
activity.setConversation(conversationReference().getConversation());
|
||||
activity.setServiceUrl(conversationReference().getServiceUrl());
|
||||
Integer next = nextId++;
|
||||
activity.setId(next.toString());
|
||||
}
|
||||
// Assume Default DateTime : DateTime(0)
|
||||
if (activity.getTimestamp() == null || activity.getTimestamp().toEpochSecond() == 0)
|
||||
activity.setTimestamp(OffsetDateTime.now(ZoneId.of("UTC")));
|
||||
|
||||
try (TurnContextImpl context = new TurnContextImpl(this, activity)) {
|
||||
super.runPipeline(context, callback).join();
|
||||
}
|
||||
return activity;
|
||||
}).thenCompose(activity1 -> {
|
||||
TurnContextImpl context = new TurnContextImpl(this, activity1);
|
||||
return super.runPipeline(context, callback);
|
||||
});
|
||||
}
|
||||
|
||||
public ConversationReference conversationReference() {
|
||||
|
@ -210,8 +213,8 @@ public class TestAdapter extends BotAdapter {
|
|||
* @param userSays
|
||||
* @return
|
||||
*/
|
||||
public void sendTextToBot(String userSays, BotCallbackHandler callback) throws Exception {
|
||||
processActivity(this.makeActivity(userSays), callback);
|
||||
public CompletableFuture<Void> sendTextToBot(String userSays, BotCallbackHandler callback) {
|
||||
return processActivity(this.makeActivity(userSays), callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
package com.microsoft.bot.builder.adapters;
|
||||
|
||||
import com.microsoft.bot.builder.Bot;
|
||||
import com.microsoft.bot.builder.BotCallbackHandler;
|
||||
import com.microsoft.bot.connector.ExecutorFactory;
|
||||
import com.microsoft.bot.schema.Activity;
|
||||
|
@ -12,28 +13,15 @@ import org.junit.Assert;
|
|||
import java.lang.management.ManagementFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static java.util.concurrent.CompletableFuture.completedFuture;
|
||||
|
||||
public class TestFlow {
|
||||
final TestAdapter adapter;
|
||||
CompletableFuture<String> testTask;
|
||||
CompletableFuture<Void> testTask;
|
||||
BotCallbackHandler callback;
|
||||
|
||||
ArrayList<Supplier<String>> tasks = new ArrayList<Supplier<String>>();
|
||||
ForkJoinPool.ForkJoinWorkerThreadFactory factory = new ForkJoinPool.ForkJoinWorkerThreadFactory() {
|
||||
@Override
|
||||
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
|
||||
final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
|
||||
worker.setName("TestFlow-" + worker.getPoolIndex());
|
||||
return worker;
|
||||
}
|
||||
};
|
||||
|
||||
ExecutorService executor = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, true);
|
||||
|
||||
//ArrayList<Supplier<String>> tasks = new ArrayList<Supplier<String>>();
|
||||
|
||||
public TestFlow(TestAdapter adapter) {
|
||||
this(adapter, null);
|
||||
|
@ -42,38 +30,23 @@ public class TestFlow {
|
|||
public TestFlow(TestAdapter adapter, BotCallbackHandler callback) {
|
||||
this.adapter = adapter;
|
||||
this.callback = callback;
|
||||
this.testTask = completedFuture(null);
|
||||
this.testTask = CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
|
||||
public TestFlow(Supplier<String> testTask, TestFlow flow) {
|
||||
this.tasks = flow.tasks;
|
||||
if (testTask != null)
|
||||
this.tasks.add(testTask);
|
||||
public TestFlow(CompletableFuture<Void> testTask, TestFlow flow) {
|
||||
this.testTask = testTask == null ? CompletableFuture.completedFuture(null) : testTask;
|
||||
this.callback = flow.callback;
|
||||
this.adapter = flow.adapter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start the execution of the test flow
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String startTest() {
|
||||
|
||||
System.out.printf("+------------------------------------------+\n");
|
||||
int count = 0;
|
||||
for (Supplier<String> task : this.tasks) {
|
||||
System.out.printf("| Running task %s of %s\n", count++, this.tasks.size());
|
||||
String result = null;
|
||||
result = task.get();
|
||||
System.out.printf("| --> Result: %s", result);
|
||||
System.out.flush();
|
||||
}
|
||||
System.out.printf("+------------------------------------------+\n");
|
||||
return "Completed";
|
||||
|
||||
public CompletableFuture<Void> startTest() {
|
||||
return testTask;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,18 +59,12 @@ public class TestFlow {
|
|||
if (userSays == null)
|
||||
throw new IllegalArgumentException("You have to pass a userSays parameter");
|
||||
|
||||
// Function<TurnContextImpl, CompletableFuture>
|
||||
return new TestFlow((() -> {
|
||||
System.out.print(String.format("USER SAYS: %s (Thread Id: %s)\n", userSays, Thread.currentThread().getId()));
|
||||
System.out.flush();
|
||||
try {
|
||||
this.adapter.sendTextToBot(userSays, this.callback);
|
||||
return "Successfully sent " + userSays;
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
return e.getMessage();
|
||||
}
|
||||
}), this);
|
||||
return new TestFlow(
|
||||
testTask
|
||||
.thenCompose(result -> {
|
||||
System.out.print(String.format("USER SAYS: %s (Thread Id: %s)\n", userSays, Thread.currentThread().getId()));
|
||||
return this.adapter.sendTextToBot(userSays, this.callback);
|
||||
}), this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,20 +77,12 @@ public class TestFlow {
|
|||
if (userActivity == null)
|
||||
throw new IllegalArgumentException("You have to pass an Activity");
|
||||
|
||||
return new TestFlow((() -> {
|
||||
System.out.printf("TestFlow(%s): Send with User Activity! %s", Thread.currentThread().getId(), userActivity.getText());
|
||||
System.out.flush();
|
||||
|
||||
|
||||
try {
|
||||
this.adapter.processActivity(userActivity, this.callback);
|
||||
return "TestFlow: Send() -> ProcessActivity: " + userActivity.getText();
|
||||
} catch (Exception e) {
|
||||
return e.getMessage();
|
||||
|
||||
return new TestFlow(
|
||||
testTask.thenCompose(result -> {
|
||||
System.out.printf("TestFlow(%s): Send with User Activity! %s", Thread.currentThread().getId(), userActivity.getText());
|
||||
return this.adapter.processActivity(userActivity, this.callback);
|
||||
}
|
||||
|
||||
}), this);
|
||||
), this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -133,17 +92,17 @@ public class TestFlow {
|
|||
* @return
|
||||
*/
|
||||
public TestFlow delay(int ms) {
|
||||
return new TestFlow(() ->
|
||||
return new TestFlow(CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
System.out.printf("TestFlow(%s): Delay(%s ms) called. ", Thread.currentThread().getId(), ms);
|
||||
System.out.flush();
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
return e.getMessage();
|
||||
|
||||
}
|
||||
return null;
|
||||
}, this);
|
||||
}, ExecutorFactory.getExecutor()), this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -178,18 +137,12 @@ public class TestFlow {
|
|||
public TestFlow assertReply(Activity expected, String description, int timeout) {
|
||||
if (description == null)
|
||||
description = Thread.currentThread().getStackTrace()[1].getMethodName();
|
||||
String finalDescription = description;
|
||||
return this.assertReply((reply) -> {
|
||||
if (expected.getType() != reply.getType())
|
||||
return String.format("%s: Type should match", finalDescription);
|
||||
if (expected.getText().equals(reply.getText())) {
|
||||
if (finalDescription == null)
|
||||
return String.format("Expected:%s\nReceived:{reply.AsMessageActivity().Text}", expected.getText());
|
||||
else
|
||||
return String.format("%s: Text should match", finalDescription);
|
||||
if (!StringUtils.equals(expected.getType(), reply.getType()))
|
||||
throw new RuntimeException(String.format("Type: '%s' should match expected '%s'", reply.getType(), expected.getType()));
|
||||
if (!expected.getText().equals(reply.getText())) {
|
||||
throw new RuntimeException(String.format("Text '%s' should match expected '%s'", reply.getText(), expected.getText()));
|
||||
}
|
||||
// TODO, expand this to do all properties set on expected
|
||||
return null;
|
||||
}, description, timeout);
|
||||
}
|
||||
|
||||
|
@ -199,61 +152,63 @@ public class TestFlow {
|
|||
* @param validateActivity
|
||||
* @return
|
||||
*/
|
||||
public TestFlow assertReply(Function<Activity, String> validateActivity) {
|
||||
public TestFlow assertReply(Consumer<Activity> validateActivity) {
|
||||
String description = Thread.currentThread().getStackTrace()[1].getMethodName();
|
||||
return assertReply(validateActivity, description, 3000);
|
||||
}
|
||||
|
||||
public TestFlow assertReply(Function<Activity, String> validateActivity, String description) {
|
||||
public TestFlow assertReply(Consumer<Activity> validateActivity, String description) {
|
||||
return assertReply(validateActivity, description, 3000);
|
||||
}
|
||||
|
||||
public TestFlow assertReply(Function<Activity, String> validateActivity, String description, int timeout) {
|
||||
return new TestFlow(() -> {
|
||||
System.out.println(String.format("AssertReply: Starting loop : %s (Thread:%s)", description, Thread.currentThread().getId()));
|
||||
System.out.flush();
|
||||
public TestFlow assertReply(Consumer<Activity> validateActivity, String description, int timeout) {
|
||||
return new TestFlow(testTask
|
||||
.thenApply(result -> {
|
||||
System.out.println(String.format("AssertReply: Starting loop : %s (Thread:%s)", description, Thread.currentThread().getId()));
|
||||
System.out.flush();
|
||||
|
||||
int finalTimeout = Integer.MAX_VALUE;
|
||||
if (isDebug())
|
||||
finalTimeout = Integer.MAX_VALUE;
|
||||
int finalTimeout = Integer.MAX_VALUE;
|
||||
if (isDebug())
|
||||
finalTimeout = Integer.MAX_VALUE;
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
while (true) {
|
||||
long current = System.currentTimeMillis();
|
||||
long start = System.currentTimeMillis();
|
||||
while (true) {
|
||||
long current = System.currentTimeMillis();
|
||||
|
||||
if ((current - start) > (long) finalTimeout) {
|
||||
System.out.println("AssertReply: Timeout!\n");
|
||||
System.out.flush();
|
||||
return String.format("%d ms Timed out waiting for:'%s'", finalTimeout, description);
|
||||
}
|
||||
if ((current - start) > (long) finalTimeout) {
|
||||
System.out.println("AssertReply: Timeout!\n");
|
||||
System.out.flush();
|
||||
throw new RuntimeException(String.format("%d ms Timed out waiting for:'%s'", finalTimeout, description));
|
||||
}
|
||||
|
||||
// System.out.println("Before GetNextReply\n");
|
||||
// System.out.flush();
|
||||
// System.out.println("Before GetNextReply\n");
|
||||
// System.out.flush();
|
||||
|
||||
Activity replyActivity = this.adapter.getNextReply();
|
||||
// System.out.println("After GetNextReply\n");
|
||||
// System.out.flush();
|
||||
Activity replyActivity = this.adapter.getNextReply();
|
||||
// System.out.println("After GetNextReply\n");
|
||||
// System.out.flush();
|
||||
|
||||
if (replyActivity != null) {
|
||||
System.out.printf("AssertReply(tid:%s): Received Reply: %s ", Thread.currentThread().getId(), (replyActivity.getText() == null) ? "No Text set" : replyActivity.getText());
|
||||
System.out.flush();
|
||||
System.out.printf("=============\n From: %s\n To:%s\n ==========\n", (replyActivity.getFrom() == null) ? "No from set" : replyActivity.getFrom().getName(),
|
||||
(replyActivity.getRecipient() == null) ? "No recipient set" : replyActivity.getRecipient().getName());
|
||||
System.out.flush();
|
||||
if (replyActivity != null) {
|
||||
System.out.printf("AssertReply(tid:%s): Received Reply: %s ", Thread.currentThread().getId(), (replyActivity.getText() == null) ? "No Text set" : replyActivity.getText());
|
||||
System.out.flush();
|
||||
System.out.printf("=============\n From: %s\n To:%s\n ==========\n", (replyActivity.getFrom() == null) ? "No from set" : replyActivity.getFrom().getName(),
|
||||
(replyActivity.getRecipient() == null) ? "No recipient set" : replyActivity.getRecipient().getName());
|
||||
System.out.flush();
|
||||
|
||||
// if we have a reply
|
||||
return validateActivity.apply(replyActivity);
|
||||
} else {
|
||||
System.out.printf("AssertReply(tid:%s): Waiting..\n", Thread.currentThread().getId());
|
||||
System.out.flush();
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
// if we have a reply
|
||||
validateActivity.accept(replyActivity);
|
||||
return null;
|
||||
} else {
|
||||
System.out.printf("AssertReply(tid:%s): Waiting..\n", Thread.currentThread().getId());
|
||||
System.out.flush();
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
}), this);
|
||||
}
|
||||
|
||||
// Hack to determine if debugger attached..
|
||||
|
@ -408,15 +363,15 @@ public class TestFlow {
|
|||
* @param expected
|
||||
* @return
|
||||
*/
|
||||
public TestFlow test(String userSays, Function<Activity, String> expected) {
|
||||
public TestFlow test(String userSays, Consumer<Activity> expected) {
|
||||
return test(userSays, expected, null, 3000);
|
||||
}
|
||||
|
||||
public TestFlow test(String userSays, Function<Activity, String> expected, String description) {
|
||||
public TestFlow test(String userSays, Consumer<Activity> expected, String description) {
|
||||
return test(userSays, expected, description, 3000);
|
||||
}
|
||||
|
||||
public TestFlow test(String userSays, Function<Activity, String> expected, String description, int timeout) {
|
||||
public TestFlow test(String userSays, Consumer<Activity> expected, String description, int timeout) {
|
||||
if (expected == null)
|
||||
throw new IllegalArgumentException("expected");
|
||||
|
||||
|
@ -445,10 +400,9 @@ public class TestFlow {
|
|||
return this.assertReply((reply) -> {
|
||||
for (String candidate : candidates) {
|
||||
if (StringUtils.equals(reply.getText(), candidate))
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
return String.format("%s: Not one of candidates: %s", description, String.join("\n ", candidates));
|
||||
throw new RuntimeException(String.format("%s: Not one of candidates: %s", description, String.join("\n ", candidates)));
|
||||
}, description, timeout);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче