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 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;
44 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
48 public class TestArtifactGeneratorToscaParser {
50 private static final String TEST_UUID = "1234";
53 * Process an Allotted Resource that does not have a Providing Service.
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);
63 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
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);
73 * Process a dummy Group object for a Service Resource.
76 public void testInstanceGroups() {
77 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
78 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
80 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
81 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
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);
88 ArrayList<Group> groups = new ArrayList<>();
89 groups.add(buildGroup("group", instanceGroupType));
90 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
92 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
93 List<Resource> resources = parser.processInstanceGroups(new InstanceGroup(), serviceNodeTemplate);
95 assertThat(resources.size(), is(1));
96 Resource resource = resources.get(0);
97 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
101 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
105 * name of the NodeTemplate
107 * type of the NodeTemplate
108 * @return a new NodeTemplate object
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);
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);
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);
136 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
137 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
138 customDefs.put("members", null);
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")));
153 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
154 LinkedHashMap<String, Object> map = new LinkedHashMap<>();