summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java
blob: 055276b243644b2b84ef033e737f0cadc1379a2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.openecomp.sdc.be.components.impl.utils;

import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
import org.openecomp.sdc.be.components.validation.AnnotationValidator;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
import org.openecomp.sdc.common.util.ZipUtil;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class YamlTemplateParsingHandlerTest {

    private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
    private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
    private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
    private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
    private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
    private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
    private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
    private final static String CAPABILITY_NAME = "vlan_assignment";
    public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "csars" ;

    private YamlTemplateParsingHandler handler;
    private AnnotationBusinessLogic annotationBusinessLogic;
    @Mock
    private ComponentsUtils componentsUtils;
    @Mock
    private GroupTypeBusinessLogic groupTypeBusinessLogic;
    @Mock
    private AnnotationTypeOperations annotationTypeOperations;
    @Mock
    private AnnotationValidator annotationValidator;
    @Mock
    private TitanDao titanDao;

    @Before
    public void init(){
        annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
        handler = new YamlTemplateParsingHandler(titanDao, groupTypeBusinessLogic, annotationBusinessLogic);
    }

    @Test
    public void parseResourceInfoFromYAMLTest(){
        Path path = Paths.get(csarsFilePath + File.separator + "with_groups.csar");
        try {
            Map<String, byte[]> csar = ZipUtil.readZip(Files.readAllBytes(path));
            String fileName = "MainServiceTemplate.yaml";
            Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(fileName)).findAny();
            byte[] mainTemplateService = csar.get(keyOp.get());
            Properties props = new Properties();
            String resourceYml = new String(mainTemplateService);
            props.load(new StringReader(resourceYml.replace("\\","\\\\")));
            Resource resource = new Resource();
            
            stubGetGroupType();
            
            ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(fileName, resourceYml, new HashMap<>(), new HashMap<>(), "");
            
            validateParsedYaml(parsedYaml);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



	private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml) {
		assertThat(parsedYaml).isNotNull();
		assertThat(parsedYaml.getGroups()).isNotNull().containsKey("x_group");
		assertThat(parsedYaml.getGroups().get("x_group")).isNotNull();
		assertThat(parsedYaml.getGroups().get("x_group").getProperties()).isNotNull();
		assertThat(parsedYaml.getGroups().get("x_group").getProperties()
				.stream()
				.map(PropertyDataDefinition::getName)
				.collect(Collectors.toList()))
		.containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
		assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()
				.get(CAPABILITY_TYPE)
				.get(0).getProperties().get(0).getValue()).isEqualTo("success");
		assertThat(parsedYaml.getGroups().get("x_group").getProperties()
				.stream()
				.map(PropertyDataDefinition::getName)
				.collect(Collectors.toList()))
		.containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
		assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()).isNotNull();
		assertThat(parsedYaml.getGroups().get("x_group").getMembers()).isNotNull();
	}

	private void stubGetGroupType() {
		when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
		when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
        when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
//        when(annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(any(Map.class))).thenReturn(null);
    }

    private static GroupTypeDefinition buildRootGroupType() {
        GroupTypeDefinition groupType = new GroupTypeDefinition();
        groupType.setType(ROOT_GROUP_TYPE);
        groupType.setDescription("The TOSCA Group Type all other TOSCA Group Types derive from");
        return groupType;
	}

	private static GroupTypeDefinition buildHeatStackGroupType() {
        GroupTypeDefinition groupType = new GroupTypeDefinition();
        groupType.setType(HEAT_GROUP_TYPE);
        groupType.setDerivedFrom("tosca.groups.Root");
        groupType.setDescription("Grouped all heat resources which are in the same heat stack");
        	      
        GroupProperty property1 = new GroupProperty();
        property1.setName("heat_file");
        property1.setType("string");
        property1.setRequired(true);
        property1.setDescription("Heat file which associate to this group/heat stack");
        property1.setStatus("SUPPORTED");

        GroupProperty property2 = new GroupProperty();
        property2.setName("description");
        property2.setType("string");
        property2.setRequired(true);
        property2.setDescription("group description");
        property2.setStatus("SUPPORTED");
        groupType.setProperties(Lists.newArrayList(property1, property2));
        return groupType;
	}

	private static GroupTypeDefinition buildVfcInstanceGroupType() {
        GroupTypeDefinition groupType = new GroupTypeDefinition();
        groupType.setType(VFC_GROUP_TYPE);
        groupType.setDerivedFrom("tosca.groups.Root");
        groupType.setDescription("groups VFCs with same parent port role");
        GroupProperty property1 = new GroupProperty();
        property1.setName("vfc_instance_group_function");
        property1.setType("string");
        property1.setRequired(true);
        property1.setDescription("function of this VFC group");

        GroupProperty property2 = new GroupProperty();
        property2.setName("vfc_parent_port_role");
        property2.setType("string");
        property2.setRequired(true);
        property2.setDescription("common role of parent ports of VFCs in this group");

        GroupProperty property3 = new GroupProperty();
        property3.setName("network_collection_function");
        property3.setType("string");
        property3.setRequired(true);
        property3.setDescription("network collection function assigned to this group");

        GroupProperty property4 = new GroupProperty();
        property4.setName("subinterface_role");
        property4.setType("string");
        property4.setRequired(true);
        property4.setDescription("common role of subinterfaces of VFCs in this group, criteria the group is created");

        groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));

        CapabilityDefinition capability = new CapabilityDefinition();
        capability.setType(CAPABILITY_TYPE);
        capability.setName(CAPABILITY_NAME);
        ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
        capabilityProperty.setName("vfc_instance_group_reference");
        capabilityProperty.setType("string");
        capability.setProperties(Arrays.asList(capabilityProperty));

        Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
        capabilityMap.put(CAPABILITY_NAME, capability);
        groupType.setCapabilities(capabilityMap);
        return groupType;
    }

}