Fix use of Optional in HeatToToscaUtil 68/126368/8
authorfranciscovila <javier.paradela.vila@est.tech>
Wed, 22 Dec 2021 12:41:19 +0000 (12:41 +0000)
committerMichael Morris <michael.morris@est.tech>
Wed, 5 Jan 2022 11:14:12 +0000 (11:14 +0000)
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

openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/pom.xml
openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java
openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java [new file with mode: 0644]

index 48dd4d7..fceb6b5 100644 (file)
       <scope>test</scope>
     </dependency>
 
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-inline</artifactId>
+      <version>${mockitoJupiter.version}</version>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>
index b7f1ff2..6864aaf 100644 (file)
@@ -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 (file)
index 0000000..a7c6256
--- /dev/null
@@ -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));
+    }
+}