diff options
author | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2020-10-02 14:56:55 +0200 |
---|---|---|
committer | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2020-10-05 15:30:50 +0200 |
commit | 4f7d736f2e4997d13727bffabc982abf1a2522d8 (patch) | |
tree | 12d1124d81946381879d1ff889b6ccd396f829dd /mod/bpgenerator/src | |
parent | e4668da0683be7194f45cd8d259eeb75ebdb98a8 (diff) |
Improve code quality in ResourceConfig
- Add unit test
- Refactor ResourceConfig class (break the public method into smaller private methods, remove code duplications)
Issue-ID: DCAEGEN2-2460
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Change-Id: Ibfbac2b6d6ea4705a2139dee7d1bb801395790c9
Diffstat (limited to 'mod/bpgenerator/src')
3 files changed, 68 insertions, 39 deletions
diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/BpConstants.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/BpConstants.java index ac14b47..d0d79a4 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/BpConstants.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/BpConstants.java @@ -37,4 +37,7 @@ public final class BpConstants { public static final String TOSCA_DATATYPES_ROOT = "tosca.datatypes.Root"; public static final String TOSCA_NODES_ROOT = "tosca.nodes.Root"; public static final String TOSCA_SIMPLE_YAML = "tosca_simple_yaml_1_0_0"; + + public static final String MEMORY_LIMIT = "128Mi"; + public static final String CPU_LIMIT = "250m"; } diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java index 030374f..3618be3 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java @@ -22,10 +22,11 @@ package org.onap.blueprintgenerator.models.blueprint; import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput; +import static org.onap.blueprintgenerator.models.blueprint.BpConstants.CPU_LIMIT; +import static org.onap.blueprintgenerator.models.blueprint.BpConstants.MEMORY_LIMIT; import java.util.LinkedHashMap; import java.util.TreeMap; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -59,69 +60,56 @@ import lombok.Setter; public class ResourceConfig { - /** - * The limits. - */ private TreeMap<String, GetInput> limits; - - /** - * The requests. - */ private TreeMap<String, GetInput> requests; /** * Creates the resource config. * - * @param inps the inps + * @param inputs the inputs * @param name the name * @return the tree map */ public TreeMap<String, LinkedHashMap<String, Object>> createResourceConfig( - TreeMap<String, LinkedHashMap<String, Object>> inps, String name) { - - LinkedHashMap<String, Object> memoryLimit = createStringInput("128Mi"); - LinkedHashMap<String, Object> cpuLimit = createStringInput("250m"); + TreeMap<String, LinkedHashMap<String, Object>> inputs, String name) { String namePrefix = getNamePrefix(name); - //set the limits - TreeMap<String, GetInput> limits = new TreeMap<>(); + limits = createInputs(inputs, namePrefix, "limit"); + requests = createInputs(inputs, namePrefix, "request"); - GetInput cpu = new GetInput(); - cpu.setBpInputName(namePrefix + "cpu_limit"); - limits.put("cpu", cpu); - - GetInput memL = new GetInput(); - memL.setBpInputName(namePrefix + "memory_limit"); - limits.put("memory", memL); - - inps.put(namePrefix + "cpu_limit", cpuLimit); - inps.put(namePrefix + "memory_limit", memoryLimit); - - this.setLimits(limits); + return inputs; + } - //set the requests - TreeMap<String, GetInput> requests = new TreeMap<>(); + private TreeMap<String, GetInput> createInputs(TreeMap<String, LinkedHashMap<String, Object>> inputs, + String namePrefix, + String inputType) { - GetInput cpuR = new GetInput(); - cpuR.setBpInputName(namePrefix + "cpu_request"); - requests.put("cpu", cpuR); + LinkedHashMap<String, Object> memoryLimit = createStringInput(MEMORY_LIMIT); + LinkedHashMap<String, Object> cpuLimit = createStringInput(CPU_LIMIT); - GetInput memR = new GetInput(); - memR.setBpInputName(namePrefix + "memory_request"); - requests.put("memory", memR); + final String cpuKey = namePrefix + "cpu_" + inputType; + final String memoryKey = namePrefix + "memory_" + inputType; + TreeMap<String, GetInput> inps = new TreeMap<>(); - inps.put(namePrefix + "cpu_request", cpuLimit); - inps.put(namePrefix + "memory_request", memoryLimit); + insertInput("cpu", cpuKey, inps); + insertInput("memory", memoryKey, inps); - this.setRequests(requests); + inputs.put(cpuKey, cpuLimit); + inputs.put(memoryKey, memoryLimit); return inps; } + private void insertInput(String type, String name, TreeMap<String, GetInput> inputs) { + GetInput input = new GetInput(); + input.setBpInputName(name); + inputs.put(type, input); + } + private String getNamePrefix(String name) { - return name.isEmpty() ? "" : name + "_"; + return (name == null || name.isEmpty()) ? "" : name + "_"; } } diff --git a/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfigTest.java b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfigTest.java new file mode 100644 index 0000000..cf12da1 --- /dev/null +++ b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfigTest.java @@ -0,0 +1,38 @@ +/*============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.util.LinkedHashMap; +import java.util.TreeMap; +import org.junit.Test; + +public class ResourceConfigTest { + + @Test + public void createResourceConfig() { + TreeMap<String, LinkedHashMap<String, Object>> result = new ResourceConfig() + .createResourceConfig(new TreeMap<>(), "demo"); + + String expectedResult = "{demo_cpu_limit={type=string, default=250m}, demo_cpu_request={type=string, default=250m}, demo_memory_limit={type=string, default=128Mi}, demo_memory_request={type=string, default=128Mi}}"; + assertEquals(expectedResult, result.toString()); + } +} |