Plugin to Generate Service ETSI NSD CSAR
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / test / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / tosca / yaml / ToscaTemplateYamlGeneratorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml;
21
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.core.Is.is;
24
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;
30 import java.util.Map;
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;
36
37 class ToscaTemplateYamlGeneratorTest {
38
39     @Test
40     void testGenerateYamlWithImportsKey() {
41         //given
42         final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1");
43         final List<Map<String, Map<String, String>>> importList =
44             ImmutableList.of(
45                 ImmutableMap.of("etsi_nfv_sol001_nsd_2_7_1_types",
46                     ImmutableMap.of("file", "etsi_nfv_sol001_nsd_2_7_1_types.yaml")
47                 ),
48                 ImmutableMap.of("anotherImport",
49                     ImmutableMap.of("file", "anotherImport.yaml")
50                 )
51             );
52         toscaTemplate.setImports(importList);
53         final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate);
54         //when
55         final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString();
56
57         //then
58         final String expectedImports = "imports:\n"
59             + "- file: etsi_nfv_sol001_nsd_2_7_1_types.yaml\n"
60             + "- file: anotherImport.yaml";
61         assertThat("Imports format should be as expected", toscaTemplateYamlString.contains(expectedImports), is(true));
62     }
63
64     @Test
65     void testGenerateYamlWithToscaProperty() {
66         //given
67         final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1");
68
69         final Map<String, ToscaProperty> toscaPropertyMap = new HashMap<>();
70         final ToscaProperty toscaProperty = new ToscaProperty();
71         final String defaultpValue = "defaultpValue";
72         toscaProperty.setDefaultp(defaultpValue);
73         ToscaPropertyConstraintValidValues toscaPropertyConstraintValidValues =
74             new ToscaPropertyConstraintValidValues(Collections.singletonList(defaultpValue));
75         toscaProperty.setConstraints(Collections.singletonList(toscaPropertyConstraintValidValues));
76         final String propertyName = "aProperty";
77         toscaPropertyMap.put(propertyName, toscaProperty);
78
79         final Map<String, ToscaNodeType> toscaNodeMap = new HashMap<>();
80         final ToscaNodeType toscaNodeType = new ToscaNodeType();
81         toscaNodeType.setProperties(toscaPropertyMap);
82         toscaNodeMap.put("aNode", toscaNodeType);
83         toscaTemplate.setNode_types(toscaNodeMap);
84         final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate);
85         //when
86         final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString();
87
88         final String expectedProperty = String.format("%s:\n"
89                 + "        default: %s\n"
90                 + "        constraints:\n"
91                 + "        - valid_values:\n"
92                 + "          - %s",
93             propertyName, defaultpValue, defaultpValue);
94         //then
95         assertThat("Property format should be as expected",
96             toscaTemplateYamlString.contains(expectedProperty), is(true));
97     }
98 }