From a811119a9c380bf01f482f08828a9a4b7a8dff6e Mon Sep 17 00:00:00 2001 From: franciscovila Date: Wed, 22 Dec 2021 12:41:19 +0000 Subject: 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 Change-Id: I9fbfbb6f9ebff1f0259b9c9113da6730bfa01cfa --- .../openecomp-sdc-translator-core/pom.xml | 7 ++ .../services/heattotosca/HeatToToscaUtil.java | 10 +- .../services/heattotosca/HeatToToscaUtilTest.java | 107 +++++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtilTest.java (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib') 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 @@ test + + org.mockito + mockito-inline + ${mockitoJupiter.version} + test + + 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 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 nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource); + Optional nestedHeatFileName = getNestedHeatFileName(resource); + if (nestedHeatFileName.isEmpty()) { + return false; + } HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil() .yamlToObject(context.getFileContentAsStream(nestedHeatFileName.get()), HeatOrchestrationTemplate.class); Map 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 propMap = new HashedMap<>(); + propMap.put("get_resource", "test"); + MockedStatic 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 propMap = new HashedMap<>(); + propMap.put("get_resource", "test"); + MockedStatic mockedStaticResourceTranslationBase = Mockito.mockStatic(ResourceTranslationBase.class); + MockedStatic 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)); + } +} -- cgit 1.2.3-korg