From cbae5bccc58823e70d5cbe807095a11747a87727 Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Thu, 1 Oct 2020 12:33:44 +0200 Subject: Improve code quality in Appconfig - Add unit test - Refactor Appconfig class (break the public method into smaller private methods, get rid of unnecessary parameter object reference copies) - Top up version to 1.6.0 Issue-ID: DCAEGEN2-2460 Signed-off-by: Joanna Jeremicz Change-Id: Ia34b5c8ed3d6eafe765075a1a2b4274beb1868e6 --- .../TestCases/expects/createAppConfigResult.txt | 1 + mod/bpgenerator/pom.xml | 2 +- .../models/blueprint/Appconfig.java | 231 ++++++++++----------- .../models/blueprint/AppconfigTest.java | 62 ++++++ mod/bpgenerator/version.properties | 4 +- 5 files changed, 180 insertions(+), 120 deletions(-) create mode 100644 mod/bpgenerator/TestCases/expects/createAppConfigResult.txt create mode 100644 mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/AppconfigTest.java (limited to 'mod/bpgenerator') diff --git a/mod/bpgenerator/TestCases/expects/createAppConfigResult.txt b/mod/bpgenerator/TestCases/expects/createAppConfigResult.txt new file mode 100644 index 0000000..11ce552 --- /dev/null +++ b/mod/bpgenerator/TestCases/expects/createAppConfigResult.txt @@ -0,0 +1 @@ +{TEST-PUB-DR_delivery_url={type=string}, TEST-PUB-DR_location={type=string}, TEST-PUB-DR_password={type=string}, TEST-PUB-DR_subscriber_id={type=string}, TEST-PUB-DR_username={type=string}, TEST-SUB-DR_delivery_url={type=string}, TEST-SUB-DR_location={type=string}, TEST-SUB-DR_password={type=string}, TEST-SUB-DR_subscriber_id={type=string}, TEST-SUB-DR_username={type=string}, TEST_PUB_MR_publish_url={type=string}, TEST_SUB_MR_subscribe_url={type=string}, service_component_name_override={type=string, default=}, testParam1={type=string, default=test-param-1}} diff --git a/mod/bpgenerator/pom.xml b/mod/bpgenerator/pom.xml index 86009f3..c8f8fdb 100644 --- a/mod/bpgenerator/pom.xml +++ b/mod/bpgenerator/pom.xml @@ -29,7 +29,7 @@ org.onap.dcaegen2.platform.mod blueprint-generator - 1.5.2-SNAPSHOT + 1.6.0-SNAPSHOT UTF-8 ${project.basedir}/target/surefire-reports diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Appconfig.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Appconfig.java index 75864be..e9157ad 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Appconfig.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Appconfig.java @@ -21,9 +21,15 @@ package org.onap.blueprintgenerator.models.blueprint; +import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType; +import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType; +import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; import java.util.LinkedHashMap; import java.util.TreeMap; - +import lombok.Getter; +import lombok.Setter; import org.onap.blueprintgenerator.models.blueprint.dmaap.DmaapObj; import org.onap.blueprintgenerator.models.componentspec.CallsObj; import org.onap.blueprintgenerator.models.componentspec.ComponentSpec; @@ -31,125 +37,116 @@ import org.onap.blueprintgenerator.models.componentspec.Parameters; import org.onap.blueprintgenerator.models.componentspec.Publishes; import org.onap.blueprintgenerator.models.componentspec.Subscribes; -import com.fasterxml.jackson.annotation.JsonAnyGetter; - - -import lombok.Getter; -import lombok.Setter; - @Getter @Setter public class Appconfig { - private CallsObj[] service_calls; - private TreeMap streams_publishes; - private TreeMap streams_subscribes; - private TreeMap params; - - @JsonAnyGetter - public TreeMap getParams(){ - return params; - } - - public TreeMap> createAppconfig(TreeMap> inps, ComponentSpec cs, String override, - boolean isDmaap) { - TreeMap> retInputs = inps; - - //set service calls - CallsObj[] call = new CallsObj[0]; - this.setService_calls(call); - - //set the stream publishes - TreeMap streamPublishes = new TreeMap(); - if(cs.getStreams().getPublishes().length != 0) { - for(Publishes p: cs.getStreams().getPublishes()) { - if(p.getType().equals("data_router") || p.getType().equals("data router")) { - //in this case the data router information gets added to the params so for now leave it alone - String config = p.getConfig_key(); - DmaapObj pub = new DmaapObj(); - String name = p.getConfig_key() +"_feed"; - retInputs = pub.createOnapDmaapDRObj(retInputs, config, 'p', name, name, isDmaap); - pub.setType(p.getType()); - streamPublishes.put(config, pub); - } else if(p.getType().equals("message_router") || p.getType().equals("message router")) { - String config = p.getConfig_key(); - DmaapObj pub = new DmaapObj(); - String name = p.getConfig_key() + "_topic"; - retInputs = pub.createOnapDmaapMRObj(retInputs, config, 'p', name, name, isDmaap); - pub.setType(p.getType()); - streamPublishes.put(config, pub); - } - } - } - - //set the stream publishes - TreeMap streamSubscribes = new TreeMap<>(); - - if(cs.getStreams().getSubscribes().length != 0) { - for(Subscribes s: cs.getStreams().getSubscribes()) { - if(s.getType().equals("data_router") || s.getType().equals("data router")) { - //in this case the data router information gets added to the params so for now leave it alone - String config = s.getConfig_key(); - DmaapObj sub = new DmaapObj(); - String name = s.getConfig_key() + "_feed"; - retInputs = sub.createOnapDmaapDRObj(retInputs, config, 'p', name, name, isDmaap); - sub.setType(s.getType()); - streamSubscribes.put(config, sub); - } else if(s.getType().equals("message_router") || s.getType().equals("message router")) { - String config = s.getConfig_key(); - DmaapObj sub = new DmaapObj(); - String name = s.getConfig_key() + "_topic"; - retInputs = sub.createOnapDmaapMRObj(retInputs, config, 's', name, name, isDmaap); - sub.setType(s.getType()); - streamSubscribes.put(config, sub); - } - } - } - - this.setStreams_publishes(streamPublishes); - this.setStreams_subscribes(streamSubscribes); - - //set the parameters into the appconfig - TreeMap parameters = new TreeMap<>(); - for(Parameters p: cs.getParameters()) { - String pName = p.getName(); - if(p.isSourced_at_deployment()) { - GetInput paramInput = new GetInput(); - paramInput.setBpInputName(pName); - parameters.put(pName, paramInput); - - if(!p.getValue().equals("")) { - LinkedHashMap inputs = new LinkedHashMap<>(); - inputs.put("type", "string"); - inputs.put("default", p.getValue()); - retInputs.put(pName, inputs); - } else { - LinkedHashMap inputs = new LinkedHashMap<>(); - inputs.put("type", "string"); - retInputs.put(pName, inputs); - } - } else { - if("string".equals(p.getType())) { - String val =(String) p.getValue(); - val = '"' + val + '"'; - parameters.put(pName, val); - } - else { - parameters.put(pName, p.getValue()); - } - } - } - if(override != null) { - GetInput ov = new GetInput(); - ov.setBpInputName("service_component_name_override"); - parameters.put("service_component_name_override", ov); - LinkedHashMap over = new LinkedHashMap<>(); - over.put("type", "string"); - over.put("default", override); - retInputs.put("service_component_name_override", over); - } - this.setParams(parameters); - return retInputs; - } + private CallsObj[] service_calls; + private TreeMap streams_publishes; + private TreeMap streams_subscribes; + private TreeMap params; + + @JsonAnyGetter + public TreeMap getParams() { + return params; + } + + public TreeMap> createAppconfig( + TreeMap> inps, ComponentSpec componentSpec, String override, + boolean isDmaap) { + + service_calls = new CallsObj[0]; + streams_publishes = createStreamPublishes(componentSpec, inps, isDmaap); + streams_subscribes = createStreamSubscribes(componentSpec, inps, isDmaap); + params = createParameters(componentSpec, inps, override); + + return inps; + } + + private TreeMap createStreamPublishes(ComponentSpec componentSpec, + TreeMap> inps, boolean isDmaap) { + TreeMap streamPublishes = new TreeMap<>(); + for (Publishes publishes : componentSpec.getStreams().getPublishes()) { + String config = publishes.getConfig_key(); + DmaapObj pub = new DmaapObj(); + if (isDataRouterType(publishes.getType())) { + //in this case the data router information gets added to the params so for now leave it alone + String name = publishes.getConfig_key() + "_feed"; + pub.createOnapDmaapDRObj(inps, config, 'p', name, name, isDmaap); + } else if (isMessageRouterType(publishes.getType())) { + String name = publishes.getConfig_key() + "_topic"; + pub.createOnapDmaapMRObj(inps, config, 'p', name, name, isDmaap); + } + pub.setType(publishes.getType()); + streamPublishes.put(config, pub); + } + return streamPublishes; + } + + private TreeMap createStreamSubscribes(ComponentSpec componentSpec, + TreeMap> inputs, boolean isDmaap) { + TreeMap streamSubscribes = new TreeMap<>(); + for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) { + String config = subscribes.getConfig_key(); + DmaapObj sub = new DmaapObj(); + if (isDataRouterType(subscribes.getType())) { + //in this case the data router information gets added to the params so for now leave it alone + String name = subscribes.getConfig_key() + "_feed"; + sub.createOnapDmaapDRObj(inputs, config, 'p', name, name, isDmaap); + } else if (isMessageRouterType(subscribes.getType())) { + String name = subscribes.getConfig_key() + "_topic"; + sub.createOnapDmaapMRObj(inputs, config, 's', name, name, isDmaap); + } + sub.setType(subscribes.getType()); + streamSubscribes.put(config, sub); + } + return streamSubscribes; + } + + private TreeMap createParameters(ComponentSpec componentSpec, + TreeMap> inputs, + String override) { + TreeMap parameters = new TreeMap<>(); + for (Parameters params : componentSpec.getParameters()) { + String pName = params.getName(); + if (params.isSourced_at_deployment()) { + GetInput paramInput = new GetInput(); + paramInput.setBpInputName(pName); + parameters.put(pName, paramInput); + + if (!params.getValue().equals("")) { + LinkedHashMap input = createStringInput(params.getValue()); + inputs.put(pName, input); + } else { + LinkedHashMap input = new LinkedHashMap<>(); + input.put("type", "string"); + inputs.put(pName, input); + } + } else { + if ("string".equals(params.getType())) { + String val = (String) params.getValue(); + val = '"' + val + '"'; + parameters.put(pName, val); + } else { + parameters.put(pName, params.getValue()); + } + } + } + handleOverride(override, parameters, inputs); + return parameters; + } + + private void handleOverride(String override, TreeMap parameters, + TreeMap> inps) { + if (override != null) { + GetInput ov = new GetInput(); + ov.setBpInputName("service_component_name_override"); + parameters.put("service_component_name_override", ov); + LinkedHashMap over = new LinkedHashMap<>(); + over.put("type", "string"); + over.put("default", override); + inps.put("service_component_name_override", over); + } + } } diff --git a/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/AppconfigTest.java b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/AppconfigTest.java new file mode 100644 index 0000000..173d230 --- /dev/null +++ b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/AppconfigTest.java @@ -0,0 +1,62 @@ +/*============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2020 Nokia. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ============LICENSE_END========================================================= + */ + +package org.onap.blueprintgenerator.models.blueprint; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; +import java.util.TreeMap; +import java.util.stream.Collectors; +import org.junit.Test; +import org.onap.blueprintgenerator.models.componentspec.ComponentSpec; + +public class AppconfigTest { + + @Test + public void createAppconfigShouldReturnExpectedResult() throws FileNotFoundException { + TreeMap> inputs = new TreeMap>(); + ComponentSpec cs = new ComponentSpec(); + cs.createComponentSpecFromFile("TestCases/testComponentSpec.json"); + + TreeMap> result = new Appconfig().createAppconfig(inputs, cs, "", false); + + assertEquals(getExpectedStringFromFile(), result.toString()); + } + + private String getExpectedStringFromFile() throws FileNotFoundException { + File file = new File("TestCases/expects/createAppConfigResult.txt"); + InputStream inputStream = new FileInputStream(file); + return readFromInputStream(inputStream); + } + + private String readFromInputStream(InputStream inputStream) { + return new BufferedReader( + new InputStreamReader(inputStream, StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining("")); + } +} diff --git a/mod/bpgenerator/version.properties b/mod/bpgenerator/version.properties index 3f9d877..eabef1f 100644 --- a/mod/bpgenerator/version.properties +++ b/mod/bpgenerator/version.properties @@ -1,6 +1,6 @@ major=1 -minor=5 -patch=2 +minor=6 +patch=0 base_version=${major}.${minor}.${patch} release_version=${base_version} snapshot_version=${base_version}-SNAPSHOT -- cgit 1.2.3-korg