From 5d3d1c3cd74fb45fbb0effefb09ca69be202124a Mon Sep 17 00:00:00 2001 From: Lee Parrish <30470292+LeeParrishMSFT@users.noreply.github.com> Date: Mon, 22 Mar 2021 15:37:31 -0500 Subject: [PATCH] Updates for Issue 1065 (#1071) * Updates for Issue 1065 * Tweak to kick off new build. --- .../com/microsoft/bot/dialogs/Dialog.java | 15 ++-- .../bot/dialogs/DialogManagerTests.java | 79 +++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/Dialog.java b/libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/Dialog.java index cf65a60e..339c8697 100644 --- a/libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/Dialog.java +++ b/libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/Dialog.java @@ -311,13 +311,16 @@ public abstract class Dialog { public static CompletableFuture run(Dialog dialog, TurnContext turnContext, StatePropertyAccessor accessor) { DialogSet dialogSet = new DialogSet(accessor); - dialogSet.add(dialog); - dialogSet.setTelemetryClient(dialog.getTelemetryClient()); + if (turnContext.getTurnState().get(BotTelemetryClient.class) != null) { + dialogSet.setTelemetryClient(turnContext.getTurnState().get(BotTelemetryClient.class)); + } else if (dialog.getTelemetryClient() != null) { + dialogSet.setTelemetryClient(dialog.getTelemetryClient()); + } else { + dialogSet.setTelemetryClient(new NullBotTelemetryClient()); + } + + dialogSet.add(dialog); - // return dialogSet.createContext(turnContext) - // .thenCompose(dialogContext -> continueOrStart(dialogContext, dialog, - // turnContext)) - // .thenApply(result -> null); return dialogSet.createContext(turnContext) .thenAccept(dialogContext -> innerRun(turnContext, dialog.getId(), dialogContext)); } diff --git a/libraries/bot-dialogs/src/test/java/com/microsoft/bot/dialogs/DialogManagerTests.java b/libraries/bot-dialogs/src/test/java/com/microsoft/bot/dialogs/DialogManagerTests.java index 8f376be3..431631e9 100644 --- a/libraries/bot-dialogs/src/test/java/com/microsoft/bot/dialogs/DialogManagerTests.java +++ b/libraries/bot-dialogs/src/test/java/com/microsoft/bot/dialogs/DialogManagerTests.java @@ -3,6 +3,8 @@ package com.microsoft.bot.dialogs; +import java.time.Duration; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -12,13 +14,16 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; import com.microsoft.bot.builder.BotAdapter; +import com.microsoft.bot.builder.BotTelemetryClient; import com.microsoft.bot.builder.ConversationState; import com.microsoft.bot.builder.MemoryStorage; import com.microsoft.bot.builder.SendActivitiesHandler; +import com.microsoft.bot.builder.Severity; import com.microsoft.bot.builder.Storage; import com.microsoft.bot.builder.TraceTranscriptLogger; import com.microsoft.bot.builder.TranscriptLoggerMiddleware; import com.microsoft.bot.builder.TurnContext; +import com.microsoft.bot.builder.TurnContextImpl; import com.microsoft.bot.builder.skills.SkillConversationReference; import com.microsoft.bot.builder.skills.SkillHandler; import com.microsoft.bot.connector.authentication.AuthenticationConstants; @@ -30,6 +35,7 @@ import com.microsoft.bot.dialogs.prompts.PromptOptions; import com.microsoft.bot.dialogs.prompts.TextPrompt; import com.microsoft.bot.schema.Activity; import com.microsoft.bot.schema.ActivityTypes; +import com.microsoft.bot.schema.ConversationAccount; import com.microsoft.bot.schema.ResourceResponse; import com.microsoft.bot.schema.ResultPair; @@ -189,6 +195,22 @@ public class DialogManagerTests { .join(); } + @Test + public void RunShouldSetTelemetryClient() { + TestAdapter adapter = new TestAdapter(); + Dialog dialog = CreateTestDialog("conversation.name"); + ConversationState conversationState = new ConversationState(new MemoryStorage()); + Activity activity = new Activity(ActivityTypes.MESSAGE); + activity.setChannelId("test-channel"); + ConversationAccount conversation = new ConversationAccount("test-conversation-id"); + activity.setConversation(conversation); + MockBotTelemetryClient telemetryClient = new MockBotTelemetryClient(); + TurnContext turnContext = new TurnContextImpl(adapter, activity); + turnContext.getTurnState().add(BotTelemetryClient.class.getName(), telemetryClient); + Dialog.run(dialog, turnContext, conversationState.createProperty("DialogState")); + Assert.assertEquals(telemetryClient, dialog.getTelemetryClient()); + } + // @Test // public CompletableFuture DialogManager_OnErrorEvent_Leaf() { // TestUtilities.RunTestScript(); @@ -559,4 +581,61 @@ public class DialogManagerTests { return outerDc.endDialog(result); } } + + private class MockBotTelemetryClient implements BotTelemetryClient { + + @Override + public void trackAvailability( + String name, + OffsetDateTime timeStamp, + Duration duration, + String runLocation, + boolean success, + String message, + Map properties, + Map metrics + ) { + + } + + @Override + public void trackDependency( + String dependencyTypeName, + String target, + String dependencyName, + String data, + OffsetDateTime startTime, + Duration duration, + String resultCode, + boolean success + ) { + + } + + @Override + public void trackEvent(String eventName, Map properties, Map metrics) { + + } + + @Override + public void trackException(Exception exception, Map properties, Map metrics) { + + } + + @Override + public void trackTrace(String message, Severity severityLevel, Map properties) { + + } + + @Override + public void trackDialogView(String dialogName, Map properties, Map metrics) { + + } + + @Override + public void flush() { + + } + + } }