public async Task GenerateBankHolidaysAsync(
            [TimerTrigger("%BankHolidayGeneratorTrigger%")] TimerInfo timer,
            ExecutionContext context,
            ILogger logger)
        {
            if (timer == null)
            {
                throw new ArgumentNullException(nameof(timer));
            }
            try
            {
                logger.LogInformation($"Function {context.FunctionName} triggered");

                var stopwatch = Stopwatch.StartNew();
                var holidays  = await _calendarApiClient.GetBankHolidaysAsync();

                var createdRecords = await SaveHolidaysAsync(holidays);

                stopwatch.Stop();

                logger.LogInformation($"Function {context.FunctionName} finished processing\n" +
                                      $"\tRows saved: {createdRecords}\n" +
                                      $"\tTime taken: {stopwatch.ElapsedMilliseconds: #,###}ms");
            }
            catch (Exception e)
            {
                var errorMessage = $"Error loading Bank Holiday Data. Internal Error Message {e}";

                logger.LogError(errorMessage);

                await _functionLogRepository.CreateAsync(new FunctionLog
                {
                    ErrorMessage = errorMessage,
                    FunctionName = context.FunctionName,
                    RowNumber    = -1
                });

                throw;
            }
        }
        public When_ManualGenerateBankHolidays_Function_Http_Trigger_Is_Called()
        {
            var dto = new List <BankHolidayResultDto>
            {
                new BankHolidayResultDto
                {
                    Title = "August Summer Holiday",
                    Date  = DateTime.Parse("2019-08-26")
                },
                new BankHolidayResultDto
                {
                    Title = "New Year",
                    Date  = DateTime.Parse("2020-01-01")
                }
            };

            _calendarApiClient = Substitute.For <ICalendarApiClient>();
            _calendarApiClient.GetBankHolidaysAsync()
            .Returns(dto);

            var config = new MapperConfiguration(c => c.AddMaps(typeof(BankHolidayMapper).Assembly));
            var mapper = new Mapper(config);

            _bankHolidayBulkInsertRepository = Substitute.For <IBulkInsertRepository <BankHoliday> >();

            _functionLogRepository = Substitute.For <IRepository <FunctionLog> >();

            var httpContext = new DefaultHttpContext();
            var request     = httpContext.Request;

            request.Method = HttpMethod.Get.ToString();

            var bankHolidayGeneratorFunctions = new Functions.BankHolidayGenerator(_calendarApiClient, mapper, _bankHolidayBulkInsertRepository, _functionLogRepository);

            bankHolidayGeneratorFunctions.ManualGenerateBankHolidaysAsync(
                request,
                new ExecutionContext(),
                new NullLogger <Functions.BankHolidayGenerator>())
            .GetAwaiter().GetResult();
        }
        public When_GenerateBankHolidays_Function_Timer_Trigger_Fires()
        {
            var dto = new List <BankHolidayResultDto>
            {
                new BankHolidayResultDto
                {
                    Title = "August Summer Holiday",
                    Date  = DateTime.Parse("2019-08-26")
                },
                new BankHolidayResultDto
                {
                    Title = "New Year",
                    Date  = DateTime.Parse("2020-01-01")
                }
            };

            _calendarApiClient = Substitute.For <ICalendarApiClient>();
            _calendarApiClient.GetBankHolidaysAsync()
            .Returns(dto);

            var timerSchedule = Substitute.For <TimerSchedule>();

            var config = new MapperConfiguration(c => c.AddMaps(typeof(BankHolidayMapper).Assembly));
            var mapper = new Mapper(config);

            _functionLogRepository = Substitute.For <IRepository <FunctionLog> >();

            _bankHolidayBulkInsertRepository = Substitute.For <IBulkInsertRepository <BankHoliday> >();

            var bankHolidayGeneratorFunctions = new Functions.BankHolidayGenerator(_calendarApiClient, mapper, _bankHolidayBulkInsertRepository, _functionLogRepository);

            bankHolidayGeneratorFunctions.GenerateBankHolidaysAsync(
                new TimerInfo(timerSchedule, new ScheduleStatus()),
                new ExecutionContext(),
                new NullLogger <Functions.BankHolidayGenerator>())
            .GetAwaiter().GetResult();
        }