public async Task Throw500WhenSendingToNutritionTopicFails()
        {
            // Arrange
            var nutrition = new mdl.Nutrition
            {
                NutritionDate = "2019-12-31"
            };
            var foodResponse = new FoodResponseObject
            {
            };

            _mockDateValidator.Setup(x => x.IsNutritionDateValid(It.IsAny <string>())).Returns(true);
            _mockFitbitApiService.Setup(x => x.GetFoodLogs(It.IsAny <string>())).ReturnsAsync(foodResponse);
            _mockMapper.Setup(x => x.Map(It.IsAny <FoodResponseObject>(), It.IsAny <mdl.Nutrition>())).Verifiable();
            _mockServiceBusHelpers.Setup(x => x.SendMessageToTopic(It.IsAny <string>(), It.IsAny <mdl.Nutrition>())).ThrowsAsync(new Exception());

            // Act
            var response = await _func.Run(_mockHttpRequest.Object, _mockLogger.Object, nutrition.NutritionDate);

            // Assert
            Assert.Equal(typeof(StatusCodeResult), response.GetType());
            var responseAsStatusCodeResult = (StatusCodeResult)response;

            Assert.Equal(500, responseAsStatusCodeResult.StatusCode);
            _mockServiceBusHelpers.Verify(x => x.SendMessageToQueue(It.IsAny <string>(), It.IsAny <Exception>()), Times.Once);
        }
        public async Task Run([TimerTrigger("0 20 5 * * *")] TimerInfo myTimer, ILogger log)
        {
            try
            {
                log.LogInformation($"{nameof(GetDailyFoodLog)} executed at: {DateTime.Now}");
                var dateParameter = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                log.LogInformation($"Attempting to retrieve Food Log for {dateParameter}");
                var foodResponse = await _fitbitApiService.GetFoodLogs(dateParameter);

                log.LogInformation("Mapping API response to Nutrition object");
                var nutrition = new mdl.Nutrition();
                nutrition.NutritionDate = dateParameter;
                _mapper.Map(foodResponse, nutrition);

                log.LogInformation("Sending mapped Nutrition Log to Service Bus");
                await _serviceBusHelpers.SendMessageToTopic(_configuration["NutritionTopic"], nutrition);
            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown in {nameof(GetDailyFoodLog)}: {ex.Message}");
                await _serviceBusHelpers.SendMessageToQueue(_configuration["ExceptionQueue"], ex);

                throw ex;
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "FoodLog/{date}")] HttpRequest req,
            ILogger log,
            string date)
        {
            IActionResult result;

            try
            {
                bool isDateValid = _dateValidator.IsNutritionDateValid(date);
                if (isDateValid == false)
                {
                    result = new BadRequestResult();
                    return(result);
                }

                log.LogInformation($"Attempting to manually retrieve Food Log for {date}");
                var foodResponse = await _fitbitApiService.GetFoodLogs(date);

                if (foodResponse == null)
                {
                    result = new NotFoundResult();
                    return(result);
                }

                log.LogInformation("Mapping API Response to Nutrition object");
                var nutrition = new mdl.Nutrition();
                nutrition.NutritionDate = date;
                _mapper.Map(foodResponse, nutrition);

                log.LogInformation("Sending mapped Nutrition Log to Service Bus");
                await _serviceBusHelpers.SendMessageToTopic(_configuration["NutritionTopic"], nutrition);

                result = new OkResult();
            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown in {nameof(RetrieveManualFoodLog)}: {ex.Message}");
                await _serviceBusHelpers.SendMessageToQueue(_configuration["ExceptionQueue"], ex);

                result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }

            return(result);
        }
        public async Task ReturnNotFoundResultWhenNoFoodResponseIsFound()
        {
            // Arrange
            var nutrition = new mdl.Nutrition
            {
                NutritionDate = "2019-12-31"
            };

            _mockDateValidator.Setup(x => x.IsNutritionDateValid(It.IsAny <string>())).Returns(true);
            _mockFitbitApiService.Setup(x => x.GetFoodLogs(It.IsAny <string>())).Returns(Task.FromResult <FoodResponseObject>(null));

            // Act
            var response = await _func.Run(_mockHttpRequest.Object, _mockLogger.Object, nutrition.NutritionDate);

            // Assert
            Assert.Equal(typeof(NotFoundResult), response.GetType());
            var responseAsStatusCodeResult = (StatusCodeResult)response;

            Assert.Equal(404, responseAsStatusCodeResult.StatusCode);
            _mockServiceBusHelpers.Verify(x => x.SendMessageToTopic(It.IsAny <string>(), It.IsAny <mdl.Nutrition>()), Times.Never);
            _mockServiceBusHelpers.Verify(x => x.SendMessageToQueue(It.IsAny <string>(), It.IsAny <Exception>()), Times.Never);
        }
        public async Task ReturnOkObjectResultWhenMappedNutritionLogIsSentToNutritionTopic()
        {
            // Arrange
            var nutrition = new mdl.Nutrition
            {
                NutritionDate = "2019-12-31"
            };
            var foodResponse = new FoodResponseObject
            {
            };

            _mockDateValidator.Setup(x => x.IsNutritionDateValid(It.IsAny <string>())).Returns(true);
            _mockFitbitApiService.Setup(x => x.GetFoodLogs(It.IsAny <string>())).ReturnsAsync(foodResponse);
            _mockMapper.Setup(x => x.Map(It.IsAny <FoodResponseObject>(), It.IsAny <mdl.Nutrition>())).Verifiable();
            _mockServiceBusHelpers.Setup(x => x.SendMessageToTopic(It.IsAny <string>(), It.IsAny <mdl.Nutrition>())).Returns(Task.CompletedTask);

            // Act
            var response = await _func.Run(_mockHttpRequest.Object, _mockLogger.Object, nutrition.NutritionDate);

            // Assert
            Assert.Equal(typeof(OkResult), response.GetType());
            _mockServiceBusHelpers.Verify(x => x.SendMessageToTopic(It.IsAny <string>(), It.IsAny <mdl.Nutrition>()), Times.Once);
            _mockServiceBusHelpers.Verify(x => x.SendMessageToQueue(It.IsAny <string>(), It.IsAny <Exception>()), Times.Never);
        }