1
1
package com .getyourguide .openapi .validation .core .throttle ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
- import static org .mockito .Mockito .mock ;
5
- import static org .mockito .Mockito .when ;
6
4
7
- import com .atlassian .oai .validator .model .ApiOperation ;
8
- import com .atlassian .oai .validator .model .ApiPath ;
9
5
import com .atlassian .oai .validator .model .Request ;
10
- import com .atlassian .oai .validator .report .ValidationReport ;
11
6
import 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 ;
12
11
import java .util .Optional ;
13
12
import org .junit .jupiter .api .BeforeEach ;
14
13
import org .junit .jupiter .api .Test ;
@@ -27,85 +26,73 @@ public void beforeEach() {
27
26
28
27
@ Test
29
28
public void testNotThrottledIfNoEntry () {
30
- var message = mockMessage ( Request .Method .GET , "/path" , 200 );
29
+ var violation = buildViolation ( DIRECTION , Request .Method .GET , "/path" , 200 );
31
30
32
- assertThrottled (false , message , DIRECTION );
31
+ assertThrottled (false , violation );
33
32
}
34
33
35
34
@ Test
36
35
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 );
39
38
40
- assertThrottled (true , message , DIRECTION );
39
+ assertThrottled (true , violation );
41
40
}
42
41
43
42
@ Test
44
43
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 );
47
46
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 ));
52
51
}
53
52
54
53
@ Test
55
54
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 );
58
57
59
58
assertThrottled (
60
59
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" )
63
61
);
64
62
assertThrottled (
65
63
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" )
68
65
);
69
66
assertThrottled (
70
67
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" )
73
69
);
74
70
}
75
71
76
- private void assertThrottled (boolean expectThrottled , ValidationReport . Message message , Direction direction ) {
72
+ private void assertThrottled (boolean expectThrottled , OpenApiViolation openApiViolation ) {
77
73
var ref = new Object () {
78
74
boolean wasThrottled = true ;
79
75
};
80
76
81
- throttler .throttle (message , direction , () -> ref .wasThrottled = false );
77
+ throttler .throttle (openApiViolation , () -> ref .wasThrottled = false );
82
78
83
79
assertEquals (expectThrottled , ref .wasThrottled );
84
80
}
85
81
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" );
88
84
}
89
85
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 ();
110
97
}
111
98
}
0 commit comments