public void WhenNumberOfEventsInBufferExceedsBatchSizeShouldReturnBatchfulOfEvents()
        {
            EventBuffer buffer = new EventBufferBuilder().WithNumberOfEvents(QueryingEvents.BatchSize + 1).Build();

            IEnumerable<Event> events = buffer.Take(QueryingEvents.BatchSize);

            Assert.AreEqual(QueryingEvents.BatchSize, events.Count());
        }
        public void IfThereAreNoEventsInBufferShouldReturnZeroEvents()
        {
            EventBuffer buffer = new EventBufferBuilder().Build();

            IEnumerable<Event> events = buffer.Take(QueryingEvents.BatchSize);

            Assert.AreEqual(0, events.Count());
        }
        public void IfBufferIsEmptyShouldTransitionToTerminate()
        {
            EventBuffer buffer = new EventBufferBuilder().Build();

            ITask start = new QueryingEvents();
            ITask end = start.Execute(new InMemoryFileSystem(), buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (Terminate), end);
        }
        public void IfBufferContainsEventsShouldTransitionToIdentifyingCurrentFeed()
        {
            int eventCount = 2;
            EventBuffer buffer = new EventBufferBuilder().WithNumberOfEvents(eventCount).Build();

            ITask start = new QueryingEvents();
            ITask end = start.Execute(new InMemoryFileSystem(), buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (IdentifyingRecentEventsFeed), end);
            Assert.AreEqual(eventCount, end.GetEvents().Count());
        }
        public void CanAddAndTakeEvent()
        {
            Event evnt = new EventBuilder().Build();

            EventBuffer buffer = new EventBufferBuilder().Build();
            buffer.Add(evnt);

            IEnumerable<Event> events = buffer.Take(1);

            Assert.AreEqual(1, events.Count());
            Assert.AreSame(evnt, events.First());
        }
        public void WhenRecentEventsFeedHasReachedQuotaAndThereAreNoRemainingEventsShouldTransitionToRequeryingEvents()
        {
            EventBuffer buffer = new EventBufferBuilder().WithNumberOfEvents(1).Build();
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(RecentEventsFeed.Quota - 1).Build();

            ITask start = new UpdatingRecentEventsFeed(recentEventsFeed, buffer.Take(QueryingEvents.BatchSize));

            ITask end = start.Execute(new InMemoryFileSystem(), buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (RequeryingEvents), end);
            Assert.AreEqual(recentEventsFeed, end.GetRecentEventsFeed());
        }
        public void WhenNumberOfEventsInBufferIsLessThanBatchSizeShouldReturnAllEventsInRepositoryOldestFirst()
        {
            Event oldestEvent = new EventBuilder().Build();
            Event newestEvent = new EventBuilder().Build();

            EventBuffer buffer = new EventBufferBuilder().WithEvents(oldestEvent, newestEvent).Build();

            IEnumerable<Event> events = buffer.Take(QueryingEvents.BatchSize);

            Assert.AreEqual(2, events.Count());
            Assert.AreSame(oldestEvent, events.ElementAt(0));
            Assert.AreSame(newestEvent, events.ElementAt(1));
        }
        public void WhenBufferReturnsSomeEventsShouldTransitionToUpdatingCurrentFeed()
        {
            int eventCount = 2;
            EventBuffer buffer = new EventBufferBuilder().WithNumberOfEvents(eventCount).Build();

            RecentEventsFeed feed = new RecentEventsFeedBuilder().WithNumberOfEntries(3).Build();

            ITask start = new RequeryingEvents(feed);
            ITask end = start.Execute(new InMemoryFileSystem(), buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof(UpdatingRecentEventsFeed), end);
            Assert.AreEqual(feed, end.GetRecentEventsFeed());
            Assert.AreEqual(eventCount, end.GetEvents().Count());
        }
        public void IfRecentEventsFeedExistsOnFileSystemTransitionsToUpdatingRecentEventsFeed()
        {
            int eventCount = 2;

            IFileSystem fileSystem = new InMemoryFileSystem();
            SyndicationFeed feed = SyndicationFeeds.Current();

            using (XmlWriter writer = fileSystem.CurrentDirectory.GetXmlWriter(FileName.TempFileName()))
            {
                feed.SaveAsAtom10(writer);
            }

            EventBuffer buffer = new EventBufferBuilder().WithNumberOfEvents(eventCount).Build();

            ITask start = new IdentifyingRecentEventsFeed(buffer.Take(QueryingEvents.BatchSize));
            ITask end = start.Execute(fileSystem, buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (UpdatingRecentEventsFeed), end);
            Assert.AreEqual(feed.Id, end.GetRecentEventsFeed().GetSyndicationFeed().Id);
            Assert.AreEqual(2, end.GetEvents().Count());
        }
        public void WhenRecentEventsFeedHasReachedQuotaShouldTransitionToArchivingRecentEventsFeedWithRemainingEvents()
        {
            EventBuilder eventBuilder = new EventBuilder();

            Event event1 = eventBuilder.WithId(1).Build();
            Event event2 = eventBuilder.WithId(2).Build();
            Event event3 = eventBuilder.WithId(3).Build();

            EventBuffer buffer = new EventBufferBuilder().WithEvents(event1, event2, event3).Build();
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(RecentEventsFeed.Quota - 1).Build();

            ITask start = new UpdatingRecentEventsFeed(recentEventsFeed, buffer.Take(QueryingEvents.BatchSize));

            ITask end = start.Execute(new InMemoryFileSystem(), buffer, new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (ArchivingRecentEventsFeed), end);
            Assert.AreEqual(recentEventsFeed, end.GetRecentEventsFeed());
            Assert.AreEqual(2, end.GetEvents().Count());
            Assert.AreEqual(event2, end.GetEvents().ElementAt(0));
            Assert.AreEqual(event3, end.GetEvents().ElementAt(1));
        }
        public void ShouldRaiseEventEachTimeATempFileIsSavedToCurrentDirectory()
        {
            int eventCount = 0;

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBufferBuilder().Build();
            EventBuilder eventBuilder = new EventBuilder();

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => eventCount++);

            //Current will contain 1 - 1st version of 1 will be saved to current
            Repeat.Times(1).Action(() => buffer.Add(eventBuilder.Build()));

            timer.Fire();

            //Current will contain full feed - 2nd version of 1 will be saved to current
            Repeat.Times(RecentEventsFeed.Quota - 1).Action(() => buffer.Add(eventBuilder.Build()));

            timer.Fire();

            //Feed will be archived 3 times - 1st version of 4 will be saved to current
            Repeat.Times((RecentEventsFeed.Quota * 3) - 1).Action(() => buffer.Add(eventBuilder.Build()));

            timer.Fire();

            Assert.AreEqual(3, eventCount);

            Assert.AreEqual(3, fileSystem.FileCount(fileSystem.CurrentDirectory));
            Assert.AreEqual(3, fileSystem.FileCount(fileSystem.ArchiveDirectory));

            Assert.IsTrue(fileSystem.FileExists(fileSystem.ArchiveDirectory, new FileName("1")));
            Assert.IsTrue(fileSystem.FileExists(fileSystem.ArchiveDirectory, new FileName("2")));
            Assert.IsTrue(fileSystem.FileExists(fileSystem.ArchiveDirectory, new FileName("3")));
        }