diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib')
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)); + } +} |