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