11package com .getyourguide .openapi .validation .core .throttle ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4- import static org .mockito .Mockito .mock ;
5- import static org .mockito .Mockito .when ;
64
7- import com .atlassian .oai .validator .model .ApiOperation ;
8- import com .atlassian .oai .validator .model .ApiPath ;
95import com .atlassian .oai .validator .model .Request ;
10- import com .atlassian .oai .validator .report .ValidationReport ;
116import com .getyourguide .openapi .validation .api .model .Direction ;
7+ import com .getyourguide .openapi .validation .api .model .OpenApiViolation ;
8+ import com .getyourguide .openapi .validation .api .model .RequestMetaData ;
9+ import java .net .URI ;
10+ import java .util .Collections ;
1211import java .util .Optional ;
1312import org .junit .jupiter .api .BeforeEach ;
1413import org .junit .jupiter .api .Test ;
@@ -27,85 +26,73 @@ public void beforeEach() {
2726
2827 @ Test
2928 public void testNotThrottledIfNoEntry () {
30- var message = mockMessage ( Request .Method .GET , "/path" , 200 );
29+ var violation = buildViolation ( DIRECTION , Request .Method .GET , "/path" , 200 );
3130
32- assertThrottled (false , message , DIRECTION );
31+ assertThrottled (false , violation );
3332 }
3433
3534 @ Test
3635 public void testThrottledIfEntryExists () {
37- var message = mockMessage ( Request .Method .GET , "/path" , 200 );
38- throttler .throttle (message , DIRECTION , NO_OP_RUNNABLE );
36+ var violation = buildViolation ( DIRECTION , Request .Method .GET , "/path" , 200 );
37+ throttler .throttle (violation , NO_OP_RUNNABLE );
3938
40- assertThrottled (true , message , DIRECTION );
39+ assertThrottled (true , violation );
4140 }
4241
4342 @ Test
4443 public void testNotThrottledIfSmallDifference () {
45- var message = mockMessage ( Request .Method .GET , "/path" , 200 );
46- throttler .throttle (message , DIRECTION , NO_OP_RUNNABLE );
44+ var violation = buildViolation ( DIRECTION , Request .Method .GET , "/path" , 200 );
45+ throttler .throttle (violation , NO_OP_RUNNABLE );
4746
48- assertThrottled (false , mockMessage ( Request .Method .GET , "/path" , 200 ), Direction . RESPONSE );
49- assertThrottled (false , mockMessage ( Request .Method .POST , "/path" , 200 ), DIRECTION );
50- assertThrottled (false , mockMessage ( Request .Method .GET , "/other-path" , 200 ), DIRECTION );
51- assertThrottled (false , mockMessage ( Request .Method .GET , "/path" , 402 ), DIRECTION );
47+ assertThrottled (false , buildViolation ( Direction . RESPONSE , Request .Method .GET , "/path" , 200 ));
48+ assertThrottled (false , buildViolation ( DIRECTION , Request .Method .POST , "/path" , 200 ));
49+ assertThrottled (false , buildViolation ( DIRECTION , Request .Method .GET , "/other-path" , 200 ));
50+ assertThrottled (false , buildViolation ( DIRECTION , Request .Method .GET , "/path" , 402 ));
5251 }
5352
5453 @ Test
5554 public void testThrottledIfInstanceContainsArrayIndex () {
56- var message = mockMessage ( Request .Method .GET , "/path" , 200 , "/items/1/name" , "/properties/items/items/properties/name" );
57- throttler .throttle (message , DIRECTION , NO_OP_RUNNABLE );
55+ var violation = buildViolation ( DIRECTION , Request .Method .GET , "/path" , 200 , "/items/1/name" , "/properties/items/items/properties/name" );
56+ throttler .throttle (violation , NO_OP_RUNNABLE );
5857
5958 assertThrottled (
6059 true ,
61- mockMessage (Request .Method .GET , "/path" , 200 , "/items/2/name" , "/properties/items/items/properties/name" ),
62- DIRECTION
60+ buildViolation (DIRECTION , Request .Method .GET , "/path" , 200 , "/items/2/name" , "/properties/items/items/properties/name" )
6361 );
6462 assertThrottled (
6563 true ,
66- mockMessage (Request .Method .GET , "/path" , 200 , "/items/3/name" , "/properties/items/items/properties/name" ),
67- DIRECTION
64+ buildViolation (DIRECTION , Request .Method .GET , "/path" , 200 , "/items/3/name" , "/properties/items/items/properties/name" )
6865 );
6966 assertThrottled (
7067 false ,
71- mockMessage (Request .Method .GET , "/path" , 200 , "/items/4/description" , "/properties/items/items/properties/description" ),
72- DIRECTION
68+ buildViolation (DIRECTION , Request .Method .GET , "/path" , 200 , "/items/4/description" , "/properties/items/items/properties/description" )
7369 );
7470 }
7571
76- private void assertThrottled (boolean expectThrottled , ValidationReport . Message message , Direction direction ) {
72+ private void assertThrottled (boolean expectThrottled , OpenApiViolation openApiViolation ) {
7773 var ref = new Object () {
7874 boolean wasThrottled = true ;
7975 };
8076
81- throttler .throttle (message , direction , () -> ref .wasThrottled = false );
77+ throttler .throttle (openApiViolation , () -> ref .wasThrottled = false );
8278
8379 assertEquals (expectThrottled , ref .wasThrottled );
8480 }
8581
86- private ValidationReport . Message mockMessage ( Request .Method method , String path , int status ) {
87- return mockMessage ( method , path , status , "/items/1/name" , "/properties/items/items/properties/name" );
82+ private OpenApiViolation buildViolation ( Direction direction , Request .Method method , String path , int status ) {
83+ return buildViolation ( direction , method , path , status , "/items/1/name" , "/properties/items/items/properties/name" );
8884 }
8985
90- private ValidationReport .Message mockMessage (Request .Method method , String path , int status , String instance , String schema ) {
91- var message = mock (ValidationReport .Message .class );
92- var context = mock (ValidationReport .MessageContext .class );
93-
94- when (context .getRequestMethod ()).thenReturn (Optional .of (method ));
95-
96- var apiOperation = mock (ApiOperation .class );
97- var apiPath = mock (ApiPath .class );
98- when (apiPath .normalised ()).thenReturn (path );
99- when (apiOperation .getApiPath ()).thenReturn (apiPath );
100- when (context .getApiOperation ()).thenReturn (Optional .of (apiOperation ));
101- when (context .getResponseStatus ()).thenReturn (Optional .of (status ));
102-
103- var pointers = mock (ValidationReport .MessageContext .Pointers .class );
104- when (pointers .getInstance ()).thenReturn (instance );
105- when (pointers .getSchema ()).thenReturn (schema );
106- when (context .getPointers ()).thenReturn (Optional .of (pointers ));
107-
108- when (message .getContext ()).thenReturn (Optional .of (context ));
109- return message ;
86+ private OpenApiViolation buildViolation (Direction direction , Request .Method method , String path , int status , String instance , String schema ) {
87+ return OpenApiViolation .builder ()
88+ .direction (direction )
89+ .requestMetaData (
90+ new RequestMetaData (method .toString (), URI .create ("https://example.com" + path ), Collections .emptyMap ())
91+ )
92+ .responseStatus (Optional .of (status ))
93+ .normalizedPath (Optional .of (path ))
94+ .instance (Optional .of (instance ))
95+ .schema (Optional .of (schema ))
96+ .build ();
11097 }
11198}
0 commit comments