2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.controlloop.utils;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertSame;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.AbstractSet;
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.Properties;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 public class ControlLoopUtilsTest {
45 public void testToControlLoopParams() throws Exception {
46 String policy = Files.readString(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json"));
47 ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
49 ControlLoopParams params = ControlLoopUtils.toControlLoopParams(toscaPolicy);
50 assertEquals("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e", params.getClosedLoopControlName());
51 assertEquals(toscaPolicy.getName(), params.getPolicyName());
52 assertEquals(toscaPolicy.getVersion(), params.getPolicyVersion());
53 assertEquals(toscaPolicy.getType() + ":" + toscaPolicy.getVersion(), params.getPolicyScope());
54 assertSame(toscaPolicy, params.getToscaPolicy());
56 assertNull(ControlLoopUtils.toControlLoopParams(null));
60 public void testToObject() {
61 Map<String, String> map = Map.of("abc", "def", "ghi", "jkl");
62 Properties props = new Properties();
66 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
67 assertEquals(map, result);
69 // with dotted prefix - other items skipped
70 map = Map.of("pfx.abc", "def", "ghi", "jkl", "pfx.mno", "pqr", "differentpfx.stu", "vwx");
72 props.putAll(Map.of("pfx.abc", "def", "ghi", "jkl", "pfx.mno", "pqr", "differentpfx.stu", "vwx"));
73 result = ControlLoopUtils.toObject(props, "pfx.");
74 map = Map.of("abc", "def", "mno", "pqr");
75 assertEquals(map, result);
77 // undotted prefix - still skips other items
78 result = ControlLoopUtils.toObject(props, "pfx");
79 assertEquals(map, result);
83 public void testSetProperty() {
84 // one, two, and three components in the name, the last two with subscripts
85 Map<String, Object> map = Map.of("one", "one.abc", "two.def", "two.ghi", "three.jkl.mno[0]", "three.pqr",
86 "three.jkl.mno[1]", "three.stu");
87 Properties props = new Properties();
90 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
94 "two", Map.of("def", "two.ghi"),
95 "three", Map.of("jkl",
97 List.of("three.pqr", "three.stu"))));
99 assertEquals(map, result);
103 public void testGetNode() {
104 Map<String, Object> map = Map.of("abc[0].def", "node.ghi", "abc[0].jkl", "node.mno", "abc[1].def", "node.pqr");
105 Properties props = new Properties();
108 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
113 Map.of("def", "node.ghi", "jkl", "node.mno"),
114 Map.of("def", "node.pqr")
117 assertEquals(map, result);
122 public void testExpand() {
123 // add subscripts out of order
124 Properties props = makeProperties("abc[2]", "expand.def", "abc[1]", "expand.ghi");
126 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
128 Map<String,Object> map =
130 Arrays.asList(null, "expand.ghi", "expand.def"));
132 assertEquals(map, result);
137 public void testGetObject() {
138 // first value is primitive, while second is a map
139 Properties props = makeProperties("object.abc", "object.def", "object.abc.ghi", "object.jkl");
141 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
143 Map<String,Object> map =
146 Map.of("ghi", "object.jkl")));
148 assertEquals(map, result);
152 public void testGetArray() {
153 // first value is primitive, while second is an array
154 Properties props = makeProperties("array.abc", "array.def", "array.abc[0].ghi", "array.jkl");
156 Map<String, Object> result = ControlLoopUtils.toObject(props, "");
158 Map<String,Object> map =
162 Map.of("ghi", "array.jkl"))));
164 assertEquals(map, result);
168 @SuppressWarnings("unchecked")
169 public void testCompressLists() throws IOException, CoderException {
170 assertEquals("plain-string", ControlLoopUtils.compressLists("plain-string").toString());
173 Map<String, Object> map =
175 "cmp.abc", "cmp.def",
177 Arrays.asList(null, "cmp.list1", null, "cmp.list2",
178 Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2", null))));
181 // the data structure needs to be modifiable, so we'll encode/decode it
182 StandardCoder coder = new StandardCoder();
183 map = coder.decode(coder.encode(map), LinkedHashMap.class);
185 ControlLoopUtils.compressLists(map);
188 Map<String, Object> expected =
190 "cmp.abc", "cmp.def",
192 Arrays.asList("cmp.list1", "cmp.list2",
193 Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2"))));
195 assertEquals(expected, map);
199 * Makes properties containing the specified key/value pairs. The property set returns
200 * names in the order listed.
202 * @return a new properties containing the specified key/value pairs
204 private Properties makeProperties(String key1, String value1, String key2, String value2) {
205 // control the order in which the names are returned
206 List<String> keyList = List.of(key1, key2);
208 Set<String> keySet = new AbstractSet<>() {
210 public Iterator<String> iterator() {
211 return keyList.iterator();
220 Properties props = new Properties() {
221 private static final long serialVersionUID = 1L;
224 public Set<String> stringPropertyNames() {
229 props.putAll(Map.of(key1, value1, key2, value2));