2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.ccsdk.apps.ms.neng.core.policy;
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;
30 import java.util.HashMap;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
37 @RunWith(MockitoJUnitRunner.class)
38 public class PropertyOperatorTest {
40 private PolicyParameters params = mock(PolicyParameters.class);
43 public void operationFunction() throws Exception {
44 assertEquals(null, PropertyOperator.operationFunction(""));
45 assertEquals(null, PropertyOperator.operationFunction(" "));
47 assertEquals(null, PropertyOperator.operationFunction(" (("));
49 assertEquals("to_lower_case", PropertyOperator.operationFunction("to_lower_case()"));
50 assertEquals("to_lower_case", PropertyOperator.operationFunction(" to_lower_case() "));
52 assertEquals("sub_str", PropertyOperator.operationFunction("sub_str(0,5)"));
53 assertEquals("sub_str", PropertyOperator.operationFunction("\tsub_str(0,5)"));
57 public void camelConverted() throws Exception {
58 assertEquals("", PropertyOperator.camelConverted(""));
60 assertEquals("toLowerCase", PropertyOperator.camelConverted("to_lower_case"));
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));
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));
80 public void testApply() throws Exception {
81 PropertyOperator op = new PropertyOperator();
82 assertNull("ASDF", op.apply("asdf", "", params));
86 public void applySubstr() throws Exception {
87 when(params.mapFunction("sub_str")).thenReturn("substring");
88 PropertyOperator op = new PropertyOperator();
90 Map<String, String> props = new HashMap<>();
92 props.put("property-operation", "sub_str(0,5)");
93 assertEquals("01234", op.apply("0123456789", props, params, null));
95 props.put("property-operation", " sub_str(0,4)");
96 assertEquals("0123", op.apply("0123456789", props, params, null));
98 props.put("property-operation", "sub_str(1,5)");
99 assertEquals("1234", op.apply("0123456789", props, params, null));
101 props.put("property-operation", "sub_str(1)");
102 assertEquals("0", op.apply("0", props, params, null));
104 props.put("property-operation", "sub_str(-2)");
105 assertEquals("89", op.apply("0123456789", props, params, null));
107 props.put("property-operation", "sub_str(-3)");
108 assertEquals("789", op.apply("0123456789", props, params, null));
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);