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