BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / test / OnapBlueprintServiceTest.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
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  */
23
24 package org.onap.blueprintgenerator.test;
25
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mockito;
28 import org.onap.blueprintgenerator.constants.Constants;
29 import org.onap.blueprintgenerator.model.common.Input;
30 import org.onap.blueprintgenerator.model.common.Node;
31 import org.onap.blueprintgenerator.model.common.OnapBlueprint;
32 import org.onap.blueprintgenerator.model.common.Properties;
33 import org.junit.Test;
34 import org.junit.jupiter.api.DisplayName;
35 import picocli.CommandLine;
36
37 import java.io.IOException;
38 import java.io.PrintStream;
39 import java.nio.file.Paths;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertNotNull;
46 import static org.junit.Assert.assertTrue;
47 import static org.junit.Assert.assertArrayEquals;
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.Mockito.times;
50 import static org.mockito.Mockito.verify;
51
52 /**
53  * @author : Ravi Mantena
54  * @date 10/16/2020 Application: ONAP - Blueprint Generator ONAP Blueprint Test Cases for ONAP and
55  * DMAAP
56  */
57 public class OnapBlueprintServiceTest extends BlueprintGeneratorTests {
58
59     private String outputfilesPath =
60         Paths.get("src", "test", "resources", "outputfiles").toAbsolutePath().toString();
61
62     /**
63      * Test Case for Service Name Override Component Spec Input File
64      *
65      */
66     @DisplayName("Testing K8s Blueprint for Service Name Override Component Spec Input File")
67     @Test
68     public void testServiceNameOverrideK8sBlueprint() throws IOException {
69         Input input =
70             onapTestUtils.getInput(
71                 Paths.get("src", "test", "resources", "componentspecs", ves).toFile()
72                     .getAbsolutePath(),
73                 outputfilesPath,
74                 "testServiceNameOverrideK8sBlueprint",
75                 "",
76                 "o",
77                 "");
78         onapComponentSpec =
79             onapComponentSpecService.createComponentSpecFromFile(input.getComponentSpecPath());
80         assertNotNull(
81             "K8s Blueprint for Service Name Override Component Spec is NULL", onapComponentSpec);
82
83         OnapBlueprint onapBlueprint = onapBlueprintService
84             .createBlueprint(onapComponentSpec, input);
85         onapBlueprintService.blueprintToYaml(onapComponentSpec, onapBlueprint, input);
86         System.out.println(
87             onapBlueprintService.blueprintToString(onapComponentSpec, onapBlueprint, input));
88
89         onapTestUtils.verifyToscaDefVersion(
90             "Service Name Override K8s", onapBlueprint, Constants.TOSCA_DEF_VERSION);
91         onapTestUtils.verifyBpImports("Service Name Override K8s", onapBlueprint, false);
92
93         Map<String, LinkedHashMap<String, Object>> k8sBpInputs = onapBlueprint.getInputs();
94         assertNotNull("Service Name Override K8s Blueprint Inputs Section is NULL", k8sBpInputs);
95         assertTrue(
96             "Service Name Override K8s Blueprint Inputs Section is Empty", k8sBpInputs.size() > 0);
97
98         assertEquals(
99             "Service Name Override K8s Blueprint:Inputs "
100                 + Constants.ONAP_INPUT_CPU_LIMIT
101                 + " Default is not matching",
102             k8sBpInputs.get(Constants.ONAP_INPUT_CPU_LIMIT).get("default"),
103             Constants.ONAP_DEFAULT250m);
104         assertEquals(
105             "Service Name Override K8s Blueprint:Inputs "
106                 + Constants.SERVICE_COMPONENT_NAME_OVERRIDE
107                 + " Default is not matching",
108             k8sBpInputs.get(Constants.SERVICE_COMPONENT_NAME_OVERRIDE).get("default"),
109             Constants.ONAP_SERVICE_COMPONENTNAME_OVERRIDE_DEFAULT);
110
111         Map<String, Node> k8sBpNodeTemplates = onapBlueprint.getNode_templates();
112         assertNotNull(
113             "Service Name Override K8s Blueprint Node Templates Section is NULL in the K8s DMAAP Blueprint",
114             k8sBpNodeTemplates);
115         assertTrue(
116             "Service Name Override K8s Blueprint Node Templates Section Size is Empty",
117             k8sBpNodeTemplates.size() > 0);
118
119         assertEquals(
120             "Service Name Override K8s Blueprint:NodeTemplates:Type is not Matching",
121             k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES).getType(),
122             Constants.ONAP_NODETEMPLATES_TYPE);
123
124         Properties k8sBpNodeTemplateProperties =
125             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getProperties();
126         assertNotNull(
127             "Service Name Override K8s Blueprint Node Templates:Properties Section is NULL",
128             k8sBpNodeTemplateProperties);
129
130         Map<String, LinkedHashMap<String, Object>> bpInputs = onapBlueprint.getInputs();
131         onapTestUtils
132             .verifyArtifacts("Service Name Override K8s", onapComponentSpec, bpInputs, "o");
133
134         onapTestUtils.verifyStreamsPublishes(
135             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
136         onapTestUtils.verifyStreamsSubscribes(
137             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
138         onapTestUtils.verifyAuxilary("Service Name Override K8s", onapComponentSpec);
139         onapTestUtils.verifyHealthCheck(
140             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
141         onapTestUtils.verifyServicesCalls("Service Name Override K8s", onapComponentSpec);
142         onapTestUtils.verifyServicesProvides("Service Name Override K8s", onapComponentSpec);
143         onapTestUtils.verifyDockerConfig(
144             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
145         onapTestUtils.verifyParameters(
146             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplates);
147         onapTestUtils.verifyVolumes(
148             "Service Name Override K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
149     }
150
151     /**
152      * Test Case for Service Name Override Component Spec Input File with Import File
153      *
154      */
155     @DisplayName(
156         "Testing K8s Blueprint for Service Name Override Component Spec Input File with Import File")
157     @Test
158     public void testServiceNameOverrideImportFileK8sBlueprint() throws IOException {
159         Input input =
160             onapTestUtils.getInput(
161                 Paths.get("src", "test", "resources", "componentspecs", ves).toFile()
162                     .getAbsolutePath(),
163                 "",
164                 "",
165                 Paths.get("src", "test", "resources", "componentspecs", testImports)
166                     .toFile()
167                     .getAbsolutePath(),
168                 "o",
169                 "");
170         onapComponentSpec =
171             onapComponentSpecService.createComponentSpecFromFile(input.getComponentSpecPath());
172         assertNotNull(
173             "K8s Blueprint for Service Name Override with Import File Component Spec is NULL",
174             onapComponentSpec);
175
176         OnapBlueprint onapBlueprint = onapBlueprintService
177             .createBlueprint(onapComponentSpec, input);
178
179         onapTestUtils.verifyToscaDefVersion(
180             "Service Name Override with Import File K8s", onapBlueprint,
181             Constants.TOSCA_DEF_VERSION);
182         onapTestUtils.verifyBpImportsFromFile(
183             "Service Name Override with Import File K8s", onapBlueprint, input.getImportPath());
184
185         Map<String, LinkedHashMap<String, Object>> k8sBpInputs = onapBlueprint.getInputs();
186         assertNotNull(
187             "Service Name Override with Import File K8s Blueprint Inputs Section is NULL",
188             k8sBpInputs);
189         assertTrue(
190             "Service Name Override with Import File K8s Blueprint Inputs Section is Empty",
191             k8sBpInputs.size() > 0);
192
193         assertEquals(
194             "Service Name Override with Import File K8s Blueprint:Inputs "
195                 + Constants.ONAP_INPUT_CPU_LIMIT
196                 + " Default is not matching",
197             k8sBpInputs.get(Constants.ONAP_INPUT_CPU_LIMIT).get("default"),
198             Constants.ONAP_DEFAULT250m);
199         assertEquals(
200             "Service Name Override with Import File K8s Blueprint:Inputs "
201                 + Constants.SERVICE_COMPONENT_NAME_OVERRIDE
202                 + " Default is not matching",
203             k8sBpInputs.get(Constants.SERVICE_COMPONENT_NAME_OVERRIDE).get("default"),
204             Constants.ONAP_SERVICE_COMPONENTNAME_OVERRIDE_DEFAULT);
205
206         Map<String, Node> k8sBpNodeTemplates = onapBlueprint.getNode_templates();
207         assertNotNull(
208             "Service Name Override with Import File K8s Blueprint Node Templates Section is NULL in the K8s DMAAP Blueprint",
209             k8sBpNodeTemplates);
210         assertTrue(
211             "Service Name Override with Import File K8s Blueprint Node Templates Section Size is Empty",
212             k8sBpNodeTemplates.size() > 0);
213
214         assertEquals(
215             "Service Name Override with Import File K8s Blueprint:NodeTemplates:Type is not Matching",
216             k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES).getType(),
217             Constants.ONAP_NODETEMPLATES_TYPE);
218
219         Properties k8sBpNodeTemplateProperties =
220             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getProperties();
221         assertNotNull(
222             "Service Name Override with Import File K8s Blueprint Node Templates:Properties Section is NULL",
223             k8sBpNodeTemplateProperties);
224
225         Map<String, LinkedHashMap<String, Object>> bpInputs = onapBlueprint.getInputs();
226         onapTestUtils.verifyArtifacts(
227             "Service Name Override with Import File K8s", onapComponentSpec, bpInputs, "o");
228
229         onapTestUtils.verifyStreamsPublishes(
230             "Service Name Override with Import File K8s",
231             onapComponentSpec,
232             k8sBpNodeTemplateProperties);
233         onapTestUtils.verifyStreamsSubscribes(
234             "Service Name Override with Import File K8s",
235             onapComponentSpec,
236             k8sBpNodeTemplateProperties);
237         onapTestUtils
238             .verifyAuxilary("Service Name Override with Import File K8s", onapComponentSpec);
239         onapTestUtils.verifyHealthCheck(
240             "Service Name Override with Import File K8s",
241             onapComponentSpec,
242             k8sBpNodeTemplateProperties);
243         onapTestUtils.verifyServicesCalls(
244             "Service Name Override with Import File K8s", onapComponentSpec);
245         onapTestUtils.verifyServicesProvides(
246             "Service Name Override with Import File K8s", onapComponentSpec);
247         onapTestUtils.verifyDockerConfig(
248             "Service Name Override with Import File K8s",
249             onapComponentSpec,
250             k8sBpNodeTemplateProperties);
251         onapTestUtils.verifyParameters(
252             "Service Name Override with Import File K8s", onapComponentSpec, k8sBpNodeTemplates);
253         onapTestUtils.verifyVolumes(
254             "Service Name Override with Import File K8s",
255             onapComponentSpec,
256             k8sBpNodeTemplateProperties);
257     }
258
259     /**
260      * Test Case for K8s Blueprint with DB
261      *
262      */
263     @DisplayName("Testing K8s Blueprint with DB")
264     @Test
265     public void testDMAAPK8sBlueprintWithDB() {
266         Input input =
267             onapTestUtils.getInput(
268                 Paths.get("src", "test", "resources", "componentspecs", ves).toFile()
269                     .getAbsolutePath(),
270                 outputfilesPath,
271                 "testDMAAPK8sBlueprintWithDB",
272                 "",
273                 "d",
274                 "");
275         onapComponentSpec =
276             onapComponentSpecService.createComponentSpecFromFile(input.getComponentSpecPath());
277         assertNotNull("K8s Blueprint for DMAAP Component Spec is NULL", onapComponentSpec);
278
279         OnapBlueprint onapBlueprint = dmaapBlueprintService
280             .createBlueprint(onapComponentSpec, input);
281         onapBlueprintService.blueprintToYaml(onapComponentSpec, onapBlueprint, input);
282     }
283
284     /**
285      * Test Case for K8s Blueprint for DMAAP Component Spec Input File
286      *
287      */
288     @DisplayName("Testing K8s Blueprint for DMAAP Component Spec Input File")
289     @Test
290     public void testDMAAPK8sBlueprint() {
291         Input input =
292             onapTestUtils.getInput(
293                 Paths.get("src", "test", "resources", "componentspecs", ves).toFile()
294                     .getAbsolutePath(),
295                 outputfilesPath,
296                 "testDMAAPK8sBlueprint",
297                 "",
298                 "d",
299                 "");
300         onapComponentSpec =
301             onapComponentSpecService.createComponentSpecFromFile(input.getComponentSpecPath());
302         assertNotNull("K8s Blueprint for DMAAP Component Spec is NULL", onapComponentSpec);
303
304         OnapBlueprint onapBlueprint = dmaapBlueprintService
305             .createBlueprint(onapComponentSpec, input);
306         onapBlueprintService.blueprintToYaml(onapComponentSpec, onapBlueprint, input);
307         System.out.println(
308             onapBlueprintService.blueprintToString(onapComponentSpec, onapBlueprint, input));
309
310         policyModelService.createPolicyModels(onapComponentSpec.getParameters(), "models");
311
312         onapTestUtils
313             .verifyToscaDefVersion("DMAAP K8s", onapBlueprint, Constants.TOSCA_DEF_VERSION);
314
315         Map<String, LinkedHashMap<String, Object>> k8sBpInputs = onapBlueprint.getInputs();
316         assertNotNull("DMAAP K8s Blueprint Inputs Section is NULL", k8sBpInputs);
317         assertTrue("DMAAP K8s Blueprint Inputs Section is Empty", k8sBpInputs.size() > 0);
318
319         assertEquals(
320             "DMAAP K8s Blueprint:Inputs " + Constants.ONAP_INPUT_CPU_LIMIT
321                 + " Default is not matching",
322             k8sBpInputs.get(Constants.ONAP_INPUT_CPU_LIMIT).get("default"),
323             Constants.ONAP_DEFAULT250m);
324         assertEquals(
325             "DMAAP K8s Blueprint:Inputs "
326                 + Constants.SERVICE_COMPONENT_NAME_OVERRIDE
327                 + " Default is not matching",
328             k8sBpInputs.get(Constants.SERVICE_COMPONENT_NAME_OVERRIDE).get("default"),
329             Constants.ONAP_SERVICE_COMPONENTNAME_OVERRIDE_DEFAULT);
330
331         Map<String, Node> k8sBpNodeTemplates = onapBlueprint.getNode_templates();
332         assertNotNull(
333             "DMAAP K8s Blueprint Node Templates Section is NULL in the K8s DMAAP Blueprint",
334             k8sBpNodeTemplates);
335         assertTrue(
336             "DMAAP K8s Blueprint Node Templates Section Size is Empty",
337             k8sBpNodeTemplates.size() > 0);
338
339         assertEquals(
340             "DMAAP K8s Blueprint:NodeTemplates:Type is not Matching",
341             k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES).getType(),
342             Constants.DMAAP_NODETEMPLATES_TYPE);
343
344         Properties k8sBpNodeTemplateProperties =
345             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getProperties();
346         assertNotNull(
347             "DMAAP K8s Blueprint Node Templates:Properties Section is NULL",
348             k8sBpNodeTemplateProperties);
349
350         List bpNodeTemplateRelationships =
351             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getRelationships();
352         assertNotNull(
353             "DMAAP K8s Blueprint Node Templates:Relationships Section is NULL",
354             bpNodeTemplateRelationships);
355
356         onapTestUtils.verifyStreamsPublishes(
357             "DMAAP K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
358         onapTestUtils.verifyStreamsSubscribes(
359             "DMAAP K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
360         onapTestUtils.verifyAuxilary("DMAAP K8s", onapComponentSpec);
361         onapTestUtils
362             .verifyHealthCheck("DMAAP K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
363         onapTestUtils.verifyServicesCalls("DMAAP K8s", onapComponentSpec);
364         onapTestUtils.verifyServicesProvides("DMAAP K8s", onapComponentSpec);
365         onapTestUtils
366             .verifyDockerConfig("DMAAP K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
367         onapTestUtils.verifyParameters("DMAAP K8s", onapComponentSpec, k8sBpNodeTemplates);
368     }
369
370     /**
371      * Test Case for K8s Blueprint for DMAAP Component Spec Input File with Import File
372      *
373      */
374     @DisplayName("Testing K8s Blueprint for DMAAP Component Spec Input File with Import File")
375     @Test
376     public void testDMAAPK8sImportFileBlueprint() throws IOException {
377         Input input =
378             onapTestUtils.getInput(
379                 Paths.get("src", "test", "resources", "componentspecs", ves).toFile()
380                     .getAbsolutePath(),
381                 "",
382                 "",
383                 Paths.get("src", "test", "resources", "componentspecs", testImports)
384                     .toFile()
385                     .getAbsolutePath(),
386                 "d",
387                 "");
388         onapComponentSpec =
389             onapComponentSpecService.createComponentSpecFromFile(input.getComponentSpecPath());
390         assertNotNull(
391             "K8s Blueprint for DMAAP Component Spec with Import File is NULL", onapComponentSpec);
392
393         OnapBlueprint onapBlueprint = dmaapBlueprintService
394             .createBlueprint(onapComponentSpec, input);
395
396         onapTestUtils.verifyToscaDefVersion(
397             "DMAAP with Import File K8s", onapBlueprint, Constants.TOSCA_DEF_VERSION);
398         onapTestUtils.verifyBpImportsFromFile(
399             "DMAAP with Import File K8s", onapBlueprint, input.getImportPath());
400
401         Map<String, LinkedHashMap<String, Object>> k8sBpInputs = onapBlueprint.getInputs();
402         assertNotNull("DMAAP with Import File K8s Blueprint Inputs Section is NULL", k8sBpInputs);
403         assertTrue(
404             "DMAAP with Import File K8s Blueprint Inputs Section is Empty", k8sBpInputs.size() > 0);
405
406         assertEquals(
407             "DMAAP with Import File K8s Blueprint:Inputs "
408                 + Constants.ONAP_INPUT_CPU_LIMIT
409                 + " Default is not matching",
410             k8sBpInputs.get(Constants.ONAP_INPUT_CPU_LIMIT).get("default"),
411             Constants.ONAP_DEFAULT250m);
412         assertEquals(
413             "DMAAP with Import File K8s Blueprint:Inputs "
414                 + Constants.SERVICE_COMPONENT_NAME_OVERRIDE
415                 + " Default is not matching",
416             k8sBpInputs.get(Constants.SERVICE_COMPONENT_NAME_OVERRIDE).get("default"),
417             Constants.ONAP_SERVICE_COMPONENTNAME_OVERRIDE_DEFAULT);
418
419         Map<String, Node> k8sBpNodeTemplates = onapBlueprint.getNode_templates();
420         assertNotNull(
421             "DMAAP with Import File K8s Blueprint Node Templates Section is NULL in the K8s DMAAP Blueprint",
422             k8sBpNodeTemplates);
423         assertTrue(
424             "DMAAP with Import File K8s Blueprint Node Templates Section Size is Empty",
425             k8sBpNodeTemplates.size() > 0);
426
427         assertEquals(
428             "DMAAP with Import File K8s Blueprint:NodeTemplates:Type is not Matching",
429             k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES).getType(),
430             Constants.DMAAP_NODETEMPLATES_TYPE);
431
432         Properties k8sBpNodeTemplateProperties =
433             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getProperties();
434         assertNotNull(
435             "DMAAP with Import File K8s Blueprint Node Templates:Properties Section is NULL",
436             k8sBpNodeTemplateProperties);
437
438         List bpNodeTemplateRelationships =
439             ((Node) k8sBpNodeTemplates.get(Constants.ONAP_NODETEMPLATES)).getRelationships();
440         assertNotNull(
441             "DMAAP with Import File K8s Blueprint Node Templates:Relationships Section is NULL",
442             bpNodeTemplateRelationships);
443
444         onapTestUtils.verifyStreamsPublishes(
445             "DMAAP with Import File K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
446         onapTestUtils.verifyStreamsSubscribes(
447             "DMAAP with Import File K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
448         onapTestUtils.verifyAuxilary("DMAAP with Import File K8s", onapComponentSpec);
449         onapTestUtils.verifyHealthCheck(
450             "DMAAP with Import File K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
451         onapTestUtils.verifyServicesCalls("DMAAP with Import File K8s", onapComponentSpec);
452         onapTestUtils.verifyServicesProvides("DMAAP with Import File K8s", onapComponentSpec);
453         onapTestUtils.verifyDockerConfig(
454             "DMAAP with Import File K8s", onapComponentSpec, k8sBpNodeTemplateProperties);
455         onapTestUtils.verifyParameters(
456             "DMAAP with Import File K8s", onapComponentSpec, k8sBpNodeTemplates);
457     }
458 }