From e3f5545168163fdbf0d83314ee9dd51983e4bcc8 Mon Sep 17 00:00:00 2001 From: imamSidero Date: Thu, 18 May 2023 17:56:32 +0100 Subject: Provide index token to tosca function for nested lists Index token capability is provided in tosca function for all nested levels of list and custom types Issue-ID: SDC-4505 Signed-off-by: Imam hussain Change-Id: If21c0078e0d17c44b5a31b00d6fac3e18ff6831d --- .../elements/ToscaFunctionJsonDeserializer.java | 38 ++++++++++++++-------- .../elements/ToscaGetFunctionDataDefinition.java | 27 ++++++++------- .../ToscaGetFunctionDataDefinitionTest.java | 2 ++ 3 files changed, 43 insertions(+), 24 deletions(-) (limited to 'common-be/src') diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java index de8e30b897..f77c6f949b 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java @@ -116,7 +116,8 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer @@ -149,20 +150,31 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer getNumberAsTextOrElseNull(final JsonNode node, final String fieldName, final DeserializationContext context) throws JsonMappingException{ + List toscaIndexList = new ArrayList(); final JsonNode jsonNode = node.get(fieldName); - if (jsonNode == null) { - return null; - } - if (jsonNode.asText().equalsIgnoreCase("INDEX")) { - return jsonNode.asText(); - } - try { - Integer.parseInt(jsonNode.asText()); - } catch(Exception e) { - return null; + if (jsonNode != null) { + if (!jsonNode.isArray()) { + throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for toscaIndexList attribute"); + } + for (int index=0;index propertyPathFromSource = new ArrayList<>(); - private Object toscaIndex; + private List toscaIndexList; public ToscaGetFunctionDataDefinition() { //necessary for JSON conversions @@ -89,10 +89,13 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct ); } if (propertySource == PropertySource.SELF) { - if (toscaIndex != null) { - Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex; + if (toscaIndexList.size() > 0) { + List parsedIndexList = new ArrayList(); + toscaIndexList.forEach((obj) -> { + parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj); + }); return Map.of(functionType.getFunctionName(), - Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList()) + Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList()) ); } return Map.of(functionType.getFunctionName(), @@ -105,10 +108,13 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct String.format("sourceName is required in order to generate the %s from INSTANCE value", functionType.getFunctionName()) ); } - if (toscaIndex != null) { - Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex; + if (toscaIndexList.size() > 0) { + List parsedIndexList = new ArrayList(); + toscaIndexList.forEach((obj) -> { + parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj); + }); return Map.of(functionType.getFunctionName(), - Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList()) + Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList()) ); } return Map.of(functionType.getFunctionName(), @@ -123,10 +129,9 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct List propertySourceCopy = new ArrayList(this.propertyPathFromSource); List propertySourceOneCopy = new ArrayList<>(); propertySourceOneCopy.add(this.propertyPathFromSource.get(0)); - if (toscaIndex != null) { - Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex; - propertySourceCopy.add(toscaIndexValue); - propertySourceOneCopy.add(toscaIndexValue); + if (toscaIndexList.size() > 0) { + propertySourceCopy.addAll(toscaIndexList); + propertySourceOneCopy.addAll(toscaIndexList); } if (this.propertyPathFromSource.size() == 1) { return Map.of(this.functionType.getFunctionName(), propertySourceOneCopy); diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java index 581f62a91f..3add149e23 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java @@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.gson.Gson; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -206,6 +207,7 @@ class ToscaGetFunctionDataDefinitionTest { toscaGetFunction.setPropertySource(propertySource); toscaGetFunction.setPropertyPathFromSource(propertyPath); toscaGetFunction.setSourceName(sourceName); + toscaGetFunction.setToscaIndexList(new ArrayList<>()); return toscaGetFunction; } -- cgit 1.2.3-korg