Beispiel #1
3
        public void Should_get_a_messagge()
        {
            var contentType = "text/text";
            var payload = "Hello!";
            var message = new Message()
            {
                Id = Identity.Random(),
                TopicId = Identity.Random(),
                Payload = Encoding.ASCII.GetBytes(payload),
                UtcReceivedOn = DateTime.UtcNow
            };

            message.Headers.Add("Content-Type", new[] { contentType });

            var key = new MessageKey { MessageId = message.Id.Value, TopicId = message.TopicId };

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

            var client = new HttpClient(baseUri);
            var url = baseUri + message.Id.Value.ToString() + "/topic/" + message.TopicId;
            var result = client.Get(url);

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

            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual(payload, result.Content.ReadAsString());
            Assert.AreEqual(contentType, result.Content.Headers.ContentType.MediaType);
        }
Beispiel #2
0
        public bool Exist(MessageKey key, string filter)
        {
            Guard.Instance.ArgumentNotNull(() => key, key);

             var query = string.IsNullOrWhiteSpace(filter) ?
                 new QueryDocument() :
                 filter.ToQueryDocument();

             if (query.Contains(Constants.FieldNames.Id))
             {
                 if (query[Constants.FieldNames.Id] != key.MessageId.ToBson())
                 {
                     // Filter already has a different message's id. So the query will always return false.
                     return false;
                 }
             }
             else
             {
                 // The filter does not include the message's id. Then add it to que query.
                 query.Add(Constants.FieldNames.Id, key.MessageId.ToBson());
             }

             string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);
             return DB.GetCollection<Message>(collectionName).Exists(query);
        }
Beispiel #3
0
        static public Facade.Link ToLink(this Model.MessageKey from)
        {
            if (from == null)
            {
                return(null);
            }

            return(new Facade.Link(ResourceLocation.OfMessageByTopic(from).ToString(), Constants.Relationships.Message));
        }
Beispiel #4
0
        public void Should_get_a_messagge_by_topic()
        {
            var key = new MessageKey {MessageId = Identity.Random(), TopicId = Identity.Random()};

            messageKeysByTopic
                .Setup(r => r.Get(key.TopicId, null, null, null))
                .Returns(new[] {key});

            var client = new HttpClient(baseUri);
            var url = baseUri + "topic/" + key.TopicId;
            var result = client.Get(url);

            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
        public void WhenMessageDoesNotExist_MustReturnNull()
        {
            var col = base.mongoDb.GetCollection<Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));
            var msg = new Message
                          {
                              UtcReceivedOn = DateTime.UtcNow,
                              TopicId = topicId,
                              Payload = new byte[] { 1, 2, 3, 4, 5 }
                          };
            col.Save(msg);

            Assert.IsNotNull(msg.Id);

            var key = new MessageKey { MessageId = Identity.Random(12), TopicId = topicId };
            var query = new MessageByMessageKey(base.connectionString);
            var result = query.Get(key);
            Assert.IsNull(result);
        }
Beispiel #6
0
        public HttpResponseMessage Get(Identity topicId, Identity messageId, HttpRequestMessage request)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK, string.Empty);

            DoProcess(() =>
            {
                var key = new MessageKey { TopicId = topicId, MessageId = messageId };
                var message = messageByMessageKey.Get(key);
                if (message == null)
                {
                    response.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    PopulateHttpResponseMessage(ref response, message);
                }
            });

            return response;
        }
        public void WhenMessageExists_MustReturnMessage()
        {
            var col = base.mongoDb.GetCollection<Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));
            var msg = new Message
                          {
                              UtcReceivedOn = DateTime.UtcNow,
                              TopicId = topicId,
                              Payload = new byte[] { 1, 2, 3, 4, 5 }
                          };
            col.Save(msg);

            Assert.IsNotNull(msg.Id);

            var key = new MessageKey { MessageId = msg.Id.Value, TopicId = topicId };
            var query = new MessageByMessageKey(base.connectionString);
            var result = query.Get(key);
            Assert.IsNotNull(result);
            Assert.AreEqual(msg.Id, result.Id);
            Assert.AreEqual(msg.UtcReceivedOn.Date, result.UtcReceivedOn.Date);
            Assert.AreEqual(msg.TopicId, result.TopicId);
            CollectionAssert.AreEqual(msg.Payload, result.Payload);
        }
 public void MakeTransient(MessageKey key)
 {
     Entities.Remove(Entities.FirstOrDefault(e =>  e.TopicId == key.TopicId && e.Id == key.MessageId));
 }
Beispiel #9
0
 public Message Get(MessageKey key)
 {
     string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);
     return DB.GetCollection<Message>(collectionName).FindById(key.MessageId);
 }
Beispiel #10
0
 public bool Exist(MessageKey key)
 {
     string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);
     return DB.GetCollection<Message>(collectionName).Exists(key.MessageId);
 }
Beispiel #11
0
 public void MakeTransient(MessageKey key)
 {
     MongoCollection<Message> collection =
         DB.GetCollection<Message>(MongoDbConstants.GetCollectionNameForMessage(key.TopicId));
     collection.Remove(key.MessageId);
 }
Beispiel #12
0
        public static Uri OfMessageByTopic(MessageKey key)
        {
            Guard.Instance.ArgumentNotNull(()=>key, key);

            return OfMessageByTopic(key.TopicId, key.MessageId);
        }
Beispiel #13
0
        public static string LinkToMessage(MessageKey key)
        {
            Guard.Instance.ArgumentNotNull(()=>key, key);

            return LinkToMessage(key.TopicId, key.MessageId);
        }
Beispiel #14
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;
            }
        }