2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.pdp.xacml.application.common;
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;
30 import com.att.research.xacml.api.XACML3;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Modifier;
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;
49 public class ToscaPolicyTranslatorUtilsTest {
50 private static final ObjectFactory factory = new ObjectFactory();
53 public void test() throws NoSuchMethodException, SecurityException {
54 final Constructor<ToscaPolicyTranslatorUtils> constructor
55 = ToscaPolicyTranslatorUtils.class.getDeclaredConstructor();
56 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
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);
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();
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();
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);
89 public void testAddingVariables() {
90 ApplyType applyType = new ApplyType();
91 applyType.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
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));
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));
104 ConditionType condition = new ConditionType();
105 condition.setExpression(factory.createApply(applyType));
107 VariableReferenceType variable = new VariableReferenceType();
109 variable.setVariableId("my-variable-id");
111 ConditionType newCondition = ToscaPolicyTranslatorUtils.addVariableToCondition(condition, variable,
112 XACML3.ID_FUNCTION_AND);
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);
120 @SuppressWarnings("deprecation")
122 public void testDecodeProperties() throws ToscaPolicyConversionException {
123 Data data = ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", 20), Data.class);
124 assertThat(data.getValue()).isEqualTo(20);
126 // null value - invalid
127 assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of(), Data.class))
128 .isInstanceOf(ToscaPolicyConversionException.class).hasMessageContaining("item \"value\"");
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);
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");
143 public static class Data {
144 private Integer value;