aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java
blob: 9988076b61bf633193861ae6202191c69f56a1cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.parser;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.aai.babel.util.ArtifactTestUtils;
import org.onap.aai.babel.util.Resources;
import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
import org.onap.aai.babel.xml.generator.data.WidgetMapping;
import org.onap.aai.babel.xml.generator.model.Model;
import org.onap.aai.babel.xml.generator.model.Resource;
import org.onap.aai.babel.xml.generator.model.Service;
import org.onap.aai.babel.xml.generator.model.WidgetType;
import org.onap.aai.babel.xml.generator.types.ModelType;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.toscaparser.api.Group;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.SubstitutionMappings;

/**
 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
 * cases.
 */

public class TestArtifactGeneratorToscaParser {

    private static final String TEST_UUID = "1234";

    /**
     * Initialize the Generator with an invalid mappings file path.
     * 
     * @throws IOException
     *             if the file content could not be read successfully
     */
    @Test
    public void testMissingMappingsFile() throws IOException {
        assertThrows(IllegalArgumentException.class, () -> {
            ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
        });
    }

    /**
     * Initialize the Generator with no Widget Mappings content.
     * 
     * @throws IOException
     *             if the file content could not be read successfully
     */
    @Test
    public void testMissingMappingsContent() throws IOException {
        assertThrows(IOException.class, () -> {
            String emptyJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
            ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(emptyJson);
        });
    }

    /**
     * Initialize the Generator with invalid Widget Mappings content.
     * 
     * @throws IOException
     *             if the file content could not be read successfully
     */
    @Test
    public void testInvalidMappingsContent() throws IOException {
        assertThrows(IOException.class, () -> {
            String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
            ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
        });
    }

    /**
     * Process an Allotted Resource that does not have a Providing Service.
     *
     * @throws XmlArtifactGenerationException
     *             because the ALLOTTED_RESOURCE lacks a Providing Service
     */
    @Test
    public void testMissingProvidingService() throws XmlArtifactGenerationException {
        assertThrows(XmlArtifactGenerationException.class, () -> {
            List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
            new ArtifactGeneratorToscaParser(null)
                    .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
        });
    }

    /**
     * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
     *
     * @throws XmlArtifactGenerationException
     *             because the ALLOTTED_RESOURCE lacks a Providing Service
     * @throws IOException
     *             if the test mappings cannot be loaded
     */
    @Test
    public void testAddResourceNotProvidingService() throws XmlArtifactGenerationException, IOException {
        assertThrows(XmlArtifactGenerationException.class, () -> {
            new ArtifactTestUtils().loadWidgetMappings();
            List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));

            // Create any Resource to which the CR can be added
            final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
            new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
        });
    }

    /**
     * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
     * 
     * @throws IOException
     *             if a WidgetMapping is invalid
     */
    @Test
    public void testToscaMappingWithoutType() throws IOException {
        assertThrows(IOException.class, () -> {
            WidgetMapping invalidMapping = new WidgetMapping();
            invalidMapping.setType(null);
            WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
        });
    }

    /**
     * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
     * 
     * @throws IOException
     *             if a WidgetMapping is invalid
     */
    @Test
    public void testToscaMappingWithInvalidType() throws IOException {
        assertThrows(IOException.class, () -> {
            WidgetMapping invalidMapping = new WidgetMapping();
            invalidMapping.setType("invalid");
            WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
        });
    }

    /**
     * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
     * 
     * @throws IOException
     *             if a WidgetMapping is invalid
     */
    @Test
    public void testToscaMappingWithoutWidget() throws IOException {
        assertThrows(IOException.class, () -> {
            WidgetMapping invalidMapping = new WidgetMapping();
            invalidMapping.setWidget(null);
            WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
        });
    }

    /**
     * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files
     * which require this functionality, but the code path exists to support it.
     *
     * @throws IOException
     *             if the widget mappings are not loaded
     * @throws XmlArtifactGenerationException
     *             if there is no configuration defined for the test resource's widget type
     */
    @Test
    public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
        assertDoesNotThrow(() -> {
            ArtifactTestUtils testUtils = new ArtifactTestUtils();
            testUtils.loadWidgetMappings();

            Model serviceModel = new Service();
            Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
            resourceModel.setModelType(ModelType.WIDGET);

            ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
            ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
            parser.addRelatedModel(serviceModel, resourceModel);
        });
    }

    /**
     * Process a dummy Group object for a Service Resource.
     * 
     * @throws XmlArtifactGenerationException
     *             if there is no configuration defined for a member Widget of an instance group
     * @throws IOException
     *             if the widget mappings cannot be loaded
     */
    @Test
    public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
        new ArtifactTestUtils().loadWidgetMappings();

        final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
        WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));

        SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);

        List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType));
        Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups));

        NodeTemplate serviceNodeTemplate =
                buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
        serviceNodeTemplate.setSubMappingToscaTemplate(sm);

        Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
        List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class))
                .processInstanceGroups(groupResource, serviceNodeTemplate);

        assertThat(resources.size(), is(1));
        Resource resource = resources.get(0);
        assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
    }

    /**
     * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
     * sdc-tosca parser.
     *
     * @param name
     *            name of the NodeTemplate
     * @param type
     *            type of the NodeTemplate
     * @return a new NodeTemplate object
     */
    private NodeTemplate buildNodeTemplate(String name, String type) {
        LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
        nodeTemplateMap.put(name, buildMap("type", type));
        nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
        return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
    }

    private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
        LinkedHashMap<String, Object> customDefs = buildCustomDefs();
        customDefs.put("attributes", null);
        customDefs.put("requirements", null);
        customDefs.put("capabilities", null);
        customDefs.put("artifacts", null);
        return customDefs;
    }

    private Group buildGroup(String name, String type) {
        LinkedHashMap<String, Object> template = new LinkedHashMap<>();
        template.put("type", type);
        template.put("metadata", new LinkedHashMap<>());
        template.put("properties", buildMap("UUID", TEST_UUID));
        LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
        customDefMap.put(type, buildGroupCustomDefs());
        return new Group(name, template, null, customDefMap);
    }

    private LinkedHashMap<String, Object> buildGroupCustomDefs() {
        LinkedHashMap<String, Object> customDefs = buildCustomDefs();
        customDefs.put("members", null);
        return customDefs;
    }

    private LinkedHashMap<String, Object> buildCustomDefs() {
        LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
        customDefs.put("derived_from", null);
        customDefs.put("metadata", null);
        customDefs.put("version", null);
        customDefs.put("description", null);
        customDefs.put("interfaces", null);
        customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
        return customDefs;
    }

    private LinkedHashMap<String, Object> buildMap(String key, Object value) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put(key, value);
        return map;
    }
}