Commit for the blueprint generator java tool
[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.onapbp.OnapNode;
58
59
60 import com.fasterxml.jackson.core.JsonParseException;
61 import com.fasterxml.jackson.databind.JsonMappingException;
62
63 import junit.framework.Assert;
64
65
66 // TODO: Auto-generated Javadoc
67 /**
68  * The Class BlueprintGeneratorTest.
69  */
70 public class BlueprintGeneratorTest {
71
72         /**
73          * Component spec test.
74          *
75          * @throws JsonParseException the json parse exception
76          * @throws JsonMappingException the json mapping exception
77          * @throws IOException Signals that an I/O exception has occurred.
78          */
79
80         @Test
81         public void componentSpecTest() throws JsonParseException, JsonMappingException, IOException {
82
83                 ComponentSpec spec = new ComponentSpec();
84                 TestComponentSpec test = new TestComponentSpec();
85                 spec.createComponentSpecFromString(test.getCs());
86
87                 //Manually fill a component spec object with the values from the file itself
88                 ComponentSpec manualSpec = new ComponentSpec();
89
90                 Self self = new Self();
91                 self.setComponent_type("docker");
92                 self.setDescription("Test component spec");
93                 self.setName("test.component.spec");
94                 self.setVersion("1.0.1");
95                 manualSpec.setSelf(self);
96
97                 //assertEquals(manualSpec.getSelf(), spec.getSelf());
98
99                 Services services = new Services();
100                 CallsObj[] calls = new CallsObj[0];
101                 ProvidesObj[] provides = new ProvidesObj[0];
102                 services.setCalls(calls);
103                 services.setProvides(provides);
104                 manualSpec.setServices(null);   
105
106                 //assertEquals(manualSpec.getServices(), spec.getServices());
107
108                 Streams streams = new Streams();
109                 Publishes[] publishes = new Publishes[2];
110                 Publishes pub1 = new Publishes();
111                 pub1.setConfig_key("TEST-PUB-DR");
112                 pub1.setFormat("dataformat_Hello_World_PM");
113                 pub1.setType("data_router");
114                 pub1.setVersion("1.0.0");
115
116                 Publishes pub2 = new Publishes();
117                 pub2.setConfig_key("TEST-PUB-MR");
118                 pub2.setFormat("dataformat_Hello_World_PM");
119                 pub2.setType("message_router");
120                 pub2.setVersion("1.0.0");
121                 publishes[0] = pub1;
122                 publishes[1] = pub2;
123                 streams.setPublishes(publishes);
124
125                 Subscribes[] subscribes = new Subscribes[2];    
126                 Subscribes sub1 = new Subscribes();
127                 sub1.setConfig_key("TEST-SUB-MR");
128                 sub1.setFormat("dataformat_Hello_World_PM");
129                 sub1.setRoute("/TEST_HELLO_WORLD_SUB_MR");
130                 sub1.setType("message_router");
131                 sub1.setVersion("1.0.0");
132
133                 Subscribes sub2 = new Subscribes();
134                 sub2.setConfig_key("TEST-SUB-DR");
135                 sub2.setFormat("dataformat_Hello_World_PM");
136                 sub2.setRoute("/TEST-HELLO-WORLD-SUB-DR");
137                 sub2.setType("data_router");
138                 sub2.setVersion("1.0.0");
139                 subscribes[0] = sub1;
140                 subscribes[1] = sub2;
141                 streams.setSubscribes(subscribes);
142
143                 manualSpec.setStreams(streams);
144
145                 //assertEquals(manualSpec.getStreams(), spec.getStreams());
146
147                 Parameters[] parameters = new Parameters[1];
148                 Parameters par1 = new Parameters();
149                 par1.setName("testParam1");
150                 par1.setValue("test-param-1");
151                 par1.setDescription("test parameter 1");
152                 par1.setSourced_at_deployment(true);
153                 par1.setDesigner_editable(true);
154                 par1.setPolicy_editable(true);
155                 par1.setPolicy_group("Test_Parameters");
156                 par1.setRequired(true);
157                 par1.setType("string");
158                 parameters[0] = par1;
159
160                 manualSpec.setParameters(parameters);
161
162                 //assertEquals(manualSpec.getParameters(), spec.getParameters());
163
164                 Auxilary auxilary = new Auxilary();
165                 HealthCheck healthcheck = new HealthCheck();
166                 healthcheck.setInterval("300s");
167                 healthcheck.setTimeout("120s");
168                 healthcheck.setScript("/etc/init.d/nagios status");
169                 healthcheck.setType("docker");
170                 auxilary.setHealthcheck(healthcheck);
171
172                 Volumes[] volumes = new Volumes[1];
173                 Volumes vol1 = new Volumes();
174                 Container con1 = new Container();
175                 con1.setBind("/opt/app/manager/config/hostname");
176                 Host host1 = new Host();
177                 host1.setPath("/etc/hostname");
178                 host1.setMode("ro");
179                 vol1.setContainer(con1);
180                 vol1.setHost(host1);
181
182
183                 volumes[0] = vol1;
184
185                 auxilary.setVolumes(volumes);
186
187                 String[] ports = new String[2];
188                 ports[0] = "80:80";
189                 ports[1] = "99:99";
190
191                 TreeMap<String, String> dataBases = new TreeMap<String, String>();
192                 dataBases.put("TestDB1", "PGaaS");
193                 dataBases.put("TestDB2", "PGaaS");
194                 auxilary.setDatabases(dataBases);
195
196                 Policy pol = new Policy();
197                 pol.setTrigger_type("docker");
198                 pol.setScript_path("/opt/app/manager/bin/reconfigure.sh");
199                 auxilary.setPolicy(pol);
200
201                 auxilary.setPorts(ports);
202
203                 manualSpec.setAuxilary(auxilary);
204
205                 //assertEquals(manualSpec.getAuxilary(), spec.getAuxilary());
206
207                 Artifacts[] artifacts = new Artifacts[1];
208                 Artifacts art = new Artifacts();
209                 art.setType("docker image");
210                 art.setUri("test.tester");
211
212                 artifacts[0] = art;
213                 manualSpec.setArtifacts(artifacts);
214
215                 //assertEquals(manualSpec.getArtifacts(), spec.getArtifacts());
216                 
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                 inputs.put("TEST_PUB_MR_publish_url", stringType);
267                 inputs.put("TEST_SUB_MR_subscribe_url", stringType);
268                 
269                 //necessary inputs
270                 LinkedHashMap<String, Object> tag = new LinkedHashMap<String, Object>();
271                 tag.put("type", "string");
272                 String tester = "test.tester";
273                 tag.put("default", '"' + tester + '"');
274                 String tagVersion = "tag_version";
275                 inputs.put("tag_version", tag);
276                 
277                 inputs.put("log_directory", stringType);
278                 
279                 
280                 LinkedHashMap<String, Object> port = new LinkedHashMap<String, Object>();
281                 port.put("type", "string");
282                 port.put("description", "Kubernetes node port on which collector is exposed");
283                 String p = "'30235'";
284                 port.put("default", '"' + p + '"');
285                 inputs.put("external_port", port);
286                 
287                 LinkedHashMap<String, Object> rep = new LinkedHashMap<String, Object>();
288                 rep.put("type", "integer");
289                 rep.put("description", "number of instances");
290                 rep.put("default", 1);
291                 inputs.put("replicas", rep);
292                 
293                 //parmaeter input
294                 LinkedHashMap<String, Object> test = new LinkedHashMap<String, Object>();
295                 test.put("type", "string");
296                 String testParam = "test-param-1";
297                 test.put("default", '"' + testParam + '"');
298                 inputs.put("testParam1", test);
299                 
300                 //mr/dr inputs
301                 inputs.put("TEST-PUB-DR_delivery_url", stringType);
302                 inputs.put("TEST-PUB-DR_location", stringType);
303                 inputs.put("TEST-PUB-DR_password", stringType);
304                 inputs.put("TEST-PUB-DR_subscriber_id", stringType);
305                 inputs.put("TEST-PUB-DR_username", stringType);
306                 inputs.put("TEST-SUB-DR_delivery_url", stringType);
307                 inputs.put("TEST-SUB-DR_location", stringType);
308                 inputs.put("TEST-SUB-DR_password", stringType);
309                 inputs.put("TEST-SUB-DR_subscriber_id", stringType);
310                 inputs.put("TEST-SUB-DR_username", stringType);
311                 inputs.put("TEST_PUB_MR_publish_url", stringType);
312                 inputs.put("TEST_SUB_MR_subscribe_url", stringType);
313                 
314                 assertEquals(bp.getInputs(), inputs);
315         }
316         
317         @Test
318         public void interfaceTest() {
319                 ComponentSpec cs = new ComponentSpec();
320                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
321                 
322                 Blueprint bp = new Blueprint();
323                 bp = bp.createBlueprint(cs, "", 'o', "");
324                 
325                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
326                 
327                 OnapNode testNode = new OnapNode();
328                 
329                 //set the type
330                 testNode.setType("dcae.nodes.ContainerizedPlatformComponent");
331                 
332                 ArrayList<String> ports = new ArrayList<String>();
333                 ports.add("concat: [\"80:\", {get_input: external_port }]");
334                 ports.add("concat: [\"99:\", {get_input: external_port }]");
335
336                 
337                 
338                 assertEquals(node.getInterfaces().get("cloudify.interfaces.lifecycle").getStart().getInputs().getPorts(), ports);
339         }
340         
341         @Test
342         public void parametersTest() {
343                 ComponentSpec cs = new ComponentSpec();
344                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
345                 
346                 Blueprint bp = new Blueprint();
347                 bp = bp.createBlueprint(cs, "", 'o', "");
348                 
349                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
350                 
351                 GetInput par = (GetInput) node.getProperties().getApplication_config().getParams().get("testParam1");
352                 assertEquals(par.getGet_input(), "testParam1");
353         }
354         
355         @Test
356         public void streamPublishesTest() {
357                 ComponentSpec cs = new ComponentSpec();
358                 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
359                 
360                 Blueprint bp = new Blueprint();
361                 bp = bp.createBlueprint(cs, "", 'o', "");
362                 
363                 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
364                 
365                 assertEquals(node.getProperties().getApplication_config().getStream_publishes().get("TEST-PUB-DR").getDmaap_info().getUsername().getGet_input(), "TEST-PUB-DR_username");
366         }
367
368         
369
370
371
372 }