public override void FixtureSetUp()
        {
            base.FixtureSetUp();

            var topics = mongoDb.GetCollectionByType<Topic>();
            var subscriptions = mongoDb.GetCollectionByType<Subscription>();
            var groups = mongoDb.GetCollectionByType<Group>();

            var group = new Group();
            groups.Insert(group);

            tc =  new Topic { Description = "T1", Name = "Aaaa", GroupId = group.Id.Value };
            tc2 = new Topic { Description = "T2", Name = "Aaaa", GroupId = group.Id.Value };
            topics.Insert(tc);
            topics.Insert(tc2);

            subs1 = new Subscription
                            {
                                TargetKind = TargetKind.Topic,
                                TargetId = tc.Id ,
                                Callback = new Callback()
                            };

            subs2 = new Subscription
                            {
                                TargetKind = TargetKind.Topic,
                                TargetId = tc2.Id,
                                Callback = new Callback ()
                            };
            subscriptions.InsertBatch(new[] { subs1, subs2 });
        }
 public void WhenEntitiyDoesNotExist_ThenThrowValidationException()
 {
     var command = CreateCommand(entityById: Mock.Of<IEntityById>(eb => eb.Exist<Subscription>(It.IsAny<Identity>()) == false));
     var subscription = new Subscription { Id = Identity.Random()};
     command.Executing(c => c.Execute(subscription))
         .Throws<EntityNotFoundException>();
 }
        public void WhenAllIsOk_ThenInsert()
        {
            var stubRepository = new StubRepository<Subscription>();
            var command = CreateCommand(repository: stubRepository);
            var targetId = Identity.Random();
            var subscription = new Subscription { TargetId = targetId };
            command.Execute(subscription);

            stubRepository.Entities.Should().Contain(subscription);
        }
        public void WhenAllIsOk_ThenUpdate()
        {
            var repository = new StubRepository<Subscription>();
            var command = CreateCommand(repository: repository);
            var subscription = new Subscription { Id = Identity.Random() };

            command.Execute(subscription);

            repository.Updates.Should().Contain(subscription);
        }
        public void WhenExistASubscriptionToATopic_MustReturnTopicsMessages()
        {
            var topicId = Identity.Random();
            var keys = new[] { new MessageKey { MessageId = Identity.Random(), TopicId = topicId } };

            var subscriptionId = Identity.Random();
            var subscription = new Subscription { Id = subscriptionId, TargetId = topicId, TargetKind = TargetKind.Topic };

            var query = CreateQuery(Mock.Of<IEntityById>(q => q.Get<Subscription>(subscriptionId) == subscription),
                messageKeysByTopic: Mock.Of<IMessageKeysByTopic>(q => q.Get(topicId, null, null, null) == keys));

            var result = query.Get(subscriptionId)
                .ToArray();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);
        }
        public static void PushToSubscription(this Message message, Subscription subscription)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, subscription.Callback.Url);

            switch (subscription.Callback.Kind)
            {
                case CallbackKind.Key:
                    request.Content = new StringContent(ResourceLocation.LinkToMessage(message), Encoding.ASCII, "application/xml");
                    break;
                case CallbackKind.Message:
                    request.PopulateWithMessage(message);
                    break;
                default:
                    throw new InvalidOperationException(string.Format(Business.Texts.CallbackKindUnknown, subscription.Callback.Kind));
            }

            new HttpClient().Send(request);
        }
 public void Execute(Subscription subscription)
 {
     if (subscription.TargetId == null) throw new ValidationException(Texts.TargetIdMustNotBeNull);
     if (!entityById.Exist<Topic>(subscription.TargetId.Value)) throw new EntityNotFoundException(typeof(Topic), subscription.TargetId.Value);
     repository.MakePersistent(subscription);
 }