1414import com .github .tomakehurst .wiremock .WireMockServer ;
1515import com .github .tomakehurst .wiremock .client .WireMock ;
1616import com .powsybl .commons .report .ReportNode ;
17- import com .powsybl .iidm .network .Country ;
18- import com .powsybl .iidm .network .Network ;
19- import com .powsybl .iidm .network .ThreeSides ;
20- import com .powsybl .iidm .network .VariantManagerConstants ;
17+ import com .powsybl .iidm .network .*;
2118import com .powsybl .iidm .network .test .EurostagTutorialExample1Factory ;
2219import com .powsybl .loadflow .LoadFlowParameters ;
2320import com .powsybl .loadflow .LoadFlowResult ;
2926import org .assertj .core .api .Assertions ;
3027import org .gridsuite .computation .dto .GlobalFilter ;
3128import org .gridsuite .computation .dto .ResourceFilterDTO ;
29+ import org .gridsuite .computation .service .AbstractFilterService ;
3230import org .gridsuite .computation .service .ReportService ;
3331import org .gridsuite .computation .service .UuidGeneratorService ;
3432import org .gridsuite .computation .utils .SpecificationUtils ;
3533import org .gridsuite .securityanalysis .server .dto .*;
3634import org .gridsuite .securityanalysis .server .entities .AbstractLimitViolationEntity ;
35+ import org .gridsuite .securityanalysis .server .entities .ContingencyEntity ;
3736import org .gridsuite .securityanalysis .server .entities .SubjectLimitViolationEntity ;
3837import org .gridsuite .securityanalysis .server .repositories .SubjectLimitViolationRepository ;
3938import org .gridsuite .securityanalysis .server .service .ActionsService ;
40- import org .gridsuite .securityanalysis .server .service .FilterService ;
4139import org .gridsuite .securityanalysis .server .service .LoadFlowService ;
4240import org .gridsuite .securityanalysis .server .service .SecurityAnalysisWorkerService ;
4341import org .gridsuite .securityanalysis .server .util .ContextConfigurationWithTestChannel ;
4644import org .junit .jupiter .api .AfterEach ;
4745import org .junit .jupiter .api .BeforeEach ;
4846import org .junit .jupiter .api .Test ;
49- import org .mockito .Mock ;
5047import org .mockito .MockitoAnnotations ;
51- import org .mockito .stubbing .Answer ;
5248import org .springframework .beans .factory .annotation .Autowired ;
5349import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
5450import org .springframework .boot .test .context .SpringBootTest ;
5753import org .springframework .http .HttpHeaders ;
5854import org .springframework .http .MediaType ;
5955import org .springframework .messaging .Message ;
56+ import org .springframework .test .util .ReflectionTestUtils ;
6057import org .springframework .test .web .servlet .MockMvc ;
6158import org .springframework .test .web .servlet .MvcResult ;
6259import org .springframework .util .StreamUtils ;
8077import static org .gridsuite .securityanalysis .server .util .DatabaseQueryUtils .assertRequestsCount ;
8178import static org .gridsuite .securityanalysis .server .util .TestUtils .assertLogMessage ;
8279import static org .hamcrest .MatcherAssert .assertThat ;
83- import static org .junit .jupiter .api .Assertions .*;
80+ import static org .junit .jupiter .api .Assertions .assertEquals ;
81+ import static org .junit .jupiter .api .Assertions .assertNull ;
8482import static org .mockito .ArgumentMatchers .any ;
85- import static org .mockito .ArgumentMatchers .eq ;
8683import static org .mockito .BDDMockito .given ;
87- import static org .mockito .Mockito .doNothing ;
88- import static org .mockito .Mockito .when ;
84+ import static org .mockito .Mockito .*;
8985import static org .springframework .http .MediaType .APPLICATION_OCTET_STREAM_VALUE ;
9086import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
9187import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
@@ -105,7 +101,7 @@ class SecurityAnalysisControllerTest {
105101 private static final UUID RESULT_UUID = UUID .fromString ("0c8de370-3e6c-4d72-b292-d355a97e0d5d" );
106102 private static final UUID REPORT_UUID = UUID .fromString ("0c4de370-3e6a-4d72-b292-d355a97e0d53" );
107103 private static final UUID OTHER_RESULT_UUID = UUID .fromString ("0c8de370-3e6c-4d72-b292-d355a97e0d5a" );
108- private static final UUID LIST_UUID = UUID .randomUUID ( );
104+ private static final UUID LIST_FILTER_ID = UUID .fromString ( "762b72a8-8c0f-11ed-a1eb-0242ac120003" );
109105
110106 private static final int TIMEOUT = 1000 ;
111107
@@ -132,11 +128,8 @@ class SecurityAnalysisControllerTest {
132128 @ MockBean
133129 private UuidGeneratorService uuidGeneratorService ;
134130
135- @ Mock
136- private Network network ;
137-
138131 @ Autowired
139- private FilterService filterService ;
132+ private AbstractFilterService filterService ;
140133
141134 @ Autowired
142135 private SecurityAnalysisWorkerService workerService ;
@@ -173,9 +166,9 @@ void setUp() throws Exception {
173166 wireMockServer .start ();
174167
175168 MockitoAnnotations .initMocks (this );
176-
169+ ReflectionTestUtils . setField ( filterService , "filterServerBaseUri" , wireMockServer . baseUrl ());
177170 // network store service mocking
178- network = EurostagTutorialExample1Factory .create (new NetworkFactoryImpl ());
171+ Network network = EurostagTutorialExample1Factory .create (new NetworkFactoryImpl ());
179172 network .getVariantManager ().cloneVariant (VariantManagerConstants .INITIAL_VARIANT_ID , VARIANT_1_ID );
180173 network .getVariantManager ().cloneVariant (VariantManagerConstants .INITIAL_VARIANT_ID , VARIANT_2_ID );
181174 network .getVariantManager ().cloneVariant (VariantManagerConstants .INITIAL_VARIANT_ID , VARIANT_3_ID );
@@ -193,7 +186,6 @@ void setUp() throws Exception {
193186 given (uuidGeneratorService .generate ()).willReturn (RESULT_UUID );
194187
195188 doNothing ().when (reportService ).sendReport (any (UUID .class ), any (ReportNode .class ));
196- filterService = new FilterService (networkStoreService , wireMockServer .baseUrl ());
197189
198190 // SecurityAnalysis.Runner constructor is private..
199191 Constructor <SecurityAnalysis .Runner > constructor = SecurityAnalysis .Runner .class .getDeclaredConstructor (SecurityAnalysisProvider .class );
@@ -211,6 +203,10 @@ void setUp() throws Exception {
211203 wireMockServer .stubFor (WireMock .get (WireMock .urlMatching ("/v1/parameters/.*/values\\ ?provider=.*" ))
212204 .willReturn (WireMock .ok ().withHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ).withBody (mapper .writeValueAsString (loadFlowParametersValues ))));
213205
206+ wireMockServer .stubFor (WireMock .get (WireMock .urlMatching ("/v1/filters/metadata\\ ?ids=" + LIST_FILTER_ID ))
207+ .willReturn (WireMock .ok ()
208+ .withBody (mapper .writeValueAsString (List .of ()))
209+ .withHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ))).getId ();
214210 // purge messages
215211 while (output .receive (1000 , "sa.result" ) != null ) {
216212 }
@@ -335,7 +331,7 @@ void runAndSaveTest() throws Exception {
335331 status ().isOk (),
336332 content ().contentType (MediaType .APPLICATION_JSON ));
337333
338- assertFiltredResultN (NETWORK_UUID , VARIANT_2_ID );
334+ assertFiltredResultN ();
339335 checkNResultEnumFilters (RESULT_UUID );
340336
341337 mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/nmk-contingencies-result/paged" ))
@@ -389,7 +385,7 @@ void testDeterministicResults() throws Exception {
389385 status ().isOk (),
390386 content ().contentType (MediaType .APPLICATION_JSON ));
391387
392- assertFiltredResultN (NETWORK_UUID , VARIANT_2_ID );
388+ assertFiltredResultN ();
393389
394390 var res = mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/nmk-constraints-result/paged" ))
395391 .andExpectAll (
@@ -422,35 +418,68 @@ void testDeterministicResults() throws Exception {
422418 assertEquals (expectedResultInOrder , result );
423419 }
424420
425- private static String buildFilterUrl (UUID networkUuid , String variantId ) throws JsonProcessingException {
421+ private static String buildFilterUrl () throws JsonProcessingException {
426422 List <ResourceFilterDTO > filters = List .of (new ResourceFilterDTO (ResourceFilterDTO .DataType .TEXT , ResourceFilterDTO .Type .STARTS_WITH , "vl1" , AbstractLimitViolationEntity .Fields .subjectLimitViolation + SpecificationUtils .FIELD_SEPARATOR + SubjectLimitViolationEntity .Fields .subjectId ),
427423 new ResourceFilterDTO (ResourceFilterDTO .DataType .TEXT , ResourceFilterDTO .Type .EQUALS , new String []{"HIGH_VOLTAGE" }, AbstractLimitViolationEntity .Fields .limitType ),
428424 new ResourceFilterDTO (ResourceFilterDTO .DataType .NUMBER , ResourceFilterDTO .Type .GREATER_THAN_OR_EQUAL , "399" , AbstractLimitViolationEntity .Fields .limit ),
429425 new ResourceFilterDTO (ResourceFilterDTO .DataType .NUMBER , ResourceFilterDTO .Type .LESS_THAN_OR_EQUAL , "420" , AbstractLimitViolationEntity .Fields .value ),
430426 new ResourceFilterDTO (ResourceFilterDTO .DataType .NUMBER , ResourceFilterDTO .Type .NOT_EQUAL , "2" , AbstractLimitViolationEntity .Fields .acceptableDuration )
431427 );
432428 GlobalFilter globalFilter = GlobalFilter .builder ()
433- .genericFilter (List .of (LIST_UUID ))
429+ .genericFilter (List .of (LIST_FILTER_ID ))
434430 .nominalV (List .of ("400.0" ))
435431 .countryCode (List .of (Country .FR ))
436432 .build ();
437433 String jsonFilters = new ObjectMapper ().writeValueAsString (filters );
438434 String jsonGlobalFilters = new ObjectMapper ().writeValueAsString (globalFilter );
439435 return "filters=" + URLEncoder .encode (jsonFilters , StandardCharsets .UTF_8 ) +
440- "&globalFilters=" + URLEncoder .encode (jsonGlobalFilters , StandardCharsets .UTF_8 ) + "&networkUuid=" + networkUuid + "&variantId=" + variantId ;
436+ "&globalFilters=" + URLEncoder .encode (jsonGlobalFilters , StandardCharsets .UTF_8 ) + "&networkUuid=" + NETWORK_UUID + "&variantId=" + "initialState" ;
441437 }
442438
443- private void assertFiltredResultN (UUID networkUuid , String variantId ) throws Exception {
444- MvcResult mvcResult = mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/n-result?" + buildFilterUrl (networkUuid , variantId )))
445- .andExpectAll (
446- status ().isOk (),
447- content ().contentType (MediaType .APPLICATION_JSON )
448- ).andReturn ();
439+ private void assertFiltredResultN () throws Exception {
440+ Network network = mock (Network .class );
441+ VariantManager variantManager = mock (VariantManager .class );
442+ when (network .getVariantManager ()).thenReturn (variantManager );
443+ doNothing ().when (variantManager ).setWorkingVariant (anyString ());
444+
445+ when (networkStoreService .getNetwork (NETWORK_UUID , PreloadingStrategy .COLLECTION )).thenReturn (network );
446+
447+ // test - n-result endpoint
448+ MvcResult mvcResult = mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/n-result?" + buildFilterUrl ()))
449+ .andExpectAll (status ().isOk ()).andReturn ();
449450 String resultAsString = mvcResult .getResponse ().getContentAsString ();
450451 List <PreContingencyLimitViolationResultDTO > preContingencyResult = mapper .readValue (resultAsString , new TypeReference <>() { });
451452 assertEquals (1 , preContingencyResult .size ());
452453 assertEquals ("vl1 (VLGEN_0, VLLOAD_0)" , preContingencyResult .get (0 ).getLimitViolation ().getLocationId ());
454+ }
455+
456+ private void assertFiltredResultNmkConstraints () throws Exception {
457+ Network network = mock (Network .class );
458+ VariantManager variantManager = mock (VariantManager .class );
459+ when (network .getVariantManager ()).thenReturn (variantManager );
460+ doNothing ().when (variantManager ).setWorkingVariant (anyString ());
461+
462+ when (networkStoreService .getNetwork (NETWORK_UUID , PreloadingStrategy .COLLECTION )).thenReturn (network );
453463
464+ // test - nmk-constraints-result/paged endpoint
465+ MvcResult mvcResult = mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/nmk-constraints-result/paged?" + buildFilterUrl ())).andExpectAll (status ().isOk ()).andReturn ();
466+ String resultAsString = mvcResult .getResponse ().getContentAsString ();
467+ List <SubjectLimitViolationEntity > subjectLimitViolationList = mapper .readValue (resultAsString , new TypeReference <>() { });
468+ }
469+
470+ private void assertFiltredResultNmkContingencies () throws Exception {
471+ Network network = mock (Network .class );
472+ VariantManager variantManager = mock (VariantManager .class );
473+ when (network .getVariantManager ()).thenReturn (variantManager );
474+ doNothing ().when (variantManager ).setWorkingVariant (anyString ());
475+
476+ when (networkStoreService .getNetwork (NETWORK_UUID , PreloadingStrategy .COLLECTION )).thenReturn (network );
477+
478+ // test - nmk-contingencies-result/paged endpoint
479+ MvcResult mvcResult = mockMvc .perform (get ("/" + VERSION + "/results/" + RESULT_UUID + "/nmk-contingencies-result/paged?" + buildFilterUrl ()))
480+ .andExpectAll (status ().isOk ()).andReturn ();
481+ String resultAsString = mvcResult .getResponse ().getContentAsString ();
482+ List <ContingencyEntity > contingencyResult = mapper .readValue (resultAsString , new TypeReference <>() { });
454483 }
455484
456485 private void checkNResultEnumFilters (UUID resultUuid ) throws Exception {
0 commit comments