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