c3471564f4bd0e9fab614774de70a6e85f073cfc
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / core / policy / PropertyOperatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.apps.ms.neng.core.policy;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class PropertyOperatorTest {
39     @Mock
40     private PolicyParameters params = mock(PolicyParameters.class);
41
42     @Test
43     public void operationFunction() throws Exception {
44         assertEquals(null, PropertyOperator.operationFunction(""));
45         assertEquals(null, PropertyOperator.operationFunction("  "));
46
47         assertEquals(null, PropertyOperator.operationFunction("  (("));
48
49         assertEquals("to_lower_case", PropertyOperator.operationFunction("to_lower_case()"));
50         assertEquals("to_lower_case", PropertyOperator.operationFunction("  to_lower_case() "));
51
52         assertEquals("sub_str", PropertyOperator.operationFunction("sub_str(0,5)"));
53         assertEquals("sub_str", PropertyOperator.operationFunction("\tsub_str(0,5)"));
54     }
55
56     @Test
57     public void camelConverted() throws Exception {
58         assertEquals("", PropertyOperator.camelConverted(""));
59
60         assertEquals("toLowerCase", PropertyOperator.camelConverted("to_lower_case"));
61     }
62
63     @Test
64     public void applyToLowerCase() throws Exception {
65         Map<String, String> props = new HashMap<>();
66         props.put("property-operation", "to_lower_case()");
67         PropertyOperator op = new PropertyOperator();
68         assertEquals("asdf", op.apply("ASDF", props, params, null));
69     }
70
71     @Test
72     public void applyToUpperCase() throws Exception {
73         Map<String, String> props = new HashMap<>();
74         props.put("property-operation", "to_upper_case()");
75         PropertyOperator op = new PropertyOperator();
76         assertEquals("ASDF", op.apply("asdf", props, params, null));
77     }
78     
79     @Test
80     public void testApply() throws Exception {
81         PropertyOperator op = new PropertyOperator();
82         assertNull("ASDF", op.apply("asdf", "", params));
83     }
84
85     @Test
86     public void applySubstr() throws Exception {
87         when(params.mapFunction("sub_str")).thenReturn("substring");
88         PropertyOperator op = new PropertyOperator();
89
90         Map<String, String> props = new HashMap<>();
91
92         props.put("property-operation", "sub_str(0,5)");
93         assertEquals("01234", op.apply("0123456789", props, params, null));
94
95         props.put("property-operation", "    sub_str(0,4)");
96         assertEquals("0123", op.apply("0123456789", props, params, null));
97
98         props.put("property-operation", "sub_str(1,5)");
99         assertEquals("1234", op.apply("0123456789", props, params, null));
100
101         props.put("property-operation", "sub_str(1)");
102         assertEquals("0", op.apply("0", props, params, null));
103
104         props.put("property-operation", "sub_str(-2)");
105         assertEquals("89", op.apply("0123456789", props, params, null));
106
107         props.put("property-operation", "sub_str(-3)");
108         assertEquals("789", op.apply("0123456789", props, params, null));
109     }
110     
111     @Test
112     public void testApply_non_mapped() throws Exception {
113         String operation = "to_upper_case";
114         PolicyParameters policyParams = mock(PolicyParameters.class);
115         when(policyParams.mapFunction("sub_str")).thenReturn("substring");
116         PropertyOperator op = new PropertyOperator();
117         String resp = op.apply("MyString", operation, policyParams);
118         assertEquals("MYSTRING", resp);
119     }
120 }