public void WhenBufferIsEmptyShouldTransitionToSavingCurrentFeed()
        {
            RecentEventsFeed feed = new RecentEventsFeedBuilder().WithNumberOfEntries(3).Build();

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

            Assert.IsInstanceOf(typeof (SavingRecentEventsFeed), end);
            Assert.AreEqual(feed, end.GetRecentEventsFeed());
        }
        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 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 WhenAnEntryIsAddedToTheFeedTheUpdatedElementOnTheFeedShouldBeUpdated()
        {
            RecentEventsFeed feed = new RecentEventsFeedBuilder().Build();
            DateTimeOffset initialDateTime = feed.GetSyndicationFeed().LastUpdatedTime;

            Thread.Sleep(1000);

            feed.AddEvent(new EventBuilder().Build());
            DateTimeOffset latestDateTime = feed.GetSyndicationFeed().LastUpdatedTime;

            Assert.Greater(latestDateTime, initialDateTime);
        }
        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 void ShouldTransitionToUpdatingRecentEventsFeedWithNewRecentEventsFeed()
        {
            IEnumerable<Event> events = new[] { new EventBuilder().Build() };
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(RecentEventsFeed.Quota).WithId(11).Build();

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

            Assert.IsInstanceOf(typeof (UpdatingRecentEventsFeed), end);

            Assert.AreEqual(new Id(12), end.GetRecentEventsFeed().GetFeedMapping().GetId());
            Assert.AreEqual(1, end.GetEvents().Count());
        }
        public void ShouldSaveArchivedVersionOfRecentEventsFeedToArchiveDirectory()
        {
            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            Assert.False(fileSystem.FileExists(fileSystem.ArchiveDirectory, new FileName("11.atom")));

            IEnumerable<Event> events = new [] { new EventBuilder().Build()};
            RecentEventsFeed recentEventsFeed = new RecentEventsFeedBuilder().WithNumberOfEntries(RecentEventsFeed.Quota).WithId(11).Build();

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

            Assert.True(fileSystem.FileExists(fileSystem.ArchiveDirectory, new FileName("11.atom")));
        }
        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 ShouldCreateSyndicationFeed()
        {
            DateTimeOffset now = DateTime.Now;

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

            Assert.True(new Regex(@"urn:uuid:\w{8}-\w{4}-\w{4}-\w{4}-\w{12}").IsMatch(syndicationFeed.Id));
            Assert.AreEqual("Restbucks products and promotions", syndicationFeed.Title.Text);
            Assert.AreEqual("Product Catalog", syndicationFeed.Authors.First().Name);
            Assert.AreEqual("Product Catalog", syndicationFeed.Generator);
            Assert.LessOrEqual(syndicationFeed.LastUpdatedTime.Subtract(now), new TimeSpan(0, 0, 2));
            Assert.AreEqual("http://restbucks.com/product-catalog/notifications/?page=2", syndicationFeed.GetViaLink().Uri.AbsoluteUri);
            Assert.AreEqual("http://restbucks.com/product-catalog/notifications/?page=1", syndicationFeed.GetPrevArchiveLink().Uri.AbsoluteUri);
            Assert.AreEqual("http://restbucks.com/product-catalog/notifications/recent", syndicationFeed.GetSelfLink().Uri.AbsoluteUri);
        }
        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 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 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());
        }