Example #1
0
        public Feed Execute(Identity topicId)
        {
            Feed workingFeed;
            lock (lck)
            {
                workingFeed = feeds.Find(Query.EQ("TopicId", topicId.ToBson()))
                    .SetSortOrder(SortBy.Descending("Updated"))
                    .SetLimit(1).FirstOrDefault();

                if (workingFeed == null || workingFeed.Entries.Count >= 10)
                {
                    var newFeed = new Feed
                                 {
                                     TopicId = topicId,
                                 };

                    if(workingFeed != null)
                    {
                        newFeed.PreviousFeed = workingFeed.Id;
                        feeds.Insert(newFeed);
                        workingFeed.NextFeed = newFeed.Id;
                        feeds.Save(workingFeed);
                    }else
                    {
                        feeds.Insert(newFeed);
                    }
                    workingFeed = newFeed;
                }
            }
            return workingFeed;
        }
        public void WhenCurrentFeedIsFull_ThenReturnANewFeed()
        {
            var getWorkingFeedForTopic = new GetWorkingFeedForTopic(connectionString);
            var oldFeed = new Feed
            {
                Updated = DateTime.UtcNow.AddMinutes(-2),
                TopicId = topic.Id.Value
            };
            var currentFeed = new Feed
            {
                Updated = DateTime.UtcNow.AddMinutes(-1),
                TopicId = topic.Id.Value,
                Entries = Enumerable.Range(0, 10)
                                    .Select(i => new FeedEntry{MessageId = Identity.Random(12)})
                                    .ToList()
            };
            mongoDb.GetCollectionByType<Feed>()
                .InsertMany(oldFeed, currentFeed);

            var read = getWorkingFeedForTopic.Execute(topic.Id.Value);

            read.Should().Not.Be.EqualTo(currentFeed);
            read.PreviousFeed.ToString().Should().Be.EqualTo(currentFeed.Id.ToString());

            mongoDb.GetCollectionByType<Feed>()
                .FindById(currentFeed.Id.Value)
                .NextFeed.ToString().Should().Be.EqualTo(read.Id.ToString());
        }
        public void WhenThereIsNotAchievedFeed_ThenCreateANewOneWithMessage()
        {
            var topicId = Identity.Random();
            var feed = new Feed { TopicId = topicId };
            var query = Mock.Of<IGetWorkingFeedForTopic>(q => q.Execute(topicId) == feed);

            var repository = new StubRepository<Feed>();

            var handler = CreateHandler(repository, query);
            var message = new Message  { Id = Identity.Random(), TopicId = topicId};

            handler.Handle(new NewMessageEvent { Message = message });

            repository.Updates
                .Satisfy(es => es.Count == 1
                            && es.First() == feed
                            && es.First().Entries.Any(e => e.MessageId == message.Id));
        }
        public void WhenCurrentFeedIsNotFull_ThenReturnThatFeed()
        {
            var getWorkingFeedForTopic = new GetWorkingFeedForTopic(connectionString);
            var oldFeed = new Feed
            {
                Updated = DateTime.UtcNow.AddMinutes(-2),
                TopicId = topic.Id.Value
            };
            var currentFeed = new Feed
                           {
                               Updated = DateTime.UtcNow.AddMinutes(-1),
                               TopicId = topic.Id.Value
                           };
            mongoDb.GetCollectionByType<Feed>()
                .InsertMany(oldFeed, currentFeed);

            var read = getWorkingFeedForTopic.Execute(topic.Id.Value);

            read.Should().Be.EqualTo(currentFeed);
        }
Example #5
0
        private SyndicationFeed CreateSyndicationFromFeed(Identity topicId, Feed currentFeed)
        {
            var topic = entityById.Get<Topic>(topicId);
            var feed = new SyndicationFeed(topic.Name, topic.Description, null)
                           {
                               Items = currentFeed.Entries.Select(e => MapEntryToSyndicationItem(topicId, e)).ToList(),
                               LastUpdatedTime = currentFeed.Updated,
                               Id = currentFeed.Id.ToString()
                           };

            feed.Links.Add(new SyndicationLink(ResourceLocation.OfCurrentTopicFeed(topicId))
                                {
                                    RelationshipType = "current",
                                    MediaType = "application/atom+xml"
                                });

            feed.Links.Add(new SyndicationLink(ResourceLocation.OfTopicFeed(topicId, currentFeed.Id.Value))
                               {
                                   RelationshipType = "self",
                                   MediaType = "application/atom+xml"
                               });

            if (currentFeed.PreviousFeed.HasValue)
            {
                feed.Links.Add(new SyndicationLink(ResourceLocation.OfTopicFeed(topicId, currentFeed.PreviousFeed.Value))
                                   {
                                       RelationshipType = "prev-archive",
                                       MediaType = "application/atom+xml"
                                   });
            }

            if (currentFeed.NextFeed.HasValue)
            {
                feed.Links.Add(new SyndicationLink(ResourceLocation.OfTopicFeed(topicId, currentFeed.NextFeed.Value))
                                   {
                                       RelationshipType = "next-archive",
                                       MediaType = "application/atom+xml"
                                   });
            }
            return feed;
        }
Example #6
0
        private SyndicationFeed GetFeedForMessage(Message message)
        {
            var messageId = message.Id.Value;
            var topicId = message.TopicId;

            var feedEntry = new FeedEntry() { MessageId = messageId, TimeStamp = DateTime.UtcNow };
            var feed = new Feed()
            {
                Entries = new List<FeedEntry>(new[] { feedEntry }),
                Id = Identity.Random(),
                TopicId = topicId,
                Updated = DateTime.UtcNow
            };

            var topic = new Topic()
            {
                Id = topicId,
                Name = "Topic Name",
                Description = "Topic Description",
                GroupId = Identity.Random()
            };

            var key = new MessageKey { MessageId = messageId, TopicId = topicId };

            messageByMessageKey
                .Setup(r => r.Get(It.Is<MessageKey>(k => k.TopicId == key.TopicId && k.MessageId == key.MessageId)))
                .Returns(message);

            getWorkingFeedForTopic
                .Setup(r => r.Execute(topicId))
                .Returns(feed);

            entityById
                .Setup(r => r.Get<Topic>(topicId))
                .Returns(topic);

            var client = new HttpClient(baseUri);

            var response = client.Get(topicId.ToString());
            var formatter = new Atom10FeedFormatter();

            using (var rdr = XmlReader.Create(response.Content.ContentReadStream))
            {
                formatter.ReadFrom(rdr);
                return formatter.Feed;
            }
        }