From 4f7d736f2e4997d13727bffabc982abf1a2522d8 Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Fri, 2 Oct 2020 14:56:55 +0200 Subject: 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 Change-Id: Ibfbac2b6d6ea4705a2139dee7d1bb801395790c9 --- .../models/blueprint/BpConstants.java | 3 + .../models/blueprint/ResourceConfig.java | 66 +++++++++------------- .../models/blueprint/ResourceConfigTest.java | 38 +++++++++++++ 3 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfigTest.java (limited to 'mod/bpgenerator') 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 limits; - - /** - * The requests. - */ private TreeMap requests; /** * Creates the resource config. * - * @param inps the inps + * @param inputs the inputs * @param name the name * @return the tree map */ public TreeMap> createResourceConfig( - TreeMap> inps, String name) { - - LinkedHashMap memoryLimit = createStringInput("128Mi"); - LinkedHashMap cpuLimit = createStringInput("250m"); + TreeMap> inputs, String name) { String namePrefix = getNamePrefix(name); - //set the limits - TreeMap 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 requests = new TreeMap<>(); + private TreeMap createInputs(TreeMap> inputs, + String namePrefix, + String inputType) { - GetInput cpuR = new GetInput(); - cpuR.setBpInputName(namePrefix + "cpu_request"); - requests.put("cpu", cpuR); + LinkedHashMap memoryLimit = createStringInput(MEMORY_LIMIT); + LinkedHashMap 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 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 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> 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()); + } +} -- cgit 1.2.3-korg