3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.core.Is.is;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
33 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
34 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
35 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
37 class ToscaTemplateYamlGeneratorTest {
40 void testGenerateYamlWithImportsKey() {
42 final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1");
43 final List<Map<String, Map<String, String>>> importList = ImmutableList
44 .of(ImmutableMap.of("etsi_nfv_sol001_nsd_2_7_1_types", ImmutableMap.of("file", "etsi_nfv_sol001_nsd_2_7_1_types.yaml")),
45 ImmutableMap.of("anotherImport", ImmutableMap.of("file", "anotherImport.yaml")));
46 toscaTemplate.setImports(importList);
47 final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate);
49 final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString();
51 final String expectedImports = "imports:\n" + "- file: etsi_nfv_sol001_nsd_2_7_1_types.yaml\n" + "- file: anotherImport.yaml";
52 assertThat("Imports format should be as expected", toscaTemplateYamlString.contains(expectedImports), is(true));
56 void testGenerateYamlWithToscaProperty() {
58 final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1");
59 final Map<String, ToscaProperty> toscaPropertyMap = new HashMap<>();
60 final ToscaProperty toscaProperty = new ToscaProperty();
61 final String defaultpValue = "defaultpValue";
62 toscaProperty.setDefaultp(defaultpValue);
63 ToscaPropertyConstraintValidValues toscaPropertyConstraintValidValues = new ToscaPropertyConstraintValidValues(
64 Collections.singletonList(defaultpValue));
65 toscaProperty.setConstraints(Collections.singletonList(toscaPropertyConstraintValidValues));
66 final String propertyName = "aProperty";
67 toscaPropertyMap.put(propertyName, toscaProperty);
68 final Map<String, ToscaNodeType> toscaNodeMap = new HashMap<>();
69 final ToscaNodeType toscaNodeType = new ToscaNodeType();
70 toscaNodeType.setProperties(toscaPropertyMap);
71 toscaNodeMap.put("aNode", toscaNodeType);
72 toscaTemplate.setNode_types(toscaNodeMap);
73 final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate);
75 final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString();
76 final String expectedProperty = String
77 .format("%s:\n" + " default: %s\n" + " constraints:\n" + " - valid_values:\n" + " - %s", propertyName,
78 defaultpValue, defaultpValue);
80 assertThat("Property format should be as expected", toscaTemplateYamlString.contains(expectedProperty), is(true));