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