diff --git a/sct-app/src/test/java/org.lfenergy.compas.sct.app/SclAutomationServiceIntegrationTest.java b/sct-app/src/test/java/org.lfenergy.compas.sct.app/SclAutomationServiceIntegrationTest.java index fb4ec7c6e..33ab8ce65 100644 --- a/sct-app/src/test/java/org.lfenergy.compas.sct.app/SclAutomationServiceIntegrationTest.java +++ b/sct-app/src/test/java/org.lfenergy.compas.sct.app/SclAutomationServiceIntegrationTest.java @@ -8,10 +8,7 @@ import org.junit.jupiter.api.Test; import org.lfenergy.compas.scl2007b4.model.LN0; import org.lfenergy.compas.scl2007b4.model.SCL; -import org.lfenergy.compas.sct.commons.ControlBlockEditorService; -import org.lfenergy.compas.sct.commons.LdeviceService; -import org.lfenergy.compas.sct.commons.SclService; -import org.lfenergy.compas.sct.commons.SubstationService; +import org.lfenergy.compas.sct.commons.*; import org.lfenergy.compas.sct.commons.api.ControlBlockEditor; import org.lfenergy.compas.sct.commons.api.SclEditor; import org.lfenergy.compas.sct.commons.api.SubstationEditor; @@ -34,7 +31,7 @@ class SclAutomationServiceIntegrationTest { private SclAutomationService sclAutomationService ; private static final SclEditor sclEditor = new SclService() ; - private static final SubstationEditor substationEditor = new SubstationService() ; + private static final SubstationEditor substationEditor = new SubstationService(new VoltageLevelService()) ; private static final ControlBlockEditor controlBlockEditor = new ControlBlockEditorService(new ControlService(), new LdeviceService()) ; private HeaderDTO headerDTO; diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubstationService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubstationService.java index 1e51e398e..6936955ec 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubstationService.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubstationService.java @@ -5,18 +5,19 @@ package org.lfenergy.compas.sct.commons; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import org.lfenergy.compas.scl2007b4.model.SCL; import org.lfenergy.compas.scl2007b4.model.TBay; import org.lfenergy.compas.scl2007b4.model.TSubstation; import org.lfenergy.compas.scl2007b4.model.TVoltageLevel; import org.lfenergy.compas.sct.commons.api.SubstationEditor; import org.lfenergy.compas.sct.commons.exception.ScdException; -import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; -import org.lfenergy.compas.sct.commons.scl.sstation.SubstationAdapter; -import org.lfenergy.compas.sct.commons.scl.sstation.VoltageLevelAdapter; +@RequiredArgsConstructor public class SubstationService implements SubstationEditor { + private final VoltageLevelService voltageLevelService; + @Override public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdException { if (scd.getSubstation().size() > 1) { @@ -25,53 +26,48 @@ public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdExceptio if (ssd.getSubstation().size() != 1) { throw new ScdException(String.format("SSD file must have exactly 1 Substation, but got %d", ssd.getSubstation().size())); } - TSubstation ssdTSubstation = ssd.getSubstation().get(0); + TSubstation ssdTSubstation = ssd.getSubstation().getFirst(); if (scd.getSubstation().isEmpty()) { scd.getSubstation().add(ssdTSubstation); } else { - TSubstation scdTSubstation = scd.getSubstation().get(0); - if (scdTSubstation.getName().equalsIgnoreCase(ssdTSubstation.getName())) { - SubstationAdapter scdSubstationAdapter = new SclRootAdapter(scd).getSubstationAdapter(scdTSubstation.getName()); + TSubstation scdTSubstation = scd.getSubstation().getFirst(); + if (scdTSubstation.getName().equalsIgnoreCase(ssdTSubstation.getName())){ for (TVoltageLevel tvl : ssdTSubstation.getVoltageLevel()) { - updateVoltageLevel(scdSubstationAdapter, tvl); + updateVoltageLevel(scd, tvl); } - } else + } else { throw new ScdException("SCD file must have only one Substation and the Substation name from SSD file is" + " different from the one in SCD file. The files are rejected."); + } } } /** * Creates new VoltageLevel section or updates VoltageLevel contents - * @param scdSubstationAdapter Substation in which VoltageLevel should be created/updated + * @param scd SCL contain Substation in which VoltageLevel should be created/updated * @param vl VoltageLevel to create/update * @throws ScdException throws when unable to create new VoltageLevel section which is not already present in Substation */ - private void updateVoltageLevel(@NonNull SubstationAdapter scdSubstationAdapter, TVoltageLevel vl) throws ScdException { - if (scdSubstationAdapter.getVoltageLevelAdapter(vl.getName()).isPresent()) { - VoltageLevelAdapter scdVoltageLevelAdapter = scdSubstationAdapter.getVoltageLevelAdapter(vl.getName()) - .orElseThrow(() -> new ScdException("Unable to create VoltageLevelAdapter")); - for (TBay tbay : vl.getBay()) { - updateBay(scdVoltageLevelAdapter, tbay); - } - } else { - scdSubstationAdapter.getCurrentElem().getVoltageLevel().add(vl); - } + private void updateVoltageLevel(@NonNull SCL scd, TVoltageLevel vl) throws ScdException { + voltageLevelService.findVoltageLevel(scd, tVoltageLevel -> tVoltageLevel.getName().equals(vl.getName())) + .ifPresentOrElse(tVoltageLevel -> vl.getBay().forEach(tBay -> updateBay(tVoltageLevel, tBay)), + ()-> scd.getSubstation().getFirst().getVoltageLevel().add(vl)); } + /** * Adds new Bay in VoltageLevel or if already exist removes and replaces it - * @param scdVoltageLevelAdapter VoltageLevel in which Bay should be created/updated + * @param tVoltageLevel VoltageLevel in which Bay should be created/updated * @param tBay Bay to add */ - private void updateBay(@NonNull VoltageLevelAdapter scdVoltageLevelAdapter, TBay tBay) { - if (scdVoltageLevelAdapter.getBayAdapter(tBay.getName()).isPresent()) { - scdVoltageLevelAdapter.getCurrentElem().getBay() - .removeIf(t -> t.getName().equalsIgnoreCase(tBay.getName())); - scdVoltageLevelAdapter.getCurrentElem().getBay().add(tBay); - } else { - scdVoltageLevelAdapter.getCurrentElem().getBay().add(tBay); - } + private void updateBay(@NonNull TVoltageLevel tVoltageLevel, TBay tBay) { + tVoltageLevel.getBay() + .stream().filter(tBay1 -> tBay1.getName().equals(tBay.getName())) + .findFirst() + .ifPresentOrElse(tBay1 -> { + tVoltageLevel.getBay().removeIf(t -> t.getName().equalsIgnoreCase(tBay.getName())); + tVoltageLevel.getBay().add(tBay); + }, ()-> tVoltageLevel.getBay().add(tBay)); } } diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/VoltageLevelService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/VoltageLevelService.java new file mode 100644 index 000000000..7778c62cb --- /dev/null +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/VoltageLevelService.java @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2024 RTE FRANCE +// +// SPDX-License-Identifier: Apache-2.0 + +package org.lfenergy.compas.sct.commons; + +import org.lfenergy.compas.scl2007b4.model.*; + +import java.util.Collection; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class VoltageLevelService { + + public Stream getVoltageLevels(SCL scd) { + if (!scd.isSetSubstation()) { + return Stream.empty(); + } + return scd.getSubstation() + .stream() + .map(TSubstation::getVoltageLevel) + .flatMap(Collection::stream); + } + + public Optional findVoltageLevel(SCL scd, Predicate tVoltageLevelPredicate) { + return getVoltageLevels(scd).filter(tVoltageLevelPredicate).findFirst(); + } +} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SubstationServiceTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SubstationServiceTest.java index 5ebba1c52..8134b2908 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SubstationServiceTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SubstationServiceTest.java @@ -12,17 +12,16 @@ import org.lfenergy.compas.sct.commons.exception.ScdException; import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable; -@ExtendWith(MockitoExtension.class) class SubstationServiceTest { - @InjectMocks - SubstationService substationService; + private final SubstationService substationService = new SubstationService(new VoltageLevelService()); @Test void addSubstation_when_SCD_has_no_substation_should_succeed() { @@ -43,8 +42,8 @@ void addSubstation_when_SCD_has_a_substation_should_succeed() { // Given SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/scd_with_substation.xml"); SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml"); - TSubstation scdSubstation = scd.getSubstation().get(0); - TSubstation ssdSubstation = ssd.getSubstation().get(0); + TSubstation scdSubstation = scd.getSubstation().getFirst(); + TSubstation ssdSubstation = ssd.getSubstation().getFirst(); assertThat(scdSubstation.getVoltageLevel().stream().map(TVoltageLevel::getBay).count()).isEqualTo(1); // When substationService.addSubstation(scd, ssd); diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/VoltageLevelServiceTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/VoltageLevelServiceTest.java new file mode 100644 index 000000000..643edfde9 --- /dev/null +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/VoltageLevelServiceTest.java @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 RTE FRANCE +// +// SPDX-License-Identifier: Apache-2.0 + +package org.lfenergy.compas.sct.commons; + +import org.junit.jupiter.api.Test; +import org.lfenergy.compas.scl2007b4.model.SCL; +import org.lfenergy.compas.scl2007b4.model.TVoltageLevel; +import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +class VoltageLevelServiceTest { + + private final VoltageLevelService voltageLevelService = new VoltageLevelService(); + + @Test + void getVoltageLevels_should_succeed() { + // Given + SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml"); + // When + List tVoltageLevels = voltageLevelService.getVoltageLevels(scd).toList(); + // Then + assertThat(tVoltageLevels).hasSize(2); + } + + @Test + void findVoltageLevel_when_voltageLevelExist_should_succeed() { + // Given + SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml"); + // When Then + assertThatCode(() -> voltageLevelService.findVoltageLevel(scd, tVoltageLevel1 -> "4".equals(tVoltageLevel1.getName())).orElseThrow()) + .doesNotThrowAnyException(); + } + + @Test + void findVoltageLevel_when_voltageLevelNotExist_should_return_empty() { + // Given + SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml"); + // When Then + assertThat(voltageLevelService.findVoltageLevel(scd, tVoltageLevel1 -> "5".equals(tVoltageLevel1.getName()))) + .isEmpty(); + } +} \ No newline at end of file