diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-08 17:04:50 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-01-08 17:04:57 +0000 |
commit | 1902963bbc9d35fc16562b815b8a3f1950b048c9 (patch) | |
tree | 711e308b9a032cba40704ec329a0b4a4ff47f3b3 /models-tosca/src/main/java | |
parent | 2ed313e69cfd0a587f072079e23ab8f6499518db (diff) |
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 <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java')
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java | 123 |
1 files changed, 70 insertions, 53 deletions
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<JpaToscaServiceTemplate> 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<JpaToscaServiceTemplate> 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<JpaToscaServiceTemplate> 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<JpaToscaServiceTemplate> 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<JpaToscaServiceTemplate> 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<T> { + String check(final T serviceTemplate); } } |