コード例 #1
0
        public async Task should_succeed_when_command_valid()
        {
            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 UpdateEndpointValidator(mockDbContext.Object);
            var result = await sut.ValidateAsync(new UpdateEndpoint(service.Id, endpoint.Id, "http", "localhost"));

            result.Success.Should().BeTrue();
        }
コード例 #2
0
        public async Task should_fail_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 <Mongo.Infrastructure.Entities.Service>(service);

            var mockDbContext = new Mock <IDbContext>();

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

            var command = new UpdateEndpoint(service.Id, Guid.NewGuid(), endpoint.Protocol, endpoint.Address);
            var sut     = new UpdateEndpointValidator(mockDbContext.Object);
            var result  = await sut.ValidateAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Any(e => e.Context == "endpoint" && e.Message.Contains(command.EndpointId.ToString())).Should().BeTrue();
        }
コード例 #3
0
            public void Validate_ValidCommand_Returns(UpdateEndpoint command, UpdateEndpointValidator subject)
            {
                // Arrange

                // Act
                var result = subject.TestValidate(command);

                // Assert
                result.ShouldNotHaveAnyValidationErrors();
            }
コード例 #4
0
            public void Validate_EmptySerialNumber_Throws(UpdateEndpointValidator subject)
            {
                // Arrange

                // Act
                var result = subject.TestValidate(new UpdateEndpoint {
                    SwitchState = (SwitchStates)1, SerialNumber = string.Empty
                });

                // Assert
                result.ShouldHaveValidationErrorFor(c => c.SerialNumber);
            }
コード例 #5
0
            public void Validate_InvalidSwitchStates_Throws(string serial, int state)
            {
                // Arrange
                var subject = new UpdateEndpointValidator();

                // Act
                var result = subject.TestValidate(new UpdateEndpoint {
                    SwitchState = (SwitchStates)state, SerialNumber = serial
                });

                // Assert
                result.ShouldHaveValidationErrorFor(c => c.SwitchState);
            }
コード例 #6
0
        public async Task should_fail_when_service_not_found()
        {
            var mockRepo = RepositoryUtils.MockRepository <Mongo.Infrastructure.Entities.Service>();

            var mockDbContext = new Mock <IDbContext>();

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

            var sut    = new UpdateEndpointValidator(mockDbContext.Object);
            var result = await sut.ValidateAsync(new UpdateEndpoint(Guid.NewGuid(), Guid.NewGuid(), "ipsum", "dolor"));

            result.Success.Should().BeFalse();
            result.Errors.Any(e => e.Context == "service" && e.Message.Contains("Unable to load service")).Should().BeTrue();
        }
コード例 #7
0
        private async Task UpdateEndpoint(CancellationToken cancellationToken)
        {
            FindEndpoint findCommand = new FindEndpoint();

            Console.Write("Insira o número serial do Endpoint: ");
            findCommand.SerialNumber = Console.ReadLine();

            var result = await _mediator.Send(findCommand, cancellationToken);

            if (result is null)
            {
                Console.WriteLine($"Não existe um Endpoint com número serial {findCommand.SerialNumber}");
            }
            else
            {
                UpdateEndpoint updateCommand = new UpdateEndpoint();

                Console.Write("Insira o estado do switch do Endpoint: ");
                updateCommand.SwitchState  = (SwitchStates)int.Parse(Console.ReadLine());
                updateCommand.SerialNumber = findCommand.SerialNumber;

                UpdateEndpointValidator validator = new UpdateEndpointValidator();
                var validation = validator.Validate(updateCommand);

                if (!validation.IsValid)
                {
                    Console.WriteLine(validation.Errors.FirstOrDefault());
                }
                else
                {
                    try
                    {
                        await _mediator.Send(updateCommand, cancellationToken);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Um erro inesperado ocorreu");
                    }
                }
            }
            Console.WriteLine("-------------------------------------");
        }
コード例 #8
0
        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 = 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 command = new UpdateEndpoint(service.Id, Guid.NewGuid(), "lorem", "ipsum");
            var sut     = new UpdateEndpointValidator(mockDbContext.Object);
            var result  = await sut.ValidateAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Any(e => e.Context == "service" && e.Message.Contains(service.Id.ToString())).Should().BeTrue();
        }