From 1902963bbc9d35fc16562b815b8a3f1950b048c9 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 8 Jan 2020 17:04:50 +0000 Subject: Update tosca utils to remove code duplication Refactored the ToscaUtils class to remove code duplication. Issue-ID: POLICY-2315 Change-Id: Ic06a0881d1797dbb3c45ae1903a150fbda421e72 Signed-off-by: liamfallon --- .../onap/policy/models/tosca/utils/ToscaUtils.java | 123 ++++++++++++--------- 1 file changed, 70 insertions(+), 53 deletions(-) (limited to 'models-tosca/src/main') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index 71158f0d1..5ba42fef5 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -48,11 +48,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing data types to be checked */ public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - String message = checkDataTypesExist(serviceTemplate); - if (message != null) { - LOGGER.warn(message); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); - } + assertExist(serviceTemplate, checkDataTypesExist()); } /** @@ -61,11 +57,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - String message = checkPolicyTypesExist(serviceTemplate); - if (message != null) { - LOGGER.warn(message); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); - } + assertExist(serviceTemplate, checkPolicyTypesExist()); } /** @@ -74,11 +66,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { - String message = checkPoliciesExist(serviceTemplate); - if (message != null) { - LOGGER.warn(message); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); - } + assertExist(serviceTemplate, checkPoliciesExist()); } /** @@ -87,7 +75,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing data types to be checked */ public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - return checkDataTypesExist(serviceTemplate) == null; + return doExist(serviceTemplate, checkDataTypesExist()); } /** @@ -96,7 +84,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - return checkPolicyTypesExist(serviceTemplate) == null; + return doExist(serviceTemplate, checkPolicyTypesExist()); } /** @@ -105,61 +93,90 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { - return checkPoliciesExist(serviceTemplate) == null; + return doExist(serviceTemplate, checkPoliciesExist()); } /** - * Check if data types have been specified correctly. + * Assert that something have been specified correctly. * - * @param serviceTemplate the service template containing data types to be checked + * @param serviceTemplate the service template containing policy types to be checked */ - public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - if (serviceTemplate.getDataTypes() == null) { - return "no data types specified on service template"; - } - - if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) { - return "list of data types specified on service template is empty"; + public static void assertExist(final JpaToscaServiceTemplate serviceTemplate, + final ToscaChecker checkerFunction) { + String message = checkerFunction.check(serviceTemplate); + if (message != null) { + LOGGER.warn(message); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); } - - return null; } /** - * Check if policy types have been specified correctly. + * Check that something have been specified correctly. * * @param serviceTemplate the service template containing policy types to be checked */ - public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { - if (serviceTemplate.getPolicyTypes() == null) { - return "no policy types specified on service template"; - } + public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate, + final ToscaChecker checkerFunction) { + return checkerFunction.check(serviceTemplate) == null; + } - if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { - return "list of policy types specified on service template is empty"; - } + /** + * Check if data types have been specified correctly. + */ + public static ToscaChecker checkDataTypesExist() { + return serviceTemplate -> { + if (serviceTemplate.getDataTypes() == null) { + return "no data types specified on service template"; + } + + if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) { + return "list of data types specified on service template is empty"; + } + + return null; + }; + } - return null; + /** + * Check if policy types have been specified correctly. + */ + public static ToscaChecker checkPolicyTypesExist() { + return serviceTemplate -> { + if (serviceTemplate.getPolicyTypes() == null) { + return "no policy types specified on service template"; + } + + if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { + return "list of policy types specified on service template is empty"; + } + + return null; + }; } /** * Check if policies have been specified correctly. - * - * @param serviceTemplate the service template containing policy types to be checked */ - public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { - if (serviceTemplate.getTopologyTemplate() == null) { - return "topology template not specified on service template"; - } - - if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { - return "no policies specified on topology template of service template"; - } - - if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { - return "list of policies specified on topology template of service template is empty"; - } + public static ToscaChecker checkPoliciesExist() { + return serviceTemplate -> { + if (serviceTemplate.getTopologyTemplate() == null) { + return "topology template not specified on service template"; + } + + if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { + return "no policies specified on topology template of service template"; + } + + if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { + return "list of policies specified on topology template of service template is empty"; + } + + return null; + }; + } - return null; + @FunctionalInterface + interface ToscaChecker { + String check(final T serviceTemplate); } } -- cgit 1.2.3-korg