2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.pdp.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.math.BigInteger;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.junit.Test;
34 import com.att.research.xacml.api.XACML3;
35 import com.att.research.xacml.std.datatypes.DataTypes;
36 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
37 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
38 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
39 import com.att.research.xacmlatt.pdp.std.StdFunctions;
40 import com.att.research.xacmlatt.pdp.std.functions.*;
43 * Tests for various classes containing only one function.
46 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
50 public class FunctionDefinitionNumberTypeConversionTest {
53 * variables useful in the following tests
55 List<FunctionArgument> arguments = new ArrayList<FunctionArgument>();
58 public void testDouble_to_integer() {
59 FunctionArgumentAttributeValue attr1 = null;
61 attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(5.432));
63 } catch (Exception e) {
64 fail("creating attribute e="+ e);
67 FunctionDefinitionNumberTypeConversion<?, ?> fd = (FunctionDefinitionNumberTypeConversion<?, ?>) StdFunctions.FD_DOUBLE_TO_INTEGER;
69 // check identity and type of the thing created
70 assertEquals(XACML3.ID_FUNCTION_DOUBLE_TO_INTEGER, fd.getId());
71 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
72 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
74 // just to be safe... If tests take too long these can probably be eliminated
75 assertFalse(fd.returnsBag());
76 assertEquals(new Integer(1), fd.getNumArgs());
81 ExpressionResult res = fd.evaluate(null, arguments);
82 assertTrue(res.isOk());
83 BigInteger resValue = (BigInteger)res.getValue().getValue();
84 assertEquals(BigInteger.valueOf(5), resValue);
89 public void testInteger_to_double() {
90 FunctionArgumentAttributeValue attr1 = null;
92 attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
94 } catch (Exception e) {
95 fail("creating attribute e="+ e);
98 FunctionDefinitionNumberTypeConversion<?, ?> fd = (FunctionDefinitionNumberTypeConversion<?, ?>) StdFunctions.FD_INTEGER_TO_DOUBLE;
100 // check identity and type of the thing created
101 assertEquals(XACML3.ID_FUNCTION_INTEGER_TO_DOUBLE, fd.getId());
102 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
103 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
105 // just to be safe... If tests take too long these can probably be eliminated
106 assertFalse(fd.returnsBag());
107 assertEquals(new Integer(1), fd.getNumArgs());
111 arguments.add(attr1);
112 ExpressionResult res = fd.evaluate(null, arguments);
113 assertTrue(res.isOk());
114 Double resValue = (Double)res.getValue().getValue();
115 assertEquals(new Double(5.0), resValue);