public void ShouldSaveRecentEventsFeedToCurrentDirectory()
        {
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(RecentEventsFeed.Quota - 1).WithId(11).Build();

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            Assert.AreEqual(0, fileSystem.FileCount(fileSystem.CurrentDirectory));

            ITask start = new SavingRecentEventsFeed(recentEventsFeed);
            start.Execute(fileSystem, new EventBufferBuilder().Build(), new FeedBuilder(SampleLinks.Instance), (args => { }));

            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.CurrentDirectory));
        }
        public void ShouldSaveFeedToCurrentDirectory()
        {
            InMemoryFileSystem fileSystem = new InMemoryFileSystem();

            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithId(2).WithPreviousId(1).WithUris(SampleLinks.Instance).Build();

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

            recentEventsFeed.Save(fileSystem);

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

            SyndicationFeed feed = recentEventsFeed.GetSyndicationFeed();
            SyndicationFeed rehydratedFeed = SyndicationFeed.Load(fileSystem.CurrentDirectory.GetXmlReader(recentEventsFeed.GetFeedMapping().GetFileName()));

            Assert.AreEqual(feed.Id, rehydratedFeed.Id);
            Assert.AreEqual(feed.Items.Count(), rehydratedFeed.Items.Count());
            Assert.AreEqual(feed.GetSelfLink().GetAbsoluteUri(), rehydratedFeed.GetSelfLink().GetAbsoluteUri());
            Assert.AreEqual(feed.GetViaLink().GetAbsoluteUri(), rehydratedFeed.GetViaLink().GetAbsoluteUri());
            Assert.AreEqual(feed.GetPrevArchiveLink().GetAbsoluteUri(), rehydratedFeed.GetPrevArchiveLink().GetAbsoluteUri());
        }
        public void IfFeedFillsExactlyToQuotaShouldNotBeArchived()
        {
            FileName tempFileName = null;
            FeedBuilder feedBuilder = new FeedBuilder(SampleLinks.Instance);

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

            Repeat.Times(3).Action(() => buffer.Add(new 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(0, fileSystem.FileCount(fileSystem.ArchiveDirectory));

            RecentEventsFeed rehydratedFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(3, rehydratedFeed.GetSyndicationFeed().Items.Count());
        }
        public void ShouldSaveFeedToArchiveDirectory()
        {
            InMemoryFileSystem fileSystem = new InMemoryFileSystem();

            RecentEventsFeed recentEvents = new RecentEventsFeedBuilder().WithId(2).WithPreviousId(1).WithNumberOfEntries(5).Build();
            ArchiveFeed archive = recentEvents.CreateArchiveFeed(recentEvents.CreateNextFeed());

            Assert.AreEqual(0, fileSystem.FileCount(fileSystem.ArchiveDirectory));

            archive.Save(fileSystem);

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

            SyndicationFeed feed = archive.GetSyndicationFeed();

            FileName archiveFileName = recentEvents.GetFeedMapping().WithArchiveFileName().FileName;
            SyndicationFeed rehydrated = SyndicationFeed.Load(fileSystem.ArchiveDirectory.GetXmlReader(archiveFileName));

            Assert.AreEqual(feed.Id, rehydrated.Id);
            Assert.AreEqual(feed.Items.Count(), rehydrated.Items.Count());
            Assert.AreEqual(feed.GetSelfLink().GetAbsoluteUri(), rehydrated.GetSelfLink().GetAbsoluteUri());
            Assert.AreEqual(feed.GetPrevArchiveLink().GetAbsoluteUri(), rehydrated.GetPrevArchiveLink().GetAbsoluteUri());
            Assert.AreEqual(feed.GetNextArchiveLink().GetAbsoluteUri(), rehydrated.GetNextArchiveLink().GetAbsoluteUri());
        }
        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 WhenThereAreNoOutstandingEventsShouldRetainTheCurrentTempFileName()
        {
            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();
            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));

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

            timer.Fire();

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

            timer.Fire();

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

            timer.Fire();

            Assert.AreEqual(2, fileSystem.FileCount(fileSystem.CurrentDirectory));
        }
        public void WhenThereAreNoOutstandingEventsShouldNotWriteFeedOrRaiseEvent()
        {
            int eventCount = 0;

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

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

            timer.Fire();

            Assert.AreEqual(0, eventCount);
            Assert.AreEqual(0, fileSystem.FileCount(fileSystem.CurrentDirectory));
        }
        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);
        }