public async Task GetScheduleAsync_When_Second_Call_And_Expired_Then_Returns_New_Schedule(TimeSpan addToCurrentExpiredDate)
        {
            IHdoScheduleProvider innerProvider = Substitute.For <IHdoScheduleProvider>();
            var firstHdoSchedule           = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());
            var newScheduleAfterExpiration = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            innerProvider.GetScheduleAsync(Arg.Any <IDictionary <string, string> >())
            .Returns(firstHdoSchedule, newScheduleAfterExpiration);
            var validFor = TimeSpan.FromHours(1);
            var now      = DateTime.Now;

            var         timeAfterExpiration = now + validFor + addToCurrentExpiredDate;
            var         isFirstCall         = true;
            HdoSchedule hdoSchedule;

            using (var cachedHdoProvider = new CachedHdoProvider(innerProvider, validFor, () => isFirstCall ? now : timeAfterExpiration))
            {
                var _ = await cachedHdoProvider.GetScheduleAsync(new Dictionary <string, string>());

                isFirstCall = false;

                hdoSchedule = await cachedHdoProvider.GetScheduleAsync(new Dictionary <string, string>());
            }

            Assert.AreSame(newScheduleAfterExpiration, hdoSchedule);
        }
Beispiel #2
0
 public void Ctor_When_Provider_Name_Is_Invalid_Then_Throws_ArgumentException(string invalidProviderName)
 {
     Assert.Catch <ArgumentException>(() =>
     {
         var _ = new HdoSchedule(invalidProviderName, Enumerable.Empty <HdoScheduleIntervalDefinition>());
     });
 }
        public async Task GetScheduleAsync_When_Second_Call_And_Not_Expired_Then_Returns_Same_Schedule()
        {
            IHdoScheduleProvider innerProvider = Substitute.For <IHdoScheduleProvider>();
            var expectedHdoSchedule            = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            innerProvider.GetScheduleAsync(Arg.Any <IDictionary <string, string> >())
            .Returns(expectedHdoSchedule);
            var         validFor       = TimeSpan.FromHours(1);
            var         now            = DateTime.Now;
            var         secondCallTime = now.AddSeconds(1);
            var         isFirstCall    = true;
            HdoSchedule hdoSchedule2;

            using (var cachedHdoProvider = new CachedHdoProvider(innerProvider, validFor, () => isFirstCall
                                                             ? now
                                                             : secondCallTime))
            {
                var _ = await cachedHdoProvider.GetScheduleAsync(new Dictionary <string, string>());

                isFirstCall = false;

                hdoSchedule2 = await cachedHdoProvider.GetScheduleAsync(new Dictionary <string, string>());
            }

            Assert.AreSame(expectedHdoSchedule, hdoSchedule2);
        }
Beispiel #4
0
        public void IsHdoActive_When_Empty_Schedule_Then_Returns_False()
        {
            var schedule = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            var isHdoActive = schedule.IsHdoActive();

            Assert.IsFalse(isHdoActive);
        }
Beispiel #5
0
        public void ToString_When_Called_Then_Result_Is_Not_Null_Or_Empty()
        {
            var schedule = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            var scheduleString = schedule.ToString();

            Assert.That(scheduleString, Is.Not.Null.Or.Empty);
        }
Beispiel #6
0
        public void IsHdoTime_When_Empty_Schedule_Then_Returns_False()
        {
            var toCheckDate = new DateTime(2020, 12, 12, 10, 20, 00);
            var schedule    = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            var isHdoTime = schedule.IsHdoTime(toCheckDate);

            Assert.IsFalse(isHdoTime);
        }
Beispiel #7
0
        public void Equals_When_Different_Instance_Then_Returns_False()
        {
            var schedule1 = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());
            var schedule2 = new HdoSchedule(TEST_HDO_PROVIDER_NAME + "diff", Enumerable.Empty <HdoScheduleIntervalDefinition>());

            bool areEqual = schedule1.Equals(schedule2);

            Assert.IsFalse(areEqual);
        }
Beispiel #8
0
        public void Equals_When_Same_Instances_Then_Returns_True()
        {
            var schedule1 = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());
            var schedule2 = new HdoSchedule(TEST_HDO_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            bool areEqual = schedule1.Equals(schedule2);

            Assert.IsTrue(areEqual);
        }
Beispiel #9
0
        public void Ctor_When_ScheduleIntervalCollection_Is_Null_Then_Throws_ArgumentNullException()
        {
            IEnumerable <HdoScheduleIntervalDefinition> nullScheduleIntervals = null;

            Assert.Catch <ArgumentNullException>(() =>
            {
                var _ = new HdoSchedule(TEST_HDO_PROVIDER_NAME, nullScheduleIntervals);
            });
        }
Beispiel #10
0
        public async Task GetScheduleAsync_When_Invalid_Json_Then_Returns_Empty_Schedule()
        {
            var expectedEmptyCezSchedule = new HdoSchedule(nameof(CezHdoProvider), Enumerable.Empty <HdoScheduleIntervalDefinition>());
            var invalidJson = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory,
                                                            TEST_DATA_DIRECTORY, EMPTY_DATA_RESPONSE_CEZ_JSON));
            HdoSchedule schedule;

            using (var provider = new CezHdoProvider(new SimpleHttpClientStub(invalidJson), CezHdoProvider.DEFAULT_HDO_API_URL))
            {
                schedule = await provider.GetScheduleAsync(new Dictionary <string, string> {
                    [CezHdoProvider.HDO_CODE_KEY] = TEST_HDO_CODE, [CezHdoProvider.AREA_KEY] = CezRegion.stred.ToString()
                });
            }

            Assert.AreEqual(expectedEmptyCezSchedule, schedule);
        }
        public async Task GetScheduleAsync_When_Using_Different_Args_Then_Returns_Different_Schedule()
        {
            IHdoScheduleProvider innerProvider = Substitute.For <IHdoScheduleProvider>();
            var expectedHdoSchedule            = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());
            var expectedHdoSchedule2           = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            var firstDictionary = new Dictionary <string, string>
            {
                ["key1"] = "value1"
            };

            var secondDictionary = new Dictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2"
            };

            innerProvider.GetScheduleAsync(firstDictionary)
            .Returns(expectedHdoSchedule);

            innerProvider.GetScheduleAsync(secondDictionary)
            .Returns(expectedHdoSchedule2);

            var         validFor = TimeSpan.FromHours(1);
            var         now      = DateTime.Now;
            HdoSchedule hdoSchedule;
            HdoSchedule hdoSchedule2;
            HdoSchedule secondCallHdoSchedule;
            HdoSchedule secondCallHdoSchedule2;

            using (var cachedHdoProvider = new CachedHdoProvider(innerProvider, validFor))
            {
                hdoSchedule = await cachedHdoProvider.GetScheduleAsync(firstDictionary);

                hdoSchedule2 = await cachedHdoProvider.GetScheduleAsync(secondDictionary);

                secondCallHdoSchedule = await cachedHdoProvider.GetScheduleAsync(firstDictionary);

                secondCallHdoSchedule2 = await cachedHdoProvider.GetScheduleAsync(secondDictionary);
            }

            Assert.AreSame(expectedHdoSchedule, hdoSchedule);
            Assert.AreSame(expectedHdoSchedule2, hdoSchedule2);
            Assert.AreSame(expectedHdoSchedule, secondCallHdoSchedule);
            Assert.AreSame(expectedHdoSchedule2, secondCallHdoSchedule2);
        }
        public async Task GetScheduleAsync_When_First_Call_Then_Returns_Data_From_Underlying_ProviderAsync()
        {
            IHdoScheduleProvider innerProvider = Substitute.For <IHdoScheduleProvider>();
            var expectedHdoSchedule            = new HdoSchedule(TEST_PROVIDER_NAME, Enumerable.Empty <HdoScheduleIntervalDefinition>());

            innerProvider.GetScheduleAsync(Arg.Any <IDictionary <string, string> >())
            .Returns(expectedHdoSchedule);
            var         validFor = TimeSpan.FromHours(1);
            var         now      = DateTime.Now;
            HdoSchedule hdoSchedule;

            using (var cachedHdoProvider = new CachedHdoProvider(innerProvider, validFor, () => now))
            {
                hdoSchedule = await cachedHdoProvider.GetScheduleAsync(new Dictionary <string, string>());
            }

            Assert.AreSame(expectedHdoSchedule, hdoSchedule);
        }
Beispiel #13
0
        private HdoSchedule parseJsonToHdoSchedule(string scheduleJson)
        {
            var cezRoot = JObject.Parse(scheduleJson);
            var data    = cezRoot["data"];

            if (data == null)
            {
                throw new HdoException(UNEXPECTED_JSON_ERROR);
            }

            var schedule = new HdoSchedule(nameof(CezHdoProvider),
                                           data.Children().Select(plan =>
                                                                  new HdoScheduleIntervalDefinition(
                                                                      Enumerable.Range(1, 10)
                                                                      .Select(timeIntervalIndex =>
            {
                var(beginHour, beginMinute) = parseHourAndMinute((string)plan[$"CAS_ZAP_{timeIntervalIndex}"]);
                if (beginHour == INVALID_TIME_PART)
                {
                    return(null);
                }

                var(endHour, endMinute) = parseHourAndMinute((string)plan[$"CAS_VYP_{timeIntervalIndex}"]);
                Debug.Assert(endHour != INVALID_TIME_PART);
                return(new HdoScheduleIntervalItem(beginHour,
                                                   beginMinute,
                                                   endHour,
                                                   endMinute));
            })
                                                                      .Where(item => item != null)
                                                                      .OrderBy(item => item.BeginHour)
                                                                      .ToArray(),
                                                                      parseWeekDays((string)plan["PLATNOST"]),
                                                                      getTimeFunc: null,
                                                                      plan.ToObject <IDictionary <string, object> >()))
                                           .ToArray()
                                           );

            schedule.AdditionalValues.Add(CEZ_FULL_JSON_RESPONSE_KEY, scheduleJson);
            schedule.AdditionalValues.Add(CEZ_FULL_OBJECT_MODEL_EQUIVALENT_TO_JSON_KEY, JsonConvert.DeserializeObject <CezJsonRoot>(scheduleJson));

            return(schedule);
        }