-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTestBase.cs
150 lines (124 loc) · 6.19 KB
/
UnitTestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using FluentValidation;
using MicroElements.Swashbuckle.FluentValidation.Generation;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using SampleWebApi.ValidatorFactories;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace MicroElements.Swashbuckle.FluentValidation.Tests
{
public class UnitTestBase
{
public SchemaGenerator SchemaGenerator(params IValidator[] validators)
{
return SchemaGenerator(options => ConfigureGenerator(options, validators));
}
public SchemaGenerator SchemaGenerator(
Action<SchemaGeneratorOptions> configureGenerator = null,
Action<JsonSerializerOptions> configureSerializer = null)
{
var generatorOptions = new SchemaGeneratorOptions();
configureGenerator?.Invoke(generatorOptions);
var serializerOptions = new JsonSerializerOptions();
configureSerializer?.Invoke(serializerOptions);
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
}
private void ConfigureGenerator(SchemaGeneratorOptions options, params IValidator[] validators)
{
IValidatorFactory validatorFactory = new CustomValidatorFactory(validators);
options.SchemaFilters.Add(new FluentValidationRules(validatorFactory: validatorFactory));
}
}
public class SchemaBuilder<T>
{
public InlineValidator<T> Validator { get; } = new InlineValidator<T>();
public SchemaRepository SchemaRepository { get; } = new SchemaRepository();
private readonly SchemaGenerationOptions _schemaGenerationOptions = new SchemaGenerationOptions();
private SchemaGenerationSettings _schemaGenerationSettings;
public SchemaBuilder()
{
_schemaGenerationSettings = new SchemaGenerationSettings()
{
NameResolver = new SystemTextJsonNameResolver(),
SchemaIdSelector = new SchemaGeneratorOptions().SchemaIdSelector
};
}
public SchemaBuilder<T> ConfigureSchemaGenerationOptions(
Action<SchemaGenerationOptions> configureSchemaGenerationOptions,
Func<SchemaGenerationSettings, SchemaGenerationSettings>? configureSchemaGenerationSettings = null)
{
configureSchemaGenerationOptions(_schemaGenerationOptions);
if(configureSchemaGenerationSettings != null)
_schemaGenerationSettings = configureSchemaGenerationSettings(_schemaGenerationSettings);
return this;
}
public OpenApiSchema AddRule<TProperty>(
Expression<Func<T, TProperty>> propertyExpression,
Action<IRuleBuilderInitial<T, TProperty>>? configureRule = null,
Action<OpenApiSchema>? schemaCheck = null)
{
IRuleBuilderInitial<T, TProperty> ruleBuilder = Validator.RuleFor(propertyExpression);
configureRule?.Invoke(ruleBuilder);
var expressionBody = propertyExpression.Body as MemberExpression;
var schema = SchemaRepository.GenerateSchemaForValidator(Validator, _schemaGenerationOptions, _schemaGenerationSettings);
PropertyInfo propertyInfo = expressionBody.Member as PropertyInfo;
string propertyName = _schemaGenerationSettings.NameResolver.GetPropertyName(propertyInfo);
var property = schema.Properties[propertyName];
schemaCheck?.Invoke(property);
return property;
}
}
public static class TestExtensions
{
public static OpenApiSchema GenerateSchemaForValidator<T>(
this SchemaRepository schemaRepository,
IValidator<T> validator,
SchemaGenerationOptions? schemaGenerationOptions = null,
SchemaGenerationSettings? schemaGenerationSettings = null,
Action<JsonSerializerOptions>? configureSerializer = null)
{
SchemaGenerator schemaGenerator = CreateSchemaGenerator(
new []{ validator },
schemaGenerationOptions: schemaGenerationOptions,
schemaGenerationSettings: schemaGenerationSettings,
configureSerializer: configureSerializer);
OpenApiSchema schema = schemaGenerator
.GenerateSchema(typeof(T), schemaRepository);
if (schema.Reference?.Id != null)
schema = schemaRepository.Schemas[schema.Reference.Id];
return schema;
}
public static SchemaGenerator CreateSchemaGenerator(
IValidator[] validators,
SchemaGenerationOptions? schemaGenerationOptions = null,
SchemaGenerationSettings? schemaGenerationSettings = null,
Action<JsonSerializerOptions>? configureSerializer = null)
{
return CreateSchemaGenerator(
configureGenerator: options =>
{
IValidatorFactory validatorFactory = new CustomValidatorFactory(validators);
options.SchemaFilters.Add(new FluentValidationRules(
validatorFactory: validatorFactory,
rules: null,
loggerFactory: null,
schemaGenerationOptions: schemaGenerationOptions != null ? new OptionsWrapper<SchemaGenerationOptions>(schemaGenerationOptions) : null,
nameResolver: schemaGenerationSettings?.NameResolver));
},
configureSerializer: configureSerializer);
}
public static SchemaGenerator CreateSchemaGenerator(
Action<SchemaGeneratorOptions>? configureGenerator = null,
Action<JsonSerializerOptions>? configureSerializer = null)
{
var generatorOptions = new SchemaGeneratorOptions();
configureGenerator?.Invoke(generatorOptions);
var serializerOptions = new JsonSerializerOptions();
configureSerializer?.Invoke(serializerOptions);
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
}
}
}