Skip to content

Commit

Permalink
use DoLinkedToDaFilter for getDOAndDAInstances
Browse files Browse the repository at this point in the history
Signed-off-by: massifben <[email protected]>
  • Loading branch information
massifben committed Jun 19, 2024
1 parent b7dc91a commit 6c0bdd8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public Stream<DoLinkedToDa> getAllDoLinkedToDa(TDataTypeTemplates dtt) {
public Stream<DoLinkedToDa> getFilteredDoLinkedToDa(TDataTypeTemplates dtt, String lNodeTypeId, DoLinkedToDaFilter doLinkedToDaFilter) {
return lnodeTypeService.findLnodeType(dtt, tlNodeType -> tlNodeType.getId().equals(lNodeTypeId))
.stream()
.flatMap(tlNodeType -> doService.getFilteredDos(tlNodeType, tdo -> StringUtils.isBlank(doLinkedToDaFilter.getDoName())
|| (StringUtils.isNotBlank(doLinkedToDaFilter.getDoName()) && doLinkedToDaFilter.getDoName().equals(tdo.getName())))
.flatMap(tlNodeType -> doService.getFilteredDos(tlNodeType, tdo -> StringUtils.isBlank(doLinkedToDaFilter.doName())
|| doLinkedToDaFilter.doName().equals(tdo.getName()))
.flatMap(tdo -> {
DoLinkedToDa doLinkedToDa = new DoLinkedToDa();
DataObject dataObject = new DataObject();
Expand All @@ -88,8 +88,9 @@ public Stream<DoLinkedToDa> getFilteredDoLinkedToDa(TDataTypeTemplates dtt, Stri
.flatMap(tdoType -> {
doLinkedToDa.getDataObject().setCdc(tdoType.getCdc());
return doTypeService.getAllSDOLinkedToDa(dtt, tdoType, doLinkedToDa).stream()
.filter(doLinkedToDa1 -> StringUtils.isBlank(doLinkedToDaFilter.getDoName()) || doLinkedToDa1.getDoRef().startsWith(doLinkedToDaFilter.getDoRef())
&& StringUtils.isBlank(doLinkedToDaFilter.getDaName()) || doLinkedToDa1.getDaRef().startsWith(doLinkedToDaFilter.getDaRef()));
.filter(doLinkedToDa1 -> StringUtils.isBlank(doLinkedToDaFilter.doName())
|| (doLinkedToDa1.getDoRef().startsWith(doLinkedToDaFilter.getDoRef()) && StringUtils.isBlank(doLinkedToDaFilter.daName()))
|| doLinkedToDa1.getDaRef().startsWith(doLinkedToDaFilter.getDaRef()));
});
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import lombok.extern.slf4j.Slf4j;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.LnEditor;
import org.lfenergy.compas.sct.commons.domain.DaVal;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DataObject;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.*;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

import java.util.Collection;
Expand Down Expand Up @@ -92,12 +89,12 @@ public Stream<TAnyLN> getActiveLns(TLDevice tlDevice) {
}

@Override
public Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DataObject dataObject, DataAttribute dataAttribute) {
List<String> structNamesList = new ArrayList<>(dataObject.getSdoNames());
structNamesList.add(dataAttribute.getDaName());
structNamesList.addAll(dataAttribute.getBdaNames());
public Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDaFilter doLinkedToDaFilter) {
List<String> structNamesList = new ArrayList<>(doLinkedToDaFilter.sdoNames());
structNamesList.add(doLinkedToDaFilter.daName());
structNamesList.addAll(doLinkedToDaFilter.bdaNames());

return tAnyLN.getDOI().stream().filter(doi -> doi.getName().equals(dataObject.getDoName()))
return tAnyLN.getDOI().stream().filter(doi -> doi.getName().equals(doLinkedToDaFilter.doName()))
.findFirst()
.flatMap(doi -> {
if(structNamesList.size() > 1) {
Expand Down Expand Up @@ -151,7 +148,7 @@ public void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedT
}

public void completeFromDAInstance(TIED tied, String ldInst, TAnyLN anyLN, DoLinkedToDa doLinkedToDa) {
getDOAndDAInstances(anyLN, doLinkedToDa.getDataObject(), doLinkedToDa.getDataAttribute())
getDOAndDAInstances(anyLN, doLinkedToDa.toFilter())
.ifPresent(tdai -> {
if(tdai.isSetVal()) {
doLinkedToDa.getDataAttribute().addDaVal(tdai.getVal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import org.lfenergy.compas.scl2007b4.model.TAnyLN;
import org.lfenergy.compas.scl2007b4.model.TDAI;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DataObject;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;

import java.util.Optional;

public interface LnEditor {

Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DataObject dataObject, DataAttribute dataAttribute);
Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDaFilter doLinkedToDaFilter);

void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedToDa);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package org.lfenergy.compas.sct.commons.domain;


import lombok.*;
import lombok.Getter;
import lombok.Setter;

import static org.lfenergy.compas.sct.commons.util.CommonConstants.MOD_DO_NAME;
import static org.lfenergy.compas.sct.commons.util.CommonConstants.STVAL_DA_NAME;
Expand Down Expand Up @@ -50,5 +51,8 @@ public boolean isUpdatable() {
private boolean isDOModDAstVal() {
return dataObject.getDoName().equals(MOD_DO_NAME) && dataAttribute.getDaName().equals(STVAL_DA_NAME);
}
}

public DoLinkedToDaFilter toFilter() {
return DoLinkedToDaFilter.from(getDoRef(), getDaRef());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,46 @@
package org.lfenergy.compas.sct.commons.domain;


import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Getter
@Setter
public class DoLinkedToDaFilter {
public record DoLinkedToDaFilter(String doName, List<String> sdoNames, String daName, List<String> bdaNames) {

private String doName;
private String daName;
private List<String> sdoNames = new ArrayList<>();
private List<String> bdaNames = new ArrayList<>();

public String getDoRef() {
return doName + (getSdoNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", getSdoNames()));
public DoLinkedToDaFilter(String doName, List<String> sdoNames, String daName, List<String> bdaNames) {
this.doName = StringUtils.isBlank(doName) ? null : doName;
this.sdoNames = sdoNames == null ? Collections.emptyList() : List.copyOf(sdoNames);
this.daName = StringUtils.isBlank(daName) ? null : daName;
this.bdaNames = bdaNames == null ? Collections.emptyList() : List.copyOf(bdaNames);
}
public DoLinkedToDaFilter(){
this(null,null,null,null);
}

public String getDaRef() {
return daName + (getBdaNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", getBdaNames()));
public static DoLinkedToDaFilter from(String doNames, String daNames) {
String doName = null;
List<String> sdoNames = null;
String daName = null;
List<String> bdaNames = null;
if (StringUtils.isNotBlank(doNames)){
doName = doNames.split("\\.")[0];
sdoNames = Arrays.stream(doNames.split("\\.")).skip(1).toList();
}
if (StringUtils.isNotBlank(daNames)){
daName = daNames.split("\\.")[0];
bdaNames = Arrays.stream(daNames.split("\\.")).skip(1).toList();
}
return new DoLinkedToDaFilter(doName, sdoNames, daName, bdaNames);
}

public String getDoRef() {
return doName + (sdoNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", sdoNames()));
}

public String getDaRef() {
return daName + (bdaNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", bdaNames()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ void getFilteredDOAndDA_when_given_DoName_should_return_expected_dataReference()
TDataTypeTemplates dtt = initDttFromFile("/dtt-test-schema-conf/scd_dtt_do_sdo_da_bda_test.xml");
String lNodeTypeId = "LNodeType0";

DoLinkedToDaFilter doLinkedToDaFilter = new DoLinkedToDaFilter();
doLinkedToDaFilter.setDoName("SecondDoName");
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("SecondDoName", "");

//When
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
Expand All @@ -208,9 +207,7 @@ void getFilteredDOAndDA_when_given_DO_with_one_structName_should_return_expected
TDataTypeTemplates dtt = initDttFromFile("/dtt-test-schema-conf/scd_dtt_do_sdo_da_bda_test.xml");
String lNodeTypeId = "LNodeType0";

DoLinkedToDaFilter doLinkedToDaFilter = new DoLinkedToDaFilter();
doLinkedToDaFilter.setDoName("FirstDoName");
doLinkedToDaFilter.setSdoNames(List.of("sdoName1"));
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("FirstDoName.sdoName1", "");
//When
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
List<DoLinkedToDa> result = dataTypeTemplatesService.getFilteredDoLinkedToDa(dtt, lNodeTypeId, doLinkedToDaFilter).toList();
Expand All @@ -232,9 +229,7 @@ void getFilteredDOAndDA_when_given_DO_with_many_structName_should_return_expecte
TDataTypeTemplates dtt = initDttFromFile("/dtt-test-schema-conf/scd_dtt_do_sdo_da_bda_test.xml");
String lNodeTypeId = "LNodeType0";

DoLinkedToDaFilter doLinkedToDaFilter = new DoLinkedToDaFilter();
doLinkedToDaFilter.setDoName("FirstDoName");
doLinkedToDaFilter.setSdoNames(List.of("sdoName1", "sdoName21"));
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("FirstDoName.sdoName1.sdoName21", "");
//When
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
List<DoLinkedToDa> result = dataTypeTemplatesService.getFilteredDoLinkedToDa(dtt, lNodeTypeId, doLinkedToDaFilter).toList();
Expand All @@ -255,11 +250,7 @@ void getFilteredDOAndDA_when_given_DO_and_DA_with_structNames_should_return_expe
TDataTypeTemplates dtt = initDttFromFile("/dtt-test-schema-conf/scd_dtt_do_sdo_da_bda_test.xml");
String lNodeTypeId = "LNodeType0";

DoLinkedToDaFilter doLinkedToDaFilter = new DoLinkedToDaFilter();
doLinkedToDaFilter.setDoName("FirstDoName");
doLinkedToDaFilter.setSdoNames(List.of("sdoName2"));
doLinkedToDaFilter.setDaName("structDaName1");
doLinkedToDaFilter.setBdaNames(List.of("structBdaName1", "enumBdaName22"));
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("FirstDoName.sdoName2", "structDaName1.structBdaName1.enumBdaName22");

//When
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
Expand Down Expand Up @@ -538,4 +529,4 @@ void getAllDOAndDA_should_return_all_dataReference() {
);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.domain.DaVal;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DataObject;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.*;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

Expand Down Expand Up @@ -171,15 +168,10 @@ void getDOAndDAInstance_should_return_true_when_DO_and_DA_instances_exists() {
new LinkedList<>(List.of("antRef","bda1", "bda2", "bda3")),
"new value",null
);
DataObject dataObject = new DataObject();
dataObject.setDoName("Do");
dataObject.setSdoNames(List.of("sdo1","d"));
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("antRef");
dataAttribute.setBdaNames(List.of("bda1","bda2","bda3"));
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("Do.sdo1.d", "antRef.bda1.bda2.bda3");
//When
LnService lnService = new LnService();
Optional<TDAI> optionalTDAI = lnService.getDOAndDAInstances(tAnyLN, dataObject, dataAttribute);
Optional<TDAI> optionalTDAI = lnService.getDOAndDAInstances(tAnyLN, doLinkedToDaFilter);
//Then
assertThat(optionalTDAI).isPresent();
assertThat(optionalTDAI.get().getName()).isEqualTo("bda3");
Expand All @@ -194,15 +186,10 @@ void getDOAndDAInstance_should_return_false_when_DO_and_DA_instances_not_exists(
new LinkedList<>(List.of("antRef","bda1", "bda2", "bda3")),
"new value",null
);
DataObject dataObject = new DataObject();
dataObject.setDoName("Do");
dataObject.setSdoNames(List.of("sdo1","d"));
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("antRef");
dataAttribute.setBdaNames(List.of("unknown","bda2","bda3"));
DoLinkedToDaFilter doLinkedToDaFilter = DoLinkedToDaFilter.from("Do.sdo1.d", "antRef.unknown.bda2.bda3");
//When
LnService lnService = new LnService();
Optional<TDAI> optionalTDAI = lnService.getDOAndDAInstances(tAnyLN, dataObject, dataAttribute);
Optional<TDAI> optionalTDAI = lnService.getDOAndDAInstances(tAnyLN, doLinkedToDaFilter);
//Then
assertThat(optionalTDAI).isEmpty();
}
Expand Down Expand Up @@ -594,4 +581,4 @@ private TDAI initDOAndDAInstances(TAnyLN tAnyLN,
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons.domain;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class DoLinkedToDaFilterTest {

public static Stream<Arguments> provideDoLinkedToDaFilter() {
return Stream.of(
Arguments.of(null, null, new DoLinkedToDaFilter(null, null, null, null)),
Arguments.of("", "", new DoLinkedToDaFilter(null, null, null, null)),
Arguments.of("Do", "", new DoLinkedToDaFilter("Do", null, null, null)),
Arguments.of("", "da", new DoLinkedToDaFilter(null, null, "da", null)),
Arguments.of("Do", "da", new DoLinkedToDaFilter("Do", null, "da", null)),
Arguments.of("Do.sdo", "da", new DoLinkedToDaFilter("Do", List.of("sdo"), "da", null)),
Arguments.of("Do", "da.bda", new DoLinkedToDaFilter("Do", null, "da", List.of("bda"))),
Arguments.of("Do.sdo", "da.bda", new DoLinkedToDaFilter("Do", List.of("sdo"), "da", List.of("bda"))),
Arguments.of("Do.sdo1.sdo2.sdo3.sdo4", "da.bda1.bda2.bda3.bda4", new DoLinkedToDaFilter("Do", List.of("sdo1", "sdo2", "sdo3", "sdo4"), "da", List.of("bda1", "bda2", "bda3", "bda4")))
);
}

@ParameterizedTest
@MethodSource("provideDoLinkedToDaFilter")
void from_should_parse_DO_and_DA(String doNames, String daNames, DoLinkedToDaFilter expected) {
// Given : parameters
// When
DoLinkedToDaFilter result = DoLinkedToDaFilter.from(doNames, daNames);
// Then
assertThat(result).isEqualTo(expected);
}
}

0 comments on commit 6c0bdd8

Please sign in to comment.