public void Validate_InvalidMethodAndModeAndActions_MergesErrors()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Method    = _defaultInvalidMethod,
                Mode      = _defaultInvalidMode,
                Responses = _defaultValidResponses
            };

            var invalidValidationResult = new ValidationResult();

            invalidValidationResult.AddError(new SectionName("$"), "Something broke");
            _responseValidator.Setup(
                m => m.Validate(It.IsAny <Response>(), It.IsAny <SectionName>())
                ).Returns(invalidValidationResult);

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.True(result.HasErrors);
            Assert.Equal(3, result.Errors.Length);
        }
        public void Validate_NullSection_ReturnsSingleError()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);

            // Act
            var result = validator.Validate(null, _name);

            // Assert
            Assert.True(result.HasErrors);
            var error = Assert.Single(result.Errors);

            Assert.Equal(ErrorMessageFormatter.Format("$", "Action is null"), error);
        }
        public void Validate_DefaultValues_ReturnsNoError()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Responses = _defaultValidResponses
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.False(result.HasErrors);
        }
        public void Validate_ValidMode_ReturnsSingleError(string mode)
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Mode      = mode,
                Responses = _defaultValidResponses
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.False(result.HasErrors);
        }
        public void Validate_AllAllowedNullValuesNull_ReturnsNoError()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Mode      = null,
                Method    = null,
                Responses = _defaultValidResponses
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.False(result.HasErrors);
        }
        public void Validate_EmptyActions_ReturnsSingleError()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Responses = new List <Response>()
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.True(result.HasErrors);
            var error = Assert.Single(result.Errors);

            Assert.Equal(ErrorMessageFormatter.Format("$.responses", $"Responses array must have at least one item"), error);
        }
        public void Validate_NullResponses_ReturnsSingleError()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Responses = null
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.True(result.HasErrors);
            var error = Assert.Single(result.Errors);

            Assert.Equal(ErrorMessageFormatter.Format("$.responses", $"Action must have a responses array"), error);
        }
        public void Validate_InvalidMethod_ReturnsSingleError(string method)
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Method    = method,
                Responses = _defaultValidResponses
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.True(result.HasErrors);
            var error = Assert.Single(result.Errors);

            Assert.Equal(ErrorMessageFormatter.Format("$.method", $"Invalid method '{method}'. Method can only contain A-Z, a-z."), error);
        }
        public void Validate_InvalidMode_ReturnsSingleError(string mode)
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var section   = new EndpointAction
            {
                Mode      = mode,
                Responses = _defaultValidResponses
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.True(result.HasErrors);
            var error = Assert.Single(result.Errors);

            Assert.Equal(ErrorMessageFormatter.Format("$.mode", $"Invalid mode '{mode}'. Mode must be one of SINGLE, RANDOM, INTERCEPT."), error);
        }
        public void Validate_MultipleResponsesInArray_CallsResponseValidatorForAllReponses()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var response1 = new Response();
            var response2 = new Response();
            var response3 = new Response();
            var section   = new EndpointAction
            {
                Responses = new List <Response>
                {
                    response1,
                    response2,
                    response3
                }
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.False(result.HasErrors);

            _responseValidator.Verify(m => m.Validate(
                                          response1,
                                          It.Is <SectionName>(s => s.PropertyPath == "$.responses[0]")
                                          ), Times.Once());

            _responseValidator.Verify(m => m.Validate(
                                          response2,
                                          It.Is <SectionName>(s => s.PropertyPath == "$.responses[1]")
                                          ), Times.Once());

            _responseValidator.Verify(m => m.Validate(
                                          response3,
                                          It.Is <SectionName>(s => s.PropertyPath == "$.responses[2]")
                                          ), Times.Once());
        }
        public void Validate_SingleResponseInArray_CallsResponseValidator()
        {
            // Arrange
            var validator = new EndpointActionValidator(_responseValidator.Object);
            var response  = new Response();
            var section   = new EndpointAction
            {
                Responses = new List <Response>
                {
                    response
                }
            };

            // Act
            var result = validator.Validate(section, _name);

            // Assert
            Assert.False(result.HasErrors);
            _responseValidator.Verify(m => m.Validate(
                                          response,
                                          It.Is <SectionName>(s => s.PropertyPath == "$.responses[0]")
                                          ), Times.Once());
        }