Skip to content

Commit

Permalink
Merge pull request #399 from com-pas/feat/added_isUpdatable_in_DoLink…
Browse files Browse the repository at this point in the history
…edToDa

added isUpdatable in DoLinkedToDa
  • Loading branch information
massifben authored Jun 20, 2024
2 parents 69d2959 + 3d3b05c commit df3e89a
Show file tree
Hide file tree
Showing 11 changed files with 271 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 @@ -46,5 +46,19 @@ public String toString(){
return daName + (getBdaNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", getBdaNames()));
}

/**
* Check if DA Object is updatable
* @return boolean value of DA state
*/
public boolean isUpdatable(){
return isValImport() &&
(fc == TFCEnum.CF ||
fc == TFCEnum.DC ||
fc == TFCEnum.SG ||
fc == TFCEnum.SP ||
fc == TFCEnum.ST ||
fc == TFCEnum.SE
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
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;


@Getter
Expand All @@ -30,5 +34,29 @@ public String getDaRef() {
return dataAttribute != null ? dataAttribute.toString() : "";
}

}
/**
* Checks if DA/DO is updatable
*
* @return true if updatable, false otherwise
*/
public boolean isUpdatable() {
return isDOModDAstVal() || dataAttribute.isUpdatable();
}

/**
* Checks if DO is Mod and DA is stVal
*
* @return true if DO is "Mod" and DA is "stVal", false otherwise
*/
private boolean isDOModDAstVal() {
return dataObject.getDoName().equals(MOD_DO_NAME) && dataAttribute.getDaName().equals(STVAL_DA_NAME);
}

/**
* Create DoLinkedToDaFilter from this
* @return new DoLinkedToDaFilter instance with same data Object and data Attribute Ref
*/
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,75 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.lfenergy.compas.scl2007b4.model.TFCEnum;

import java.util.List;

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

class DataAttributeTest {

@Test
void isUpdatable_should_return_true_whenValImportIsTrue() {
// Given
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("daName");
dataAttribute.setBdaNames(List.of("bdaName1"));
dataAttribute.setValImport(true);
dataAttribute.setFc(TFCEnum.SE);
// When Then
assertThat(dataAttribute.isUpdatable()).isTrue();
}

@Test
void isUpdatable_should_return_false_whenValImportIsFalse() {
// Given
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("daName");
dataAttribute.setBdaNames(List.of("bdaName1"));
dataAttribute.setValImport(false);
dataAttribute.setFc(TFCEnum.SE);
// When Then
assertThat(dataAttribute.isUpdatable()).isFalse();
}


@Test
void isUpdatable_should_return_true_whenFcIsNotAppropriate() {
// Given
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("daName");
dataAttribute.setBdaNames(List.of("bdaName1"));
dataAttribute.setValImport(true);
dataAttribute.setFc(TFCEnum.MX);
// When Then
assertThat(dataAttribute.isUpdatable()).isFalse();
}


@ParameterizedTest
@CsvSource(value = {"MX:true:false", "CF:true:true", "CF:false:false",
"DC:false:false", "DC:false:false",
"SG:false:false", "SG:false:false",
"SP:false:false", "SP:false:false",
"ST:false:false", "ST:false:false",
"SE:false:false", "SE:false:false"}, delimiter = ':')
void isUpdatable_should_return_ExpectedValue(String fcVal, boolean valImport, boolean expected) {
// Given
DataAttribute dataAttribute = new DataAttribute();
dataAttribute.setDaName("daName");
dataAttribute.setBdaNames(List.of("bdaName1"));
dataAttribute.setValImport(valImport);
dataAttribute.setFc(TFCEnum.valueOf(fcVal));
// When Then
assertThat(dataAttribute.isUpdatable()).isEqualTo(expected);

}

}
Loading

0 comments on commit df3e89a

Please sign in to comment.