Add support for ResourceInstanceGroup
[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.Model;
38 import org.onap.aai.babel.xml.generator.model.Resource;
39 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
40 import org.onap.sdc.toscaparser.api.Group;
41 import org.onap.sdc.toscaparser.api.NodeTemplate;
42 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
43
44 /**
45  * Direct tests of the TOSCA parser-based Artifact Generator, to cover exceptional cases.
46  */
47
48 public class TestArtifactGeneratorToscaParser {
49
50     private static final String TEST_UUID = "1234";
51
52     /**
53      * Process a dummy Node Template object for a Service. A WARNING should be logged for the missing metadata.
54      */
55     @Test
56     public void testMissingServiceData() {
57         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
58         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(null);
59         parser.processServiceTosca(null, Collections.emptyMap(), nodeTemplateList);
60         parser.processResourceToscas(nodeTemplateList, null);
61     }
62
63     /**
64      * Process a dummy Group object for a Service Resource.
65      */
66     @Test
67     public void testInstanceGroups() {
68         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
69         Properties props = new Properties();
70         props.put("AAI.instance-group-types", instanceGroupType);
71         WidgetConfigurationUtil.setFilterConfig(props);
72
73         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
74         NodeTemplate serviceNodeTemplate = Mockito.mock(NodeTemplate.class);
75         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
76
77         Mockito.when(serviceNodeTemplate.getSubMappingToscaTemplate()).thenReturn(sm);
78
79         NodeTemplate serviceNode = buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
80         Mockito.when(helper.getNodeTemplateByName(serviceNode.getName())).thenReturn(serviceNodeTemplate);
81
82         ArrayList<Group> groups = new ArrayList<>();
83         groups.add(buildGroup("group", instanceGroupType));
84         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNode)).thenReturn(groups);
85
86         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
87         Model resourceModel = new InstanceGroup();
88         List<Resource> resources = parser.processInstanceGroups(resourceModel, serviceNode);
89
90         assertThat(resources.size(), is(1));
91         Resource resource = resources.get(0);
92         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
93     }
94
95     private NodeTemplate buildNodeTemplate(String name, String type) {
96         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
97         nodeTemplateMap.put(name, buildMap("type", type));
98         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
99         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
100     }
101
102     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
103         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
104         customDefs.put("attributes", null);
105         customDefs.put("requirements", null);
106         customDefs.put("capabilities", null);
107         customDefs.put("artifacts", null);
108         return customDefs;
109     }
110
111     private Group buildGroup(String name, String type) {
112         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
113         template.put("type", type);
114         template.put("metadata", new LinkedHashMap<>());
115         template.put("properties", buildMap("UUID", TEST_UUID));
116         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
117         customDefMap.put(type, buildGroupCustomDefs());
118         return new Group(name, template, null, customDefMap);
119     }
120
121     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
122         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
123         customDefs.put("members", null);
124         return customDefs;
125     }
126
127     private LinkedHashMap<String, Object> buildCustomDefs() {
128         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
129         customDefs.put("derived_from", null);
130         customDefs.put("metadata", null);
131         customDefs.put("version", null);
132         customDefs.put("description", null);
133         customDefs.put("interfaces", null);
134         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
135         return customDefs;
136     }
137
138     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
139         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
140         map.put(key, value);
141         return map;
142     }
143 }