public void HandleAlexaRequestTest_HandlesSuccessCorrectly()
        {
            // arrange
            Word word = new Word
            {
                Id           = 1,
                WordName     = "test",
                PartOfSpeech = "noun",
                Definition   = "a test",
                Example      = " a moq uses a test"
            };

            AlexaRequestPayload alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadSayWordIntent.json");

            alexaRequestPayload.Session.Attributes.LastWord           = word.WordName;
            alexaRequestPayload.Session.Attributes.LastWordDefinition = word.Definition;

            var cacheService = new Mock <ICacheService>();

            cacheService.Setup(c => c.RetrieveAlexaRequest(alexaRequestPayload.Session.SessionId)).Returns(alexaRequestPayload);


            var wordOfTheDayIntentHandlerStrategy = new SayWordIntentHandlerStrategy(cacheService.Object);

            // act
            AlexaResponse alexaResponse = wordOfTheDayIntentHandlerStrategy.HandleAlexaRequest(alexaRequestPayload);


            // assert
            Assert.IsTrue(alexaResponse.Response.OutputSpeech.Ssml.Contains(
                              Utility.GetDescriptionFromEnumValue(SuccessPhrases.Great)));
        }
        public void CreateAlexaRequestHandlerStrategyTest_SessionEndedRequestHandlerStrategy()
        {
            // arrange
            AlexaRequestHandlerStrategyFactory alexaRequestHandlerStrategyFactory = BuildTestAlexaRequestHandlerStrategyFactory();

            var alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            alexaRequestPayload.Request.Type = "SessionEndedRequest";

            // act
            var strategyHandler = alexaRequestHandlerStrategyFactory.CreateAlexaRequestHandlerStrategy(alexaRequestPayload);

            // assert
            Assert.AreEqual(typeof(SessionEndedRequestHandlerStrategy), strategyHandler.GetType());
        }
        public void CreateAlexaRequestHandlerStrategyTest_IntentRequestHandlerStrategy_WordOfTheDayIntentHandlerStrategy()
        {
            // arrange
            AlexaRequestHandlerStrategyFactory alexaRequestHandlerStrategyFactory = BuildTestAlexaRequestHandlerStrategyFactory();

            var alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            alexaRequestPayload.Request.Type        = "IntentRequest";
            alexaRequestPayload.Request.Intent.Name = "WordOfTheDayIntent";

            // act
            var strategyHandler = alexaRequestHandlerStrategyFactory.CreateAlexaRequestHandlerStrategy(alexaRequestPayload);

            // assert
            Assert.AreEqual(typeof(WordOfTheDayIntentHandlerStrategy), strategyHandler.GetType());
        }
        public void CreateAlexaRequestHandlerStrategyTest_LaunchRequestHandlerStrategy()
        {
            // arrange
            IList <IAlexaRequestHandlerStrategy> availableStrategies = new List <IAlexaRequestHandlerStrategy>();
            AlexaRequestHandlerStrategyFactory   alexaRequestHandlerStrategyFactory = BuildTestAlexaRequestHandlerStrategyFactory();

            var alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            alexaRequestPayload.Request.Type = "LaunchRequest";

            // act
            var strategyHandler = alexaRequestHandlerStrategyFactory.CreateAlexaRequestHandlerStrategy(alexaRequestPayload);

            // assert
            Assert.AreEqual(typeof(LaunchRequestHandlerStrategy), strategyHandler.GetType());
        }
        public void CreateAlexaRequestHandlerStrategyTest_IntentRequestHandlerStrategy_NullIntentHandlerStrategy()
        {
            // arrange
            AlexaRequestHandlerStrategyFactory alexaRequestHandlerStrategyFactory = BuildTestAlexaRequestHandlerStrategyFactory();

            var alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            alexaRequestPayload.Request.Type        = "IntentRequest";
            alexaRequestPayload.Request.Intent.Name = "DoesNotExist";

            // act
            var strategyHandler = alexaRequestHandlerStrategyFactory.CreateAlexaRequestHandlerStrategy(alexaRequestPayload);

            // assert
            Assert.IsNull(strategyHandler);
        }
Пример #6
0
        public void HandleAlexaRequestTest()
        {
            // arrange
            Word word = new Word
            {
                Id           = 1,
                WordName     = "test",
                PartOfSpeech = "noun",
                Definition   = "a test",
                Example      = " a moq uses a test"
            };

            var wordService = new Mock <IWordService>();

            wordService.Setup(w => w.GetRandomWord()).Returns(word);


            Dictionary <WordEnum, string> mockDictionary = new Dictionary <WordEnum, string>();

            mockDictionary[WordEnum.Word]             = word.WordName;
            mockDictionary[WordEnum.WordPartOfSpeech] = word.PartOfSpeech;
            mockDictionary[WordEnum.WordDefinition]   = word.Definition;
            mockDictionary[WordEnum.WordExample]      = word.Example;


            var dictionaryService = new Mock <IDictionaryService>();

            dictionaryService.Setup(w => w.GetWordDictionaryFromString(word.WordName)).Returns(mockDictionary);

            var cacheService = new Mock <ICacheService>();

            var wordOfTheDayIntentHandlerStrategy = new WordOfTheDayIntentHandlerStrategy(wordService.Object, dictionaryService.Object, cacheService.Object);

            AlexaRequestPayload alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            alexaRequestPayload.Request.Type        = "IntentRequest";
            alexaRequestPayload.Request.Intent.Name = "WordOfTheDayIntent";

            // act
            AlexaResponse alexaResponse = wordOfTheDayIntentHandlerStrategy.HandleAlexaRequest(alexaRequestPayload);

            // assert
            Assert.IsTrue(alexaResponse.Response.OutputSpeech.Ssml.Contains("<speak><p>The Word is test</p>"));
            Assert.AreEqual(alexaRequestPayload.Session.Attributes.LastWord, word.WordName);
            Assert.AreEqual(alexaRequestPayload.Session.Attributes.LastWordDefinition, word.Definition);
        }
        public void HandleAlexaRequestTest_HandlesFailureCorrectly()
        {
            // arrange
            Word word = new Word
            {
                Id           = 1,
                WordName     = "test",
                PartOfSpeech = "noun",
                Definition   = "a test",
                Example      = " a moq uses a test"
            };

            // get the example payload and set the session attributes
            AlexaRequestPayload alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadSayWordIntent.json");

            alexaRequestPayload.Session.Attributes.LastWord           = "Incorrect";
            alexaRequestPayload.Session.Attributes.LastWordDefinition = "Incorrect definition";

            // these will be the attributes returned by the mock cache service
            var cacheService = new Mock <ICacheService>();

            cacheService.Setup(c => c.RetrieveAlexaRequest(alexaRequestPayload.Session.SessionId)).Returns(alexaRequestPayload);

            var wordOfTheDayIntentHandlerStrategy = new SayWordIntentHandlerStrategy(cacheService.Object);

            // reset the session attributes to not match the cached session
            alexaRequestPayload.Session.Attributes.LastWord = word.WordName;
            alexaRequestPayload.Session.Attributes.LastWord = word.Definition;

            // act
            AlexaResponse alexaResponse = wordOfTheDayIntentHandlerStrategy.HandleAlexaRequest(alexaRequestPayload);


            // assert
            Assert.IsTrue(alexaResponse.Response.OutputSpeech.Ssml.Contains(
                              Utility.GetDescriptionFromEnumValue(ErrorPhrases.Incorrect)));
        }
        public void ProcessAlexaRequestTest()
        {
            // arrange
            AlexaRequestPayload alexaRequestPayload = AlexaSkillProjectTestHelpers.GetAlexaRequestPayload("AlexaRequestPayloadTest.json");

            var alexaRequestMapper = new Mock <IAlexaRequestMapper>();

            var alexaRequestPersistenceService = new Mock <IAlexaRequestPersistenceService>();

            Word word = new Word
            {
                Id           = 1,
                WordName     = "test",
                PartOfSpeech = "noun",
                Definition   = "a test",
                Example      = " a moq uses a test"
            };

            var wordService = new Mock <IWordService>();

            wordService.Setup(w => w.GetRandomWord()).Returns(word);

            var cacheService = new Mock <ICacheService>();
            //cacheService.Setup(c => c.CacheAlexaRequest(alexaRequestPayload))

            Dictionary <WordEnum, string> mockDictionary = new Dictionary <WordEnum, string>();

            mockDictionary[WordEnum.Word]             = word.WordName;
            mockDictionary[WordEnum.WordPartOfSpeech] = word.PartOfSpeech;
            mockDictionary[WordEnum.WordDefinition]   = word.Definition;
            mockDictionary[WordEnum.WordExample]      = word.Example;


            var dictionaryService = new Mock <IDictionaryService>();

            dictionaryService.Setup(w => w.GetWordDictionaryFromString(word.WordName)).Returns(mockDictionary);

            var wordOfTheDayIntentHandlerStrategy = new WordOfTheDayIntentHandlerStrategy(wordService.Object, dictionaryService.Object, cacheService.Object);

            var alexaRequestHandlerStrategyFactory = new Mock <IAlexaRequestHandlerStrategyFactory>();

            alexaRequestHandlerStrategyFactory.Setup(x => x.CreateAlexaRequestHandlerStrategy(alexaRequestPayload)).Returns(wordOfTheDayIntentHandlerStrategy);


            var alexaRequestValidationService = new Mock <IAlexaRequestValidationService>();

            alexaRequestValidationService.Setup(x => x.ValidateAlexaRequest(alexaRequestPayload)).Returns(SpeechletRequestValidationResult.OK);


            var alexaRequestService = new AlexaRequestService(
                alexaRequestMapper.Object,
                alexaRequestPersistenceService.Object,
                alexaRequestHandlerStrategyFactory.Object,
                alexaRequestValidationService.Object
                );


            alexaRequestPayload.Request.Type        = "IntentRequest";
            alexaRequestPayload.Request.Intent.Name = "WordOfTheDayIntent";

            // act
            AlexaResponse alexaResponse = alexaRequestService.ProcessAlexaRequest(alexaRequestPayload);

            // assert
            Assert.IsTrue(alexaResponse.Response.OutputSpeech.Ssml.Contains("<speak><p>The Word is test</p>"));
        }