b7957f780d1f5508477a85a4cc6a849d91840b6e
[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.AllotedResource;
37 import org.onap.aai.babel.xml.generator.model.InstanceGroup;
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 {@link ArtifactGeneratorToscaParser}., to cover exceptional
46  * cases.
47  */
48
49 public class TestArtifactGeneratorToscaParser {
50
51     private static final String TEST_UUID = "1234";
52
53     /**
54      * Process an Allotted Resource that does not have a Providing Service.
55      */
56     @Test(expected = IllegalArgumentException.class)
57     public void testMissingProvidingService() {
58         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
59         new ArtifactGeneratorToscaParser(null).processResourceModels(new AllotedResource(), nodeTemplateList);
60     }
61
62     /**
63      *
64      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
65      */
66     @Test(expected = IllegalArgumentException.class)
67     public void testAddResourceNotProvidingService() {
68         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
69         final Resource dummyResource = new AllotedResource(); // Any Resource to which the CR can be added
70         new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
71     }
72
73     /**
74      * Process a dummy Group object for a Service Resource.
75      */
76     @Test
77     public void testInstanceGroups() {
78         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
79         Properties props = new Properties();
80         props.put("AAI.instance-group-types", instanceGroupType);
81         WidgetConfigurationUtil.setFilterConfig(props);
82
83         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
84         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
85
86         NodeTemplate serviceNodeTemplate =
87                 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
88         serviceNodeTemplate.setSubMappingToscaTemplate(sm);
89         Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
90
91         ArrayList<Group> groups = new ArrayList<>();
92         groups.add(buildGroup("group", instanceGroupType));
93         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
94
95         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
96         List<Resource> resources = parser.processInstanceGroups(new InstanceGroup(), serviceNodeTemplate);
97
98         assertThat(resources.size(), is(1));
99         Resource resource = resources.get(0);
100         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
101     }
102
103     /**
104      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
105      * sdc-tosca parser.
106      *
107      * @param name
108      *        name of the NodeTemplate
109      * @param type
110      *        type of the NodeTemplate
111      * @return a new NodeTemplate object
112      */
113     private NodeTemplate buildNodeTemplate(String name, String type) {
114         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
115         nodeTemplateMap.put(name, buildMap("type", type));
116         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
117         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
118     }
119
120     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
121         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
122         customDefs.put("attributes", null);
123         customDefs.put("requirements", null);
124         customDefs.put("capabilities", null);
125         customDefs.put("artifacts", null);
126         return customDefs;
127     }
128
129     private Group buildGroup(String name, String type) {
130         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
131         template.put("type", type);
132         template.put("metadata", new LinkedHashMap<>());
133         template.put("properties", buildMap("UUID", TEST_UUID));
134         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
135         customDefMap.put(type, buildGroupCustomDefs());
136         return new Group(name, template, null, customDefMap);
137     }
138
139     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
140         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
141         customDefs.put("members", null);
142         return customDefs;
143     }
144
145     private LinkedHashMap<String, Object> buildCustomDefs() {
146         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
147         customDefs.put("derived_from", null);
148         customDefs.put("metadata", null);
149         customDefs.put("version", null);
150         customDefs.put("description", null);
151         customDefs.put("interfaces", null);
152         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
153         return customDefs;
154     }
155
156     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
157         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
158         map.put(key, value);
159         return map;
160     }
161 }