From de64591d974d86bc13f5bf3f88b0461680d28c34 Mon Sep 17 00:00:00 2001 From: JAFFRE Guillaume Date: Thu, 24 Oct 2024 17:20:44 +0200 Subject: [PATCH] feat(#439): Add generic function matchesLn to LnService Signed-off-by: JAFFRE Guillaume --- .../compas/sct/commons/LnService.java | 14 ++- .../compas/sct/commons/LnServiceTest.java | 90 +++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) 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/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: "); + } }