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.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
38 @RunWith(MockitoJUnitRunner.class)
39 public class PropertyOperatorTest {
41 private PolicyParameters params = mock(PolicyParameters.class);
44 public void operationFunction() throws Exception {
45 assertEquals(null, PropertyOperator.operationFunction(""));
46 assertEquals(null, PropertyOperator.operationFunction(" "));
48 assertEquals(null, PropertyOperator.operationFunction(" (("));
50 assertEquals("to_lower_case", PropertyOperator.operationFunction("to_lower_case()"));
51 assertEquals("to_lower_case", PropertyOperator.operationFunction(" to_lower_case() "));
53 assertEquals("sub_str", PropertyOperator.operationFunction("sub_str(0,5)"));
54 assertEquals("sub_str", PropertyOperator.operationFunction("\tsub_str(0,5)"));
58 public void camelConverted() throws Exception {
59 assertEquals("", PropertyOperator.camelConverted(""));
61 assertEquals("toLowerCase", PropertyOperator.camelConverted("to_lower_case"));
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));
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));
81 public void testApply() throws Exception {
82 PropertyOperator op = new PropertyOperator();
83 assertNull("ASDF", op.apply("asdf", "", params));
87 public void applySubstr() throws Exception {
88 Mockito.lenient().when(params.mapFunction("sub_str")).thenReturn("substring");
89 PropertyOperator op = new PropertyOperator();
91 Map<String, String> props = new HashMap<>();
93 props.put("property-operation", "sub_str(0,5)");
94 assertEquals("01234", op.apply("0123456789", props, params, null));
96 props.put("property-operation", " sub_str(0,4)");
97 assertEquals("0123", op.apply("0123456789", props, params, null));
99 props.put("property-operation", "sub_str(1,5)");
100 assertEquals("1234", op.apply("0123456789", props, params, null));
102 props.put("property-operation", "sub_str(1)");
103 assertEquals("0", op.apply("0", props, params, null));
105 props.put("property-operation", "sub_str(-2)");
106 assertEquals("89", op.apply("0123456789", props, params, null));
108 props.put("property-operation", "sub_str(-3)");
109 assertEquals("789", op.apply("0123456789", props, params, null));
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);