12
12
import com .powsybl .network .store .client .PreloadingStrategy ;
13
13
import org .gridsuite .securityanalysis .server .service .ActionsService ;
14
14
import org .gridsuite .securityanalysis .server .service .SecurityAnalysisConfigService ;
15
- import org .gridsuite .securityanalysis .server .service .SecurityAnalysisService ;
16
15
import org .gridsuite .securityanalysis .server .service .UuidGeneratorService ;
17
16
import org .junit .Before ;
18
17
import org .junit .Test ;
37
36
38
37
import static com .powsybl .network .store .model .NetworkStoreApi .VERSION ;
39
38
import static org .gridsuite .securityanalysis .server .SecurityAnalysisFactoryMock .*;
39
+ import static org .gridsuite .securityanalysis .server .service .SecurityAnalysisStoppedPublisherService .CANCEL_MESSAGE ;
40
+ import static org .gridsuite .securityanalysis .server .service .SecurityAnalysisStoppedPublisherService .FAIL_MESSAGE ;
40
41
import static org .junit .Assert .assertEquals ;
42
+ import static org .junit .Assert .assertNull ;
41
43
import static org .mockito .BDDMockito .given ;
42
44
43
45
/**
@@ -59,6 +61,8 @@ public class SecurityAnalysisControllerTest extends AbstractEmbeddedCassandraSet
59
61
60
62
private static final String EXPECTED_FILTERED_JSON = "{\" version\" :\" 1.0\" ,\" preContingencyResult\" :{\" computationOk\" :true,\" limitViolations\" :[{\" subjectId\" :\" l3\" ,\" limitType\" :\" CURRENT\" ,\" acceptableDuration\" :1200,\" limit\" :10.0,\" limitReduction\" :1.0,\" value\" :11.0,\" side\" :\" ONE\" }],\" actionsTaken\" :[]},\" postContingencyResults\" :[{\" contingency\" :{\" id\" :\" l1\" ,\" elements\" :[{\" id\" :\" l1\" ,\" type\" :\" BRANCH\" }]},\" limitViolationsResult\" :{\" computationOk\" :true,\" limitViolations\" :[],\" actionsTaken\" :[]}},{\" contingency\" :{\" id\" :\" l2\" ,\" elements\" :[{\" id\" :\" l2\" ,\" type\" :\" BRANCH\" }]},\" limitViolationsResult\" :{\" computationOk\" :true,\" limitViolations\" :[],\" actionsTaken\" :[]}}]}" ;
61
63
64
+ private static final String ERROR_MESSAGE = "Error message test" ;
65
+
62
66
@ Autowired
63
67
private OutputDestination output ;
64
68
@@ -74,9 +78,6 @@ public class SecurityAnalysisControllerTest extends AbstractEmbeddedCassandraSet
74
78
@ MockBean
75
79
private UuidGeneratorService uuidGeneratorService ;
76
80
77
- @ Autowired
78
- private SecurityAnalysisService securityAnalysisService ;
79
-
80
81
@ Autowired
81
82
private SecurityAnalysisConfigService configService ;
82
83
@@ -96,12 +97,18 @@ public void setUp() {
96
97
.willReturn (Flux .fromIterable (SecurityAnalysisFactoryMock .CONTINGENCIES ));
97
98
given (actionsService .getContingencyList (CONTINGENCY_LIST2_NAME , NETWORK_UUID ))
98
99
.willReturn (Flux .fromIterable (SecurityAnalysisFactoryMock .CONTINGENCIES ));
100
+ given (actionsService .getContingencyList (CONTINGENCY_LIST_ERROR_NAME , NETWORK_UUID ))
101
+ .willReturn (Flux .fromIterable (SecurityAnalysisFactoryMock .CONTINGENCIES ).thenMany (Flux .error (new RuntimeException (ERROR_MESSAGE ))));
99
102
100
103
// UUID service mocking to always generate the same result UUID
101
104
given (uuidGeneratorService .generate ()).willReturn (RESULT_UUID );
102
105
103
106
// mock the powsybl security analysis service
104
107
configService .setSecurityAnalysisFactoryClass (SecurityAnalysisFactoryMock .class .getName ());
108
+
109
+ // purge messages
110
+ while (output .receive (1000 ) != null ) {
111
+ }
105
112
}
106
113
107
114
@ Test
@@ -244,14 +251,59 @@ public void stopTest() {
244
251
.expectBody (UUID .class )
245
252
.isEqualTo (RESULT_UUID );
246
253
254
+ Message <byte []> message = output .receive (1000 , "sa.run.destination" );
255
+ assertEquals (NETWORK_UUID .toString (), message .getHeaders ().get ("networkUuid" ));
256
+ assertEquals (RESULT_UUID .toString (), message .getHeaders ().get ("resultUuid" ));
257
+ assertEquals (CONTINGENCY_LIST_NAME , message .getHeaders ().get ("contingencyListNames" ));
258
+ assertEquals ("me" , message .getHeaders ().get ("receiver" ));
259
+
247
260
webTestClient .put ()
248
261
.uri ("/" + VERSION + "/results/" + RESULT_UUID + "/stop"
249
262
+ "?receiver=me" )
250
263
.exchange ()
251
264
.expectStatus ().isOk ();
252
265
253
- Message <byte []> cancelMessage = output .receive (1000 , "sa.cancel.destination" );
254
- assertEquals (RESULT_UUID .toString (), cancelMessage .getHeaders ().get ("resultUuid" ));
255
- assertEquals ("me" , cancelMessage .getHeaders ().get ("receiver" ));
266
+ message = output .receive (1000 , "sa.cancel.destination" );
267
+ assertEquals (RESULT_UUID .toString (), message .getHeaders ().get ("resultUuid" ));
268
+ assertEquals ("me" , message .getHeaders ().get ("receiver" ));
269
+
270
+ message = output .receive (1000 , "sa.stopped.destination" );
271
+ assertEquals (RESULT_UUID .toString (), message .getHeaders ().get ("resultUuid" ));
272
+ assertEquals ("me" , message .getHeaders ().get ("receiver" ));
273
+ assertEquals (CANCEL_MESSAGE , message .getHeaders ().get ("message" ));
274
+
275
+ assertNull (output .receive (1000 ));
276
+ }
277
+
278
+ @ Test
279
+ public void runTestWithError () {
280
+ webTestClient .post ()
281
+ .uri ("/" + VERSION + "/networks/" + NETWORK_UUID + "/run-and-save?contingencyListName=" + CONTINGENCY_LIST_ERROR_NAME
282
+ + "&receiver=me" )
283
+ .exchange ()
284
+ .expectStatus ().isOk () // Because fully asynchronous (just publish a message)
285
+ .expectHeader ().contentType (MediaType .APPLICATION_JSON )
286
+ .expectBody (UUID .class )
287
+ .isEqualTo (RESULT_UUID );
288
+
289
+ Message <byte []> message = output .receive (1000 , "sa.run.destination" );
290
+ assertEquals (NETWORK_UUID .toString (), message .getHeaders ().get ("networkUuid" ));
291
+ assertEquals (RESULT_UUID .toString (), message .getHeaders ().get ("resultUuid" ));
292
+ assertEquals (CONTINGENCY_LIST_ERROR_NAME , message .getHeaders ().get ("contingencyListNames" ));
293
+ assertEquals ("me" , message .getHeaders ().get ("receiver" ));
294
+
295
+ // Message stopped has been sent
296
+ message = output .receive (1000 , "sa.stopped.destination" );
297
+ assertEquals (RESULT_UUID .toString (), message .getHeaders ().get ("resultUuid" ));
298
+ assertEquals ("me" , message .getHeaders ().get ("receiver" ));
299
+ assertEquals (FAIL_MESSAGE + " : " + ERROR_MESSAGE , message .getHeaders ().get ("message" ));
300
+
301
+ assertNull (output .receive (1000 ));
302
+
303
+ // No result
304
+ webTestClient .get ()
305
+ .uri ("/" + VERSION + "/results/" + RESULT_UUID )
306
+ .exchange ()
307
+ .expectStatus ().isNotFound ();
256
308
}
257
309
}
0 commit comments