Update third party versions in ccsdk/apps
[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.Mockito;
36 import org.mockito.junit.MockitoJUnitRunner;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class PropertyOperatorTest {
40     @Mock
41     private PolicyParameters params = mock(PolicyParameters.class);
42
43     @Test
44     public void operationFunction() throws Exception {
45         assertEquals(null, PropertyOperator.operationFunction(""));
46         assertEquals(null, PropertyOperator.operationFunction("  "));
47
48         assertEquals(null, PropertyOperator.operationFunction("  (("));
49
50         assertEquals("to_lower_case", PropertyOperator.operationFunction("to_lower_case()"));
51         assertEquals("to_lower_case", PropertyOperator.operationFunction("  to_lower_case() "));
52
53         assertEquals("sub_str", PropertyOperator.operationFunction("sub_str(0,5)"));
54         assertEquals("sub_str", PropertyOperator.operationFunction("\tsub_str(0,5)"));
55     }
56
57     @Test
58     public void camelConverted() throws Exception {
59         assertEquals("", PropertyOperator.camelConverted(""));
60
61         assertEquals("toLowerCase", PropertyOperator.camelConverted("to_lower_case"));
62     }
63
64     @Test
65     public void applyToLowerCase() throws Exception {
66         Map<String, String> props = new HashMap<>();
67         props.put("property-operation", "to_lower_case()");
68         PropertyOperator op = new PropertyOperator();
69         assertEquals("asdf", op.apply("ASDF", props, params, null));
70     }
71
72     @Test
73     public void applyToUpperCase() throws Exception {
74         Map<String, String> props = new HashMap<>();
75         props.put("property-operation", "to_upper_case()");
76         PropertyOperator op = new PropertyOperator();
77         assertEquals("ASDF", op.apply("asdf", props, params, null));
78     }
79     
80     @Test
81     public void testApply() throws Exception {
82         PropertyOperator op = new PropertyOperator();
83         assertNull("ASDF", op.apply("asdf", "", params));
84     }
85
86     @Test
87     public void applySubstr() throws Exception {
88         Mockito.lenient().when(params.mapFunction("sub_str")).thenReturn("substring");
89         PropertyOperator op = new PropertyOperator();
90
91         Map<String, String> props = new HashMap<>();
92
93         props.put("property-operation", "sub_str(0,5)");
94         assertEquals("01234", op.apply("0123456789", props, params, null));
95
96         props.put("property-operation", "    sub_str(0,4)");
97         assertEquals("0123", op.apply("0123456789", props, params, null));
98
99         props.put("property-operation", "sub_str(1,5)");
100         assertEquals("1234", op.apply("0123456789", props, params, null));
101
102         props.put("property-operation", "sub_str(1)");
103         assertEquals("0", op.apply("0", props, params, null));
104
105         props.put("property-operation", "sub_str(-2)");
106         assertEquals("89", op.apply("0123456789", props, params, null));
107
108         props.put("property-operation", "sub_str(-3)");
109         assertEquals("789", op.apply("0123456789", props, params, null));
110     }
111     
112     @Test
113     public void testApply_non_mapped() throws Exception {
114         String operation = "to_upper_case";
115         PolicyParameters policyParams = mock(PolicyParameters.class);
116         Mockito.lenient().when(policyParams.mapFunction("sub_str")).thenReturn("substring");
117         PropertyOperator op = new PropertyOperator();
118         String resp = op.apply("MyString", operation, policyParams);
119         assertEquals("MYSTRING", resp);
120     }
121 }