summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-01-08 17:04:50 +0000
committerliamfallon <liam.fallon@est.tech>2020-01-08 17:04:57 +0000
commit1902963bbc9d35fc16562b815b8a3f1950b048c9 (patch)
tree711e308b9a032cba40704ec329a0b4a4ff47f3b3
parent2ed313e69cfd0a587f072079e23ab8f6499518db (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>
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java123
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java20
2 files changed, 80 insertions, 63 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);
}
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
index 82636940c..ebe01a65c 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
@@ -47,7 +47,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
assertEquals("no data types specified on service template",
- ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkDataTypesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
}).hasMessage("no data types specified on service template");
@@ -56,7 +56,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
assertEquals("list of data types specified on service template is empty",
- ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkDataTypesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
}).hasMessage("list of data types specified on service template is empty");
@@ -64,7 +64,7 @@ public class ToscaUtilsTest {
jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
- assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
+ assertEquals(null, ToscaUtils.checkDataTypesExist().check(jpaToscaServiceTemplate));
assertThatCode(() -> {
ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
}).doesNotThrowAnyException();
@@ -77,7 +77,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
assertEquals("no policy types specified on service template",
- ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkPolicyTypesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
}).hasMessage("no policy types specified on service template");
@@ -86,7 +86,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
assertEquals("list of policy types specified on service template is empty",
- ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkPolicyTypesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
}).hasMessage("list of policy types specified on service template is empty");
@@ -94,7 +94,7 @@ public class ToscaUtilsTest {
jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
- assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
+ assertEquals(null, ToscaUtils.checkPolicyTypesExist().check(jpaToscaServiceTemplate));
assertThatCode(() -> {
ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
}).doesNotThrowAnyException();
@@ -106,7 +106,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
assertEquals("topology template not specified on service template",
- ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkPoliciesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
}).hasMessage("topology template not specified on service template");
@@ -115,7 +115,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
assertEquals("no policies specified on topology template of service template",
- ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkPoliciesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
}).hasMessage("no policies specified on topology template of service template");
@@ -124,7 +124,7 @@ public class ToscaUtilsTest {
assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
assertEquals("list of policies specified on topology template of service template is empty",
- ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
+ ToscaUtils.checkPoliciesExist().check(jpaToscaServiceTemplate));
assertThatThrownBy(() -> {
ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
}).hasMessage("list of policies specified on topology template of service template is empty");
@@ -132,7 +132,7 @@ public class ToscaUtilsTest {
jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
- assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
+ assertEquals(null, ToscaUtils.checkPoliciesExist().check(jpaToscaServiceTemplate));
assertThatCode(() -> {
ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
}).doesNotThrowAnyException();