99627f67b9661657be6659018a0dc2e1feccf799
[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.junit.Assert.assertTrue;
27
28 import com.att.research.xacml.api.XACML3;
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Modifier;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
41 import org.junit.Test;
42
43 public class ToscaPolicyTranslatorUtilsTest {
44     private static final ObjectFactory factory = new ObjectFactory();
45
46     @Test
47     public void test() throws NoSuchMethodException, SecurityException {
48         final Constructor<ToscaPolicyTranslatorUtils> constructor
49             = ToscaPolicyTranslatorUtils.class.getDeclaredConstructor();
50         assertTrue(Modifier.isPrivate(constructor.getModifiers()));
51
52     }
53
54     @Test
55     public void testTimeInRange() {
56         ApplyType apply = ToscaPolicyTranslatorUtils.generateTimeInRange("00:00:00Z", "08:00:00Z", true);
57         assertThat(apply).isNotNull();
58         assertThat(apply.getExpression()).hasSize(3);
59     }
60
61     @Test
62     public void testBuildAndAppend() {
63         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new MatchType())).isInstanceOf(AnyOfType.class);
64         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new AllOfType())).isInstanceOf(AnyOfType.class);
65         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendAllof(null, new String())).isNull();
66
67         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
68                 new AnyOfType()).getAnyOf()).hasSize(1);
69         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
70                 new MatchType()).getAnyOf()).hasSize(1);
71         assertThat(ToscaPolicyTranslatorUtils.buildAndAppendTarget(new TargetType(),
72                 new String()).getAnyOf()).isEmpty();
73     }
74
75     @Test
76     public void testInteger() {
77         assertThat(ToscaPolicyTranslatorUtils.parseInteger("foo")).isNull();
78         assertThat(ToscaPolicyTranslatorUtils.parseInteger("1")).isEqualTo(1);
79         assertThat(ToscaPolicyTranslatorUtils.parseInteger("1.0")).isEqualTo(1);
80     }
81
82     @Test
83     public void testAddingVariables() {
84         ApplyType applyType = new ApplyType();
85         applyType.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
86
87         AttributeValueType value = new AttributeValueType();
88         value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
89         value.getContent().add("1");
90         applyType.getExpression().add(factory.createAttributeValue(value));
91
92         AttributeDesignatorType designator = new AttributeDesignatorType();
93         designator.setAttributeId(XACML3.ID_RESOURCE.stringValue());
94         designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
95         designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
96         applyType.getExpression().add(factory.createAttributeDesignator(designator));
97
98         ConditionType condition = new ConditionType();
99         condition.setExpression(factory.createApply(applyType));
100
101         VariableReferenceType variable = new VariableReferenceType();
102
103         variable.setVariableId("my-variable-id");
104
105         ConditionType newCondition = ToscaPolicyTranslatorUtils.addVariableToCondition(condition, variable,
106                 XACML3.ID_FUNCTION_AND);
107
108         assertThat(newCondition.getExpression().getValue()).isInstanceOf(ApplyType.class);
109         Object obj = newCondition.getExpression().getValue();
110         assertThat(((ApplyType) obj).getFunctionId()).isEqualTo(XACML3.ID_FUNCTION_AND.stringValue());
111         assertThat(((ApplyType) obj).getExpression()).hasSize(2);
112     }
113 }