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