Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add voltage level service #426

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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<TVoltageLevel> getVoltageLevels(SCL scd) {
if (!scd.isSetSubstation()) {
return Stream.empty();
}
return scd.getSubstation()
.stream()
.map(TSubstation::getVoltageLevel)
.flatMap(Collection::stream);
}

public Optional<TVoltageLevel> findVoltageLevel(SCL scd, Predicate<TVoltageLevel> tVoltageLevelPredicate) {
return getVoltageLevels(scd).filter(tVoltageLevelPredicate).findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TVoltageLevel> 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();
}
}
Loading