1 /**============LICENSE_START=======================================================
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
10 http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.blueprintgenerator.core;
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;
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;
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;
59 import com.fasterxml.jackson.core.JsonParseException;
60 import com.fasterxml.jackson.databind.JsonMappingException;
61 import picocli.CommandLine;
64 // TODO: Auto-generated Javadoc
66 * The Class BlueprintGeneratorTest.
68 public class BlueprintGeneratorTest {
71 * Component spec test.
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.
79 public void componentSpecTest() throws JsonParseException, JsonMappingException, IOException {
81 ComponentSpec spec = new ComponentSpec();
82 TestComponentSpec test = new TestComponentSpec();
83 spec.createComponentSpecFromString(test.getCs());
85 //Manually fill a component spec object with the values from the file itself
86 ComponentSpec manualSpec = new ComponentSpec();
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);
95 //assertEquals(manualSpec.getSelf(), spec.getSelf());
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);
104 //assertEquals(manualSpec.getServices(), spec.getServices());
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");
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");
121 streams.setPublishes(publishes);
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");
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);
141 manualSpec.setStreams(streams);
143 //assertEquals(manualSpec.getStreams(), spec.getStreams());
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;
158 manualSpec.setParameters(parameters);
160 //assertEquals(manualSpec.getParameters(), spec.getParameters());
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);
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");
177 vol1.setContainer(con1);
183 auxilary.setVolumes(volumes);
185 ArrayList<Object> ports = new ArrayList();
189 TreeMap<String, String> dataBases = new TreeMap<String, String>();
190 dataBases.put("TestDB1", "PGaaS");
191 dataBases.put("TestDB2", "PGaaS");
192 auxilary.setDatabases(dataBases);
194 Policy pol = new Policy();
195 pol.setTrigger_type("docker");
196 pol.setScript_path("/opt/app/manager/bin/reconfigure.sh");
197 auxilary.setPolicy(pol);
199 auxilary.setPorts(ports);
201 manualSpec.setAuxilary(auxilary);
203 //assertEquals(manualSpec.getAuxilary(), spec.getAuxilary());
205 Artifacts[] artifacts = new Artifacts[1];
206 Artifacts art = new Artifacts();
207 art.setType("docker image");
208 art.setUri("test.tester");
211 manualSpec.setArtifacts(artifacts);
213 //assertEquals(manualSpec.getArtifacts(), spec.getArtifacts());
217 * Tosca definition 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', "", "");
227 assertEquals(bp.getTosca_definitions_version(), "cloudify_dsl_1_3");
234 public void importsTest() {
235 ComponentSpec cs = new ComponentSpec();
236 TestComponentSpec test = new TestComponentSpec();
237 cs.createComponentSpecFromString(test.getCs());
239 Blueprint bp = new Blueprint();
240 bp = bp.createBlueprint(cs, "", 'o', "", "");
242 ArrayList<String> imps = new ArrayList<String>();
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);
251 public void inputTest() {
252 ComponentSpec cs = new ComponentSpec();
253 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
255 Blueprint bp = new Blueprint();
256 bp = bp.createBlueprint(cs, "", 'o', "", "");
258 TreeMap<String, LinkedHashMap<String, Object>> inputs = new TreeMap<String, LinkedHashMap<String, Object>>();
261 LinkedHashMap<String, Object> stringType = new LinkedHashMap<String, Object>();
262 stringType.put("type", "string");
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);
273 inputs.put("log_directory", stringType);
275 LinkedHashMap cert = new LinkedHashMap();
276 cert.put("type", "string");
277 cert.put("default", "");
278 inputs.put("cert_directory", cert);
280 LinkedHashMap<String, Object> env = new LinkedHashMap();
281 env.put("default", "{}");
282 inputs.put("envs", env);
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);
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);
296 LinkedHashMap<String, Object> aaf = new LinkedHashMap();
297 aaf.put("type", "boolean");
298 aaf.put("default", false);
299 inputs.put("use_tls", aaf);
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);
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);
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);
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);
340 assertEquals(true, true);
343 public void interfaceTest() {
344 ComponentSpec cs = new ComponentSpec();
345 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
347 Blueprint bp = new Blueprint();
348 bp = bp.createBlueprint(cs, "", 'o', "", "");
350 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
352 OnapNode testNode = new OnapNode();
355 testNode.setType("dcae.nodes.ContainerizedServiceComponent");
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);
364 public void parametersTest() {
365 ComponentSpec cs = new ComponentSpec();
366 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
368 Blueprint bp = new Blueprint();
369 bp = bp.createBlueprint(cs, "", 'o', "", "");
371 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
373 GetInput par = (GetInput) node.getProperties().getApplication_config().getParams().get("testParam1");
374 assertEquals(par.getGet_input(), "testParam1");
378 public void streamPublishesTest() {
379 ComponentSpec cs = new ComponentSpec();
380 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
382 Blueprint bp = new Blueprint();
383 bp = bp.createBlueprint(cs, "", 'o', "", "");
385 OnapNode node = (OnapNode) bp.getNode_templates().get("test.component.spec");
387 boolean test = false;
388 if(!node.getProperties().getApplication_config().getStreams_publishes().isEmpty()) {
390 System.out.println("tst");
393 assertEquals(true, test);
398 public void dmaapPluginTest() {
399 ComponentSpec cs = new ComponentSpec();
400 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
402 Blueprint bp = new Blueprint();
403 bp = bp.createBlueprint(cs, "", 'd', "", "");
405 DmaapNode dmaap = (DmaapNode) bp.getNode_templates().get("test.component.spec");
407 //check if the stream publishes and subscribes are not null to see if the dmaap plugin was invoked properly
410 if(dmaap.getProperties().getStreams_publishes() != null || dmaap.getProperties().getStreams_subscribes() != null) {
413 assertEquals(true, d);
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));
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));
438 public void testPolicyModels() {
439 ComponentSpec cs = new ComponentSpec();
440 cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
442 PolicyModel p = new PolicyModel();
443 p.createPolicyModels(cs, "TestModels");
445 assertEquals(true, true);