public void Given_RunAtTimeIsLaterThanDateTimeNow_Then_RightTimeSpanIsCalculated()
        {
            // Arrange
            var mockConfigHelper = new Mock<IConfigurationHelper>();
            mockConfigHelper.Setup<TimedServicesConfig>(x => x.GetSection<TimedServicesConfig>()).Returns(new TimedServicesConfig { });

            var mockLogger = new Mock<ILogger>();
            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(mlf => mlf.ProduceLogger(It.IsAny<string>())).Returns(mockLogger.Object);

            var timedService = new TimedServiceTestWrapper(mockConfigHelper.Object, mockLoggerFactory.Object);

            var inputDatetime = new DateTime(2014, 5, 4, 16, 0, 0);  // 4pm at 4/May/2014
            var expectedTimeSpan = new TimeSpan(5, 0, 0);

            // Act
            using (ShimsContext.Create())
            {
                System.Fakes.ShimDateTime.NowGet = () => new DateTime(2014, 5, 4, 11, 0, 0); // 11am at 4/May/2014
                var actualTimeSpan = timedService.CalculateRunAtTimeSpan(inputDatetime);

                // Assert
                Assert.Equal(expectedTimeSpan, actualTimeSpan);
            }
        }
        public void Given_ValidTimedServiceCommandClass_Then_TimeCommandInstanceCreatedSuccessfully()
        {
            var mockConfigureHelper = new Mock<IConfigurationHelper>();
            mockConfigureHelper.Setup<TimedServicesConfig>(mch => mch.GetSection<TimedServicesConfig>()).Returns(new TimedServicesConfig()); // empty config is fine cos I only need the time service as a shell.

            var mockLogger = new Mock<ILogger>();
            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(mlf => mlf.ProduceLogger(It.IsAny<string>())).Returns(mockLogger.Object);

            var timedServiceTestWrapper = new TimedServiceTestWrapper(mockConfigureHelper.Object, mockLoggerFactory.Object);

            var commandConfig = new TimedServiceCommandConfig{
                Name = "Test",
                Type = "TimedService.UnitTests.TimedServiceCommands.Test, TimedService.UnitTests",
                RunAt = DateTime.Today.AddHours(11) // 11 am today.
            };

            var expected = (new TimedServiceCommands.Test()).GetType();

            // Act
            var actual = timedServiceTestWrapper.GetTimedCommandInstance(commandConfig).GetType();

            // Assert
            Assert.Equal(expected, actual);

            //"TimedService.TimedServiceCommands.TestTimedService, TimedService"
        }
        public void Given_MultipleTimedServiceCommandsInTimedServicesConfig_Then_MultipleThreadingTimersCreated()
        {
            // Arrange
            var mockConfigHelper = new Mock<IConfigurationHelper>();
            mockConfigHelper.Setup<TimedServicesConfig>(x => x.GetSection<TimedServicesConfig>(It.IsAny<string>())).Returns(new TimedServicesConfig
            {
                TimedServiceCommandConfigs = new TimedServiceCommandConfig[] {
                    new TimedServiceCommandConfig{
                            Name = "TestCommand2",
                            Type = "KelvinSoft.TestLib",
                            RunAt = DateTime.Today.AddHours(11) //11 am today
                    },
                    new TimedServiceCommandConfig{
                            Name = "TestCommand3",
                            Type = "KelvinSoft.TestLib",
                            RunEvery = DateTime.Today.AddHours(17) //every 17 hours
                    }
                }
            });

            var mockLogger = new Mock<ILogger>();
            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(mlf => mlf.ProduceLogger(It.IsAny<string>())).Returns(mockLogger.Object);

            var timedService = new TimedServiceTestWrapper(mockConfigHelper.Object, mockLoggerFactory.Object);
            var expected = timedService.Timers.Count + 2; // The Timer is a static property of the dictionary type. Therefore it is a global count.

            // Act
            timedService.TriggerOnStart(new string[] { });

            // Assert
            var actual = timedService.Timers.Count;//TimedServiceTestWrapper._timers.Count; timedService.
            Assert.Equal(expected, actual);
        }
        public void Given_MultipleTimedServiceInstances_Then_ShareTheSameTimersPropertyObject()
        {
            // Arrange
            var mockConfigHelper = new Mock<IConfigurationHelper>();
            mockConfigHelper.Setup<TimedServicesConfig>(x => x.GetSection<TimedServicesConfig>()).Returns(new TimedServicesConfig { });

            var mockLogger = new Mock<ILogger>();
            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(mlf => mlf.ProduceLogger(It.IsAny<string>())).Returns(mockLogger.Object);

            // Act
            var timers1 = new TimedServiceTestWrapper(mockConfigHelper.Object, mockLoggerFactory.Object).Timers;
            var timers2 = new TimedServiceTestWrapper(mockConfigHelper.Object, mockLoggerFactory.Object).Timers;

            // Assert
            Assert.Same(timers1, timers2);
        }
        public void Given_RunEveryTime_Then_CorrectRunEveryTimeSpanIsCalculated()
        {
            // Arrange
            var mockConfigureHelper = new Mock<IConfigurationHelper>();
            mockConfigureHelper.Setup<TimedServicesConfig>(mch => mch.GetSection<TimedServicesConfig>()).Returns(new TimedServicesConfig());

            var mockLogger = new Mock<ILogger>();
            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(mlf => mlf.ProduceLogger(It.IsAny<string>())).Returns(mockLogger.Object);

            var timedServiceTestWrapper = new TimedServiceTestWrapper(mockConfigureHelper.Object, mockLoggerFactory.Object);

            var expectedResult = new TimeSpan(hours: 4, minutes: 25, seconds: 30);

            //Act
            var actualResult = timedServiceTestWrapper.CalculateRunEveryTimeSpan(new DateTime(year: DateTime.Today.Year,
                                                                            month: DateTime.Today.Month,
                                                                            day: DateTime.Today.Day,
                                                                            hour: 4,
                                                                            minute: 25,
                                                                            second: 30));

            Assert.Equal(expectedResult, actualResult);
        }