23
23
import okhttp3 .Headers ;
24
24
import okhttp3 .HttpUrl ;
25
25
import org .gridsuite .study .server .dto .*;
26
- import org .gridsuite .study .server .networkmodificationtree .dto .*;
26
+ import org .gridsuite .study .server .networkmodificationtree .dto .BuildStatus ;
27
+ import org .gridsuite .study .server .networkmodificationtree .dto .InsertMode ;
28
+ import org .gridsuite .study .server .networkmodificationtree .dto .NetworkModificationNode ;
29
+ import org .gridsuite .study .server .networkmodificationtree .dto .RootNode ;
27
30
import org .gridsuite .study .server .networkmodificationtree .entities .NodeBuildStatusEmbeddable ;
28
31
import org .gridsuite .study .server .networkmodificationtree .entities .RootNetworkNodeInfoEntity ;
29
32
import org .gridsuite .study .server .notification .NotificationService ;
30
33
import org .gridsuite .study .server .repository .StudyEntity ;
31
34
import org .gridsuite .study .server .repository .StudyRepository ;
32
- import org .gridsuite .study .server .repository .networkmodificationtree .NetworkModificationNodeInfoRepository ;
33
35
import org .gridsuite .study .server .repository .nonevacuatedenergy .NonEvacuatedEnergyParametersEntity ;
34
36
import org .gridsuite .study .server .repository .rootnetwork .RootNetworkNodeInfoRepository ;
35
37
import org .gridsuite .study .server .service .*;
36
- import org .gridsuite .study .server .service .LoadFlowService ;
37
38
import org .gridsuite .study .server .utils .TestUtils ;
38
39
import org .gridsuite .study .server .utils .elasticsearch .DisableElasticsearch ;
39
40
import org .jetbrains .annotations .NotNull ;
42
43
import org .junit .jupiter .api .BeforeEach ;
43
44
import org .junit .jupiter .api .Test ;
44
45
import org .junit .jupiter .api .extension .ExtendWith ;
45
- import org .mockito .Mockito ;
46
46
import org .skyscreamer .jsonassert .JSONAssert ;
47
47
import org .skyscreamer .jsonassert .JSONCompareMode ;
48
48
import org .slf4j .Logger ;
51
51
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
52
52
import org .springframework .boot .test .context .SpringBootTest ;
53
53
import org .springframework .boot .test .mock .mockito .MockBean ;
54
+ import org .springframework .boot .test .mock .mockito .SpyBean ;
54
55
import org .springframework .cloud .stream .binder .test .InputDestination ;
55
56
import org .springframework .cloud .stream .binder .test .OutputDestination ;
56
57
import org .springframework .http .HttpHeaders ;
71
72
import static org .gridsuite .study .server .dto .ComputationType .LOAD_FLOW ;
72
73
import static org .gridsuite .study .server .notification .NotificationService .*;
73
74
import static org .junit .jupiter .api .Assertions .*;
74
- import static org .mockito .ArgumentMatchers .* ;
75
+ import static org .mockito .ArgumentMatchers .any ;
75
76
import static org .mockito .Mockito .doAnswer ;
76
77
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
77
78
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
@@ -167,7 +168,7 @@ class LoadFlowTest {
167
168
private RootNetworkNodeInfoRepository rootNetworkNodeInfoRepository ;
168
169
@ Autowired
169
170
private RootNetworkNodeInfoService rootNetworkNodeInfoService ;
170
- @ Autowired
171
+ @ SpyBean
171
172
private StudyService studyService ;
172
173
@ MockBean
173
174
private NetworkModificationService networkModificationService ;
@@ -176,7 +177,7 @@ class LoadFlowTest {
176
177
@ Autowired
177
178
private TestUtils studyTestUtils ;
178
179
@ Autowired
179
- private NetworkModificationNodeInfoRepository networkModificationNodeInfoRepository ;
180
+ private ConsumerService consumerService ;
180
181
181
182
@ BeforeEach
182
183
void setup (final MockWebServer server ) throws Exception {
@@ -250,10 +251,6 @@ public MockResponse dispatch(RecordedRequest request) {
250
251
String path = Objects .requireNonNull (request .getPath ());
251
252
String method = Objects .requireNonNull (request .getMethod ());
252
253
if (path .matches ("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2 + ".*" )) {
253
- input .send (MessageBuilder .withPayload ("" )
254
- .setHeader ("resultUuid" , LOADFLOW_RESULT_UUID )
255
- .setHeader ("receiver" , "%7B%22nodeUuid%22%3A%22" + request .getPath ().split ("%" )[5 ].substring (4 ) + "%22%2C%20%22rootNetworkUuid%22%3A%20%22" + request .getPath ().split ("%" )[11 ].substring (4 ) + "%22%2C%20%22userId%22%3A%22userId%22%7D" )
256
- .build (), LOADFLOW_RESULT_DESTINATION );
257
254
return new MockResponse (200 , Headers .of (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ), loadFlowResultUuidStr );
258
255
} else if (path .matches ("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID )) {
259
256
input .send (MessageBuilder .withPayload ("" )
@@ -340,6 +337,25 @@ public MockResponse dispatch(RecordedRequest request) {
340
337
server .setDispatcher (dispatcher );
341
338
}
342
339
340
+ private void assertNodeBlocked (UUID nodeUuid , UUID rootNetworkUuid , boolean isNodeBlocked ) {
341
+ Optional <RootNetworkNodeInfoEntity > networkNodeInfoEntity = rootNetworkNodeInfoService .getRootNetworkNodeInfo (nodeUuid , rootNetworkUuid );
342
+ assertTrue (networkNodeInfoEntity .isPresent ());
343
+ assertEquals (isNodeBlocked , networkNodeInfoEntity .get ().getBlockedBuild ());
344
+ }
345
+
346
+ private void consumeLoadFlowResult (UUID studyUuid , UUID nodeUuid , UUID rootNetworkUuid ) throws JsonProcessingException {
347
+ assertNodeBlocked (nodeUuid , rootNetworkUuid , true );
348
+
349
+ // consume loadflow result
350
+ String resultUuidJson = objectMapper .writeValueAsString (new NodeReceiver (nodeUuid , rootNetworkUuid ));
351
+ MessageHeaders messageHeaders = new MessageHeaders (Map .of ("resultUuid" , LOADFLOW_RESULT_UUID , "withRatioTapChangers" , false , HEADER_RECEIVER , resultUuidJson ));
352
+ consumerService .consumeLoadFlowResult ().accept (MessageBuilder .createMessage ("" , messageHeaders ));
353
+ checkUpdateModelStatusMessagesReceived (studyUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
354
+ checkUpdateModelStatusMessagesReceived (studyUuid , NotificationService .UPDATE_TYPE_LOADFLOW_RESULT );
355
+
356
+ assertNodeBlocked (nodeUuid , rootNetworkUuid , false );
357
+ }
358
+
343
359
@ Test
344
360
void testLoadFlow (final MockWebServer server ) throws Exception {
345
361
MvcResult mvcResult ;
@@ -370,17 +386,15 @@ void testLoadFlow(final MockWebServer server) throws Exception {
370
386
.header ("userId" , "userId" ))
371
387
.andExpect (status ().isOk ());
372
388
373
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
374
-
375
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_RESULT );
376
-
377
389
// running loadflow now invalidate node children and their computations
378
390
checkUpdateModelsStatusMessagesReceived (studyNameUserIdUuid , modificationNode3Uuid );
379
391
380
392
checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
381
393
382
394
assertRequestsDone (server , List .of ("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2 , "/v1/parameters/" + LOADFLOW_PARAMETERS_UUID + "/provider" ));
383
395
396
+ consumeLoadFlowResult (studyNameUserIdUuid , modificationNode3Uuid , firstRootNetworkUuid );
397
+
384
398
// get loadflow result
385
399
mvcResult = mockMvc .perform (get ("/v1/studies/{studyUuid}/root-networks/{rootNetworkUuid}/nodes/{nodeUuid}/loadflow/result" , studyNameUserIdUuid , firstRootNetworkUuid , modificationNode3Uuid )).andExpectAll (
386
400
status ().isOk ()).andReturn ();
@@ -443,15 +457,14 @@ void testGetLimitViolations(final MockWebServer server) throws Exception {
443
457
.andExpect (status ().isOk ())
444
458
.andReturn ();
445
459
446
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
447
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_RESULT );
448
-
449
460
// running loadflow now invalidate node children and their computations
450
461
checkUpdateModelsStatusMessagesReceived (studyNameUserIdUuid , modificationNode1Uuid );
451
462
452
463
checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
453
464
assertRequestsDone (server , List .of ("/v1/parameters/" + LOADFLOW_PARAMETERS_UUID + "/provider" , "/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2 ));
454
465
466
+ consumeLoadFlowResult (studyNameUserIdUuid , modificationNode1Uuid , firstRootNetworkUuid );
467
+
455
468
// get computing status
456
469
mockMvc .perform (get ("/v1/studies/{studyUuid}/root-networks/{rootNetworkUuid}/nodes/{nodeUuid}/computation/result/enum-values?computingType={computingType}&enumName={enumName}" ,
457
470
studyNameUserIdUuid , firstRootNetworkUuid , modificationNode1Uuid , LOAD_FLOW , "computation-status" ))
@@ -500,15 +513,14 @@ void testInvalidateStatus(final MockWebServer server) throws Exception {
500
513
.andExpect (status ().isOk ())
501
514
.andReturn ();
502
515
503
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
504
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_RESULT );
505
-
506
516
// running loadflow now invalidate children and their computations
507
517
checkUpdateModelsStatusMessagesReceived (studyNameUserIdUuid , modificationNode1Uuid );
508
518
509
519
checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
510
520
assertRequestsDone (server , List .of ("/v1/parameters/" + LOADFLOW_PARAMETERS_UUID + "/provider" , "/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2 ));
511
521
522
+ consumeLoadFlowResult (studyNameUserIdUuid , modificationNode1Uuid , firstRootNetworkUuid );
523
+
512
524
// invalidate status
513
525
mockMvc .perform (put ("/v1/studies/{studyUuid}/loadflow/invalidate-status" , studyNameUserIdUuid )
514
526
.header ("userId" , "userId" )).andExpect (status ().isOk ());
@@ -545,14 +557,14 @@ void testDeleteLoadFlowResults(final MockWebServer server) throws Exception {
545
557
.header ("userId" , "userId" ))
546
558
.andExpect (status ().isOk ());
547
559
548
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
549
- checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_RESULT );
550
-
551
560
// running loadflow now invalidate node children and their computations
552
561
checkUpdateModelsStatusMessagesReceived (studyNameUserIdUuid , modificationNode3Uuid );
553
562
554
563
checkUpdateModelStatusMessagesReceived (studyNameUserIdUuid , NotificationService .UPDATE_TYPE_LOADFLOW_STATUS );
555
564
assertRequestsDone (server , List .of ("/v1/parameters/" + LOADFLOW_PARAMETERS_UUID + "/provider" , "/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\ ?withRatioTapChangers=.*&receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2 ));
565
+
566
+ consumeLoadFlowResult (studyNameUserIdUuid , modificationNode3Uuid , firstRootNetworkUuid );
567
+
556
568
//Test result count
557
569
testResultCount (server );
558
570
//Delete Voltage init results
@@ -573,7 +585,6 @@ void testResetUuidResultWhenLFFailed() throws Exception {
573
585
assertNotNull (rootNetworkNodeInfoService .getComputationResultUuid (modificationNode .getId (), rootNetworkUuid , LOAD_FLOW ));
574
586
assertEquals (resultUuid , rootNetworkNodeInfoService .getComputationResultUuid (modificationNode .getId (), rootNetworkUuid , LOAD_FLOW ));
575
587
576
- StudyService studyService = Mockito .mock (StudyService .class );
577
588
doAnswer (invocation -> {
578
589
input .send (MessageBuilder .withPayload ("" ).setHeader (HEADER_RECEIVER , resultUuidJson ).build (), LOADFLOW_FAILED_DESTINATION );
579
590
return resultUuid ;
0 commit comments