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