diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/ConnectedAPService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/ConnectedAPService.java new file mode 100644 index 000000000..ae2b946af --- /dev/null +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/ConnectedAPService.java @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2023 RTE FRANCE +// +// SPDX-License-Identifier: Apache-2.0 + +package org.lfenergy.compas.sct.commons; + +import org.lfenergy.compas.scl2007b4.model.TConnectedAP; +import org.lfenergy.compas.scl2007b4.model.TSubNetwork; + +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class ConnectedAPService { + + public Stream getConnectedAP(TSubNetwork tSubNetwork) { + if (!tSubNetwork.isSetConnectedAP()) { + return Stream.empty(); + } + return tSubNetwork.getConnectedAP().stream(); + } + + public Stream getFilteredConnectedAP(TSubNetwork tSubNetwork, Predicate tConnectedAPPredicate) { + return getConnectedAP(tSubNetwork).filter(tConnectedAPPredicate); + } + + public Optional findConnectedAP(TSubNetwork tSubNetwork, Predicate tConnectedAPPredicate) { + return getFilteredConnectedAP(tSubNetwork, tConnectedAPPredicate).findFirst(); + } + +} diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/LnService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/LnService.java index 27b2d7eee..2ccad6508 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/LnService.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/LnService.java @@ -5,15 +5,17 @@ package org.lfenergy.compas.sct.commons; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.lfenergy.compas.scl2007b4.model.*; import org.lfenergy.compas.sct.commons.api.LnEditor; import org.lfenergy.compas.sct.commons.domain.*; import org.lfenergy.compas.sct.commons.util.ActiveStatus; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.function.Predicate; -import java.util.*; import java.util.stream.Stream; import static org.lfenergy.compas.sct.commons.util.CommonConstants.MOD_DO_NAME; @@ -175,6 +177,16 @@ public void completeFromDAInstance(TIED tied, String ldInst, TAnyLN anyLN, DoLin }); } + public boolean matchesLn(TAnyLN tAnyLN, String lnClass, String lnInst, String lnPrefix) { + return switch (tAnyLN) { + case TLN ln -> lnClass.equals(ln.getLnClass().getFirst()) + && lnInst.equals(ln.getInst()) + && (StringUtils.trimToEmpty(lnPrefix).equals(StringUtils.trimToEmpty(ln.getPrefix()))); + case LN0 ignored -> lnClass.equals(TLLN0Enum.LLN_0.value()); + default -> throw new IllegalStateException("Unexpected value: " + tAnyLN); + }; + } + private boolean hasSettingGroup(TDAI tdai) { return tdai.isSetVal() && tdai.getVal().stream().anyMatch(tVal -> tVal.isSetSGroup() && tVal.getSGroup() > 0); } diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubNetworkService.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubNetworkService.java new file mode 100644 index 000000000..4dee868bc --- /dev/null +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/SubNetworkService.java @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2023 RTE FRANCE +// +// SPDX-License-Identifier: Apache-2.0 + +package org.lfenergy.compas.sct.commons; + +import org.lfenergy.compas.scl2007b4.model.TSubNetwork; +import org.lfenergy.compas.scl2007b4.model.SCL; + +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class SubNetworkService { + + public Stream getSubNetworks(SCL scl) { + if (!scl.isSetCommunication()) { + return Stream.empty(); + } + if (!scl.getCommunication().isSetSubNetwork()) { + return Stream.empty(); + } + return scl.getCommunication().getSubNetwork().stream(); + } + + public Stream getFilteredSubNetworks(SCL tlNodeType, Predicate tdoPredicate) { + return getSubNetworks(tlNodeType).filter(tdoPredicate); + } + + public Optional findSubNetwork(SCL tlNodeType, Predicate tdoPredicate) { + return getFilteredSubNetworks(tlNodeType, tdoPredicate).findFirst(); + } + +} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/LnServiceTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/LnServiceTest.java index b0eb87112..8daa222d7 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/LnServiceTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/LnServiceTest.java @@ -4,9 +4,11 @@ package org.lfenergy.compas.sct.commons; +import org.assertj.core.api.Assertions; import org.assertj.core.groups.Tuple; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.MethodSource; import org.lfenergy.compas.scl2007b4.model.*; @@ -15,6 +17,7 @@ import org.lfenergy.compas.sct.commons.util.ActiveStatus; import java.util.*; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -580,5 +583,92 @@ private TDAI initDOAndDAInstances(TAnyLN tAnyLN, return dai; } + @ParameterizedTest + @MethodSource("provideLnAttributesForMatchingLns") + void matchesLn_should_return_True_in_these_LN_cases(String lnClass, String lnInst, String lnPrefix) { + //GIVEN: + TLN tln = new TLN(); + tln.getLnClass().add(lnClass); + tln.setInst(lnInst); + tln.setPrefix(lnPrefix); + //WHEN: + Boolean response = lnService.matchesLn(tln, lnClass, lnInst, lnPrefix); + //THEN: + assertThat(response).isTrue(); + } + + private static Stream provideLnAttributesForMatchingLns() { + return Stream.of( + Arguments.of("LPHD", "1", "LnPrefix", "LPHD", "1", "LnPrefix"), + Arguments.of("LPHD", "1", null, "LPHD", "1", null) + ); + } + + @ParameterizedTest + @MethodSource("provideLnAttributesForUnMatchingLns") + void matchesLn_should_return_False_in_these_LN_cases(String lnClass, String lnInst, String lnPrefix, String lnClassString, String lnInstString, String lnPrefixString) { + //GIVEN: + TLN tln = new TLN(); + tln.getLnClass().add(lnClass); + tln.setInst(lnInst); + tln.setPrefix(lnPrefix); + //WHEN: + Boolean response = lnService.matchesLn(tln, lnClassString, lnInstString, lnPrefixString); + //THEN: + assertThat(response).isFalse(); + } + + private static Stream provideLnAttributesForUnMatchingLns() { + return Stream.of( + Arguments.of("FAKE", "1", "LnPrefix", "LPHD", "1", "LnPrefix"), + Arguments.of("LPHD", "FAKE", "LnPrefix", "LPHD", "1", "LnPrefix"), + Arguments.of("LPHD", "1", "FAKE", "LPHD", "1", "LnPrefix"), + Arguments.of("FAKE", "1", null, "LPHD", "1", null), + Arguments.of("LPHD", "FAKE", null, "LPHD", "1", null) + ); + } + + + @ParameterizedTest + @MethodSource("provideLn0AttributesForMatchingLn0s") + void matchesLn_should_return_True_for_matching_LLN0(String lnClass, String lnInst, String lnPrefix) { + //GIVEN: + LN0 ln0 = new LN0(); + ln0.getLnClass().add(lnClass); + //WHEN: + Boolean response = lnService.matchesLn(ln0, lnClass, lnInst, lnPrefix); + //THEN: + assertThat(response).isTrue(); + } + + private static Stream provideLn0AttributesForMatchingLn0s() { + return Stream.of( + Arguments.of("LLN0", "", ""), + Arguments.of("LLN0", "1", ""), + Arguments.of("LLN0", "", "LnPrefix") + ); + } + + @Test + void matchesLn_should_return_False_for_Unmatching_LLN0() { + //GIVEN: + LN0 ln0 = new LN0(); + ln0.getLnClass().add("LLN0"); + //WHEN: + Boolean response = lnService.matchesLn(ln0, "LPHD", "", ""); + //THEN: + assertThat(response).isFalse(); + } + + @Test + void matchesLn_should_throw_an_exception() { + //GIVEN: + TLN0 tln0 = new TLN0(); + tln0.getLnClass().add("LLN0"); + //WHEN && //THEN: + Assertions.assertThatThrownBy(() -> lnService.matchesLn(tln0, "LLN0", "", "")) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Unexpected value: "); + } }