2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.xacml.application.common;
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;
29 import com.att.research.xacml.api.XACML3;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.Modifier;
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;
48 public class ToscaPolicyTranslatorUtilsTest {
49 private static final ObjectFactory factory = new ObjectFactory();
52 public void test() throws NoSuchMethodException, SecurityException {
53 final Constructor<ToscaPolicyTranslatorUtils> constructor
54 = ToscaPolicyTranslatorUtils.class.getDeclaredConstructor();
55 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
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);
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();
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();
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);
88 public void testAddingVariables() {
89 ApplyType applyType = new ApplyType();
90 applyType.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
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));
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));
103 ConditionType condition = new ConditionType();
104 condition.setExpression(factory.createApply(applyType));
106 VariableReferenceType variable = new VariableReferenceType();
108 variable.setVariableId("my-variable-id");
110 ConditionType newCondition = ToscaPolicyTranslatorUtils.addVariableToCondition(condition, variable,
111 XACML3.ID_FUNCTION_AND);
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);
120 public void testDecodeProperties() throws ToscaPolicyConversionException {
121 Data data = ToscaPolicyTranslatorUtils.decodeProperties(Map.of("value", 20), Data.class);
122 assertThat(data.getValue()).isEqualTo(20);
124 // null value - invalid
125 assertThatThrownBy(() -> ToscaPolicyTranslatorUtils.decodeProperties(Map.of(), Data.class))
126 .isInstanceOf(ToscaPolicyConversionException.class).hasMessageContaining("item \"value\"");
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);
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");
141 public static class Data {
142 private Integer value;