Skip to content

Commit e412abe

Browse files
committed
feat: add voltage level service
Signed-off-by: Samir Romdhani <[email protected]>
1 parent 01d4d62 commit e412abe

File tree

5 files changed

+108
-39
lines changed

5 files changed

+108
-39
lines changed

sct-app/src/test/java/org.lfenergy.compas.sct.app/SclAutomationServiceIntegrationTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import org.junit.jupiter.api.Test;
99
import org.lfenergy.compas.scl2007b4.model.LN0;
1010
import org.lfenergy.compas.scl2007b4.model.SCL;
11-
import org.lfenergy.compas.sct.commons.ControlBlockEditorService;
12-
import org.lfenergy.compas.sct.commons.LdeviceService;
13-
import org.lfenergy.compas.sct.commons.SclService;
14-
import org.lfenergy.compas.sct.commons.SubstationService;
11+
import org.lfenergy.compas.sct.commons.*;
1512
import org.lfenergy.compas.sct.commons.api.ControlBlockEditor;
1613
import org.lfenergy.compas.sct.commons.api.SclEditor;
1714
import org.lfenergy.compas.sct.commons.api.SubstationEditor;
@@ -34,7 +31,7 @@ class SclAutomationServiceIntegrationTest {
3431

3532
private SclAutomationService sclAutomationService ;
3633
private static final SclEditor sclEditor = new SclService() ;
37-
private static final SubstationEditor substationEditor = new SubstationService() ;
34+
private static final SubstationEditor substationEditor = new SubstationService(new VoltageLevelService()) ;
3835
private static final ControlBlockEditor controlBlockEditor = new ControlBlockEditorService(new ControlService(), new LdeviceService()) ;
3936

4037
private HeaderDTO headerDTO;

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubstationService.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
package org.lfenergy.compas.sct.commons;
66

77
import lombok.NonNull;
8+
import lombok.RequiredArgsConstructor;
89
import org.lfenergy.compas.scl2007b4.model.SCL;
910
import org.lfenergy.compas.scl2007b4.model.TBay;
1011
import org.lfenergy.compas.scl2007b4.model.TSubstation;
1112
import org.lfenergy.compas.scl2007b4.model.TVoltageLevel;
1213
import org.lfenergy.compas.sct.commons.api.SubstationEditor;
1314
import org.lfenergy.compas.sct.commons.exception.ScdException;
14-
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
15-
import org.lfenergy.compas.sct.commons.scl.sstation.SubstationAdapter;
16-
import org.lfenergy.compas.sct.commons.scl.sstation.VoltageLevelAdapter;
1715

16+
@RequiredArgsConstructor
1817
public class SubstationService implements SubstationEditor {
1918

19+
private final VoltageLevelService voltageLevelService;
20+
2021
@Override
2122
public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdException {
2223
if (scd.getSubstation().size() > 1) {
@@ -25,53 +26,48 @@ public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdExceptio
2526
if (ssd.getSubstation().size() != 1) {
2627
throw new ScdException(String.format("SSD file must have exactly 1 Substation, but got %d", ssd.getSubstation().size()));
2728
}
28-
TSubstation ssdTSubstation = ssd.getSubstation().get(0);
29+
TSubstation ssdTSubstation = ssd.getSubstation().getFirst();
2930
if (scd.getSubstation().isEmpty()) {
3031
scd.getSubstation().add(ssdTSubstation);
3132
} else {
32-
TSubstation scdTSubstation = scd.getSubstation().get(0);
33-
if (scdTSubstation.getName().equalsIgnoreCase(ssdTSubstation.getName())) {
34-
SubstationAdapter scdSubstationAdapter = new SclRootAdapter(scd).getSubstationAdapter(scdTSubstation.getName());
33+
TSubstation scdTSubstation = scd.getSubstation().getFirst();
34+
if (scdTSubstation.getName().equalsIgnoreCase(ssdTSubstation.getName())){
3535
for (TVoltageLevel tvl : ssdTSubstation.getVoltageLevel()) {
36-
updateVoltageLevel(scdSubstationAdapter, tvl);
36+
updateVoltageLevel(scd, tvl);
3737
}
38-
} else
38+
} else {
3939
throw new ScdException("SCD file must have only one Substation and the Substation name from SSD file is" +
4040
" different from the one in SCD file. The files are rejected.");
41+
}
4142
}
4243
}
4344

4445
/**
4546
* Creates new VoltageLevel section or updates VoltageLevel contents
46-
* @param scdSubstationAdapter Substation in which VoltageLevel should be created/updated
47+
* @param scd SCL contain Substation in which VoltageLevel should be created/updated
4748
* @param vl VoltageLevel to create/update
4849
* @throws ScdException throws when unable to create new VoltageLevel section which is not already present in Substation
4950
*/
50-
private void updateVoltageLevel(@NonNull SubstationAdapter scdSubstationAdapter, TVoltageLevel vl) throws ScdException {
51-
if (scdSubstationAdapter.getVoltageLevelAdapter(vl.getName()).isPresent()) {
52-
VoltageLevelAdapter scdVoltageLevelAdapter = scdSubstationAdapter.getVoltageLevelAdapter(vl.getName())
53-
.orElseThrow(() -> new ScdException("Unable to create VoltageLevelAdapter"));
54-
for (TBay tbay : vl.getBay()) {
55-
updateBay(scdVoltageLevelAdapter, tbay);
56-
}
57-
} else {
58-
scdSubstationAdapter.getCurrentElem().getVoltageLevel().add(vl);
59-
}
51+
private void updateVoltageLevel(@NonNull SCL scd, TVoltageLevel vl) throws ScdException {
52+
voltageLevelService.findVoltageLevel(scd, tVoltageLevel -> tVoltageLevel.getName().equals(vl.getName()))
53+
.ifPresentOrElse(tVoltageLevel -> vl.getBay().forEach(tBay -> updateBay(tVoltageLevel, tBay)),
54+
()-> scd.getSubstation().getFirst().getVoltageLevel().add(vl));
6055
}
6156

57+
6258
/**
6359
* Adds new Bay in VoltageLevel or if already exist removes and replaces it
64-
* @param scdVoltageLevelAdapter VoltageLevel in which Bay should be created/updated
60+
* @param tVoltageLevel VoltageLevel in which Bay should be created/updated
6561
* @param tBay Bay to add
6662
*/
67-
private void updateBay(@NonNull VoltageLevelAdapter scdVoltageLevelAdapter, TBay tBay) {
68-
if (scdVoltageLevelAdapter.getBayAdapter(tBay.getName()).isPresent()) {
69-
scdVoltageLevelAdapter.getCurrentElem().getBay()
70-
.removeIf(t -> t.getName().equalsIgnoreCase(tBay.getName()));
71-
scdVoltageLevelAdapter.getCurrentElem().getBay().add(tBay);
72-
} else {
73-
scdVoltageLevelAdapter.getCurrentElem().getBay().add(tBay);
74-
}
63+
private void updateBay(@NonNull TVoltageLevel tVoltageLevel, TBay tBay) {
64+
tVoltageLevel.getBay()
65+
.stream().filter(tBay1 -> tBay1.getName().equals(tBay.getName()))
66+
.findFirst()
67+
.ifPresentOrElse(tBay1 -> {
68+
tVoltageLevel.getBay().removeIf(t -> t.getName().equalsIgnoreCase(tBay.getName()));
69+
tVoltageLevel.getBay().add(tBay);
70+
}, ()-> tVoltageLevel.getBay().add(tBay));
7571
}
7672

7773
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-FileCopyrightText: 2024 RTE FRANCE
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package org.lfenergy.compas.sct.commons;
6+
7+
import org.lfenergy.compas.scl2007b4.model.*;
8+
9+
import java.util.Collection;
10+
import java.util.Optional;
11+
import java.util.function.Predicate;
12+
import java.util.stream.Stream;
13+
14+
public class VoltageLevelService {
15+
16+
public Stream<TVoltageLevel> getVoltageLevels(SCL scd) {
17+
if (!scd.isSetSubstation()) {
18+
return Stream.empty();
19+
}
20+
return scd.getSubstation()
21+
.stream()
22+
.map(TSubstation::getVoltageLevel)
23+
.flatMap(Collection::stream);
24+
}
25+
26+
public Optional<TVoltageLevel> findVoltageLevel(SCL scd, Predicate<TVoltageLevel> tVoltageLevelPredicate) {
27+
return getVoltageLevels(scd).filter(tVoltageLevelPredicate).findFirst();
28+
}
29+
}

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/SubstationServiceTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
import org.lfenergy.compas.sct.commons.exception.ScdException;
1313
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
1414
import org.mockito.InjectMocks;
15+
import org.mockito.Mock;
1516
import org.mockito.junit.jupiter.MockitoExtension;
1617

1718
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.junit.jupiter.api.Assertions.assertThrows;
1920
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable;
2021

21-
@ExtendWith(MockitoExtension.class)
2222
class SubstationServiceTest {
2323

24-
@InjectMocks
25-
SubstationService substationService;
24+
private final SubstationService substationService = new SubstationService(new VoltageLevelService());
2625

2726
@Test
2827
void addSubstation_when_SCD_has_no_substation_should_succeed() {
@@ -43,8 +42,8 @@ void addSubstation_when_SCD_has_a_substation_should_succeed() {
4342
// Given
4443
SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/scd_with_substation.xml");
4544
SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml");
46-
TSubstation scdSubstation = scd.getSubstation().get(0);
47-
TSubstation ssdSubstation = ssd.getSubstation().get(0);
45+
TSubstation scdSubstation = scd.getSubstation().getFirst();
46+
TSubstation ssdSubstation = ssd.getSubstation().getFirst();
4847
assertThat(scdSubstation.getVoltageLevel().stream().map(TVoltageLevel::getBay).count()).isEqualTo(1);
4948
// When
5049
substationService.addSubstation(scd, ssd);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-FileCopyrightText: 2024 RTE FRANCE
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package org.lfenergy.compas.sct.commons;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.lfenergy.compas.scl2007b4.model.SCL;
9+
import org.lfenergy.compas.scl2007b4.model.TVoltageLevel;
10+
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
11+
12+
import java.util.List;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.Assertions.assertThatCode;
16+
17+
class VoltageLevelServiceTest {
18+
19+
private final VoltageLevelService voltageLevelService = new VoltageLevelService();
20+
21+
@Test
22+
void getVoltageLevels_should_succeed() {
23+
// Given
24+
SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml");
25+
// When
26+
List<TVoltageLevel> tVoltageLevels = voltageLevelService.getVoltageLevels(scd).toList();
27+
// Then
28+
assertThat(tVoltageLevels).hasSize(2);
29+
}
30+
31+
@Test
32+
void findVoltageLevel_when_voltageLevelExist_should_succeed() {
33+
// Given
34+
SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml");
35+
// When Then
36+
assertThatCode(() -> voltageLevelService.findVoltageLevel(scd, tVoltageLevel1 -> "4".equals(tVoltageLevel1.getName())).orElseThrow())
37+
.doesNotThrowAnyException();
38+
}
39+
40+
@Test
41+
void findVoltageLevel_when_voltageLevelNotExist_should_return_empty() {
42+
// Given
43+
SCL scd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd.xml");
44+
// When Then
45+
assertThat(voltageLevelService.findVoltageLevel(scd, tVoltageLevel1 -> "5".equals(tVoltageLevel1.getName())))
46+
.isEmpty();
47+
}
48+
}

0 commit comments

Comments
 (0)