Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / test / OnapTestUtils.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  Copyright (c) 2021  Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  *  you may not use this file except in compliance with the License.
11  *  *  You may obtain a copy of the License at
12  *  *
13  *  *       http://www.apache.org/licenses/LICENSE-2.0
14  *  *
15  *  *  Unless required by applicable law or agreed to in writing, software
16  *  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  *  See the License for the specific language governing permissions and
19  *  *  limitations under the License.
20  *  *  ============LICENSE_END=========================================================
21  *
22  *
23  */
24
25 package org.onap.blueprintgenerator.test;
26
27 import org.onap.blueprintgenerator.constants.Constants;
28 import org.onap.blueprintgenerator.model.base.Blueprint;
29 import org.onap.blueprintgenerator.model.common.Input;
30 import org.onap.blueprintgenerator.model.common.Node;
31 import org.onap.blueprintgenerator.model.common.Properties;
32 import org.onap.blueprintgenerator.model.componentspec.OnapAuxilary;
33 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
34
35 import org.onap.blueprintgenerator.model.componentspec.common.Calls;
36 import org.onap.blueprintgenerator.model.componentspec.common.Provides;
37 import org.onap.blueprintgenerator.model.componentspec.common.HealthCheck;
38 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
39 import org.onap.blueprintgenerator.model.componentspec.common.Volumes;
40 import org.onap.blueprintgenerator.model.componentspec.common.Artifacts;
41 import org.onap.blueprintgenerator.model.dmaap.Streams;
42 import org.junit.Ignore;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.stereotype.Component;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.Arrays;
49 import java.util.List;
50 import java.util.Map;
51
52 import static org.junit.Assert.assertArrayEquals;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertNotNull;
55 import static org.junit.Assert.assertTrue;
56
57 /**
58  * @author : Ravi Mantena
59  * @date 10/16/2020 Application: ONAP - Blueprint Generator Test Utilities used in Test Suite and
60  * Test Cases
61  */
62 @Component
63 @Ignore
64 public class OnapTestUtils extends BlueprintGeneratorTests {
65
66     @Value("${imports.onap.types}")
67     private String importsOnapTypes;
68
69     @Value("${imports.onap.K8s.plugintypes}")
70     private String importsOnapK8sPlugintypes;
71
72     @Value("${imports.onap.K8s.dcaepolicyplugin}")
73     private String importsOnapK8sDcaepolicyplugin;
74
75     @Value("${imports.dmaap.dmaapplugin}")
76     private String importsDmaapDmaapplugin;
77
78     @Value("${import.Postgres}")
79     private String importPostgres;
80
81     @Value("${import.Clamp}")
82     private String importClamp;
83
84     /**
85      * Creates Input
86      *
87      * @param componentSpecPath
88      * @param outputPath
89      * @param bluePrintName
90      * @param importPath
91      * @param bpType
92      * @param serviceNameOverride
93      * @return
94      */
95     public Input getInput(
96         String componentSpecPath,
97         String outputPath,
98         String bluePrintName,
99         String importPath,
100         String bpType,
101         String serviceNameOverride) {
102         Input input = new Input();
103         input.setComponentSpecPath(componentSpecPath);
104         input.setBluePrintName(bluePrintName);
105         input.setOutputPath(outputPath);
106         input.setBpType(bpType);
107         input.setServiceNameOverride(serviceNameOverride);
108         input.setImportPath(importPath);
109         return input;
110     }
111
112     /**
113      * Creates Input from Component Spec Path
114      *
115      * @param componentSpecPath
116      * @return
117      */
118     public Input getInput(String componentSpecPath) {
119         Input input = new Input();
120         input.setComponentSpecPath(componentSpecPath);
121         return input;
122     }
123
124     /**
125      * Verifies Tosca Def Version
126      *
127      * @param type
128      * @param blueprint
129      * @param toscaDefVersion
130      */
131     public void verifyToscaDefVersion(String type, Blueprint blueprint, String toscaDefVersion) {
132         String bpToscaDefVersion = blueprint.getTosca_definitions_version();
133         assertNotNull(type + " TOSCA Definition Version is NULL", bpToscaDefVersion);
134         assertEquals(
135             type + " TOSCA Definition Version is not Matching", bpToscaDefVersion, toscaDefVersion);
136     }
137
138     /**
139      * Verifies Imports
140      *
141      * @param type
142      * @param blueprint
143      * @param validateimps
144      */
145     public void verifyBpImports(String type, Blueprint blueprint, boolean validateimps) {
146         String[] bpImports = blueprint.getImports()
147             .toArray(new String[blueprint.getImports().size()]);
148         if (validateimps) {
149             String[] testImports = {
150                 importsOnapTypes,
151                 importsOnapK8sPlugintypes,
152                 importsDmaapDmaapplugin,
153                 importPostgres,
154                 importClamp
155             };
156             assertArrayEquals(
157                 type
158                     + " Blueprint Imports is not matching with default Dmaap K8s Blueprint imports",
159                 bpImports,
160                 testImports);
161         } else {
162             String[] testImports = {
163                 importsOnapTypes,
164                 importsOnapK8sPlugintypes,
165                 importsOnapK8sDcaepolicyplugin,
166                 importPostgres,
167                 importClamp
168             };
169             assertArrayEquals(
170                 type + " Blueprint Imports is not matching with default Onap K8s Blueprint imports",
171                 bpImports,
172                 testImports);
173         }
174     }
175
176     /**
177      * Verifies Imports from file
178      *
179      * @param type
180      * @param blueprint
181      * @param importPath
182      */
183     public void verifyBpImportsFromFile(String type, Blueprint blueprint, String importPath)
184         throws IOException {
185         Blueprint importFileRead = yamlObjectMapper
186             .readValue(new File(importPath), Blueprint.class);
187         String[] importFileImports =
188             importFileRead.getImports().toArray(new String[importFileRead.getImports().size()]);
189         String[] bpImports = blueprint.getImports()
190             .toArray(new String[blueprint.getImports().size()]);
191         assertArrayEquals(
192             type + " Blueprint Imports is not matching with default Dmaap K8s Blueprint imports",
193             bpImports,
194             importFileImports);
195     }
196
197     /**
198      * Verifies Streams Publishes
199      *
200      * @param type
201      * @param onapComponentSpec
202      * @param nodeTemplateProperties
203      */
204     public void verifyStreamsPublishes(
205         String type, OnapComponentSpec onapComponentSpec, Properties nodeTemplateProperties) {
206         List<Streams> streamsPublishes = nodeTemplateProperties.getStreams_publishes();
207         if (!(streamsPublishes == null)) {
208             assertNotNull(
209                 type + " Blueprint:NodeTemplates:Properties:StreamsPublishes is NULL",
210                 streamsPublishes);
211             assertTrue(
212                 type + " Blueprint:NodeTemplates:Properties:StreamsPublishes Section Size is 0",
213                 streamsPublishes.size() > 0);
214             assertEquals(
215                 type + " Blueprint:NodeTemplates:Properties:StreamsPublishes is Not Matching",
216                 streamsPublishes.get(0).getType(),
217                 Constants.MESSAGEROUTER_VALUE);
218             assertEquals(
219                 type + " Blueprint:NodeTemplates:Properties:StreamsPublishes is Not Matching",
220                 streamsPublishes.get(1).getType(),
221                 Constants.MESSAGEROUTER_VALUE);
222         }
223     }
224
225     /**
226      * Verifies Streams Subscribes
227      *
228      * @param type
229      * @param onapComponentSpec
230      * @param nodeTemplateProperties
231      */
232     public void verifyStreamsSubscribes(
233         String type, OnapComponentSpec onapComponentSpec, Properties nodeTemplateProperties) {
234         List<Streams> streamsSubscribes = nodeTemplateProperties.getStreams_subscribes();
235         if (!(streamsSubscribes == null)) {
236             assertNotNull(
237                 type + " Blueprint:NodeTemplates:Properties:StreamsSubscribes is NULL",
238                 streamsSubscribes);
239             assertTrue(
240                 type + " Blueprint:NodeTemplates:Properties:StreamsSubscribes Section Size is 0",
241                 streamsSubscribes.size() > 0);
242             assertEquals(
243                 type + " Blueprint:NodeTemplates:Properties:StreamsSubscribes is Not Matching",
244                 streamsSubscribes.get(0).getType(),
245                 Constants.MESSAGE_ROUTER);
246             assertEquals(
247                 type + " Blueprint:NodeTemplates:Properties:StreamsSubscribes is Not Matching",
248                 streamsSubscribes.get(1).getType(),
249                 Constants.DATA_ROUTER);
250         }
251     }
252
253     /**
254      * Verifies Services Calls
255      *
256      * @param type
257      * @param onapComponentSpec
258      */
259     public void verifyServicesCalls(String type, OnapComponentSpec onapComponentSpec) {
260         Calls[] csServicesCalls = onapComponentSpec.getServices().getCalls();
261         assertNotNull(type + " ComponentSpec Services Calls is NULL", csServicesCalls);
262         // assertTrue(type + " ComponentSpec Services Calls Section Size is 0", csServicesCalls.length >
263         // 0);
264     }
265
266     /**
267      * Verifies Services Provides
268      *
269      * @param type
270      * @param onapComponentSpec
271      */
272     public void verifyServicesProvides(String type, OnapComponentSpec onapComponentSpec) {
273         Provides[] csServicesProvides = onapComponentSpec.getServices().getProvides();
274         assertNotNull(type + " ComponentSpec Services Provides is NULL", csServicesProvides);
275         assertTrue(
276             type + " ComponentSpec Services Provides Section Size is 0",
277             csServicesProvides.length > 0);
278     }
279
280     /**
281      * Verifies Docker Config
282      *
283      * @param type
284      * @param onapComponentSpec
285      * @param nodeTemplateProperties
286      */
287     public void verifyDockerConfig(
288         String type, OnapComponentSpec onapComponentSpec, Properties nodeTemplateProperties) {
289         OnapAuxilary dockerConfig = nodeTemplateProperties.getDocker_config();
290         assertNotNull(type + " Blueprint Docker Config Section is NULL", dockerConfig);
291     }
292
293     /**
294      * Verifies Parameters
295      *
296      * @param type
297      * @param onapComponentSpec
298      * @param nodeTemplates
299      */
300     public void verifyParameters(
301         String type, OnapComponentSpec onapComponentSpec, Map<String, Node> nodeTemplates) {
302         Parameters[] csParameters = onapComponentSpec.getParameters();
303         assertNotNull(type + " ComponentSpec Parameters Section is NULL", csParameters);
304         assertTrue(type + " ComponentSpec Parameters Section Size is 0", csParameters.length > 0);
305     }
306
307     /**
308      * Verifies Auxilary
309      *
310      * @param type
311      * @param onapComponentSpec
312      */
313     public void verifyAuxilary(String type, OnapComponentSpec onapComponentSpec) {
314         OnapAuxilary csAuxilary = onapComponentSpec.getAuxilary();
315         assertNotNull(type + " ComponentSpec Auxilary Section is NULL", csAuxilary);
316     }
317
318     /**
319      * Verifies HealthCheck
320      *
321      * @param type
322      * @param onapComponentSpec
323      * @param nodeTemplateProperties
324      */
325     public void verifyHealthCheck(
326         String type, OnapComponentSpec onapComponentSpec, Properties nodeTemplateProperties) {
327         HealthCheck csAuxilaryHealthcheck = onapComponentSpec.getAuxilary().getHealthcheck();
328         assertNotNull(
329             type + " ComponentSpec Auxilary Health Check Section is NULL", csAuxilaryHealthcheck);
330         HealthCheck healthCheck = nodeTemplateProperties.getDocker_config().getHealthcheck();
331         assertNotNull(
332             type + " Blueprint:NodeTemplates:DockerConfig:Healthcheck Section is NULL",
333             healthCheck);
334         assertEquals(
335             type + " Blueprint:NodeTemplates:DockerConfig:Healthcheck:Interval Tag is not matching",
336             healthCheck.getInterval(),
337             csAuxilaryHealthcheck.getInterval());
338         assertEquals(
339             type + " Blueprint:NodeTemplates:DockerConfig:Healthcheck:Timeout Tag is not matching",
340             healthCheck.getTimeout(),
341             csAuxilaryHealthcheck.getTimeout());
342         assertEquals(
343             type + " Blueprint:NodeTemplates:DockerConfig:Healthcheck:Script Tag is not matching",
344             healthCheck.getEndpoint(),
345             csAuxilaryHealthcheck.getEndpoint());
346         assertEquals(
347             type + " Blueprint:NodeTemplates:DockerConfig:Healthcheck:Type Tag is not matching",
348             healthCheck.getType(),
349             csAuxilaryHealthcheck.getType());
350     }
351
352     /**
353      * Verifies Volumes
354      *
355      * @param type
356      * @param onapComponentSpec
357      * @param nodeTemplateProperties
358      */
359     public void verifyVolumes(
360         String type, OnapComponentSpec onapComponentSpec, Properties nodeTemplateProperties) {
361         Volumes[] csAuxilaryVolumes = onapComponentSpec.getAuxilary().getVolumes();
362         assertNotNull(
363             type + " ComponentSpec Auxilary Live Health Check Section is NULL", csAuxilaryVolumes);
364         Volumes[] onapVolumes = nodeTemplateProperties.getDocker_config().getVolumes();
365         assertNotNull(
366             type + " Blueprint:NodeTemplates:DockerConfig:LiveHealthcheck Section is NULL",
367             onapVolumes);
368         assertNotNull(type + " Blueprint:NodeTemplates:DockerConfig:Volumes:ConfigVolume:GetName Section is NULL", Arrays.stream(onapVolumes)
369                 .filter(c -> c.getConfigVolume() != null)
370                 .filter(c -> c.getName() != null)
371                 .filter(c -> c.equals("myConfig"))
372                 .findAny());
373     }
374
375     /**
376      * Verifies Artifacts
377      *
378      * @param type
379      * @param onapComponentSpec
380      * @param inputs
381      * @param bptype
382      */
383     public void verifyArtifacts(
384         String type,
385         OnapComponentSpec onapComponentSpec,
386         Map<String, Map<String, Object>> inputs,
387         String bptype) {
388         Artifacts[] csArtifacts = onapComponentSpec.getArtifacts();
389         assertNotNull(type + " ComponentSpec Artifacts Section is NULL", csArtifacts);
390         assertEquals(
391             type + " Blueprint:Artifacts:image is not matching",
392             ((String) inputs.get("image").get("default")),
393             "\"" + csArtifacts[0].getUri() + "\"");
394         // assertEquals(type + " Blueprint:Artifacts:image is not matching", ((String)
395         // inputs.get("tag_version").get("default")), "\"" + csArtifacts[0].getUri() + "\"");
396
397     }
398 }