summaryrefslogtreecommitdiffstats
path: root/applications/common
diff options
context:
space:
mode:
Diffstat (limited to 'applications/common')
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtils.java36
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java31
2 files changed, 67 insertions, 0 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtils.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtils.java
index e19130d3..e6824378 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtils.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtils.java
@@ -24,6 +24,7 @@ package org.onap.policy.pdp.xacml.application.common;
import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.XACML3;
+import java.util.Map;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
@@ -35,6 +36,10 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.BeanValidator;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
/**
* This class contains static methods of helper classes to convert TOSCA policies
@@ -45,6 +50,7 @@ import org.apache.commons.lang3.StringUtils;
*/
public final class ToscaPolicyTranslatorUtils {
private static final ObjectFactory factory = new ObjectFactory();
+ private static final StandardCoder CODER = new StandardCoder();
private ToscaPolicyTranslatorUtils() {
super();
@@ -240,4 +246,34 @@ public final class ToscaPolicyTranslatorUtils {
newCondition.setExpression(factory.createApply(applyFunction));
return newCondition;
}
+
+ /**
+ * Decodes TOSCA Policy properties into a particular type and validates the result.
+ *
+ * @param <T> desired type
+ * @param properties properties to be decoded
+ * @param clazz desired class
+ * @return the decoded properties
+ * @throws ToscaPolicyConversionException if the properties cannot be decoded or are
+ * invalid
+ */
+ public static <T> T decodeProperties(Map<String, Object> properties, Class<T> clazz)
+ throws ToscaPolicyConversionException {
+
+ if (properties == null) {
+ throw new ToscaPolicyConversionException(
+ "Cannot decode " + clazz.getSimpleName() + " from null properties");
+ }
+
+ try {
+ T data = CODER.convert(properties, clazz);
+ BeanValidationResult result = new BeanValidator().validateTop("properties", data);
+ if (!result.isValid()) {
+ throw new ToscaPolicyConversionException(result.getResult());
+ }
+ return data;
+ } catch (CoderException e) {
+ throw new ToscaPolicyConversionException("Cannot decode " + clazz.getSimpleName() + " from properties", e);
+ }
+ }
}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java
index 99627f67..e684cde4 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java
@@ -23,11 +23,14 @@
package org.onap.policy.pdp.xacml.application.common;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertTrue;
import com.att.research.xacml.api.XACML3;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
+import java.util.Map;
+import lombok.Getter;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
@@ -39,6 +42,8 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
import org.junit.Test;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.utils.coder.CoderException;
public class ToscaPolicyTranslatorUtilsTest {
private static final ObjectFactory factory = new ObjectFactory();
@@ -110,4 +115,30 @@ public class ToscaPolicyTranslatorUtilsTest {
assertThat(((ApplyType) obj).getFunctionId()).isEqualTo(XACML3.ID_FUNCTION_AND.stringValue());
assertThat(((ApplyType) obj).getExpression()).hasSize(2);
}
+
+ @Test
+ public void testDecodeProperties() throws ToscaPolicyConversionException {
+ Data data = ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", 20), Data.class);
+ assertThat(data.getValue()).isEqualTo(20);
+
+ // null value - invalid
+ assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of(), Data.class))
+ .isInstanceOf(ToscaPolicyConversionException.class).hasMessageContaining("item \"value\"");
+
+ // value is not an integer - cannot even decode it
+ assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", "abc"), Data.class))
+ .isInstanceOf(ToscaPolicyConversionException.class).getCause()
+ .isInstanceOf(CoderException.class);
+
+ // null properties - cannot even decode
+ assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(null, Data.class))
+ .isInstanceOf(ToscaPolicyConversionException.class)
+ .hasMessage("Cannot decode Data from null properties");
+ }
+
+ @Getter
+ @NotNull
+ public static class Data {
+ private Integer value;
+ }
}