1+ using Microsoft . OpenApi . Models ;
2+ using Wiremock . OpenAPIValidator . Models ;
3+ using Wiremock . OpenAPIValidator . Queries ;
4+
5+ namespace Wiremock . OpenAPIValidator . Tests . Queries ;
6+
7+ public class PropertyRequiredQueryHandlerTests
8+ {
9+
10+ private PropertyRequiredQueryHandler _handler ;
11+
12+ [ SetUp ]
13+ public void Setup ( )
14+ {
15+ _handler = new PropertyRequiredQueryHandler ( ) ;
16+ }
17+
18+ [ Test ]
19+ public async Task Handle_ValidMatchingRequiredProperties ( )
20+ {
21+ var wiremockProps = new WiremockResponseProperties ( ) ;
22+ wiremockProps . Properties . Add ( "Prop1" , typeof ( string ) ) ;
23+
24+ OpenApiResponses apiResponse = RequiredOpenApiSchema ( ) ;
25+
26+ var response = await _handler . Handle ( new PropertyRequiredQuery
27+ {
28+ Responses = apiResponse ,
29+ MockProperties = wiremockProps ,
30+ Name = "UnitTest"
31+ } , CancellationToken . None ) ;
32+
33+ Assert . Multiple ( ( ) =>
34+ {
35+ Assert . That ( response . Count , Is . EqualTo ( 1 ) ) ;
36+ Assert . That ( response [ 0 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
37+ Assert . That ( response [ 0 ] . Name , Is . EqualTo ( "UnitTest - prop1" ) ) ;
38+ Assert . That ( response [ 0 ] . ValidationResult , Is . EqualTo ( ValidationResult . Passed ) ) ;
39+ Assert . That ( response [ 0 ] . Description , Is . Null . Or . Empty ) ;
40+ } ) ;
41+ }
42+
43+ [ Test ]
44+ public async Task Handle_MissingRequiredProperties ( )
45+ {
46+ var wiremockProps = new WiremockResponseProperties ( ) ;
47+ wiremockProps . Properties . Add ( "Prop2" , typeof ( string ) ) ;
48+
49+ OpenApiResponses apiResponse = RequiredOpenApiSchema ( ) ;
50+
51+ var response = await _handler . Handle ( new PropertyRequiredQuery
52+ {
53+ Responses = apiResponse ,
54+ MockProperties = wiremockProps ,
55+ Name = "UnitTest"
56+ } , CancellationToken . None ) ;
57+
58+ Assert . Multiple ( ( ) =>
59+ {
60+ Assert . That ( response . Count , Is . EqualTo ( 1 ) ) ;
61+ Assert . That ( response [ 0 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
62+ Assert . That ( response [ 0 ] . Name , Is . EqualTo ( "UnitTest - prop1" ) ) ;
63+ Assert . That ( response [ 0 ] . ValidationResult , Is . EqualTo ( ValidationResult . Failed ) ) ;
64+ Assert . That ( response [ 0 ] . Description , Is . Not . Null . And . Contains ( "prop1" ) ) ;
65+ } ) ;
66+ }
67+
68+ [ Test ]
69+ public async Task Handle_MissingOptionalProperties ( )
70+ {
71+ var wiremockProps = new WiremockResponseProperties ( ) ;
72+ wiremockProps . Properties . Add ( "Prop2" , typeof ( string ) ) ;
73+
74+ OpenApiResponses apiResponse = RequiredOpenApiSchema ( ) ;
75+ apiResponse [ "200" ] . Content [ "application/json" ] . Schema . Required = new HashSet < string > ( ) ;
76+
77+ var response = await _handler . Handle ( new PropertyRequiredQuery
78+ {
79+ Responses = apiResponse ,
80+ MockProperties = wiremockProps ,
81+ Name = "UnitTest"
82+ } , CancellationToken . None ) ;
83+
84+ Assert . Multiple ( ( ) =>
85+ {
86+ Assert . That ( response . Count , Is . EqualTo ( 1 ) ) ;
87+ Assert . That ( response [ 0 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
88+ Assert . That ( response [ 0 ] . Name , Is . EqualTo ( "UnitTest - prop1" ) ) ;
89+ Assert . That ( response [ 0 ] . ValidationResult , Is . EqualTo ( ValidationResult . Warning ) ) ;
90+ Assert . That ( response [ 0 ] . Description , Is . Not . Null . And . Contains ( "prop1" ) ) ;
91+ } ) ;
92+ }
93+
94+
95+ [ Test ]
96+ public async Task Handle_MultipleMissingOptionalAndRequiredProperties ( )
97+ {
98+ var wiremockProps = new WiremockResponseProperties ( ) ;
99+ wiremockProps . Properties . Add ( "Prop1" , typeof ( string ) ) ;
100+ wiremockProps . Properties . Add ( "Prop4" , typeof ( string ) ) ;
101+
102+ OpenApiResponses apiResponse = RequiredOpenApiSchema ( ) ;
103+ apiResponse [ "200" ] . Content [ "application/json" ] . Schema . Required . Add ( "Prop2" ) ;
104+ apiResponse [ "200" ] . Content [ "application/json" ] . Schema . Properties . Add ( "Prop2" , new OpenApiSchema ( ) ) ;
105+ apiResponse [ "200" ] . Content [ "application/json" ] . Schema . Properties . Add ( "Prop3" , new OpenApiSchema ( ) ) ;
106+
107+ var response = await _handler . Handle ( new PropertyRequiredQuery
108+ {
109+ Responses = apiResponse ,
110+ MockProperties = wiremockProps ,
111+ Name = "UnitTest"
112+ } , CancellationToken . None ) ;
113+
114+ Assert . Multiple ( ( ) =>
115+ {
116+ Assert . That ( response . Count , Is . EqualTo ( 3 ) ) ;
117+ Assert . That ( response [ 0 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
118+ Assert . That ( response [ 0 ] . Name , Is . EqualTo ( "UnitTest - prop1" ) ) ;
119+ Assert . That ( response [ 0 ] . ValidationResult , Is . EqualTo ( ValidationResult . Passed ) ) ;
120+ Assert . That ( response [ 0 ] . Description , Is . Null . Or . Empty ) ;
121+ Assert . That ( response [ 1 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
122+ Assert . That ( response [ 1 ] . Name , Is . EqualTo ( "UnitTest - Prop2" ) ) ;
123+ Assert . That ( response [ 1 ] . ValidationResult , Is . EqualTo ( ValidationResult . Failed ) ) ;
124+ Assert . That ( response [ 1 ] . Description , Is . Not . Null . And . Contains ( "Prop2" ) ) ;
125+ Assert . That ( response [ 2 ] . Type , Is . EqualTo ( ValidatorType . ResponsePropertyRequired ) ) ;
126+ Assert . That ( response [ 2 ] . Name , Is . EqualTo ( "UnitTest - Prop3" ) ) ;
127+ Assert . That ( response [ 2 ] . ValidationResult , Is . EqualTo ( ValidationResult . Warning ) ) ;
128+ Assert . That ( response [ 2 ] . Description , Is . Not . Null . And . Contains ( "Prop3" ) ) ;
129+ } ) ;
130+ }
131+
132+ private static OpenApiResponses RequiredOpenApiSchema ( )
133+ {
134+ return new OpenApiResponses
135+ {
136+ { "200" , new OpenApiResponse
137+ {
138+ Content = new Dictionary < string , OpenApiMediaType >
139+ {
140+ { "application/json" , new OpenApiMediaType
141+ {
142+ Schema = new OpenApiSchema
143+ {
144+ Required = new HashSet < string > { "prop1" } ,
145+ Properties = new Dictionary < string , OpenApiSchema >
146+ {
147+ { "prop1" , new OpenApiSchema ( ) }
148+ }
149+ }
150+ } }
151+ }
152+ } }
153+ } ;
154+ }
155+ }
0 commit comments