Exemple #1
0
        public async Task AdaptiveDialogBotTurnState()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceProvider.Add("main.dialog", new MockResource("{ \"$kind\": \"Microsoft.AdaptiveDialog\" }"));
            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            var telemetryClient = new NullBotTelemetryClient();

            // Act
            var bot = new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                telemetryClient,
                logger: logger);

            await bot.OnTurnAsync(turnContext, CancellationToken.None);

            // Assert
            Assert.NotNull(turnContext.TurnState.Get <BotFrameworkClient>());
            Assert.NotNull(turnContext.TurnState.Get <SkillConversationIdFactoryBase>());
            Assert.NotNull(turnContext.TurnState.Get <ConversationState>());
            Assert.NotNull(turnContext.TurnState.Get <UserState>());
            Assert.NotNull(turnContext.TurnState.Get <ResourceExplorer>());
            Assert.NotNull(turnContext.TurnState.Get <LanguageGenerator>());
            Assert.NotNull(turnContext.TurnState.Get <LanguageGeneratorManager>());
            Assert.NotNull(turnContext.TurnState.Get <LanguagePolicy>());
        }
Exemple #2
0
        public async Task AdaptiveDialogBotSetTestOptions()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceProvider.Add("main.dialog", new MockResource("{ \"$kind\": \"Microsoft.AdaptiveDialog\" }"));
            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // Type "event" and Name of "SetTestOptions" should store Value in ConversationState.
            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                Type         = "event",
                Name         = "SetTestOptions",
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                },
                Value = new JObject {
                    { "randomSeed", new JValue(123) }, { "randomValue", new JValue(456) }
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            var telemetryClient = new NullBotTelemetryClient();

            // Act
            var bot = new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                telemetryClient,
                logger: logger);

            await bot.OnTurnAsync(turnContext, CancellationToken.None);

            // Assert TestOptions are in Conversation
            var testOptionsAccessor = conversationState.CreateProperty <JObject>("TestOptions");

            Assert.Equal(123, (await testOptionsAccessor.GetAsync(turnContext)).GetValue("randomSeed"));
            Assert.Equal(456, (await testOptionsAccessor.GetAsync(turnContext)).GetValue("randomValue"));
        }