2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.parser;
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
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;
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;
46 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
50 public class TestArtifactGeneratorToscaParser {
52 private static final String TEST_UUID = "1234";
55 * Process an Allotted Resource that does not have a Providing Service.
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);
65 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
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);
75 * Process a dummy Group object for a Service Resource.
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);
84 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
85 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
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);
92 ArrayList<Group> groups = new ArrayList<>();
93 groups.add(buildGroup("group", instanceGroupType));
94 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
96 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
97 List<Resource> resources = parser.processInstanceGroups(new InstanceGroup(), serviceNodeTemplate);
99 assertThat(resources.size(), is(1));
100 Resource resource = resources.get(0);
101 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
105 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
109 * name of the NodeTemplate
111 * type of the NodeTemplate
112 * @return a new NodeTemplate object
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);
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);
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);
140 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
141 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
142 customDefs.put("members", null);
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")));
157 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
158 LinkedHashMap<String, Object> map = new LinkedHashMap<>();