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