Skip to content

Commit fd98b51

Browse files
authored
Merge pull request #261 from com-pas/fix/use-updatability-for-binding-inref
fix: check updatability before updating DO InRef binding
2 parents 06ec04a + 3704a09 commit fd98b51

File tree

8 files changed

+213
-71
lines changed

8 files changed

+213
-71
lines changed

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/dto/ResumedDataTemplate.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.annotation.JsonIgnore;
88
import lombok.*;
99
import org.lfenergy.compas.scl2007b4.model.*;
10+
import org.lfenergy.compas.sct.commons.scl.ied.AbstractLNAdapter;
1011
import org.lfenergy.compas.sct.commons.util.SclConstructorHelper;
1112

1213
import java.util.ArrayList;
@@ -50,12 +51,25 @@ public class ResumedDataTemplate {
5051
@NonNull
5152
private DaTypeName daName = new DaTypeName("");
5253

54+
/**
55+
* Constructor
56+
*/
57+
public ResumedDataTemplate(AbstractLNAdapter<?> lnAdapter, String doName, String daName) {
58+
this.lnClass = lnAdapter.getLNClass();
59+
this.lnInst = lnAdapter.getLNInst();
60+
this.prefix = lnAdapter.getPrefix();
61+
this.lnType = lnAdapter.getLnType();
62+
this.doName = new DoTypeName(doName);
63+
this.daName = new DaTypeName(daName);
64+
}
65+
5366
/**
5467
* Copies summarized DataTypeTemplate information to another one
68+
*
5569
* @param dtt input
5670
* @return Updated ResumedDataTemplate object
5771
*/
58-
public static ResumedDataTemplate copyFrom(ResumedDataTemplate dtt){
72+
public static ResumedDataTemplate copyFrom(ResumedDataTemplate dtt) {
5973
ResumedDataTemplate resumedDataTemplate = new ResumedDataTemplate();
6074
resumedDataTemplate.prefix = dtt.prefix;
6175
resumedDataTemplate.lnClass = dtt.lnClass;

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapter.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.lfenergy.compas.scl2007b4.model.*;
88
import org.lfenergy.compas.sct.commons.dto.ExtrefTarget;
9+
import org.lfenergy.compas.sct.commons.dto.ResumedDataTemplate;
910
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
1011
import org.lfenergy.compas.sct.commons.scl.ObjectReference;
1112
import org.lfenergy.compas.sct.commons.scl.SclElementAdapter;
@@ -109,36 +110,45 @@ public AbstractDAIAdapter<?> toAdapter(TDAI childTDAI) {
109110
* @return a filled SclReportItem if an error occurs, empty SclReportItem otherwise
110111
*/
111112
public Optional<SclReportItem> updateDaiFromExtRef(List<TExtRef> tExtRefs) {
113+
Optional<SclReportItem> optionalSclReportItem;
112114
Optional<TExtRef> tExtRefMinOptional = tExtRefs.stream().min(EXTREF_DESC_SUFFIX_COMPARATOR);
113-
114115
if (tExtRefMinOptional.isPresent() && extractDescSuffix(tExtRefMinOptional.get().getDesc()) == 1) {
115116
TExtRef tExtRefMin = tExtRefMinOptional.get();
116-
findDataAdapterByName(DA_NAME_SET_SRC_REF)
117-
.orElse(addDAI(DA_NAME_SET_SRC_REF, true))
118-
.setVal(createInRefValNominalString(tExtRefMin));
117+
String valueSrcRef = createInRefValNominalString(tExtRefMin);
118+
optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_REF, valueSrcRef);
119119
if (tExtRefMin.isSetSrcCBName()) {
120-
findDataAdapterByName(DA_NAME_SET_SRC_CB)
121-
.orElse(addDAI(DA_NAME_SET_SRC_CB, true))
122-
.setVal(createInRefValTestString(tExtRefMin));
120+
String valueSrcCb = createInRefValTestString(tExtRefMin);
121+
optionalSclReportItem = updateDAI(DA_NAME_SET_SRC_CB, valueSrcCb);
123122
}
124123

125124
Optional<TExtRef> tExtRefMaxOptional = tExtRefs.stream().max(EXTREF_DESC_SUFFIX_COMPARATOR);
126125
if (tExtRefMaxOptional.isPresent() && extractDescSuffix(tExtRefMaxOptional.get().getDesc()) > 1) {
127126
TExtRef tExtRefMax = tExtRefMaxOptional.get();
128-
findDataAdapterByName(DA_NAME_SET_TST_REF)
129-
.orElse(addDAI(DA_NAME_SET_TST_REF, true))
130-
.setVal(createInRefValNominalString(tExtRefMax));
127+
String valueTstRef = createInRefValNominalString(tExtRefMax);
128+
optionalSclReportItem = updateDAI(DA_NAME_SET_TST_REF, valueTstRef);
131129
if (tExtRefMax.isSetSrcCBName()) {
132-
findDataAdapterByName(DA_NAME_SET_TST_CB)
133-
.orElse(addDAI(DA_NAME_SET_TST_CB, true))
134-
.setVal(createInRefValTestString(tExtRefMax));
130+
String valueTstCb = createInRefValTestString(tExtRefMax);
131+
optionalSclReportItem = updateDAI(DA_NAME_SET_TST_CB, valueTstCb);
135132
}
136133
}
137134
} else {
138-
return Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath())));
135+
optionalSclReportItem = Optional.of(SclReportItem.warning(getXPath(), "The DOI %s can't be bound with an ExtRef".formatted(getXPath())));
139136
}
140137

138+
return optionalSclReportItem;
139+
}
140+
141+
private Optional<SclReportItem> updateDAI(String daName, String value) {
142+
ResumedDataTemplate daiFilterSrcRef = new ResumedDataTemplate(getParentAdapter(), getName(), daName);
143+
Optional<ResumedDataTemplate> foundDais = getParentAdapter().getDAI(daiFilterSrcRef, true).stream().findFirst();
144+
if (foundDais.isEmpty()) {
145+
return Optional.of(SclReportItem.warning(getXPath() + "/DAI@name=\"" + daName + "\"/Val", "The DAI cannot be updated"));
146+
}
147+
ResumedDataTemplate filterForUpdate = foundDais.get();
148+
filterForUpdate.setVal(value);
149+
getParentAdapter().updateDAI(filterForUpdate);
141150
return Optional.empty();
151+
142152
}
143153

144154
private static int extractDescSuffix(String desc) throws NumberFormatException {

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/dto/ResumedDataTemplateTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import org.junit.jupiter.params.ParameterizedTest;
99
import org.junit.jupiter.params.provider.Arguments;
1010
import org.junit.jupiter.params.provider.MethodSource;
11-
import org.lfenergy.compas.scl2007b4.model.TFCEnum;
12-
import org.lfenergy.compas.scl2007b4.model.TPredefinedCDCEnum;
13-
import org.lfenergy.compas.scl2007b4.model.TVal;
11+
import org.lfenergy.compas.scl2007b4.model.*;
12+
import org.lfenergy.compas.sct.commons.scl.ied.LNAdapter;
1413

1514
import java.util.List;
1615
import java.util.Map;
@@ -275,4 +274,27 @@ void setVal_should_replace_reference_val(){
275274
assertThat(rDTT.getDaName().getDaiValues()).hasSize(1)
276275
.isEqualTo(Map.of(0L, "newValue"));
277276
}
277+
278+
@Test
279+
void constructorTest() {
280+
// Given
281+
TLN tln = new TLN();
282+
tln.setLnType("T1");
283+
tln.getLnClass().add(TLLN0Enum.LLN_0.value());
284+
tln.setPrefix("P1");
285+
LNAdapter lnAdapter = new LNAdapter(null, tln);
286+
// When
287+
ResumedDataTemplate expected = new ResumedDataTemplate(lnAdapter, "do", "da");
288+
// Then
289+
assertThat(expected.getLnClass()).isEqualTo(TLLN0Enum.LLN_0.value());
290+
assertThat(expected.getLnType()).isEqualTo("T1");
291+
assertThat(expected.getPrefix()).isEqualTo("P1");
292+
assertThat(expected.getDoName().getName()).isEqualTo("do");
293+
assertThat(expected.getDoName().getName()).isEqualTo("do");
294+
assertThat(expected.getDoName().getStructNames()).isEmpty();
295+
assertThat(expected.getDaName().getName()).isEqualTo("da");
296+
assertThat(expected.getDaName().getDaiValues()).isEmpty();
297+
assertThat(expected.getDaName().isValImport()).isFalse();
298+
assertThat(expected.getDaName().isUpdatable()).isFalse();
299+
}
278300
}

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/SclServiceTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ private Optional<TVal> getLDeviceStatusValue(SCL scl, String iedName, String ldI
10901090
@ParameterizedTest(name = "{0}")
10911091
@CsvSource({
10921092
"Test update setSrcRef Value,LD_WITH_1_InRef,InRef2,setSrcRef,IED_NAME1LD_WITH_1_InRef/PRANCR1.Do11.sdo11",
1093-
"Test update setSrcCB Value,LD_WITH_1_InRef,InRef2,setSrcCB,IED_NAME1LD_WITH_1_InRef/prefixANCR1.GSE1",
1093+
"Test update setSrcCB Value,LD_WITH_1_InRef,InRef2,setSrcCB,OLD_VAL",
10941094
"Test update setSrcRef Value,LD_WITH_3_InRef,InRef3,setSrcRef,IED_NAME1LD_WITH_3_InRef/PRANCR1.Do11.sdo11",
10951095
"Test update setSrcCB Value,LD_WITH_3_InRef,InRef3,setSrcCB,IED_NAME1LD_WITH_3_InRef/prefixANCR1.GSE1",
10961096
"Test update setTstRef Value,LD_WITH_3_InRef,InRef3,setTstRef,IED_NAME1LD_WITH_3_InRef/PRANCR1.Do11.sdo11",
@@ -1105,6 +1105,7 @@ void updateDoInRef_shouldReturnUpdatedFile(String testName, String ldInst, Strin
11051105

11061106
// Then
11071107
assertThat(sclReport.isSuccess()).isTrue();
1108+
SclTestMarshaller.assertIsMarshallable(sclReport.getSclRootAdapter().currentElem);
11081109
assertThat(getValFromDaiName(sclReport.getSclRootAdapter().getCurrentElem(), "IED_NAME1", ldInst, doName, daName)
11091110
.map(TVal::getValue))
11101111
.hasValue(expected);
@@ -1208,6 +1209,4 @@ void analyzeDataGroups_should_return_errors_messages() {
12081209
"There are too much GOOSE Control Blocks for the IED IED_NAME2: 3 > 2 max",
12091210
"There are too much SMV Control Blocks for the IED IED_NAME2: 3 > 1 max");
12101211
}
1211-
1212-
12131212
}

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DOIAdapterTest.java

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.lang3.tuple.Pair;
88
import org.assertj.core.groups.Tuple;
99
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
1011
import org.lfenergy.compas.scl2007b4.model.*;
1112
import org.lfenergy.compas.sct.commons.dto.DaTypeName;
1213
import org.lfenergy.compas.sct.commons.dto.DoTypeName;
@@ -15,13 +16,15 @@
1516
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
1617
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
1718
import org.lfenergy.compas.sct.commons.util.CommonConstants;
19+
import org.mockito.junit.jupiter.MockitoExtension;
1820

1921
import java.util.*;
2022

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

27+
@ExtendWith(MockitoExtension.class)
2528
class DOIAdapterTest {
2629

2730
@Test
@@ -183,7 +186,7 @@ void DAIAdapter_update_when_valImport_is_set_to_false_but_da_is_Mod_StVal_should
183186
}
184187

185188
@Test
186-
void testFindDeepestMatch() throws Exception {
189+
void testFindDeepestMatch() {
187190
// Given
188191
SCL scd = SclTestMarshaller.getSCLFromFile("/ied-test-schema-conf/ied_unit_test.xml");
189192
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
@@ -215,7 +218,6 @@ void testFindDeepestMatch() throws Exception {
215218
assertThat(pair.getLeft()).isInstanceOf(SDIAdapter.DAIAdapter.class);
216219
}
217220

218-
219221
private DOIAdapter.DAIAdapter initInnerDAIAdapter(String doName, String daName) {
220222
TDOI tdoi = new TDOI();
221223
tdoi.setName(doName);
@@ -302,8 +304,7 @@ void findDataAdapterByName_should_return_DAIAdapter_when_DA_name_dont_exist() {
302304
@Test
303305
void updateDaiFromExtRef_should_update_setSrcXX_values_when_ExtRef_desc_suffix_ends_with_1() {
304306
// Given
305-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
306-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
307+
DOIAdapter doiAdapter = createDOIAdapterInScl();
307308
TDAI daiSrcRef = new TDAI();
308309
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
309310
TDAI daiSrcCb = new TDAI();
@@ -331,8 +332,7 @@ private static Optional<TVal> getDaiValOfDoi(DOIAdapter doiAdapter, String daNam
331332
@Test
332333
void updateDaiFromExtRef_should_update_setSrcRef_value_but_not_setSrcCB_when_ExtRef_dont_contains_CB() {
333334
// Given
334-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
335-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
335+
DOIAdapter doiAdapter = createDOIAdapterInScl();
336336
TDAI daiSrcRef = new TDAI();
337337
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
338338
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
@@ -353,8 +353,7 @@ void updateDaiFromExtRef_should_update_setSrcRef_value_but_not_setSrcCB_when_Ext
353353
@Test
354354
void updateDaiFromExtRef_should_update_setSrcXX_and_setTstXX_values_when_ExtRef_desc_suffix_ends_with_1_and_3() {
355355
// Given
356-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
357-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
356+
DOIAdapter doiAdapter = createDOIAdapterInScl();
358357
TDAI daiSrcRef = new TDAI();
359358
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
360359
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
@@ -406,8 +405,7 @@ private static TExtRef givenExtRef(int num, boolean withCbName) {
406405
@Test
407406
void updateDaiFromExtRef_should_update_only_setSrcRef_and_setTstRef_values_when_ExtRef_desc_suffix_ends_with_1_and_3_without_CB() {
408407
// Given
409-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
410-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
408+
DOIAdapter doiAdapter = createDOIAdapterInScl();
411409
TDAI daiSrcRef = new TDAI();
412410
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
413411
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
@@ -468,8 +466,7 @@ void updateDaiFromExtRef_should_return_warning_report_when_none_ExtRef_endin_wit
468466
@Test
469467
void updateDaiFromExtRef_should_create_DAI_when_no_DAI_name_setSrcRef() {
470468
// Given
471-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
472-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
469+
DOIAdapter doiAdapter = createDOIAdapterInScl();
473470

474471
TExtRef extRef1 = givenExtRef(1, false);
475472

@@ -504,8 +501,7 @@ void updateDaiFromExtRef_should_return_filled_ReportItem_when_no_ExtRef_in_LNode
504501
@Test
505502
void updateDaiFromExtRef_should_compose_correct_name_when_optional_ExtRef_attributes_are_missing() {
506503
// Given
507-
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "da");
508-
DOIAdapter doiAdapter = daiAdapter.getParentAdapter();
504+
DOIAdapter doiAdapter = createDOIAdapterInScl();
509505
TDAI daiSrcRef = new TDAI();
510506
daiSrcRef.setName(DOIAdapter.DA_NAME_SET_SRC_REF);
511507
doiAdapter.getCurrentElem().getSDIOrDAI().add(daiSrcRef);
@@ -569,4 +565,65 @@ void updateDaiFromExtRef_should_throw_exception_when_ExtRef_desc_dont_end_with__
569565
assertThatThrownBy(() -> doiAdapter.updateDaiFromExtRef(extRefList))
570566
.isInstanceOf(NumberFormatException.class);
571567
}
568+
569+
private DOIAdapter createDOIAdapterInScl() {
570+
TDOI tdoi = new TDOI();
571+
tdoi.setName("InRef");
572+
573+
LN0 ln0 = new LN0();
574+
ln0.setLnType("T1");
575+
ln0.getDOI().add(tdoi);
576+
TLDevice tlDevice = new TLDevice();
577+
tlDevice.setInst("Inst");
578+
tlDevice.setLN0(ln0);
579+
TServer tServer = new TServer();
580+
tServer.getLDevice().add(tlDevice);
581+
TAccessPoint tAccessPoint = new TAccessPoint();
582+
tAccessPoint.setName("AP_NAME");
583+
tAccessPoint.setServer(tServer);
584+
TIED tied = new TIED();
585+
tied.setName("IED_NAME");
586+
tied.getAccessPoint().add(tAccessPoint);
587+
//SCL file
588+
SCL scd = new SCL();
589+
scd.getIED().add(tied);
590+
THeader tHeader = new THeader();
591+
tHeader.setRevision("1");
592+
scd.setHeader(tHeader);
593+
// DataTypeTemplate
594+
TLNodeType tlNodeType = new TLNodeType();
595+
tlNodeType.setId("T1");
596+
tlNodeType.getLnClass().add("LLN0");
597+
TDO tdo = new TDO();
598+
tdo.setName("InRef");
599+
tdo.setType("REF");
600+
tlNodeType.getDO().add(tdo);
601+
TDOType tdoType = new TDOType();
602+
tdoType.setId("REF");
603+
TDA tda1 = createDa(DOIAdapter.DA_NAME_SET_SRC_REF);
604+
TDA tda2 = createDa(DOIAdapter.DA_NAME_SET_SRC_CB);
605+
TDA tda3 = createDa(DOIAdapter.DA_NAME_SET_TST_REF);
606+
TDA tda4 = createDa(DOIAdapter.DA_NAME_SET_TST_CB);
607+
tdoType.getSDOOrDA().addAll(List.of(tda1, tda2, tda3, tda4));
608+
609+
TDataTypeTemplates tDataTypeTemplates = new TDataTypeTemplates();
610+
tDataTypeTemplates.getLNodeType().add(tlNodeType);
611+
tDataTypeTemplates.getDOType().add(tdoType);
612+
scd.setDataTypeTemplates(tDataTypeTemplates);
613+
614+
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
615+
LN0Adapter ln0Adapter = sclRootAdapter.getIEDAdapterByName("IED_NAME").getLDeviceAdapterByLdInst("Inst").getLN0Adapter();
616+
617+
DOIAdapter doiAdapter = new DOIAdapter(ln0Adapter, tdoi);
618+
return doiAdapter;
619+
}
620+
621+
private TDA createDa(String daName) {
622+
TDA tda1 = new TDA();
623+
tda1.setName(daName);
624+
tda1.setValImport(true);
625+
tda1.setBType(TPredefinedBasicTypeEnum.OBJ_REF);
626+
tda1.setFc(TFCEnum.SP);
627+
return tda1;
628+
}
572629
}

0 commit comments

Comments
 (0)