e684cde4343d72c425503b2aa5871fc9a000e0ed
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / ToscaPolicyTranslatorUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertTrue;
28
29 import com.att.research.xacml.api.XACML3;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.Modifier;
32 import java.util.Map;
33 import lombok.Getter;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
44 import org.junit.Test;
45 import org.onap.policy.common.parameters.annotations.NotNull;
46 import org.onap.policy.common.utils.coder.CoderException;
47
48 public class ToscaPolicyTranslatorUtilsTest {
49     private static final ObjectFactory factory = new ObjectFactory();
50
51     @Test
52     public void test() throws NoSuchMethodException, SecurityException {
53         final Constructor<ToscaPolicyTranslatorUtils> constructor
54             = ToscaPolicyTranslatorUtils.class.getDeclaredConstructor();
55         assertTrue(Modifier.isPrivate(constructor.getModifiers()));
56
57     }
58
59     @Test
60     public void testTimeInRange() {
61         ApplyType apply = ToscaPolicyTranslatorUtils.generateTimeInRange("00:00:00Z", "08:00:00Z", true);
62         assertThat(apply).isNotNull();
63         assertThat(apply.getExpression()).hasSize(3);
64     }
65
66     @Test
67     public void testBuildAndAppend() {
68         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new MatchType())).isInstanceOf(AnyOfType.class);
69         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new AllOfType())).isInstanceOf(AnyOfType.class);
70         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new String())).isNull();
71
72         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
73                 new AnyOfType()).getAnyOf()).hasSize(1);
74         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
75                 new MatchType()).getAnyOf()).hasSize(1);
76         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
77                 new String()).getAnyOf()).isEmpty();
78     }
79
80     @Test
81     public void testInteger() {
82         assertThat(ToscaPolicyTranslatorUtils.parseInteger("foo")).isNull();
83         assertThat(ToscaPolicyTranslatorUtils.parseInteger("1")).isEqualTo(1);
84         assertThat(ToscaPolicyTranslatorUtils.parseInteger("1.0")).isEqualTo(1);
85     }
86
87     @Test
88     public void testAddingVariables() {
89         ApplyType applyType = new ApplyType();
90         applyType.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
91
92         AttributeValueType value = new AttributeValueType();
93         value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
94         value.getContent().add("1");
95         applyType.getExpression().add(factory.createAttributeValue(value));
96
97         AttributeDesignatorType designator = new AttributeDesignatorType();
98         designator.setAttributeId(XACML3.ID_RESOURCE.stringValue());
99         designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
100         designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
101         applyType.getExpression().add(factory.createAttributeDesignator(designator));
102
103         ConditionType condition = new ConditionType();
104         condition.setExpression(factory.createApply(applyType));
105
106         VariableReferenceType variable = new VariableReferenceType();
107
108         variable.setVariableId("my-variable-id");
109
110         ConditionType newCondition = ToscaPolicyTranslatorUtils.addVariableToCondition(condition, variable,
111                 XACML3.ID_FUNCTION_AND);
112
113         assertThat(newCondition.getExpression().getValue()).isInstanceOf(ApplyType.class);
114         Object obj = newCondition.getExpression().getValue();
115         assertThat(((ApplyType) obj).getFunctionId()).isEqualTo(XACML3.ID_FUNCTION_AND.stringValue());
116         assertThat(((ApplyType) obj).getExpression()).hasSize(2);
117     }
118
119     @Test
120     public void testDecodeProperties() throws ToscaPolicyConversionException {
121         Data data = ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", 20), Data.class);
122         assertThat(data.getValue()).isEqualTo(20);
123
124         // null value - invalid
125         assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of(), Data.class))
126                         .isInstanceOf(ToscaPolicyConversionException.class).hasMessageContaining("item \"value\"");
127
128         // value is not an integer - cannot even decode it
129         assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", "abc"), Data.class))
130                         .isInstanceOf(ToscaPolicyConversionException.class).getCause()
131                         .isInstanceOf(CoderException.class);
132
133         // null properties - cannot even decode
134         assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(null, Data.class))
135                         .isInstanceOf(ToscaPolicyConversionException.class)
136                         .hasMessage("Cannot decode Data from null properties");
137     }
138
139     @Getter
140     @NotNull
141     public static class Data {
142         private Integer value;
143     }
144 }