88
99import com .atlassian .oai .validator .model .SimpleRequest ;
1010import com .atlassian .oai .validator .report .ValidationReport ;
11+ import com .getyourguide .openapi .validation .api .log .LogLevel ;
1112import com .getyourguide .openapi .validation .api .model .OpenApiViolation ;
1213import com .getyourguide .openapi .validation .api .model .RequestMetaData ;
14+ import com .getyourguide .openapi .validation .api .model .ResponseMetaData ;
1315import com .getyourguide .openapi .validation .core .exclusions .InternalViolationExclusions ;
1416import com .getyourguide .openapi .validation .core .mapper .ValidationReportToOpenApiViolationsMapper ;
1517import com .getyourguide .openapi .validation .core .validator .OpenApiInteractionValidatorWrapper ;
1921import java .util .concurrent .Executor ;
2022import java .util .concurrent .RejectedExecutionException ;
2123import org .junit .jupiter .api .BeforeEach ;
24+ import org .junit .jupiter .api .DisplayName ;
25+ import org .junit .jupiter .api .Nested ;
2226import org .junit .jupiter .api .Test ;
2327import org .mockito .ArgumentCaptor ;
2428import org .mockito .Mockito ;
@@ -51,41 +55,179 @@ public void setup() {
5155 }
5256
5357 @ Test
58+ @ DisplayName ("When thread pool executor rejects execution then it should not throw" )
5459 public void testWhenThreadPoolExecutorRejectsExecutionThenItShouldNotThrow () {
5560 Mockito .doThrow (new RejectedExecutionException ()).when (executor ).execute (any ());
5661
5762 openApiRequestValidator .validateRequestObjectAsync (mock (), null , null , mock ());
5863 }
5964
60- @ Test
61- public void testWhenEncodedQueryParamIsPassedThenValidationShouldHappenWithQueryParamDecoded () {
62- var uri = URI .create ("https://api.example.com?ids=1%2C2%2C3&text=e%3Dmc2%20%26%20more&spaces=this+is+a+sparta" );
63- var request = new RequestMetaData ("GET" , uri , new HashMap <>());
64-
65- openApiRequestValidator .validateRequestObject (request , null );
66-
67- var simpleRequestArgumentCaptor = ArgumentCaptor .forClass (SimpleRequest .class );
68- verify (validator ).validateRequest (simpleRequestArgumentCaptor .capture ());
69- verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "ids" , "1,2,3" );
70- verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "text" , "e=mc2 & more" );
71- verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "spaces" , "this is a sparta" );
65+ @ Nested
66+ @ DisplayName ("validateRequestObject" )
67+ class ValidateRequestObjectTests {
68+
69+ @ Test
70+ @ DisplayName ("When encoded query param is passed then validation should happen with query param decoded" )
71+ public void testWhenEncodedQueryParamIsPassedThenValidationShouldHappenWithQueryParamDecoded () {
72+ var uri = URI .create ("https://api.example.com?ids=1%2C2%2C3&text=e%3Dmc2%20%26%20more&spaces=this+is+a+sparta" );
73+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
74+
75+ openApiRequestValidator .validateRequestObject (request , null );
76+
77+ var simpleRequestArgumentCaptor = ArgumentCaptor .forClass (SimpleRequest .class );
78+ verify (validator ).validateRequest (simpleRequestArgumentCaptor .capture ());
79+ verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "ids" , "1,2,3" );
80+ verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "text" , "e=mc2 & more" );
81+ verifyQueryParamValueEquals (simpleRequestArgumentCaptor , "spaces" , "this is a sparta" );
82+ }
83+
84+ @ Test
85+ @ DisplayName ("When violation is excluded then it should not be returned" )
86+ public void testWhenViolationIsExcludedThenItShouldNotBeReturned () {
87+ var uri = URI .create ("https://api.example.com/path" );
88+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
89+ var validationReport = mock (ValidationReport .class );
90+ when (validator .validateRequest (any ())).thenReturn (validationReport );
91+ var violationExcluded = mock (OpenApiViolation .class );
92+ var violations = List .of (violationExcluded , mock (OpenApiViolation .class ));
93+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
94+ when (internalViolationExclusions .isExcluded (violationExcluded )).thenReturn (true );
95+
96+ var result = openApiRequestValidator .validateRequestObject (request , null );
97+
98+ assertEquals (1 , result .size ());
99+ assertEquals (violations .get (1 ), result .getFirst ());
100+ }
101+
102+ @ Test
103+ @ DisplayName ("When violation has log level IGNORE then it should not be returned" )
104+ public void testWhenRequestViolationHasLogLevelIgnoreThenItShouldNotBeReturned () {
105+ var uri = URI .create ("https://api.example.com/path" );
106+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
107+ var validationReport = mock (ValidationReport .class );
108+ when (validator .validateRequest (any ())).thenReturn (validationReport );
109+
110+ var violationIgnored = createViolation (LogLevel .IGNORE );
111+ var violationError = createViolation (LogLevel .ERROR );
112+
113+ var violations = List .of (violationIgnored , violationError );
114+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
115+
116+ var result = openApiRequestValidator .validateRequestObject (request , null );
117+
118+ assertEquals (1 , result .size ());
119+ assertEquals (violationError , result .getFirst ());
120+ }
121+
122+ @ Test
123+ @ DisplayName ("When violation has log level IGNORE and another is excluded then both should not be returned" )
124+ public void testWhenRequestViolationHasLogLevelIgnoreAndIsExcludedThenItShouldNotBeReturned () {
125+ var uri = URI .create ("https://api.example.com/path" );
126+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
127+ var validationReport = mock (ValidationReport .class );
128+ when (validator .validateRequest (any ())).thenReturn (validationReport );
129+
130+ var violationIgnored = createViolation (LogLevel .IGNORE );
131+ var violationExcluded = createViolation (LogLevel .WARN );
132+ when (internalViolationExclusions .isExcluded (violationExcluded )).thenReturn (true );
133+ var violationError = createViolation (LogLevel .ERROR );
134+
135+ var violations = List .of (violationIgnored , violationExcluded , violationError );
136+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
137+
138+ var result = openApiRequestValidator .validateRequestObject (request , null );
139+
140+ assertEquals (1 , result .size ());
141+ assertEquals (violationError , result .getFirst ());
142+ }
143+
144+ @ Test
145+ @ DisplayName ("When all violations are ignored then empty list is returned" )
146+ public void testWhenAllRequestViolationsAreIgnoredThenEmptyListIsReturned () {
147+ var uri = URI .create ("https://api.example.com/path" );
148+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
149+ var validationReport = mock (ValidationReport .class );
150+ when (validator .validateRequest (any ())).thenReturn (validationReport );
151+
152+ var violation1 = createViolation (LogLevel .IGNORE );
153+ var violation2 = createViolation (LogLevel .IGNORE );
154+
155+ var violations = List .of (violation1 , violation2 );
156+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
157+
158+ var result = openApiRequestValidator .validateRequestObject (request , null );
159+
160+ assertEquals (0 , result .size ());
161+ }
72162 }
73163
74- @ Test
75- public void testWhenViolationIsExcludedThenItShouldNotBeReturned () {
76- var uri = URI .create ("https://api.example.com/path" );
77- var request = new RequestMetaData ("GET" , uri , new HashMap <>());
78- var validationReport = mock (ValidationReport .class );
79- when (validator .validateRequest (any ())).thenReturn (validationReport );
80- var violationExcluded = mock (OpenApiViolation .class );
81- var violations = List .of (violationExcluded , mock (OpenApiViolation .class ));
82- when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
83- when (internalViolationExclusions .isExcluded (violationExcluded )).thenReturn (true );
84-
85- var result = openApiRequestValidator .validateRequestObject (request , null );
86-
87- assertEquals (1 , result .size ());
88- assertEquals (violations .get (1 ), result .getFirst ());
164+ @ Nested
165+ @ DisplayName ("validateResponseObject" )
166+ class ValidateResponseObjectTests {
167+
168+ @ Test
169+ @ DisplayName ("When violation has log level IGNORE then it should not be returned" )
170+ public void testWhenResponseViolationHasLogLevelIgnoreThenItShouldNotBeReturned () {
171+ var uri = URI .create ("https://api.example.com/path" );
172+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
173+ var response = new ResponseMetaData (200 , "application/json" , new HashMap <>());
174+ var validationReport = mock (ValidationReport .class );
175+ when (validator .validateResponse (any (), any (), any ())).thenReturn (validationReport );
176+
177+ var violationIgnored = createViolation (LogLevel .IGNORE );
178+ var violationWarn = createViolation (LogLevel .WARN );
179+
180+ var violations = List .of (violationIgnored , violationWarn );
181+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
182+
183+ var result = openApiRequestValidator .validateResponseObject (request , response , null );
184+
185+ assertEquals (1 , result .size ());
186+ assertEquals (violationWarn , result .getFirst ());
187+ }
188+
189+ @ Test
190+ @ DisplayName ("When violation has log level IGNORE and another is excluded then both should not be returned" )
191+ public void testWhenResponseViolationHasLogLevelIgnoreAndIsExcludedThenItShouldNotBeReturned () {
192+ var uri = URI .create ("https://api.example.com/path" );
193+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
194+ var response = new ResponseMetaData (200 , "application/json" , new HashMap <>());
195+ var validationReport = mock (ValidationReport .class );
196+ when (validator .validateResponse (any (), any (), any ())).thenReturn (validationReport );
197+
198+ var violationIgnored = createViolation (LogLevel .IGNORE );
199+ var violationExcluded = createViolation (LogLevel .INFO );
200+ when (internalViolationExclusions .isExcluded (violationExcluded )).thenReturn (true );
201+ var violationError = createViolation (LogLevel .ERROR );
202+
203+ var violations = List .of (violationIgnored , violationExcluded , violationError );
204+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
205+
206+ var result = openApiRequestValidator .validateResponseObject (request , response , null );
207+
208+ assertEquals (1 , result .size ());
209+ assertEquals (violationError , result .getFirst ());
210+ }
211+
212+ @ Test
213+ @ DisplayName ("When all violations are ignored then empty list is returned" )
214+ public void testWhenAllResponseViolationsAreIgnoredThenEmptyListIsReturned () {
215+ var uri = URI .create ("https://api.example.com/path" );
216+ var request = new RequestMetaData ("GET" , uri , new HashMap <>());
217+ var response = new ResponseMetaData (200 , "application/json" , new HashMap <>());
218+ var validationReport = mock (ValidationReport .class );
219+ when (validator .validateResponse (any (), any (), any ())).thenReturn (validationReport );
220+
221+ var violation1 = createViolation (LogLevel .IGNORE );
222+ var violation2 = createViolation (LogLevel .IGNORE );
223+
224+ var violations = List .of (violation1 , violation2 );
225+ when (mapper .map (any (), any (), any (), any (), any ())).thenReturn (violations );
226+
227+ var result = openApiRequestValidator .validateResponseObject (request , response , null );
228+
229+ assertEquals (0 , result .size ());
230+ }
89231 }
90232
91233 private void verifyQueryParamValueEquals (
@@ -96,4 +238,10 @@ private void verifyQueryParamValueEquals(
96238 var ids = simpleRequestArgumentCaptor .getValue ().getQueryParameterValues (name ).iterator ().next ();
97239 assertEquals (expected , ids );
98240 }
241+
242+ private OpenApiViolation createViolation (LogLevel level ) {
243+ var violation = mock (OpenApiViolation .class );
244+ when (violation .getLevel ()).thenReturn (level );
245+ return violation ;
246+ }
99247}
0 commit comments