diff options
author | JvD_Ericsson <jeff.van.dam@est.tech> | 2023-03-29 12:34:59 +0100 |
---|---|---|
committer | JvD_Ericsson <jeff.van.dam@est.tech> | 2023-04-13 09:03:40 +0100 |
commit | 35ee855d664246d55694f06b61bde82277cb2c5c (patch) | |
tree | d0cabb56d4eb6ab853ffccb95d1f4267f8ea913e /common-be/src/test | |
parent | 4e11060f0af869eba2c6129cfdfab84f25e51a6b (diff) |
Backend support for custom functions
Issue-ID: SDC-4455
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: Idb0fd38681066ba9d541f8564c85e316cf03e927
Diffstat (limited to 'common-be/src/test')
2 files changed, 95 insertions, 0 deletions
diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java index b17a3dc3a3..d7c2e7c65e 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java @@ -139,6 +139,56 @@ class ToscaFunctionJsonDeserializerTest { assertDoesNotThrow(() -> new Yaml().load(toscaFunction.getValue())); } + @Test + void testCustomToscaFunction() throws IOException { + //given + final String toscaCustomFunction = Files.readString(TEST_RESOURCES_PATH.resolve("customFunction.json")); + //when + final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunction); + //then + assertTrue(toscaFunction instanceof ToscaCustomFunction); + final Object yamlObject = new Yaml().load(toscaFunction.getValue()); + assertTrue(yamlObject instanceof Map); + final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject; + final Object customFunctionObj = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName()); + assertNotNull(customFunctionObj); + assertTrue(customFunctionObj instanceof List); + final List<Object> customFunctionParameters = (List<Object>) customFunctionObj; + assertEquals(3, customFunctionParameters.size(), "Expecting three parameters"); + assertEquals("string1", customFunctionParameters.get(0)); + + assertTrue(customFunctionParameters.get(1) instanceof Map); + final Map<String, Object> parameter1Map = (Map<String, Object>) customFunctionParameters.get(1); + assertNotNull(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName())); + assertTrue(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()) instanceof List); + List<String> getAttributeParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()); + assertEquals(2, getAttributeParameters.size(), "Expecting two parameters in the get_attribute function"); + assertEquals("SELF", getAttributeParameters.get(0)); + assertEquals("descriptor_id", getAttributeParameters.get(1)); + + assertTrue(customFunctionParameters.get(2) instanceof Map); + final Map<String, Object> parameter2Map = (Map<String, Object>) customFunctionParameters.get(2); + Object customFunctionObj2 = parameter2Map.get(parameter2Map.keySet().stream().iterator().next()); + assertNotNull(customFunctionObj2); + assertTrue(customFunctionObj2 instanceof List); + List<Object> customParameters = (List<Object>) customFunctionObj2; + assertEquals(2, customParameters.size(), "Expecting two parameters in the sub custom function"); + assertTrue(customParameters.get(0) instanceof Map); + final Map<String, Object> concatFunctionValueMap = (Map<String, Object>) customParameters.get(0); + assertNotNull(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName())); + assertTrue(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()) instanceof List); + List<Object> concatParameters = (List<Object>) concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()); + assertEquals(2, concatParameters.size(), "Expecting two parameters in the sub concat function"); + assertEquals("string2", concatParameters.get(0)); + assertTrue(concatParameters.get(1) instanceof Map); + Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(1); + assertTrue(yamlFunctionValueMap.get("myList") instanceof List); + assertTrue(yamlFunctionValueMap.get("get_something") instanceof List); + assertTrue(yamlFunctionValueMap.get("string") instanceof String); + + assertEquals("string3", customParameters.get(1)); + } + private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException { return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class); } diff --git a/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunction.json b/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunction.json new file mode 100644 index 0000000000..0042975e29 --- /dev/null +++ b/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunction.json @@ -0,0 +1,45 @@ +{ + "type": "CUSTOM", + "name": "first_custom_function_name", + "parameters": [ + { + "type": "STRING", + "value": "string1" + }, + { + "type": "GET_ATTRIBUTE", + "propertyUniqueId": "36897651-f5e5-4603-8064-b60c771a3c37.descriptor_id", + "propertyName": "descriptor_id", + "propertySource": "SELF", + "sourceUniqueId": "36897651-f5e5-4603-8064-b60c771a3c37", + "sourceName": "testService", + "functionType": "GET_ATTRIBUTE", + "propertyPathFromSource": [ + "descriptor_id" + ] + }, + { + "type": "CUSTOM", + "name": "second_custom_function_name", + "parameters": [ + { + "type": "CONCAT", + "parameters": [ + { + "type": "STRING", + "value": "string2" + }, + { + "type": "YAML", + "value": "myList: [1, two, three three]\nget_something: [SELF, something]\nstring: this is my string\n" + } + ] + }, + { + "type": "STRING", + "value": "string3" + } + ] + } + ] +}
\ No newline at end of file |