diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapter.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapter.java index b48255600..65df19895 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapter.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapter.java @@ -12,10 +12,7 @@ import org.lfenergy.compas.sct.commons.scl.SclElementAdapter; import org.lfenergy.compas.sct.commons.util.Utils; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; /** @@ -109,33 +106,33 @@ public AbstractDAIAdapter toAdapter(TDAI childTDAI) { * @param tExtRefs all the ExtRefs contained in the current LDevice/LLN0 * @return a filled SclReportItem if an error occurs, empty SclReportItem otherwise */ - public Optional updateDaiFromExtRef(List tExtRefs) { - Optional optionalSclReportItem; + public List updateDaiFromExtRef(List tExtRefs) { + List sclReportItems = new ArrayList<>(); Optional tExtRefMinOptional = tExtRefs.stream().min(EXTREF_DESC_SUFFIX_COMPARATOR); if (tExtRefMinOptional.isPresent() && extractDescSuffix(tExtRefMinOptional.get().getDesc()) == 1) { TExtRef tExtRefMin = tExtRefMinOptional.get(); String valueSrcRef = createInRefValNominalString(tExtRefMin); - optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_REF, valueSrcRef); + updateDAI(DA_NAME_SET_SRC_REF, valueSrcRef).ifPresent(sclReportItems::add); if (tExtRefMin.isSetSrcCBName()) { String valueSrcCb = createInRefValTestString(tExtRefMin); - optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_CB, valueSrcCb); + updateDAI(DA_NAME_SET_SRC_CB, valueSrcCb).ifPresent(sclReportItems::add); } Optional tExtRefMaxOptional = tExtRefs.stream().max(EXTREF_DESC_SUFFIX_COMPARATOR); if (tExtRefMaxOptional.isPresent() && extractDescSuffix(tExtRefMaxOptional.get().getDesc()) > 1) { TExtRef tExtRefMax = tExtRefMaxOptional.get(); String valueTstRef = createInRefValNominalString(tExtRefMax); - optionalSclReportItem = updateDAI(DA_NAME_SET_TST_REF, valueTstRef); + updateDAI(DA_NAME_SET_TST_REF, valueTstRef).ifPresent(sclReportItems::add); if (tExtRefMax.isSetSrcCBName()) { String valueTstCb = createInRefValTestString(tExtRefMax); - optionalSclReportItem = updateDAI(DA_NAME_SET_TST_CB, valueTstCb); + updateDAI(DA_NAME_SET_TST_CB, valueTstCb).ifPresent(sclReportItems::add); } } } else { - optionalSclReportItem = Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath()))); + sclReportItems.add(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath()))); } - return optionalSclReportItem; + return sclReportItems; } private Optional updateDAI(String daName, String value) { diff --git a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/LN0Adapter.java b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/LN0Adapter.java index fc010cf5d..ba83d9ecd 100644 --- a/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/LN0Adapter.java +++ b/sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/LN0Adapter.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import static org.lfenergy.compas.sct.commons.util.CommonConstants.*; @@ -265,9 +266,10 @@ public List updateDoInRef() { .map(doiAdapter -> doiAdapter.getDataAdapterByName(DAI_NAME_PURPOSE).getCurrentElem().getVal().stream() .findFirst() .map(tVal -> doiAdapter.updateDaiFromExtRef(getBoundExtRefsByDesc(tVal.getValue()))) - .orElse(Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath()))))) - .flatMap(Optional::stream) - .toList(); + .orElse(List.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath())))) + ) + .flatMap(List::stream) + .collect(Collectors.toList()); } private List getBoundExtRefsByDesc(String desc) { diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapterTest.java index 0aa5081a0..d6e561178 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapterTest.java @@ -315,7 +315,7 @@ void updateDaiFromExtRef_should_update_setSrcXX_values_when_ExtRef_desc_suffix_e TExtRef extRef1 = givenExtRef(1, true); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); // Then assertThat(sclReportItems).isEmpty(); @@ -340,7 +340,7 @@ void updateDaiFromExtRef_should_update_setSrcRef_value_but_not_setSrcCB_when_Ext TExtRef extRef1 = givenExtRef(1, false); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); // Then assertThat(sclReportItems).isEmpty(); @@ -371,7 +371,7 @@ void updateDaiFromExtRef_should_update_setSrcXX_and_setTstXX_values_when_ExtRef_ TExtRef extRef3 = givenExtRef(3, true); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); // Then assertThat(sclReportItems).isEmpty(); @@ -423,7 +423,7 @@ void updateDaiFromExtRef_should_update_only_setSrcRef_and_setTstRef_values_when_ TExtRef extRef3 = givenExtRef(3, false); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); // Then assertThat(sclReportItems).isEmpty(); @@ -451,14 +451,13 @@ void updateDaiFromExtRef_should_return_warning_report_when_none_ExtRef_endin_wit TExtRef extRef3 = givenExtRef(3, false); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef3)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef3)); // Then assertThat(sclReportItems) - .isPresent() - .isNotEmpty(); - assertThat(sclReportItems.get().getMessage()) - .contains("can't be bound with an ExtRef"); + .isNotEmpty() + .extracting(SclReportItem::getMessage) + .contains("The DOI /DOI[@name=\"Do\"] can't be bound with an ExtRef"); assertThat(doiAdapter.getDataAdapterByName(DOIAdapter.DA_NAME_SET_SRC_REF)).isNotNull(); assertThat(getDaiValOfDoi(doiAdapter, DOIAdapter.DA_NAME_SET_SRC_REF)).isNotPresent(); } @@ -471,7 +470,7 @@ void updateDaiFromExtRef_should_create_DAI_when_no_DAI_name_setSrcRef() { TExtRef extRef1 = givenExtRef(1, false); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1)); // Then assertThat(sclReportItems).isEmpty(); @@ -489,13 +488,14 @@ void updateDaiFromExtRef_should_return_filled_ReportItem_when_no_ExtRef_in_LNode DOIAdapter doiAdapter = daiAdapter.getParentAdapter(); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of()); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of()); // Then assertThat(sclReportItems) - .isPresent() .isNotEmpty(); - assertThat(sclReportItems.get().getMessage()).contains("can't be bound with an ExtRef"); + assertThat(sclReportItems) + .extracting(SclReportItem::getMessage) + .contains("The DOI /DOI[@name=\"Do\"] can't be bound with an ExtRef"); } @Test @@ -529,7 +529,7 @@ void updateDaiFromExtRef_should_compose_correct_name_when_optional_ExtRef_attrib extRef3.setDoName("DO_NAME_3"); // When - Optional sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); + List sclReportItems = doiAdapter.updateDaiFromExtRef(List.of(extRef1, extRef3)); // Then assertThat(sclReportItems).isEmpty(); diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LN0AdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LN0AdapterTest.java index f7b9a243b..fbde82f6e 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LN0AdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LN0AdapterTest.java @@ -1079,9 +1079,10 @@ void updateDoInRef_should_not_update_setSrcRef_and_setSrcCB_and_setTstRef_and_se assertThat(finalSetTstRef).isEqualTo(expectedVal); assertThat(finalSetTstCB).isEqualTo(expectedVal); assertThat(sclReportItems) - .hasSize(1) + .hasSize(4) .extracting(SclReportItem::getMessage) - .containsExactly("The DAI cannot be updated"); + .containsExactly("The DAI cannot be updated", "The DAI cannot be updated", "The DAI cannot be updated", + "The DAI cannot be updated"); }