public async Task should_update_service_when_found_with_no_endpoints()
        {
            var command = new AddEndpoint(Guid.NewGuid(), Guid.NewGuid(), "ipsum", "dolor");

            var service = new Mongo.Infrastructure.Entities.Service()
            {
                Id        = command.ServiceId,
                Name      = "lorem",
                Active    = false,
                Endpoints = null
            };

            var mockRepo = RepositoryUtils.MockRepository(service);

            var mockDbContext = new Mock <IDbContext>();

            mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object);

            var validator = new NullValidator <AddEndpoint>();

            var sut = new AddEndpointHandler(mockDbContext.Object, validator);
            await sut.Handle(command);

            mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >(),
                                                  It.Is <Mongo.Infrastructure.Entities.Service>(r =>
                                                                                                r.Id == command.ServiceId &&
                                                                                                r.Active == false &&
                                                                                                null != r.Endpoints && 1 == r.Endpoints.Count() &&
                                                                                                r.Endpoints.Any(es => es.Active == false &&
                                                                                                                es.Id == command.EndpointId &&
                                                                                                                es.Address == command.Address &&
                                                                                                                es.Protocol == command.Protocol))
                                                  ), Times.Once());
        }
        public async Task should_fail_when_command_null()
        {
            var mockDbContext = new Mock <IDbContext>();
            var mockValidator = new Mock <IValidator <AddEndpoint> >();

            var sut = new AddEndpointHandler(mockDbContext.Object, mockValidator.Object);
            await Assert.ThrowsAsync <ArgumentNullException>(() => sut.Handle(null));
        }
        public async Task should_throw_when_service_not_found()
        {
            var command = new AddEndpoint(Guid.NewGuid(), Guid.NewGuid(), "ipsum", "dolor");

            var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>();

            var mockDbContext = new Mock <IDbContext>();

            mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object);

            var validator = new NullValidator <AddEndpoint>();

            var sut = new AddEndpointHandler(mockDbContext.Object, validator);
            await Assert.ThrowsAsync <NullReferenceException>(() => sut.Handle(command));
        }
        public async Task should_not_replace_endpoint_when_found_by_id()
        {
            var endpoint = new Mongo.Infrastructure.Entities.ServiceEndpoint()
            {
                Id       = Guid.NewGuid(),
                Active   = false,
                Address  = "localhost",
                Protocol = "http"
            };

            var service = new Mongo.Infrastructure.Entities.Service()
            {
                Id        = Guid.NewGuid(),
                Name      = "lorem",
                Active    = false,
                Endpoints = new[]
                {
                    endpoint
                }
            };

            var command = new AddEndpoint(service.Id, endpoint.Id, "ipsum", "dolor");

            var mockRepo = RepositoryUtils.MockRepository(service);

            var mockDbContext = new Mock <IDbContext>();

            mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object);

            var validator = new NullValidator <AddEndpoint>();

            var sut = new AddEndpointHandler(mockDbContext.Object, validator);
            await sut.Handle(command);

            mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >(),
                                                  It.IsAny <Mongo.Infrastructure.Entities.Service>()), Times.Never());
        }