summaryrefslogtreecommitdiffstats
path: root/applications/common/src/test/java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-05-27 15:10:18 -0400
committerJim Hahn <jrh3@att.com>2021-05-27 17:09:00 -0400
commitae9007f3554ba021d76d001ca555a13d49babc8d (patch)
treeddf359c36bfd60f49e55e25f91788c04ec99726d /applications/common/src/test/java
parentd1fa4ea52b243f60047a0bad5e63e947572b036b (diff)
Replace validation code with annotations
Instead of having code to validate various values, created POJOs to represent the decoded data so that bean validation annotations could be used instead. Didn't see any obvious ways to use annotations in the Optimization code, but did notice a bug (passed role instead of provisions). Extracted a common method which fixed the bug as a side-effect. Issue-ID: POLICY-2418 Change-Id: I9ef589086fc8f7f66810b66405fbf302d7570e5a Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'applications/common/src/test/java')
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslatorUtilsTest.java31
1 files changed, 31 insertions, 0 deletions
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;
+ }
}