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 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 WhileThereAreEventsInTheListOfEventsAndTheFeedIsUnderQuotaShouldContinueAddingEventsToFeed()
        {
            EventBuilder builder = new EventBuilder();

            IEnumerable<Event> events = new[] {builder.Build(), builder.Build(), builder.Build()};
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(1).Build();

            ITask start = new UpdatingRecentEventsFeed(recentEventsFeed, events);
            ITask end = start.Execute(new InMemoryFileSystem(), new EventBufferBuilder().Build(), new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.IsInstanceOf(typeof (RequeryingEvents), end);
            Assert.AreEqual(4, recentEventsFeed.GetNumberOfEntries());
        }
        public EventBuffer Build()
        {
            EventBuilder eventBuilder = new EventBuilder();

            EventBuffer buffer = new EventBuffer();
            Repeat.Times(eventCount).Action(() => buffer.Add(eventBuilder.Build()));

            foreach (var evnt in events)
            {
                buffer.Add(evnt);
            }

            return buffer;
        }
        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")));
        }
        public void WhenTriggeredShouldAddBatchOfEventsToCurrentFeed()
        {
            FileName tempFileName = null;

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            EventBuilder eventBuilder = new EventBuilder();
            Repeat.Times(2).Action(() => buffer.Add(eventBuilder.Build()));

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

            timer.Fire();

            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.CurrentDirectory));

            RecentEventsFeed rehydratedFeed = new FeedBuilder(SampleLinks.Instance).LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(2, rehydratedFeed.GetNumberOfEntries());
        }
        public void WhenNumberOfEventsExceedsQuotaShouldArchiveFeedAndBeginAnotherOne()
        {
            FileName tempFileName = null;
            FeedBuilder feedBuilder = new FeedBuilder(SampleLinks.Instance);

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            EventBuilder eventBuilder = new EventBuilder();
            Repeat.Times(RecentEventsFeed.Quota + 1).Action(() => buffer.Add(eventBuilder.Build()));

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

            timer.Fire();

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

            ArchiveFeed archivedFeed = LoadArchiveFeed(fileSystem, new FileName("1"));
            Assert.AreEqual(10, archivedFeed.GetNumberOfEntries());
            Assert.AreEqual("?page=1", archivedFeed.GetSyndicationFeed().GetSelfLink().Uri.Query);
            Assert.AreEqual("?page=2", archivedFeed.GetSyndicationFeed().GetNextArchiveLink().Uri.Query);

            RecentEventsFeed recentEventsFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(1, recentEventsFeed.GetSyndicationFeed().Items.Count());
            Assert.AreEqual("?page=2", recentEventsFeed.GetSyndicationFeed().GetViaLink().Uri.Query);
        }