6cf6d3157bdf6b8366949ccadcbbd350b9a43837
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.parser;
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Properties;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
36 import org.onap.aai.babel.xml.generator.model.InstanceGroup;
37 import org.onap.aai.babel.xml.generator.model.Resource;
38 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
39 import org.onap.sdc.toscaparser.api.Group;
40 import org.onap.sdc.toscaparser.api.NodeTemplate;
41 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
42
43 /**
44  * Direct tests of the TOSCA parser-based Artifact Generator, to cover exceptional cases.
45  */
46
47 public class TestArtifactGeneratorToscaParser {
48
49         private static final String TEST_UUID = "1234";
50
51         /**
52          * Process a dummy Node Template object for a Service. A WARNING should be logged for the missing metadata.
53          */
54         @Test
55         public void testMissingServiceData() {
56                 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
57                 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(null);
58                 parser.processServiceTosca(null, Collections.emptyMap(), nodeTemplateList);
59                 parser.processResourceToscas(nodeTemplateList, null);
60         }
61
62         /**
63          * Process a dummy Group object for a Service Resource.
64          */
65         @Test
66         public void testInstanceGroups() {
67                 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
68                 Properties props = new Properties();
69                 props.put("AAI.instance-group-types", instanceGroupType);
70                 WidgetConfigurationUtil.setFilterConfig(props);
71
72                 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
73                 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
74
75                 NodeTemplate serviceNode = buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
76                 serviceNode.setSubMappingToscaTemplate(sm);
77                 Mockito.when(helper.getNodeTemplateByName(serviceNode.getName())).thenReturn(serviceNode);
78
79                 ArrayList<Group> groups = new ArrayList<>();
80                 groups.add(buildGroup("group", instanceGroupType));
81                 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNode)).thenReturn(groups);
82
83                 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
84                 List<Resource> resources = parser.processInstanceGroups(new InstanceGroup(), serviceNode);
85
86                 assertThat(resources.size(), is(1));
87                 Resource resource = resources.get(0);
88                 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
89         }
90
91         private NodeTemplate buildNodeTemplate(String name, String type) {
92                 LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
93                 nodeTemplateMap.put(name, buildMap("type", type));
94                 nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
95                 return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
96         }
97
98         private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
99                 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
100                 customDefs.put("attributes", null);
101                 customDefs.put("requirements", null);
102                 customDefs.put("capabilities", null);
103                 customDefs.put("artifacts", null);
104                 return customDefs;
105         }
106
107         private Group buildGroup(String name, String type) {
108                 LinkedHashMap<String, Object> template = new LinkedHashMap<>();
109                 template.put("type", type);
110                 template.put("metadata", new LinkedHashMap<>());
111                 template.put("properties", buildMap("UUID", TEST_UUID));
112                 LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
113                 customDefMap.put(type, buildGroupCustomDefs());
114                 return new Group(name, template, null, customDefMap);
115         }
116
117         private LinkedHashMap<String, Object> buildGroupCustomDefs() {
118                 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
119                 customDefs.put("members", null);
120                 return customDefs;
121         }
122
123         private LinkedHashMap<String, Object> buildCustomDefs() {
124                 LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
125                 customDefs.put("derived_from", null);
126                 customDefs.put("metadata", null);
127                 customDefs.put("version", null);
128                 customDefs.put("description", null);
129                 customDefs.put("interfaces", null);
130                 customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
131                 return customDefs;
132         }
133
134         private LinkedHashMap<String, Object> buildMap(String key, Object value) {
135                 LinkedHashMap<String, Object> map = new LinkedHashMap<>();
136                 map.put(key, value);
137                 return map;
138         }
139 }