6
6
*/
7
7
package org .gridsuite .securityanalysis .server .service ;
8
8
9
- import com .fasterxml .jackson .core .JsonProcessingException ;
10
9
import com .fasterxml .jackson .databind .ObjectMapper ;
11
10
import com .powsybl .contingency .BranchContingency ;
12
11
import com .powsybl .contingency .Contingency ;
16
15
import okhttp3 .mockwebserver .MockWebServer ;
17
16
import okhttp3 .mockwebserver .RecordedRequest ;
18
17
import org .gridsuite .securityanalysis .server .WebFluxConfig ;
18
+ import org .junit .After ;
19
19
import org .junit .Before ;
20
20
import org .junit .Test ;
21
21
import org .junit .runner .RunWith ;
22
+ import org .springframework .http .HttpStatus ;
22
23
import org .springframework .http .MediaType ;
23
24
import org .springframework .http .codec .json .Jackson2JsonDecoder ;
24
25
import org .springframework .http .codec .json .Jackson2JsonEncoder ;
27
28
import org .springframework .web .reactive .function .client .WebClient ;
28
29
29
30
import java .io .IOException ;
30
- import java .io .UncheckedIOException ;
31
31
import java .util .List ;
32
+ import java .util .Objects ;
32
33
import java .util .UUID ;
34
+ import java .util .stream .Collectors ;
35
+ import java .util .stream .IntStream ;
33
36
34
37
import static org .junit .Assert .assertEquals ;
35
38
36
39
/**
37
40
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
41
+ * @author Slimane Amar <slimane.amar at rte-france.com>
38
42
*/
39
43
@ RunWith (SpringRunner .class )
40
44
public class ActionsServiceTest {
41
45
46
+ private static final int DATA_BUFFER_LIMIT = 256 * 1024 ; // AbstractJackson2Decoder.maxInMemorySize
47
+
42
48
private static final String NETWORK_UUID = "7928181c-7977-4592-ba19-88027e4254e4" ;
43
49
44
50
private static final String LIST_NAME = "myList" ;
45
51
52
+ private static final String VERY_LARGE_LIST_NAME = "veryLargelist" ;
53
+
46
54
private static final Contingency CONTINGENCY = new Contingency ("c1" , new BranchContingency ("b1" ));
47
55
48
56
private final ObjectMapper objectMapper = WebFluxConfig .createObjectMapper ();
49
57
50
58
private WebClient .Builder webClientBuilder ;
51
59
60
+ private MockWebServer server ;
61
+
62
+ private ActionsService actionsService ;
63
+
52
64
@ Before
53
- public void setUp () {
65
+ public void setUp () throws IOException {
54
66
webClientBuilder = WebClient .builder ();
55
67
ExchangeStrategies strategies = ExchangeStrategies
56
68
.builder ()
@@ -60,41 +72,66 @@ public void setUp() {
60
72
61
73
}).build ();
62
74
webClientBuilder .exchangeStrategies (strategies );
75
+
76
+ actionsService = new ActionsService (webClientBuilder , initMockWebServer ());
63
77
}
64
78
65
- @ Test
66
- public void test () throws IOException {
67
- MockWebServer server = new MockWebServer ();
79
+ @ After
80
+ public void tearDown () {
81
+ try {
82
+ server .shutdown ();
83
+ } catch (Exception e ) {
84
+ // Nothing to do
85
+ }
86
+ }
87
+
88
+ private String initMockWebServer () throws IOException {
89
+ server = new MockWebServer ();
68
90
server .start ();
91
+
92
+ String jsonExpected = objectMapper .writeValueAsString (List .of (CONTINGENCY ));
93
+ String veryLargeJsonExpected = objectMapper .writeValueAsString (createVeryLargeList ());
94
+
69
95
final Dispatcher dispatcher = new Dispatcher () {
70
96
@ Override
71
97
public MockResponse dispatch (RecordedRequest request ) {
72
- if (( "/" + ActionsService . ACTIONS_API_VERSION + "/contingency-lists/" + LIST_NAME + "/export?networkUuid=" + NETWORK_UUID )
73
- .equals (request . getPath ( ))) {
74
- try {
75
- String json = objectMapper . writeValueAsString ( List . of ( CONTINGENCY ));
76
- return new MockResponse ()
77
- . setResponseCode ( 200 )
78
- . setBody ( json )
79
- . addHeader ( "Content-Type" , "application/json; charset=utf-8" );
80
- } catch ( JsonProcessingException e ) {
81
- throw new UncheckedIOException ( e );
82
- }
98
+ String requestPath = Objects . requireNonNull ( request . getPath ());
99
+ if ( requestPath .equals (String . format ( "/v1/contingency-lists/%s/export?networkUuid=%s" , LIST_NAME , NETWORK_UUID ))) {
100
+ return new MockResponse (). setResponseCode ( HttpStatus . OK . value ())
101
+ . setBody ( jsonExpected )
102
+ . addHeader ( "Content-Type" , "application/json; charset=utf-8" );
103
+ } else if ( requestPath . equals ( String . format ( "/v1/contingency-lists/%s/export?networkUuid=%s" , VERY_LARGE_LIST_NAME , NETWORK_UUID ))) {
104
+ return new MockResponse (). setResponseCode ( HttpStatus . OK . value () )
105
+ . setBody ( veryLargeJsonExpected )
106
+ . addHeader ( "Content-Type" , "application/json; charset=utf-8" );
107
+ } else {
108
+ return new MockResponse (). setResponseCode ( HttpStatus . NOT_FOUND . value ()). setBody ( "Path not supported: " + request . getPath ());
83
109
}
84
- return new MockResponse ().setResponseCode (404 );
85
110
}
86
111
};
112
+
87
113
server .setDispatcher (dispatcher );
88
114
89
- // get server base URL
115
+ // Ask the server for its URL. You'll need this to make HTTP requests.
90
116
HttpUrl baseHttpUrl = server .url ("" );
91
- String baseUrl = baseHttpUrl .toString ().substring (0 , baseHttpUrl .toString ().length () - 1 );
92
117
93
- ActionsService actionsService = new ActionsService (webClientBuilder , baseUrl );
118
+ return baseHttpUrl .toString ().substring (0 , baseHttpUrl .toString ().length () - 1 );
119
+ }
120
+
121
+ private List <Contingency > createVeryLargeList () {
122
+ return IntStream .range (0 , DATA_BUFFER_LIMIT ).mapToObj (i -> new Contingency ("l" + i , new BranchContingency ("l" + i ))).collect (Collectors .toList ());
123
+ }
94
124
95
- List <Contingency > list = actionsService .getContingencyList (LIST_NAME , UUID .fromString (NETWORK_UUID )).block ();
125
+ @ Test
126
+ public void test () {
127
+ List <Contingency > list = actionsService .getContingencyList (LIST_NAME , UUID .fromString (NETWORK_UUID )).collectList ().block ();
96
128
assertEquals (List .of (CONTINGENCY ), list );
129
+ }
97
130
98
- server .shutdown ();
131
+ @ Test
132
+ public void testVeryLargeList () {
133
+ // DataBufferLimitException should not be thrown with this message : "Exceeded limit on max bytes to buffer : DATA_BUFFER_LIMIT"
134
+ List <Contingency > list = actionsService .getContingencyList (VERY_LARGE_LIST_NAME , UUID .fromString (NETWORK_UUID )).collectList ().block ();
135
+ assertEquals (createVeryLargeList (), list );
99
136
}
100
137
}
0 commit comments