Skip to content

Commit

Permalink
Merge pull request #261 from com-pas/fix/use-updatability-for-binding…
Browse files Browse the repository at this point in the history
…-inref

fix: check updatability before updating DO InRef binding
  • Loading branch information
AliouDIAITE authored Mar 30, 2023
2 parents 06ec04a + 3704a09 commit fd98b51
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.scl.ied.AbstractLNAdapter;
import org.lfenergy.compas.sct.commons.util.SclConstructorHelper;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,12 +51,25 @@ public class ResumedDataTemplate {
@NonNull
private DaTypeName daName = new DaTypeName("");

/**
* Constructor
*/
public ResumedDataTemplate(AbstractLNAdapter<?> lnAdapter, String doName, String daName) {
this.lnClass = lnAdapter.getLNClass();
this.lnInst = lnAdapter.getLNInst();
this.prefix = lnAdapter.getPrefix();
this.lnType = lnAdapter.getLnType();
this.doName = new DoTypeName(doName);
this.daName = new DaTypeName(daName);
}

/**
* Copies summarized DataTypeTemplate information to another one
*
* @param dtt input
* @return Updated ResumedDataTemplate object
*/
public static ResumedDataTemplate copyFrom(ResumedDataTemplate dtt){
public static ResumedDataTemplate copyFrom(ResumedDataTemplate dtt) {
ResumedDataTemplate resumedDataTemplate = new ResumedDataTemplate();
resumedDataTemplate.prefix = dtt.prefix;
resumedDataTemplate.lnClass = dtt.lnClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.dto.ExtrefTarget;
import org.lfenergy.compas.sct.commons.dto.ResumedDataTemplate;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.scl.ObjectReference;
import org.lfenergy.compas.sct.commons.scl.SclElementAdapter;
Expand Down Expand Up @@ -109,36 +110,45 @@ public AbstractDAIAdapter<?> toAdapter(TDAI childTDAI) {
* @return a filled SclReportItem if an error occurs, empty SclReportItem otherwise
*/
public Optional<SclReportItem> updateDaiFromExtRef(List<TExtRef> tExtRefs) {
Optional<SclReportItem> optionalSclReportItem;
Optional<TExtRef> tExtRefMinOptional = tExtRefs.stream().min(EXTREF_DESC_SUFFIX_COMPARATOR);

if (tExtRefMinOptional.isPresent() && extractDescSuffix(tExtRefMinOptional.get().getDesc()) == 1) {
TExtRef tExtRefMin = tExtRefMinOptional.get();
findDataAdapterByName(DA_NAME_SET_SRC_REF)
.orElse(addDAI(DA_NAME_SET_SRC_REF, true))
.setVal(createInRefValNominalString(tExtRefMin));
String valueSrcRef = createInRefValNominalString(tExtRefMin);
optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_REF, valueSrcRef);
if (tExtRefMin.isSetSrcCBName()) {
findDataAdapterByName(DA_NAME_SET_SRC_CB)
.orElse(addDAI(DA_NAME_SET_SRC_CB, true))
.setVal(createInRefValTestString(tExtRefMin));
String valueSrcCb = createInRefValTestString(tExtRefMin);
optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_CB, valueSrcCb);
}

Optional<TExtRef> tExtRefMaxOptional = tExtRefs.stream().max(EXTREF_DESC_SUFFIX_COMPARATOR);
if (tExtRefMaxOptional.isPresent() && extractDescSuffix(tExtRefMaxOptional.get().getDesc()) > 1) {
TExtRef tExtRefMax = tExtRefMaxOptional.get();
findDataAdapterByName(DA_NAME_SET_TST_REF)
.orElse(addDAI(DA_NAME_SET_TST_REF, true))
.setVal(createInRefValNominalString(tExtRefMax));
String valueTstRef = createInRefValNominalString(tExtRefMax);
optionalSclReportItem = updateDAI(DA_NAME_SET_TST_REF, valueTstRef);
if (tExtRefMax.isSetSrcCBName()) {
findDataAdapterByName(DA_NAME_SET_TST_CB)
.orElse(addDAI(DA_NAME_SET_TST_CB, true))
.setVal(createInRefValTestString(tExtRefMax));
String valueTstCb = createInRefValTestString(tExtRefMax);
optionalSclReportItem = updateDAI(DA_NAME_SET_TST_CB, valueTstCb);
}
}
} else {
return Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath())));
optionalSclReportItem = Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath())));
}

return optionalSclReportItem;
}

private Optional<SclReportItem> updateDAI(String daName, String value) {
ResumedDataTemplate daiFilterSrcRef = new ResumedDataTemplate(getParentAdapter(), getName(), daName);
Optional<ResumedDataTemplate> foundDais = getParentAdapter().getDAI(daiFilterSrcRef, true).stream().findFirst();
if (foundDais.isEmpty()) {
return Optional.of(SclReportItem.warning(getXPath() + "/DAI@name=\"" + daName + "\"/Val", "The DAI cannot be updated"));
}
ResumedDataTemplate filterForUpdate = foundDais.get();
filterForUpdate.setVal(value);
getParentAdapter().updateDAI(filterForUpdate);
return Optional.empty();

}

private static int extractDescSuffix(String desc) throws NumberFormatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.lfenergy.compas.scl2007b4.model.TFCEnum;
import org.lfenergy.compas.scl2007b4.model.TPredefinedCDCEnum;
import org.lfenergy.compas.scl2007b4.model.TVal;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.scl.ied.LNAdapter;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -275,4 +274,27 @@ void setVal_should_replace_reference_val(){
assertThat(rDTT.getDaName().getDaiValues()).hasSize(1)
.isEqualTo(Map.of(0L, "newValue"));
}

@Test
void constructorTest() {
// Given
TLN tln = new TLN();
tln.setLnType("T1");
tln.getLnClass().add(TLLN0Enum.LLN_0.value());
tln.setPrefix("P1");
LNAdapter lnAdapter = new LNAdapter(null, tln);
// When
ResumedDataTemplate expected = new ResumedDataTemplate(lnAdapter, "do", "da");
// Then
assertThat(expected.getLnClass()).isEqualTo(TLLN0Enum.LLN_0.value());
assertThat(expected.getLnType()).isEqualTo("T1");
assertThat(expected.getPrefix()).isEqualTo("P1");
assertThat(expected.getDoName().getName()).isEqualTo("do");
assertThat(expected.getDoName().getName()).isEqualTo("do");
assertThat(expected.getDoName().getStructNames()).isEmpty();
assertThat(expected.getDaName().getName()).isEqualTo("da");
assertThat(expected.getDaName().getDaiValues()).isEmpty();
assertThat(expected.getDaName().isValImport()).isFalse();
assertThat(expected.getDaName().isUpdatable()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ private Optional<TVal> getLDeviceStatusValue(SCL scl, String iedName, String ldI
@ParameterizedTest(name = "{0}")
@CsvSource({
"Test update setSrcRef Value,LD_WITH_1_InRef,InRef2,setSrcRef,IED_NAME1LD_WITH_1_InRef/PRANCR1.Do11.sdo11",
"Test update setSrcCB Value,LD_WITH_1_InRef,InRef2,setSrcCB,IED_NAME1LD_WITH_1_InRef/prefixANCR1.GSE1",
"Test update setSrcCB Value,LD_WITH_1_InRef,InRef2,setSrcCB,OLD_VAL",
"Test update setSrcRef Value,LD_WITH_3_InRef,InRef3,setSrcRef,IED_NAME1LD_WITH_3_InRef/PRANCR1.Do11.sdo11",
"Test update setSrcCB Value,LD_WITH_3_InRef,InRef3,setSrcCB,IED_NAME1LD_WITH_3_InRef/prefixANCR1.GSE1",
"Test update setTstRef Value,LD_WITH_3_InRef,InRef3,setTstRef,IED_NAME1LD_WITH_3_InRef/PRANCR1.Do11.sdo11",
Expand All @@ -1105,6 +1105,7 @@ void updateDoInRef_shouldReturnUpdatedFile(String testName, String ldInst, Strin

// Then
assertThat(sclReport.isSuccess()).isTrue();
SclTestMarshaller.assertIsMarshallable(sclReport.getSclRootAdapter().currentElem);
assertThat(getValFromDaiName(sclReport.getSclRootAdapter().getCurrentElem(), "IED_NAME1", ldInst, doName, daName)
.map(TVal::getValue))
.hasValue(expected);
Expand Down Expand Up @@ -1208,6 +1209,4 @@ void analyzeDataGroups_should_return_errors_messages() {
"There are too much GOOSE Control Blocks for the IED IED_NAME2: 3 > 2 max",
"There are too much SMV Control Blocks for the IED IED_NAME2: 3 > 1 max");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
Expand All @@ -15,13 +16,15 @@
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
import org.lfenergy.compas.sct.commons.util.CommonConstants;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.*;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.lfenergy.compas.sct.commons.util.SclConstructorHelper.newVal;

@ExtendWith(MockitoExtension.class)
class DOIAdapterTest {

@Test
Expand Down Expand Up @@ -183,7 +186,7 @@ void DAIAdapter_update_when_valImport_is_set_to_false_but_da_is_Mod_StVal_should
}

@Test
void testFindDeepestMatch() throws Exception {
void testFindDeepestMatch() {
// Given
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
Expand Down Expand Up @@ -215,7 +218,6 @@ void testFindDeepestMatch() throws Exception {
assertThat(pair.getLeft()).isInstanceOf(SDIAdapter.DAIAdapter.class);
}


private DOIAdapter.DAIAdapter initInnerDAIAdapter(String doName, String daName) {
TDOI tdoi = new TDOI();
tdoi.setName(doName);
Expand Down Expand Up @@ -302,8 +304,7 @@ void findDataAdapterByName_should_return_DAIAdapter_when_DA_name_dont_exist() {
@Test
void updateDaiFromExtRef_should_update_setSrcXX_values_when_ExtRef_desc_suffix_ends_with_1() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();
TDAI daiSrcRef = new TDAI();
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
TDAI daiSrcCb = new TDAI();
Expand Down Expand Up @@ -331,8 +332,7 @@ private static Optional<TVal> getDaiValOfDoi(DOIAdapter doiAdapter, String daNam
@Test
void updateDaiFromExtRef_should_update_setSrcRef_value_but_not_setSrcCB_when_ExtRef_dont_contains_CB() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();
TDAI daiSrcRef = new TDAI();
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
Expand All @@ -353,8 +353,7 @@ void updateDaiFromExtRef_should_update_setSrcRef_value_but_not_setSrcCB_when_Ext
@Test
void updateDaiFromExtRef_should_update_setSrcXX_and_setTstXX_values_when_ExtRef_desc_suffix_ends_with_1_and_3() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();
TDAI daiSrcRef = new TDAI();
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
Expand Down Expand Up @@ -406,8 +405,7 @@ private static TExtRef givenExtRef(int num, boolean withCbName) {
@Test
void updateDaiFromExtRef_should_update_only_setSrcRef_and_setTstRef_values_when_ExtRef_desc_suffix_ends_with_1_and_3_without_CB() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();
TDAI daiSrcRef = new TDAI();
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
Expand Down Expand Up @@ -468,8 +466,7 @@ void updateDaiFromExtRef_should_return_warning_report_when_none_ExtRef_endin_wit
@Test
void updateDaiFromExtRef_should_create_DAI_when_no_DAI_name_setSrcRef() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();

TExtRef extRef1 = givenExtRef(1, false);

Expand Down Expand Up @@ -504,8 +501,7 @@ void updateDaiFromExtRef_should_return_filled_ReportItem_when_no_ExtRef_in_LNode
@Test
void updateDaiFromExtRef_should_compose_correct_name_when_optional_ExtRef_attributes_are_missing() {
// Given
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
DOIAdapter doiAdapter = createDOIAdapterInScl();
TDAI daiSrcRef = new TDAI();
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
Expand Down Expand Up @@ -569,4 +565,65 @@ void updateDaiFromExtRef_should_throw_exception_when_ExtRef_desc_dont_end_with__
assertThatThrownBy(() -> doiAdapter.updateDaiFromExtRef(extRefList))
.isInstanceOf(NumberFormatException.class);
}

private DOIAdapter createDOIAdapterInScl() {
TDOI tdoi = new TDOI();
tdoi.setName("InRef");

LN0 ln0 = new LN0();
ln0.setLnType("T1");
ln0.getDOI().add(tdoi);
TLDevice tlDevice = new TLDevice();
tlDevice.setInst("Inst");
tlDevice.setLN0(ln0);
TServer tServer = new TServer();
tServer.getLDevice().add(tlDevice);
TAccessPoint tAccessPoint = new TAccessPoint();
tAccessPoint.setName("AP_NAME");
tAccessPoint.setServer(tServer);
TIED tied = new TIED();
tied.setName("IED_NAME");
tied.getAccessPoint().add(tAccessPoint);
//SCL file
SCL scd = new SCL();
scd.getIED().add(tied);
THeader tHeader = new THeader();
tHeader.setRevision("1");
scd.setHeader(tHeader);
// DataTypeTemplate
TLNodeType tlNodeType = new TLNodeType();
tlNodeType.setId("T1");
tlNodeType.getLnClass().add("LLN0");
TDO tdo = new TDO();
tdo.setName("InRef");
tdo.setType("REF");
tlNodeType.getDO().add(tdo);
TDOType tdoType = new TDOType();
tdoType.setId("REF");
TDA tda1 = createDa(DOIAdapter.DA_NAME_SET_SRC_REF);
TDA tda2 = createDa(DOIAdapter.DA_NAME_SET_SRC_CB);
TDA tda3 = createDa(DOIAdapter.DA_NAME_SET_TST_REF);
TDA tda4 = createDa(DOIAdapter.DA_NAME_SET_TST_CB);
tdoType.getSDOOrDA().addAll(List.of(tda1, tda2, tda3, tda4));

TDataTypeTemplates tDataTypeTemplates = new TDataTypeTemplates();
tDataTypeTemplates.getLNodeType().add(tlNodeType);
tDataTypeTemplates.getDOType().add(tdoType);
scd.setDataTypeTemplates(tDataTypeTemplates);

SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
LN0Adapter ln0Adapter = sclRootAdapter.getIEDAdapterByName("IED_NAME").getLDeviceAdapterByLdInst("Inst").getLN0Adapter();

DOIAdapter doiAdapter = new DOIAdapter(ln0Adapter, tdoi);
return doiAdapter;
}

private TDA createDa(String daName) {
TDA tda1 = new TDA();
tda1.setName(daName);
tda1.setValImport(true);
tda1.setBType(TPredefinedBasicTypeEnum.OBJ_REF);
tda1.setFc(TFCEnum.SP);
return tda1;
}
}
Loading

0 comments on commit fd98b51

Please sign in to comment.