From 27efb125df167ce7b143830a9f2089867ca4e078 Mon Sep 17 00:00:00 2001 From: AAJELLAL Date: Tue, 31 Dec 2024 16:19:34 +0100 Subject: [PATCH] SA results: add locationId to entities and dtos. Signed-off-by: AAJELLAL --- pom.xml | 5 ++ ...PreContingencyLimitViolationResultDTO.java | 6 ++- .../server/dto/SubjectLimitViolationDTO.java | 6 +++ .../dto/SubjectLimitViolationResultDTO.java | 3 ++ .../server/entities/ContingencyEntity.java | 5 +- .../ContingencyLimitViolationEntity.java | 5 +- .../PreContingencyLimitViolationEntity.java | 9 ++-- .../SecurityAnalysisResultEntity.java | 7 +-- .../entities/SubjectLimitViolationEntity.java | 5 ++ .../SecurityAnalysisResultService.java | 5 +- .../SecurityAnalysisWorkerService.java | 2 +- .../db/changelog/db.changelog-master.yaml | 3 ++ .../server/FindContingenciesTest.java | 47 +++++++++++++++++- .../FindPreContingencyLimitViolationTest.java | 46 +++++++++++++++++- .../FindSubjectLimitViolationsTest.java | 47 +++++++++++++++++- .../server/SecurityAnalysisProviderMock.java | 5 ++ .../SecurityAnalysisResultServiceTest.java | 48 ++++++++++++++++++- 17 files changed, 236 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 3f616b35..689a6b68 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,9 @@ org.gridsuite.securityanalysis.server 1.0.5 5.0.0-alpha.14 + + 1.17.0 @@ -144,6 +147,8 @@ com.powsybl powsybl-ws-commons + ${powsybl-ws-commons.version} + org.springframework.cloud diff --git a/src/main/java/org/gridsuite/securityanalysis/server/dto/PreContingencyLimitViolationResultDTO.java b/src/main/java/org/gridsuite/securityanalysis/server/dto/PreContingencyLimitViolationResultDTO.java index 6555757c..6083c552 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/dto/PreContingencyLimitViolationResultDTO.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/dto/PreContingencyLimitViolationResultDTO.java @@ -24,6 +24,7 @@ public class PreContingencyLimitViolationResultDTO { private String subjectId; + private String locationId; private String status; private LimitViolationDTO limitViolation; @@ -31,9 +32,12 @@ public static PreContingencyLimitViolationResultDTO toDto(PreContingencyLimitVio String subjectId = preContingencyLimitViolation.getSubjectLimitViolation() != null ? preContingencyLimitViolation.getSubjectLimitViolation().getSubjectId() : null; - + String locationId = preContingencyLimitViolation.getSubjectLimitViolation() != null + ? preContingencyLimitViolation.getSubjectLimitViolation().getLocationId() + : null; return PreContingencyLimitViolationResultDTO.builder() .subjectId(subjectId) + .locationId(locationId) .status(preContingencyLimitViolation.getResult().getPreContingencyStatus()) .limitViolation(LimitViolationDTO.toDto(preContingencyLimitViolation)) .build(); diff --git a/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationDTO.java b/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationDTO.java index 67e6cad0..b47bb046 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationDTO.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationDTO.java @@ -21,6 +21,7 @@ @Builder public class SubjectLimitViolationDTO { private String subjectId; + private String locationId; private LimitViolationDTO limitViolation; @@ -29,8 +30,13 @@ public static SubjectLimitViolationDTO toDto(ContingencyLimitViolationEntity lim ? limitViolation.getSubjectLimitViolation().getSubjectId() : null; + String locationId = limitViolation.getSubjectLimitViolation() != null + ? limitViolation.getSubjectLimitViolation().getLocationId() + : null; + return SubjectLimitViolationDTO.builder() .subjectId(subjectId) + .locationId(locationId) .limitViolation(LimitViolationDTO.toDto(limitViolation)) .build(); } diff --git a/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationResultDTO.java b/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationResultDTO.java index a71338af..afe16692 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationResultDTO.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/dto/SubjectLimitViolationResultDTO.java @@ -28,6 +28,8 @@ public class SubjectLimitViolationResultDTO { private String subjectId; + private String locationId; + private List contingencies; public static SubjectLimitViolationResultDTO toDto(SubjectLimitViolationEntity subjectLimitViolation) { @@ -37,6 +39,7 @@ public static SubjectLimitViolationResultDTO toDto(SubjectLimitViolationEntity s return SubjectLimitViolationResultDTO.builder() .subjectId(subjectLimitViolation.getSubjectId()) + .locationId(subjectLimitViolation.getLocationId()) .contingencies(contingencies) .build(); } diff --git a/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyEntity.java b/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyEntity.java index 0f883dca..194068ba 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyEntity.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyEntity.java @@ -6,6 +6,7 @@ */ package org.gridsuite.securityanalysis.server.entities; +import com.powsybl.iidm.network.Network; import com.powsybl.security.results.PostContingencyResult; import jakarta.persistence.*; import lombok.Getter; @@ -64,11 +65,11 @@ private void setContingencyLimitViolations(List } } - public static ContingencyEntity toEntity(PostContingencyResult postContingencyResult, Map subjectLimitViolationsBySubjectId) { + public static ContingencyEntity toEntity(Network network, PostContingencyResult postContingencyResult, Map subjectLimitViolationsBySubjectId) { List contingencyElements = postContingencyResult.getContingency().getElements().stream().map(contingencyElement -> ContingencyElementEmbeddable.toEntity(contingencyElement)).collect(Collectors.toList()); List contingencyLimitViolations = postContingencyResult.getLimitViolationsResult().getLimitViolations().stream() - .map(limitViolation -> ContingencyLimitViolationEntity.toEntity(limitViolation, subjectLimitViolationsBySubjectId.get(limitViolation.getSubjectId()))) + .map(limitViolation -> ContingencyLimitViolationEntity.toEntity(network, limitViolation, subjectLimitViolationsBySubjectId.get(limitViolation.getSubjectId()))) .collect(Collectors.toList()); return new ContingencyEntity(postContingencyResult.getContingency().getId(), postContingencyResult.getStatus().name(), contingencyElements, contingencyLimitViolations); } diff --git a/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyLimitViolationEntity.java b/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyLimitViolationEntity.java index 3db04004..9ebd0a82 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyLimitViolationEntity.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/entities/ContingencyLimitViolationEntity.java @@ -6,7 +6,9 @@ */ package org.gridsuite.securityanalysis.server.entities; +import com.powsybl.iidm.network.Network; import com.powsybl.security.LimitViolation; +import com.powsybl.ws.commons.computation.utils.ComputationResultUtils; import jakarta.persistence.*; import lombok.*; import lombok.experimental.FieldNameConstants; @@ -30,7 +32,8 @@ public class ContingencyLimitViolationEntity extends AbstractLimitViolationEntit @Setter private ContingencyEntity contingency; - public static ContingencyLimitViolationEntity toEntity(LimitViolation limitViolation, SubjectLimitViolationEntity subjectLimitViolation) { + public static ContingencyLimitViolationEntity toEntity(Network network, LimitViolation limitViolation, SubjectLimitViolationEntity subjectLimitViolation) { + subjectLimitViolation.setLocationId(ComputationResultUtils.getViolationLocationId(limitViolation, network)); ContingencyLimitViolationEntity contingencyLimitViolationEntity = ContingencyLimitViolationEntity.builder() .limit(limitViolation.getLimit()) .limitName(limitViolation.getLimitName()) diff --git a/src/main/java/org/gridsuite/securityanalysis/server/entities/PreContingencyLimitViolationEntity.java b/src/main/java/org/gridsuite/securityanalysis/server/entities/PreContingencyLimitViolationEntity.java index 9a7ab5a1..c426e37b 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/entities/PreContingencyLimitViolationEntity.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/entities/PreContingencyLimitViolationEntity.java @@ -6,8 +6,10 @@ */ package org.gridsuite.securityanalysis.server.entities; +import com.powsybl.iidm.network.Network; import com.powsybl.security.LimitViolation; import com.powsybl.security.results.PreContingencyResult; +import com.powsybl.ws.commons.computation.utils.ComputationResultUtils; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.ManyToOne; @@ -40,11 +42,12 @@ public class PreContingencyLimitViolationEntity extends AbstractLimitViolationEn @Setter SecurityAnalysisResultEntity result; - public static List toEntityList(PreContingencyResult preContingencyResult, Map subjectLimitViolationsBySubjectId) { - return preContingencyResult.getLimitViolationsResult().getLimitViolations().stream().map(limitViolation -> toEntity(limitViolation, subjectLimitViolationsBySubjectId.get(limitViolation.getSubjectId()))).collect(Collectors.toList()); + public static List toEntityList(Network network, PreContingencyResult preContingencyResult, Map subjectLimitViolationsBySubjectId) { + return preContingencyResult.getLimitViolationsResult().getLimitViolations().stream().map(limitViolation -> toEntity(network, limitViolation, subjectLimitViolationsBySubjectId.get(limitViolation.getSubjectId()))).collect(Collectors.toList()); } - public static PreContingencyLimitViolationEntity toEntity(LimitViolation limitViolation, SubjectLimitViolationEntity subjectLimitViolation) { + public static PreContingencyLimitViolationEntity toEntity(Network network, LimitViolation limitViolation, SubjectLimitViolationEntity subjectLimitViolation) { + subjectLimitViolation.setLocationId(ComputationResultUtils.getViolationLocationId(limitViolation, network)); return PreContingencyLimitViolationEntity.builder() .subjectLimitViolation(subjectLimitViolation) .limit(limitViolation.getLimit()) diff --git a/src/main/java/org/gridsuite/securityanalysis/server/entities/SecurityAnalysisResultEntity.java b/src/main/java/org/gridsuite/securityanalysis/server/entities/SecurityAnalysisResultEntity.java index afd34e19..6d192143 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/entities/SecurityAnalysisResultEntity.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/entities/SecurityAnalysisResultEntity.java @@ -6,6 +6,7 @@ */ package org.gridsuite.securityanalysis.server.entities; +import com.powsybl.iidm.network.Network; import com.powsybl.security.SecurityAnalysisResult; import jakarta.persistence.*; import lombok.*; @@ -52,7 +53,7 @@ public SecurityAnalysisResultEntity(UUID id) { this.id = id; } - public static SecurityAnalysisResultEntity toEntity(UUID resultUuid, SecurityAnalysisResult securityAnalysisResult, SecurityAnalysisStatus securityAnalysisStatus) { + public static SecurityAnalysisResultEntity toEntity(Network network, UUID resultUuid, SecurityAnalysisResult securityAnalysisResult, SecurityAnalysisStatus securityAnalysisStatus) { Map subjectLimitViolationsBySubjectId = getUniqueSubjectLimitViolationsFromResult(securityAnalysisResult) .stream().collect(Collectors.toMap( SubjectLimitViolationEntity::getSubjectId, @@ -60,9 +61,9 @@ public static SecurityAnalysisResultEntity toEntity(UUID resultUuid, SecurityAna ); List contingencies = securityAnalysisResult.getPostContingencyResults().stream() - .map(postContingencyResult -> ContingencyEntity.toEntity(postContingencyResult, subjectLimitViolationsBySubjectId)).collect(Collectors.toList()); + .map(postContingencyResult -> ContingencyEntity.toEntity(network, postContingencyResult, subjectLimitViolationsBySubjectId)).collect(Collectors.toList()); - List preContingencyLimitViolations = PreContingencyLimitViolationEntity.toEntityList(securityAnalysisResult.getPreContingencyResult(), subjectLimitViolationsBySubjectId); + List preContingencyLimitViolations = PreContingencyLimitViolationEntity.toEntityList(network, securityAnalysisResult.getPreContingencyResult(), subjectLimitViolationsBySubjectId); List subjectLimitViolations = Stream.concat( contingencies.stream().flatMap(c -> c.getContingencyLimitViolations().stream()), diff --git a/src/main/java/org/gridsuite/securityanalysis/server/entities/SubjectLimitViolationEntity.java b/src/main/java/org/gridsuite/securityanalysis/server/entities/SubjectLimitViolationEntity.java index 64e637df..e7d65985 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/entities/SubjectLimitViolationEntity.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/entities/SubjectLimitViolationEntity.java @@ -39,6 +39,11 @@ public SubjectLimitViolationEntity(String subjectId, String subjectName) { private String subjectName; + @Column + @Setter + @Getter + private String locationId; + @ManyToOne(fetch = FetchType.LAZY) @Setter @Getter diff --git a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java index 1e59f723..2226dd39 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.ThreeSides; import com.powsybl.security.LimitViolationType; import com.powsybl.security.SecurityAnalysisResult; @@ -228,11 +229,11 @@ public void assertResultExists(UUID resultUuid) { } @Transactional - public void insert(UUID resultUuid, SecurityAnalysisResult result, SecurityAnalysisStatus status) { + public void insert(Network network, UUID resultUuid, SecurityAnalysisResult result, SecurityAnalysisStatus status) { Objects.requireNonNull(resultUuid); Objects.requireNonNull(result); - SecurityAnalysisResultEntity securityAnalysisResult = SecurityAnalysisResultEntity.toEntity(resultUuid, result, status); + SecurityAnalysisResultEntity securityAnalysisResult = SecurityAnalysisResultEntity.toEntity(network, resultUuid, result, status); securityAnalysisResultRepository.save(securityAnalysisResult); } diff --git a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java index 1bbdcdea..6f8a3cf4 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java @@ -242,7 +242,7 @@ protected void postRun(SecurityAnalysisRunContext runContext, AtomicReference resultContext, SecurityAnalysisResult result) { - resultService.insert( + resultService.insert(network, resultContext.getResultUuid(), result, result.getPreContingencyResult().getStatus() == LoadFlowResult.ComponentResult.Status.CONVERGED diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index bfcbcc95..f4b720f1 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -36,3 +36,6 @@ databaseChangeLog: - include: file: changesets/changelog_20240705T122208Z.xml relativeToChangelogFile: true + - include: + file: changesets/changelog_20241223T154019Z.xml + relativeToChangelogFile: true diff --git a/src/test/java/org/gridsuite/securityanalysis/server/FindContingenciesTest.java b/src/test/java/org/gridsuite/securityanalysis/server/FindContingenciesTest.java index 5ac54bd0..6fda6a27 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/FindContingenciesTest.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/FindContingenciesTest.java @@ -6,8 +6,18 @@ */ package org.gridsuite.securityanalysis.server; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.powsybl.commons.report.ReportNode; +import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.ThreeSides; +import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.network.store.client.NetworkStoreService; +import com.powsybl.network.store.client.PreloadingStrategy; +import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl; import com.powsybl.security.LimitViolationType; +import com.powsybl.ws.commons.computation.service.ReportService; +import com.powsybl.ws.commons.computation.service.UuidGeneratorService; import org.gridsuite.securityanalysis.server.dto.ContingencyResultDTO; import org.gridsuite.securityanalysis.server.dto.ResourceFilterDTO; import org.gridsuite.securityanalysis.server.dto.SecurityAnalysisStatus; @@ -26,8 +36,10 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -38,12 +50,16 @@ import java.util.UUID; import java.util.stream.Stream; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static com.vladmihalcea.sql.SQLStatementCountValidator.assertSelectCount; import static com.vladmihalcea.sql.SQLStatementCountValidator.reset; import static org.assertj.core.api.Assertions.assertThat; import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; /** * @author Kevin Le Saulnier @@ -51,6 +67,10 @@ @SpringBootTest // would be better with @DataJpaTest but does not work here @TestInstance(TestInstance.Lifecycle.PER_CLASS) // improve tests speed as we only read DB class FindContingenciesTest { + + private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4"); + private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d"); + @Autowired private SecurityAnalysisResultRepository securityAnalysisResultRepository; @@ -59,9 +79,34 @@ class FindContingenciesTest { @Autowired private SecurityAnalysisResultService securityAnalysisResultService; + @MockBean + private NetworkStoreService networkStoreService; + @MockBean + private UuidGeneratorService uuidGeneratorService; + + @MockBean + private ReportService reportService; + @BeforeAll void setUp() { - resultEntity = SecurityAnalysisResultEntity.toEntity(UUID.randomUUID(), RESULT, SecurityAnalysisStatus.CONVERGED); + WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + MockitoAnnotations.initMocks(this); + + // network store service mocking + Network network = EurostagTutorialExample1Factory.create(new NetworkFactoryImpl()); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID); + + given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network); + + // UUID service mocking to always generate the same result UUID + given(uuidGeneratorService.generate()).willReturn(RESULT_UUID); + + doNothing().when(reportService).sendReport(any(UUID.class), any(ReportNode.class)); + + resultEntity = SecurityAnalysisResultEntity.toEntity(network, UUID.randomUUID(), RESULT, SecurityAnalysisStatus.CONVERGED); securityAnalysisResultRepository.save(resultEntity); } diff --git a/src/test/java/org/gridsuite/securityanalysis/server/FindPreContingencyLimitViolationTest.java b/src/test/java/org/gridsuite/securityanalysis/server/FindPreContingencyLimitViolationTest.java index 8d957d0c..da9f45ce 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/FindPreContingencyLimitViolationTest.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/FindPreContingencyLimitViolationTest.java @@ -6,7 +6,17 @@ */ package org.gridsuite.securityanalysis.server; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.powsybl.commons.report.ReportNode; +import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.ThreeSides; +import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.network.store.client.NetworkStoreService; +import com.powsybl.network.store.client.PreloadingStrategy; +import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl; +import com.powsybl.ws.commons.computation.service.ReportService; +import com.powsybl.ws.commons.computation.service.UuidGeneratorService; import org.gridsuite.securityanalysis.server.dto.PreContingencyLimitViolationResultDTO; import org.gridsuite.securityanalysis.server.dto.ResourceFilterDTO; import org.gridsuite.securityanalysis.server.dto.SecurityAnalysisStatus; @@ -23,8 +33,10 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Sort; import java.util.Comparator; @@ -32,10 +44,14 @@ import java.util.UUID; import java.util.stream.Stream; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static com.vladmihalcea.sql.SQLStatementCountValidator.assertSelectCount; import static com.vladmihalcea.sql.SQLStatementCountValidator.reset; import static org.assertj.core.api.Assertions.assertThat; import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; /** * @author Anis Touri @@ -43,6 +59,10 @@ @SpringBootTest // would be better with @DataJpaTest but does not work here @TestInstance(TestInstance.Lifecycle.PER_CLASS) // improve tests speed as we only read DB class FindPreContingencyLimitViolationTest { + + private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4"); + private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d"); + @Autowired private SecurityAnalysisResultRepository securityAnalysisResultRepository; @@ -51,9 +71,33 @@ class FindPreContingencyLimitViolationTest { @Autowired private SecurityAnalysisResultService securityAnalysisResultService; + @MockBean + private NetworkStoreService networkStoreService; + @MockBean + private UuidGeneratorService uuidGeneratorService; + @MockBean + private ReportService reportService; + @BeforeAll void setUp() { - resultEntity = SecurityAnalysisResultEntity.toEntity(UUID.randomUUID(), PRECONTINGENCY_RESULT, SecurityAnalysisStatus.CONVERGED); + WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + MockitoAnnotations.initMocks(this); + + // network store service mocking + Network network = EurostagTutorialExample1Factory.create(new NetworkFactoryImpl()); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID); + + given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network); + + // UUID service mocking to always generate the same result UUID + given(uuidGeneratorService.generate()).willReturn(RESULT_UUID); + + doNothing().when(reportService).sendReport(any(UUID.class), any(ReportNode.class)); + + resultEntity = SecurityAnalysisResultEntity.toEntity(network, UUID.randomUUID(), PRECONTINGENCY_RESULT, SecurityAnalysisStatus.CONVERGED); securityAnalysisResultRepository.save(resultEntity); } diff --git a/src/test/java/org/gridsuite/securityanalysis/server/FindSubjectLimitViolationsTest.java b/src/test/java/org/gridsuite/securityanalysis/server/FindSubjectLimitViolationsTest.java index 8c07fba8..d276322a 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/FindSubjectLimitViolationsTest.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/FindSubjectLimitViolationsTest.java @@ -6,8 +6,18 @@ */ package org.gridsuite.securityanalysis.server; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.powsybl.commons.report.ReportNode; +import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.ThreeSides; +import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.network.store.client.NetworkStoreService; +import com.powsybl.network.store.client.PreloadingStrategy; +import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl; import com.powsybl.security.LimitViolationType; +import com.powsybl.ws.commons.computation.service.ReportService; +import com.powsybl.ws.commons.computation.service.UuidGeneratorService; import org.gridsuite.securityanalysis.server.dto.ContingencyLimitViolationDTO; import org.gridsuite.securityanalysis.server.dto.ResourceFilterDTO; import org.gridsuite.securityanalysis.server.dto.SecurityAnalysisStatus; @@ -23,8 +33,10 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -35,12 +47,16 @@ import java.util.UUID; import java.util.stream.Stream; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static com.vladmihalcea.sql.SQLStatementCountValidator.assertSelectCount; import static com.vladmihalcea.sql.SQLStatementCountValidator.reset; import static org.assertj.core.api.Assertions.assertThat; import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; /** * @author Kevin Le Saulnier @@ -48,6 +64,10 @@ @SpringBootTest // would be better with @DataJpaTest but does not work here @TestInstance(TestInstance.Lifecycle.PER_CLASS) // improve tests speed as we only read DB class FindSubjectLimitViolationsTest { + + private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4"); + private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d"); + @Autowired private SecurityAnalysisResultRepository securityAnalysisResultRepository; @@ -56,9 +76,34 @@ class FindSubjectLimitViolationsTest { @Autowired private SecurityAnalysisResultService securityAnalysisResultService; + @MockBean + private NetworkStoreService networkStoreService; + @MockBean + + private UuidGeneratorService uuidGeneratorService; + @MockBean + private ReportService reportService; + @BeforeAll void setUp() { - resultEntity = SecurityAnalysisResultEntity.toEntity(UUID.randomUUID(), RESULT, SecurityAnalysisStatus.CONVERGED); + WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + MockitoAnnotations.initMocks(this); + + // network store service mocking + Network network = EurostagTutorialExample1Factory.create(new NetworkFactoryImpl()); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID); + + given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network); + + // UUID service mocking to always generate the same result UUID + given(uuidGeneratorService.generate()).willReturn(RESULT_UUID); + + doNothing().when(reportService).sendReport(any(UUID.class), any(ReportNode.class)); + + resultEntity = SecurityAnalysisResultEntity.toEntity(network, UUID.randomUUID(), RESULT, SecurityAnalysisStatus.CONVERGED); securityAnalysisResultRepository.save(resultEntity); } diff --git a/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisProviderMock.java b/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisProviderMock.java index 7cb374f7..64670c18 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisProviderMock.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisProviderMock.java @@ -150,6 +150,7 @@ static List getResultConstraintsWithNestedFilter return RESULT_CONSTRAINTS.stream().map(r -> new SubjectLimitViolationResultDTO( r.getSubjectId(), + r.getSubjectId() + "_0", r.getContingencies().stream() .filter(filterMethod::apply) .toList() @@ -163,6 +164,7 @@ static List getResultConstraintsSorted(Comparato return RESULT_CONSTRAINTS.stream().map(r -> new SubjectLimitViolationResultDTO( r.getSubjectId(), + r.getSubjectId() + "_0", r.getContingencies().stream() .sorted(limitViolationDTOComparator) .toList() @@ -227,6 +229,7 @@ private static SubjectLimitViolationDTO toSubjectLimitViolationDTO(LimitViolatio : null; return new SubjectLimitViolationDTO( + limitViolation.getSubjectId(), limitViolation.getSubjectId(), new LimitViolationDTO( limitViolation.getLimitType(), @@ -244,6 +247,7 @@ private static SubjectLimitViolationDTO toSubjectLimitViolationDTO(LimitViolatio private static SubjectLimitViolationResultDTO toSubjectLimitViolationResultDTO(LimitViolation limitViolation, List convergedContingencies, LoadFlowResult.ComponentResult.Status status) { return new SubjectLimitViolationResultDTO( limitViolation.getSubjectId(), + limitViolation.getSubjectId() + "_0", convergedContingencies.stream().map(c -> toContingencyLimitViolationDTO(c, limitViolation, status.name())).toList()); } @@ -253,6 +257,7 @@ private static PreContingencyLimitViolationResultDTO toPreContingencyResultDTO(L : null; return new PreContingencyLimitViolationResultDTO( limitViolation.getSubjectId(), + limitViolation.getSubjectId() + "_0", status.name(), new LimitViolationDTO( limitViolation.getLimitType(), diff --git a/src/test/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultServiceTest.java b/src/test/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultServiceTest.java index e970a378..41b830a9 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultServiceTest.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultServiceTest.java @@ -6,16 +6,32 @@ */ package org.gridsuite.securityanalysis.server.service; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.powsybl.commons.report.ReportNode; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.network.store.client.NetworkStoreService; +import com.powsybl.network.store.client.PreloadingStrategy; +import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl; +import com.powsybl.ws.commons.computation.service.ReportService; +import com.powsybl.ws.commons.computation.service.UuidGeneratorService; import com.vladmihalcea.sql.SQLStatementCountValidator; import org.gridsuite.securityanalysis.server.dto.SecurityAnalysisStatus; import org.junit.jupiter.api.Test; +import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import java.util.UUID; -import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.RESULT; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.*; import static org.gridsuite.securityanalysis.server.util.DatabaseQueryUtils.assertRequestsCount; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; /** * @author Florent MILLOT @@ -23,13 +39,41 @@ @SpringBootTest class SecurityAnalysisResultServiceTest { + private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4"); + private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d"); + private static final String VARIANT_1_ID = "variant_1"; + private static final String VARIANT_2_ID = "variant_2"; @Autowired private SecurityAnalysisResultService securityAnalysisResultService; + @MockBean + private NetworkStoreService networkStoreService; + @MockBean + private UuidGeneratorService uuidGeneratorService; + + @MockBean + private ReportService reportService; + @Test void deleteResultPerfTest() { + WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + MockitoAnnotations.initMocks(this); + + // network store service mocking + Network network = EurostagTutorialExample1Factory.create(new NetworkFactoryImpl()); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID); + + given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network); + + // UUID service mocking to always generate the same result UUID + given(uuidGeneratorService.generate()).willReturn(RESULT_UUID); + + // doNothing().when(reportService).sendReport(any(UUID.class), any(ReportNode.class)); + UUID resultUuid = UUID.randomUUID(); - securityAnalysisResultService.insert(resultUuid, RESULT, SecurityAnalysisStatus.CONVERGED); + securityAnalysisResultService.insert(network, resultUuid, RESULT, SecurityAnalysisStatus.CONVERGED); SQLStatementCountValidator.reset(); securityAnalysisResultService.delete(resultUuid);