summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib
diff options
context:
space:
mode:
authorfranciscovila <javier.paradela.vila@est.tech>2021-12-22 12:41:19 +0000
committerMichael Morris <michael.morris@est.tech>2022-01-05 11:14:12 +0000
commita811119a9c380bf01f482f08828a9a4b7a8dff6e (patch)
tree2628f4a185b9140f705c33f9aedbddf318c8aa82 /openecomp-be/lib/openecomp-sdc-translator-lib
parenta8a5863cfb100d4cbbecd0aefe5512234e101d97 (diff)
Fix use of Optional in HeatToToscaUtil
Checking the Optionals are present before getting their values in the HeatToToscaUtil class. Adding a new test class for this purpose Issue-ID: SDC-3018 Signed-off-by: franciscovila <javier.paradela.vila@est.tech> Change-Id: I9fbfbb6f9ebff1f0259b9c9113da6730bfa01cfa
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib')
-rw-r--r--openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml7
-rw-r--r--openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java10
-rw-r--r--openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java107
3 files changed, 122 insertions, 2 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml
index 48dd4d7ef6..fceb6b5bb4 100644
--- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml
+++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml
@@ -77,6 +77,13 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-inline</artifactId>
+ <version>${mockitoJupiter.version}</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
<build>
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java
index b7f1ff2cb9..6864aafffc 100644
--- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java
+++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java
@@ -360,7 +360,10 @@ public class HeatToToscaUtil {
if (FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) {
FunctionTranslator functionTranslator = new FunctionTranslator(
getFunctionTranslateTo(null, null, heatFileName, heatOrchestrationTemplate, context), null, entry.getValue(), null);
- translatedId = FunctionTranslationFactory.getInstance(entry.getKey()).get().translateFunction(functionTranslator);
+ Optional<FunctionTranslation> optionalFunctionTranslation = FunctionTranslationFactory.getInstance(entry.getKey());
+ if (optionalFunctionTranslation.isPresent()) {
+ translatedId = optionalFunctionTranslation.get().translateFunction(functionTranslator);
+ }
if (translatedId instanceof String && !new FunctionTranslator().isResourceSupported((String) translatedId)) {
translatedId = null;
}
@@ -615,7 +618,10 @@ public class HeatToToscaUtil {
* @return true if the resource represents a VFC and false otherwise.
*/
public static boolean isNestedVfcResource(Resource resource, TranslationContext context) {
- Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
+ Optional<String> nestedHeatFileName = getNestedHeatFileName(resource);
+ if (nestedHeatFileName.isEmpty()) {
+ return false;
+ }
HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil()
.yamlToObject(context.getFileContentAsStream(nestedHeatFileName.get()), HeatOrchestrationTemplate.class);
Map<String, Resource> resources = nestedHeatOrchestrationTemplate.getResources();
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java
new file mode 100644
index 0000000000..a7c6256190
--- /dev/null
+++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java
@@ -0,0 +1,107 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ *
+ *
+ */
+
+package org.openecomp.sdc.translator.services.heattotosca;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+
+import java.util.Map;
+import java.util.Optional;
+import org.apache.commons.collections4.map.HashedMap;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
+import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
+import org.openecomp.sdc.heat.datatypes.model.Resource;
+import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
+import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
+public class HeatToToscaUtilTest {
+ @Mock
+ private static HeatOrchestrationTemplate heatOrchestrationTemplate;
+
+ @Mock
+ private static TranslationContext translationContext;
+
+ @Test
+ public void testExtractAttachedResourceIdReturnsTranslatedId() {
+
+ Map<String, Object> propMap = new HashedMap<>();
+ propMap.put("get_resource", "test");
+ MockedStatic<ResourceTranslationBase> mockedStatic = Mockito.mockStatic(ResourceTranslationBase.class);
+ Mockito.when(ResourceTranslationBase.getResourceTranslatedId(
+ "heatFileName",
+ heatOrchestrationTemplate,
+ "test",
+ translationContext)).thenReturn(Optional.of("translatedResourceId"));
+
+ assertTrue(HeatToToscaUtil.extractAttachedResourceId("heatFileName",
+ heatOrchestrationTemplate,
+ translationContext,
+ propMap).isPresent());
+
+ assertEquals("translatedResourceId", HeatToToscaUtil.extractAttachedResourceId("heatFileName",
+ heatOrchestrationTemplate,
+ translationContext,
+ propMap).get().getTranslatedId());
+
+ mockedStatic.clearInvocations();
+ mockedStatic.close();
+ }
+
+ @Test
+ public void testExtractAttachedResourceIdReturnsNullTranslatedId() {
+
+ Map<String, Object> propMap = new HashedMap<>();
+ propMap.put("get_resource", "test");
+ MockedStatic<ResourceTranslationBase> mockedStaticResourceTranslationBase = Mockito.mockStatic(ResourceTranslationBase.class);
+ MockedStatic<FunctionTranslationFactory> mockedStaticFunctionTranslationFactory = Mockito.mockStatic(FunctionTranslationFactory.class);
+ Mockito.when(ResourceTranslationBase.getResourceTranslatedId(
+ "heatFileName",
+ heatOrchestrationTemplate,
+ "test",
+ translationContext)).thenReturn(Optional.of("translatedResourceId"));
+ Mockito.when(FunctionTranslationFactory.getInstance(anyString())).thenReturn(Optional.empty());
+
+ assertTrue(HeatToToscaUtil.extractAttachedResourceId("heatFileName",
+ heatOrchestrationTemplate,
+ translationContext,
+ propMap).isPresent());
+
+ assertEquals(null, HeatToToscaUtil.extractAttachedResourceId("heatFileName",
+ heatOrchestrationTemplate,
+ translationContext,
+ propMap).get().getTranslatedId());
+
+ mockedStaticResourceTranslationBase.close();
+ mockedStaticFunctionTranslationFactory.close();
+ }
+
+ @Test
+ void testisNestedVfcResourceReturnsNestedHeatFileName() {
+ final var resource = new Resource();
+ resource.setType(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.name());
+ assertFalse(HeatToToscaUtil.isNestedVfcResource(resource, translationContext));
+ }
+}