public async Task should_not_return_null_when_service_has_no_active_endpoints() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Name = "lorem", Endpoints = new[] { new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = Guid.NewGuid(), Active = false, Address = "localhost" } } }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var query = new FindService(service.Id); var sut = new FindServiceHandler(mockDbContext.Object); var result = await sut.Handle(query); result.Should().NotBeNull(); result.Name.ShouldBeEquivalentTo(service.Name); result.Endpoints.Should().NotBeNullOrEmpty(); }
public async Task should_succeed_when_endpoint_not_found() { var endpoint = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = Guid.NewGuid(), Address = "ipsum", Protocol = "dolor", Active = false }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = new[] { endpoint } }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var sut = new AddEndpointValidator(mockDbContext.Object); var result = await sut.ValidateAsync(new AddEndpoint(service.Id, Guid.NewGuid(), endpoint.Protocol, Guid.NewGuid().ToString())); result.Success.Should().BeTrue(); }
public async Task should_fail_when_endpoint_already_exists_by_address_and_protocol() { var endpoint = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = Guid.NewGuid(), Address = "ipsum", Protocol = "dolor", Active = false }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = new[] { endpoint } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var sut = new AddEndpointValidator(mockDbContext.Object); var result = await sut.ValidateAsync(new AddEndpoint(service.Id, Guid.NewGuid(), endpoint.Protocol, endpoint.Address)); result.Success.Should().BeFalse(); result.Errors.Any(e => e.Context == "endpoint" && e.Message.Contains(endpoint.Address)).Should().BeTrue(); }
public async Task should_deactivate_service_when_no_endpoints_available() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockPinger = new Mock <IPinger>(); var mockMediator = new Mock <IMediator>(); var validator = new NullValidator <RefreshServiceStatus>(); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.Object, validator); await sut.Handle(new RefreshServiceStatus(service.Id, 10)); mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >(), It.Is <Mongo.Infrastructure.Entities.Service>(r => r.Active == false)), Times.Once()); }
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_create_trace_data_when_input_valid() { var startTicks = DateTime.UtcNow.Ticks; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockServicesRepo = RepositoryUtils.MockRepository(service); var mockEventsRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.TraceEvent>(); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockServicesRepo.Object); mockDbContext.Setup(db => db.TraceEvents).Returns(mockEventsRepo.Object); var @event = new ServiceRefreshed(service.Id); var sut = new ServiceRefreshedHandler(mockDbContext.Object); await sut.Handle(@event); mockEventsRepo.Verify(m => m.InsertOneAsync(It.Is <Mongo.Infrastructure.Entities.TraceEvent>( te => te.Id != Guid.Empty && te.CreationDate >= startTicks && te.Name == Core.TraceEventNames.ServiceRefreshed && te.CustomData == service ) ), Times.Once); }
public async Task should_emit_ServiceRefreshed_event_even_with_no_endpoints() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = new Mongo.Infrastructure.Entities.ServiceEndpoint[] { } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockMediator = new Mock <IMediator>(); var mockPinger = new Mock <IPinger>(); var validator = new NullValidator <RefreshServiceStatus>(); var command = new RefreshServiceStatus(service.Id, 10); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.Object, validator); await sut.Handle(command); mockMediator.Verify(m => m.Publish(It.Is <Core.Events.ServiceRefreshed>(e => e.ServiceId == service.Id), It.IsAny <System.Threading.CancellationToken>()), Times.Once); }
public async Task should_delete_service_when_input_valid() { var startTicks = DateTime.UtcNow.Ticks; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockServicesRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockServicesRepo.Object); var validator = new NullValidator <DeleteService>(); var sut = new DeleteServiceHandler(mockDbContext.Object, validator); await sut.Handle(new DeleteService(service.Id)); mockServicesRepo.Verify(m => m.DeleteOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >()), Times.Once()); }
public async Task should_activate_service_when_at_least_one_endpoint_responds_to_ping() { var endpoint1 = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Address = "localhost1", Protocol = "dolor", Active = true }; var endpoint2 = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Address = "localhost2", Protocol = "dolor", Active = true }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = new[] { endpoint1, endpoint2 } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockPinger = new Mock <IPinger>(); mockPinger.Setup(p => p.PingAsync(It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync((string Address, int timeout) => { return(new PingResult((Address == endpoint2.Address), 0)); }); var mockMediator = new Mock <IMediator>(); var validator = new NullValidator <RefreshServiceStatus>(); var command = new RefreshServiceStatus(service.Id, 10); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.Object, validator); await sut.Handle(command); foreach (var endpoint in service.Endpoints) { mockPinger.Verify(m => m.PingAsync(endpoint.Address, command.Timeout), Times.Once()); } mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >(), It.Is <Mongo.Infrastructure.Entities.Service>(r => r.Active == true && 1 == r.Endpoints.Count(es => es.Active && es.Address == endpoint2.Address))), Times.Once()); }
public async Task should_ping_all_endpoints_even_with_exceptions() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = new[] { new Mongo.Infrastructure.Entities.ServiceEndpoint() { Active = true, Address = "localhost" }, new Mongo.Infrastructure.Entities.ServiceEndpoint() { Active = true, Address = "localhost1" } } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockMediator = new Mock <IMediator>(); var mockPinger = new Mock <IPinger>(); mockPinger.Setup(p => p.PingAsync(It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync((string Address, int timeout) => { if (Address == service.Endpoints.ElementAt(0).Address) { throw new Exception(Address); } return(new PingResult(true, 0)); }); var validator = new NullValidator <RefreshServiceStatus>(); var command = new RefreshServiceStatus(service.Id, 10); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.Object, validator); await sut.Handle(command); mockPinger.Verify(m => m.PingAsync(It.IsAny <string>(), command.Timeout), Times.Exactly(service.Endpoints.Count())); }
public async Task should_not_remove_endpoints_if_none_found() { var endpoint1 = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = Guid.NewGuid(), Active = false, Address = "dolor", Protocol = "ipsum" }; var endpoint2 = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = Guid.NewGuid(), Active = false, Address = "dolor", Protocol = "amet" }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Name = "lorem", Active = false, Endpoints = new[] { endpoint1, endpoint2 } }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var validator = new NullValidator <RemoveEndpoint>(); var sut = new RemoveEndpointHandler(mockDbContext.Object, validator); var command = new RemoveEndpoint(service.Id, Guid.NewGuid()); 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 == service.Id && null != r.Endpoints && 2 == r.Endpoints.Count()) ), Times.Once()); }
public async Task should_deactivate_service_when_no_endpoints_responds_to_ping() { var endpoint = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Address = "localhost", Protocol = "dolor", Active = true }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = new[] { endpoint } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockPinger = new Mock <IPinger>(); mockPinger.Setup(p => p.PingAsync(It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync(new PingResult(false, 0)); var mockMediator = new Mock <IMediator>(); var validator = new NullValidator <RefreshServiceStatus>(); var command = new RefreshServiceStatus(service.Id, 10); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.Object, validator); await sut.Handle(command); mockPinger.Verify(m => m.PingAsync(service.Endpoints.ElementAt(0).Address, command.Timeout), Times.Once()); mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny <Expression <Func <Mongo.Infrastructure.Entities.Service, bool> > >(), It.Is <Mongo.Infrastructure.Entities.Service>(r => r.Active == false && !r.Endpoints.Any(es => es.Active))), Times.Once()); }
public async Task should_update_endpoint_roundtrip_time() { var endpoint = new Mongo.Infrastructure.Entities.ServiceEndpoint() { Address = "localhost1", Protocol = "dolor", Active = false, RoundtripTime = long.MaxValue }; var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = new[] { endpoint } }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockPinger = new Mock <IPinger>(); mockPinger.Setup(p => p.PingAsync(It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync(new PingResult(true, 42)); var mockMediator = new Mock <IMediator>(); var validator = new NullValidator <RefreshServiceStatus>(); var command = new RefreshServiceStatus(service.Id, 10); var sut = new RefreshServiceStatusHandler(mockDbContext.Object, mockPinger.Object, mockMediator.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.Endpoints.Any(es => es.RoundtripTime == 42))), Times.Once()); }
public void should_throw_ArgumentOutOfRangeException_when_service_does_not_exist() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var @event = new ServiceRefreshed(Guid.NewGuid()); var sut = new ServiceRefreshedHandler(mockDbContext.Object); Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => sut.Handle(@event)); }
public async Task should_succeed_when_command_valid() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var sut = new AddEndpointValidator(mockDbContext.Object); var result = await sut.ValidateAsync(new AddEndpoint(service.Id, Guid.NewGuid(), "ipsum", "dolor")); result.Success.Should().BeTrue(); }
public async Task should_fail_when_service_has_no_endpoints() { var service = new Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = false, Name = "lorem", Endpoints = null }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var sut = new RemoveEndpointValidator(mockDbContext.Object); var result = await sut.ValidateAsync(new RemoveEndpoint(service.Id, Guid.NewGuid())); result.Success.Should().BeFalse(); result.Errors.Any(e => e.Context == "service" && e.Message.Contains("no endpoints")).Should().BeTrue(); }
public async Task should_fail_when_service_already_exists() { var service = new Mongo.Infrastructure.Entities.Service() { Id = System.Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var sut = new CreateServiceValidator(mockDbContext.Object); var result = await sut.ValidateAsync(new CreateService(service.Id, service.Name)); result.Success.Should().BeFalse(); result.Errors.Any(e => e.Context == "service" && e.Message.Contains("already exists")).Should().BeTrue(); }
public async Task should_throw_when_service_has_null_endpoints() { var command = new RemoveEndpoint(Guid.NewGuid(), Guid.NewGuid()); var service = new Mongo.Infrastructure.Entities.Service() { Id = command.ServiceId, 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 <RemoveEndpoint>(); var sut = new RemoveEndpointHandler(mockDbContext.Object, validator); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.Handle(command)); }
public async Task should_throw_when_endpoint_not_found() { var command = new UpdateEndpoint(Guid.NewGuid(), Guid.NewGuid(), "lorem", "ipsum"); var service = new Mongo.Infrastructure.Entities.Service() { Id = command.ServiceId, Active = false, Endpoints = Enumerable.Empty <Mongo.Infrastructure.Entities.ServiceEndpoint>() }; 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 <UpdateEndpoint>(); var sut = new UpdateEndpointHandler(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()); }
public async Task should_remove_endpoint() { var command = new RemoveEndpoint(Guid.NewGuid(), Guid.NewGuid()); var service = new Mongo.Infrastructure.Entities.Service() { Id = command.ServiceId, Active = false, Endpoints = new[] { new Mongo.Infrastructure.Entities.ServiceEndpoint() { Id = command.EndpointId, Active = false, } } }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var validator = new NullValidator <RemoveEndpoint>(); var sut = new RemoveEndpointHandler(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 && !r.Endpoints.Any(e => e.Id == command.EndpointId)) ), Times.Once()); }