Skip to content

Commit

Permalink
feat(#439): Add generic function matchesLn to LnService
Browse files Browse the repository at this point in the history
Signed-off-by: JAFFRE Guillaume <[email protected]>
  • Loading branch information
GuillaumeJAFFRE committed Oct 24, 2024
1 parent f92c09c commit de64591
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Arguments> 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<Arguments> 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<Arguments> 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: ");
}

}

0 comments on commit de64591

Please sign in to comment.