|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel.DataAnnotations; |
| 5 | +using System.Reflection; |
| 6 | +using Microsoft.AspNetCore.Http.Validation; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Moq; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Http.Tests; |
| 11 | + |
| 12 | +public class ValidatableInfoResolverTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void GetValidatableTypeInfo_ReturnsNull_ForNonValidatableType() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + var resolver = new Mock<IValidatableInfoResolver>(); |
| 19 | + resolver.Setup(r => r.GetValidatableTypeInfo(It.IsAny<Type>())).Returns((Type t) => null); |
| 20 | + |
| 21 | + // Act |
| 22 | + var result = resolver.Object.GetValidatableTypeInfo(typeof(NonValidatableType)); |
| 23 | + |
| 24 | + // Assert |
| 25 | + Assert.Null(result); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void GetValidatableTypeInfo_ReturnsTypeInfo_ForValidatableType() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var mockTypeInfo = new Mock<ValidatableTypeInfo>( |
| 33 | + typeof(ValidatableType), |
| 34 | + Array.Empty<ValidatablePropertyInfo>(), |
| 35 | + false, |
| 36 | + null).Object; |
| 37 | + |
| 38 | + var resolver = new Mock<IValidatableInfoResolver>(); |
| 39 | + resolver.Setup(r => r.GetValidatableTypeInfo(typeof(ValidatableType))).Returns(mockTypeInfo); |
| 40 | + |
| 41 | + // Act |
| 42 | + var result = resolver.Object.GetValidatableTypeInfo(typeof(ValidatableType)); |
| 43 | + |
| 44 | + // Assert |
| 45 | + Assert.NotNull(result); |
| 46 | + Assert.Equal(typeof(ValidatableType), result.Type); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void GetValidatableParameterInfo_ReturnsNull_ForNonValidatableParameter() |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + var method = typeof(TestMethods).GetMethod(nameof(TestMethods.MethodWithNonValidatableParam))!; |
| 54 | + var parameter = method.GetParameters()[0]; |
| 55 | + |
| 56 | + var resolver = new Mock<IValidatableInfoResolver>(); |
| 57 | + resolver.Setup(r => r.GetValidatableParameterInfo(It.IsAny<ParameterInfo>())).Returns((ParameterInfo p) => null); |
| 58 | + |
| 59 | + // Act |
| 60 | + var result = resolver.Object.GetValidatableParameterInfo(parameter); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.Null(result); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void GetValidatableParameterInfo_ReturnsParameterInfo_ForValidatableParameter() |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + var method = typeof(TestMethods).GetMethod(nameof(TestMethods.MethodWithValidatableParam))!; |
| 71 | + var parameter = method.GetParameters()[0]; |
| 72 | + |
| 73 | + var mockParamInfo = new Mock<ValidatableParameterInfo>( |
| 74 | + "model", |
| 75 | + "model", |
| 76 | + false, |
| 77 | + false, |
| 78 | + true, |
| 79 | + false).Object; |
| 80 | + |
| 81 | + var resolver = new Mock<IValidatableInfoResolver>(); |
| 82 | + resolver.Setup(r => r.GetValidatableParameterInfo(parameter)).Returns(mockParamInfo); |
| 83 | + |
| 84 | + // Act |
| 85 | + var result = resolver.Object.GetValidatableParameterInfo(parameter); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.NotNull(result); |
| 89 | + Assert.Equal("model", result.Name); |
| 90 | + Assert.True(result.HasValidatableType); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void ResolversChain_ProcessesInCorrectOrder() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + var services = new ServiceCollection(); |
| 98 | + |
| 99 | + var resolver1 = new Mock<IValidatableInfoResolver>(); |
| 100 | + var resolver2 = new Mock<IValidatableInfoResolver>(); |
| 101 | + var resolver3 = new Mock<IValidatableInfoResolver>(); |
| 102 | + |
| 103 | + resolver1.Setup(r => r.GetValidatableTypeInfo(typeof(ValidatableType))).Returns((Type t) => null); |
| 104 | + resolver2.Setup(r => r.GetValidatableTypeInfo(typeof(ValidatableType))).Returns( |
| 105 | + new Mock<ValidatableTypeInfo>(typeof(ValidatableType), Array.Empty<ValidatablePropertyInfo>(), false, null).Object); |
| 106 | + |
| 107 | + services.AddSingleton(resolver1.Object); |
| 108 | + services.AddSingleton(resolver2.Object); |
| 109 | + services.AddSingleton(resolver3.Object); |
| 110 | + |
| 111 | + var serviceProvider = services.BuildServiceProvider(); |
| 112 | + var resolvers = serviceProvider.GetServices<IValidatableInfoResolver>().ToList(); |
| 113 | + |
| 114 | + // Act |
| 115 | + var result = resolvers.Select(r => r.GetValidatableTypeInfo(typeof(ValidatableType))) |
| 116 | + .FirstOrDefault(info => info != null); |
| 117 | + |
| 118 | + // Assert |
| 119 | + Assert.NotNull(result); |
| 120 | + Assert.Equal(typeof(ValidatableType), result.Type); |
| 121 | + resolver1.Verify(r => r.GetValidatableTypeInfo(typeof(ValidatableType)), Times.Once); |
| 122 | + resolver2.Verify(r => r.GetValidatableTypeInfo(typeof(ValidatableType)), Times.Once); |
| 123 | + resolver3.Verify(r => r.GetValidatableTypeInfo(typeof(ValidatableType)), Times.Never); |
| 124 | + } |
| 125 | + |
| 126 | + // Test types |
| 127 | + private class NonValidatableType { } |
| 128 | + |
| 129 | + [ValidatableType] |
| 130 | + private class ValidatableType |
| 131 | + { |
| 132 | + [Required] |
| 133 | + public string Name { get; set; } = ""; |
| 134 | + } |
| 135 | + |
| 136 | + private static class TestMethods |
| 137 | + { |
| 138 | + public static void MethodWithNonValidatableParam(NonValidatableType param) { } |
| 139 | + public static void MethodWithValidatableParam(ValidatableType model) { } |
| 140 | + } |
| 141 | + |
| 142 | + // Test implementations |
| 143 | + private class TestValidatablePropertyInfo : ValidatablePropertyInfo |
| 144 | + { |
| 145 | + private readonly ValidationAttribute[] _validationAttributes; |
| 146 | + |
| 147 | + public TestValidatablePropertyInfo( |
| 148 | + Type containingType, |
| 149 | + Type propertyType, |
| 150 | + string name, |
| 151 | + string displayName, |
| 152 | + bool isEnumerable, |
| 153 | + bool isNullable, |
| 154 | + bool isRequired, |
| 155 | + bool hasValidatableType, |
| 156 | + ValidationAttribute[] validationAttributes) |
| 157 | + : base(containingType, propertyType, name, displayName, isEnumerable, isNullable, isRequired, hasValidatableType) |
| 158 | + { |
| 159 | + _validationAttributes = validationAttributes; |
| 160 | + } |
| 161 | + |
| 162 | + protected override ValidationAttribute[] GetValidationAttributes() => _validationAttributes; |
| 163 | + } |
| 164 | + |
| 165 | + private class TestValidatableParameterInfo : ValidatableParameterInfo |
| 166 | + { |
| 167 | + private readonly ValidationAttribute[] _validationAttributes; |
| 168 | + |
| 169 | + public TestValidatableParameterInfo( |
| 170 | + string name, |
| 171 | + string displayName, |
| 172 | + bool isNullable, |
| 173 | + bool isRequired, |
| 174 | + bool hasValidatableType, |
| 175 | + bool isEnumerable, |
| 176 | + ValidationAttribute[] validationAttributes) |
| 177 | + : base(name, displayName, isNullable, isRequired, hasValidatableType, isEnumerable) |
| 178 | + { |
| 179 | + _validationAttributes = validationAttributes; |
| 180 | + } |
| 181 | + |
| 182 | + protected override ValidationAttribute[] GetValidationAttributes() => _validationAttributes; |
| 183 | + } |
| 184 | + |
| 185 | + private class TestValidatableTypeInfo : ValidatableTypeInfo |
| 186 | + { |
| 187 | + public TestValidatableTypeInfo( |
| 188 | + Type type, |
| 189 | + ValidatablePropertyInfo[] members, |
| 190 | + bool implementsIValidatableObject, |
| 191 | + Type[]? validatableSubTypes = null) |
| 192 | + : base(type, members, implementsIValidatableObject, validatableSubTypes) |
| 193 | + { |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments