Add external tls info to bpgenerator and component spec schema
[dcaegen2/platform.git] / mod / bpgenerator / src / test / java / org / onap / blueprintgenerator / core / BlueprintGeneratorTest.java
1 /**============LICENSE_START======================================================= 
2  org.onap.dcae 
3  ================================================================================ 
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. 
5  ================================================================================ 
6  Licensed under the Apache License, Version 2.0 (the "License"); 
7  you may not use this file except in compliance with the License. 
8  You may obtain a copy of the License at 
9
10       http://www.apache.org/licenses/LICENSE-2.0 
11
12  Unless required by applicable law or agreed to in writing, software 
13  distributed under the License is distributed on an "AS IS" BASIS, 
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  See the License for the specific language governing permissions and 
16  limitations under the License. 
17  ============LICENSE_END========================================================= 
18
19  */
20
21 package org.onap.blueprintgenerator.core;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.TreeMap;
33
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mockito;
37 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
38 import org.onap.blueprintgenerator.models.blueprint.GetInput;
39 import org.onap.blueprintgenerator.models.componentspec.Artifacts;
40 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
41 import org.onap.blueprintgenerator.models.componentspec.CallsObj;
42 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
43 import org.onap.blueprintgenerator.models.componentspec.Container;
44 import org.onap.blueprintgenerator.models.componentspec.HealthCheck;
45 import org.onap.blueprintgenerator.models.componentspec.Host;
46 import org.onap.blueprintgenerator.models.componentspec.Parameters;
47 import org.onap.blueprintgenerator.models.componentspec.Policy;
48 import org.onap.blueprintgenerator.models.componentspec.ProvidesObj;
49 import org.onap.blueprintgenerator.models.componentspec.Publishes;
50 import org.onap.blueprintgenerator.models.componentspec.Self;
51 import org.onap.blueprintgenerator.models.componentspec.Services;
52 import org.onap.blueprintgenerator.models.componentspec.Streams;
53 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
54 import org.onap.blueprintgenerator.models.componentspec.Volumes;
55 import org.onap.blueprintgenerator.models.dmaapbp.DmaapNode;
56 import org.onap.blueprintgenerator.models.onapbp.OnapNode;
57 import org.onap.blueprintgenerator.models.policymodel.PolicyModel;
58
59 import com.fasterxml.jackson.core.JsonParseException;
60 import com.fasterxml.jackson.databind.JsonMappingException;
61 import picocli.CommandLine;
62
63
64 // TODO: Auto-generated Javadoc
65 /**
66  * The Class BlueprintGeneratorTest.
67  */
68 public class BlueprintGeneratorTest {
69
70         /**
71          * Component spec test.
72          *
73          * @throws JsonParseException the json parse exception
74          * @throws JsonMappingException the json mapping exception
75          * @throws IOException Signals that an I/O exception has occurred.
76          */
77
78         @Test
79         public void componentSpecTest() throws JsonParseException, JsonMappingException, IOException {
80
81                 ComponentSpec spec = new ComponentSpec();
82                 TestComponentSpec test = new TestComponentSpec();
83                 spec.createComponentSpecFromString(test.getCs());
84
85                 //Manually fill a component spec object with the values from the file itself
86                 ComponentSpec manualSpec = new ComponentSpec();
87
88                 Self self = new Self();
89                 self.setComponent_type("docker");
90                 self.setDescription("Test component spec");
91                 self.setName("test.component.spec");
92                 self.setVersion("1.0.1");
93                 manualSpec.setSelf(self);
94
95                 //assertEquals(manualSpec.getSelf(), spec.getSelf());
96
97                 Services services = new Services();
98                 CallsObj[] calls = new CallsObj[0];
99                 ProvidesObj[] provides = new ProvidesObj[0];
100                 services.setCalls(calls);
101                 services.setProvides(provides);
102                 manualSpec.setServices(null);   
103
104                 //assertEquals(manualSpec.getServices(), spec.getServices());
105
106                 Streams streams = new Streams();
107                 Publishes[] publishes = new Publishes[2];
108                 Publishes pub1 = new Publishes();
109                 pub1.setConfig_key("TEST-PUB-DR");
110                 pub1.setFormat("dataformat_Hello_World_PM");
111                 pub1.setType("data_router");
112                 pub1.setVersion("1.0.0");
113
114                 Publishes pub2 = new Publishes();
115                 pub2.setConfig_key("TEST-PUB-MR");
116                 pub2.setFormat("dataformat_Hello_World_PM");
117                 pub2.setType("message_router");
118                 pub2.setVersion("1.0.0");
119                 publishes[0] = pub1;
120                 publishes[1] = pub2;
121                 streams.setPublishes(publishes);
122
123                 Subscribes[] subscribes = new Subscribes[2];    
124                 Subscribes sub1 = new Subscribes();
125                 sub1.setConfig_key("TEST-SUB-MR");
126                 sub1.setFormat("dataformat_Hello_World_PM");
127                 sub1.setRoute("/TEST_HELLO_WORLD_SUB_MR");
128                 sub1.setType("message_router");
129                 sub1.setVersion("1.0.0");
130
131                 Subscribes sub2 = new Subscribes();
132                 sub2.setConfig_key("TEST-SUB-DR");
133                 sub2.setFormat("dataformat_Hello_World_PM");
134                 sub2.setRoute("/TEST-HELLO-WORLD-SUB-DR");
135                 sub2.setType("data_router");
136                 sub2.setVersion("1.0.0");
137                 subscribes[0] = sub1;
138                 subscribes[1] = sub2;
139                 streams.setSubscribes(subscribes);
140
141                 manualSpec.setStreams(streams);
142
143                 //assertEquals(manualSpec.getStreams(), spec.getStreams());
144
145                 Parameters[] parameters = new Parameters[1];
146                 Parameters par1 = new Parameters();
147                 par1.setName("testParam1");
148                 par1.setValue("test-param-1");
149                 par1.setDescription("test parameter 1");
150                 par1.setSourced_at_deployment(true);
151                 par1.setDesigner_editable(true);
152                 par1.setPolicy_editable(true);
153                 par1.setPolicy_group("Test_Parameters");
154                 par1.setRequired(true);
155                 par1.setType("string");
156                 parameters[0] = par1;
157
158                 manualSpec.setParameters(parameters);
159
160                 //assertEquals(manualSpec.getParameters(), spec.getParameters());
161
162                 Auxilary auxilary = new Auxilary();
163                 HealthCheck healthcheck = new HealthCheck();
164                 healthcheck.setInterval("300s");
165                 healthcheck.setTimeout("120s");
166                 healthcheck.setScript("/etc/init.d/nagios status");
167                 healthcheck.setType("docker");
168                 auxilary.setHealthcheck(healthcheck);
169
170                 Volumes[] volumes = new Volumes[1];
171                 Volumes vol1 = new Volumes();
172                 Container con1 = new Container();
173                 con1.setBind("/opt/app/manager/config/hostname");
174                 Host host1 = new Host();
175                 host1.setPath("/etc/hostname");
176                 host1.setMode("ro");
177                 vol1.setContainer(con1);
178                 vol1.setHost(host1);
179
180
181                 volumes[0] = vol1;
182
183                 auxilary.setVolumes(volumes);
184
185                 ArrayList<Object> ports = new ArrayList();
186                 ports.add("80:90");
187                 ports.add("99:99");
188
189                 TreeMap<String, String> dataBases = new TreeMap<String, String>();
190                 dataBases.put("TestDB1", "PGaaS");
191                 dataBases.put("TestDB2", "PGaaS");
192                 auxilary.setDatabases(dataBases);
193
194                 Policy pol = new Policy();
195                 pol.setTrigger_type("docker");
196                 pol.setScript_path("/opt/app/manager/bin/reconfigure.sh");
197                 auxilary.setPolicy(pol);
198
199                 auxilary.setPorts(ports);
200
201                 manualSpec.setAuxilary(auxilary);
202
203                 //assertEquals(manualSpec.getAuxilary(), spec.getAuxilary());
204
205                 Artifacts[] artifacts = new Artifacts[1];
206                 Artifacts art = new Artifacts();
207                 art.setType("docker image");
208                 art.setUri("test.tester");
209
210                 artifacts[0] = art;
211                 manualSpec.setArtifacts(artifacts);
212
213                 //assertEquals(manualSpec.getArtifacts(), spec.getArtifacts());
214         }
215
216         /**
217          * Tosca definition test.
218          */
219         @Test
220         public void toscaDefinitionTest() {
221                 ComponentSpec cs = new ComponentSpec();
222                 TestComponentSpec test = new TestComponentSpec();
223                 cs.createComponentSpecFromString(test.getCs());
224                 Blueprint bp = new Blueprint();
225                 bp = bp.createBlueprint(cs, "", 'o', "", "");
226
227                 assertEquals(bp.getTosca_definitions_version(), "cloudify_dsl_1_3");
228         }
229
230         /**
231          * Imports test.
232          */
233         @Test
234         public void importsTest() {
235                 ComponentSpec cs = new ComponentSpec();
236                 TestComponentSpec test = new TestComponentSpec();
237                 cs.createComponentSpecFromString(test.getCs());
238
239                 Blueprint bp = new Blueprint();
240                 bp = bp.createBlueprint(cs, "", 'o', "", "");
241
242                 ArrayList<String> imps = new ArrayList<String>();
243
244                 imps.add("http://www.getcloudify.org/spec/cloudify/3.4/types.yaml");
245                 imps.add("https://nexus.onap.org/service/local/repositories/raw/content/org.onap.dcaegen2.platform.plugins/R6/k8splugin/1.7.2/k8splugin_types.yaml");
246                 imps.add("https://nexus.onap.org/service/local/repositories/raw/content/org.onap.dcaegen2.platform.plugins/R6/dcaepolicyplugin/2.4.0/dcaepolicyplugin_types.yaml");
247                 assertEquals(bp.getImports(), imps);
248         }
249
250         @Test
251         public void inputTest() {
252                 ComponentSpec cs = new ComponentSpec();
253                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
254
255                 Blueprint bp = new Blueprint();
256                 bp = bp.createBlueprint(cs, "", 'o', "", "");
257
258                 TreeMap<String, LinkedHashMap<String, Object>> inputs = new TreeMap<String, LinkedHashMap<String, Object>>();
259
260                 //mr inputs
261                 LinkedHashMap<String, Object> stringType = new LinkedHashMap<String, Object>();
262                 stringType.put("type", "string");
263
264
265                 //necessary inputs
266                 LinkedHashMap<String, Object> tag = new LinkedHashMap<String, Object>();
267                 tag.put("type", "string");
268                 String tester = "test.tester";
269                 tag.put("default", '"' + tester + '"');
270                 String tagVersion = "tag_version";
271                 inputs.put("tag_version", tag);
272
273                 inputs.put("log_directory", stringType);
274
275                 LinkedHashMap cert = new LinkedHashMap();
276                 cert.put("type", "string");
277                 cert.put("default", "");
278                 inputs.put("cert_directory", cert);
279
280                 LinkedHashMap<String, Object> env = new LinkedHashMap();
281                 env.put("default", "{}");
282                 inputs.put("envs", env);
283
284                 LinkedHashMap port = new LinkedHashMap();
285                 port.put("type", "string");
286                 port.put("description", "Kubernetes node port on which collector is exposed");
287                 port.put("default", "99");
288                 inputs.put("external_port", port);
289
290                 LinkedHashMap<String, Object> rep = new LinkedHashMap<String, Object>();
291                 rep.put("type", "integer");
292                 rep.put("description", "number of instances");
293                 rep.put("default", 1);
294                 inputs.put("replicas", rep);
295
296                 LinkedHashMap<String, Object> aaf = new LinkedHashMap();
297                 aaf.put("type", "boolean");
298                 aaf.put("default", false);
299                 inputs.put("use_tls", aaf);
300
301                 //parmaeter input
302                 LinkedHashMap<String, Object> test = new LinkedHashMap<String, Object>();
303                 test.put("type", "string");
304                 String testParam = "test-param-1";
305                 test.put("default", '"' + testParam + '"');
306                 inputs.put("testParam1", test);
307
308                 //mr/dr inputs
309                 inputs.put("TEST-PUB-DR_feed0_client_role", stringType);
310                 inputs.put("TEST-PUB-DR_feed0_password", stringType);
311                 inputs.put("TEST-PUB-DR_feed0_username", stringType);
312                 inputs.put("TEST-PUB-MR_topic1_aaf_password", stringType);
313                 inputs.put("TEST-PUB-MR_topic1_aaf_username", stringType);
314                 inputs.put("TEST-PUB-MR_topic1_client_role", stringType);
315                 inputs.put("TEST-SUB-DR_feed1_client_role", stringType);
316                 inputs.put("TEST-SUB-DR_feed1_password", stringType);
317                 inputs.put("TEST-SUB-DR_feed1_username", stringType);
318                 inputs.put("TEST-SUB-MR_topic0_client_role", stringType);
319                 inputs.put("TEST-SUB-MR_topic2_aaf_password", stringType);
320                 inputs.put("TEST-SUB-MR_topic2_aaf_username", stringType);
321                 inputs.put("namespace", stringType);
322                 inputs.put("idn_fqdn", cert);
323                 inputs.put("feed0_name", stringType);
324                 inputs.put("feed1_name", stringType);
325                 inputs.put("topic0_name", stringType);
326                 inputs.put("topic1_name", stringType);
327
328                 LinkedHashMap<String, Object> cpu = new LinkedHashMap();
329                 cpu.put("type", "string");
330                 cpu.put("default", "250m");
331                 inputs.put("test.component.spec_cpu_limit", cpu);
332                 inputs.put("test.component.spec_cpu_request", cpu);
333
334                 LinkedHashMap<String, Object> mem = new LinkedHashMap();
335                 mem.put("type", "string");
336                 mem.put("default", "128Mi");
337                 inputs.put("test.component.spec_memory_limit", mem);
338                 inputs.put("test.component.spec_memory_request", mem);
339
340                 assertEquals(true, true);
341         }
342         @Test
343         public void interfaceTest() {
344                 ComponentSpec cs = new ComponentSpec();
345                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
346
347                 Blueprint bp = new Blueprint();
348                 bp = bp.createBlueprint(cs, "", 'o', "", "");
349
350                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
351
352                 OnapNode testNode = new OnapNode();
353
354                 //set the type
355                 testNode.setType("dcae.nodes.ContainerizedServiceComponent");
356
357                 ArrayList<String> ports = new ArrayList<String>();
358                 ports.add("concat: [\"80:\", {get_input: external_port }]");
359                 ports.add("concat: [\"99:\", {get_input: external_port }]");
360                 assertEquals(true, true);
361         }
362
363         @Test
364         public void parametersTest() {
365                 ComponentSpec cs = new ComponentSpec();
366                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
367
368                 Blueprint bp = new Blueprint();
369                 bp = bp.createBlueprint(cs, "", 'o', "", "");
370
371                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
372
373                 GetInput par = (GetInput) node.getProperties().getApplication_config().getParams().get("testParam1");
374                 assertEquals(par.getGet_input(), "testParam1");
375         }
376
377         @Test
378         public void streamPublishesTest() {
379                 ComponentSpec cs = new ComponentSpec();
380                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
381
382                 Blueprint bp = new Blueprint();
383                 bp = bp.createBlueprint(cs, "", 'o', "", "");
384
385                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
386
387                 boolean test = false;
388                 if(!node.getProperties().getApplication_config().getStreams_publishes().isEmpty()) {
389                         test = true;
390                         System.out.println("tst");
391                 }
392
393                 assertEquals(true, test);
394         }
395
396
397         @Test
398         public void dmaapPluginTest() {
399                 ComponentSpec cs = new ComponentSpec();
400                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
401
402                 Blueprint bp = new Blueprint();
403                 bp = bp.createBlueprint(cs, "", 'd', "", "");
404
405                 DmaapNode dmaap = (DmaapNode) bp.getNode_templates().get("test.component.spec");
406
407                 //check if the stream publishes and subscribes are not null to see if the dmaap plugin was invoked properly
408                 boolean d = false;
409
410                 if(dmaap.getProperties().getStreams_publishes() != null || dmaap.getProperties().getStreams_subscribes() != null) {
411                         d = true;
412                 }
413                 assertEquals(true, d);
414         }
415
416         @Test
417         public void testPrintInstructionsBlueprintCommand() {
418                 BlueprintCommand objUnderTest = new BlueprintCommand();
419                 CommandLine cli = new CommandLine(objUnderTest);
420                 PrintStream mockStdOutWriter = Mockito.mock(PrintStream.class);
421                 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
422                 cli.usage(mockStdOutWriter);
423                 verify(mockStdOutWriter, times(1)).print(any(Object.class));
424
425         }
426
427         @Test
428         public void testPrintInstructionsPolicyCommand() {
429                 PolicyCommand objUnderTest = new PolicyCommand();
430                 CommandLine cli = new CommandLine(objUnderTest);
431                 PrintStream mockStdOutWriter = Mockito.mock(PrintStream.class);
432                 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
433                 cli.usage(mockStdOutWriter);
434                 verify(mockStdOutWriter, times(1)).print(any(Object.class));
435         }
436         
437         @Test
438         public void testPolicyModels() {
439                 ComponentSpec cs = new ComponentSpec();
440                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
441                 
442                 PolicyModel p = new PolicyModel();
443                 p.createPolicyModels(cs, "TestModels");
444                 
445                 assertEquals(true, true);
446         }
447 }