2f14954ca16c3f671413b8f5e71f9e70d4fa2def
[policy/drools-applications.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.controlloop.utils;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertSame;
24
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;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.Set;
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;
41
42 public class ControlLoopUtilsTest {
43
44     @Test
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);
48
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());
55
56         assertNull(ControlLoopUtils.toControlLoopParams(null));
57     }
58
59     @Test
60     public void testToObject() {
61         Map<String, String> map = Map.of("abc", "def", "ghi", "jkl");
62         Properties props = new Properties();
63         props.putAll(map);
64
65         // with empty prefix
66         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
67         assertEquals(map, result);
68
69         // with dotted prefix - other items skipped
70         map = Map.of("pfx.abc", "def", "ghi", "jkl", "pfx.mno", "pqr", "differentpfx.stu", "vwx");
71         props.clear();
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);
76
77         // undotted prefix - still skips other items
78         result = ControlLoopUtils.toObject(props, "pfx");
79         assertEquals(map, result);
80     }
81
82     @Test
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();
88         props.putAll(map);
89
90         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
91         // @formatter:off
92         map = Map.of(
93                 "one", "one.abc",
94                 "two", Map.of("def", "two.ghi"),
95                 "three", Map.of("jkl",
96                             Map.of("mno",
97                                 List.of("three.pqr", "three.stu"))));
98         // @formatter:on
99         assertEquals(map, result);
100     }
101
102     @Test
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();
106         props.putAll(map);
107
108         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
109         // @formatter:off
110         map = Map.of(
111                 "abc",
112                     List.of(
113                         Map.of("def", "node.ghi", "jkl", "node.mno"),
114                         Map.of("def", "node.pqr")
115                     ));
116         // @formatter:on
117         assertEquals(map, result);
118
119     }
120
121     @Test
122     public void testExpand() {
123         // add subscripts out of order
124         Properties props = makeProperties("abc[2]", "expand.def", "abc[1]", "expand.ghi");
125
126         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
127         // @formatter:off
128         Map<String,Object> map =
129             Map.of("abc",
130                 Arrays.asList(null, "expand.ghi", "expand.def"));
131         // @formatter:on
132         assertEquals(map, result);
133
134     }
135
136     @Test
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");
140
141         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
142         // @formatter:off
143         Map<String,Object> map =
144             Map.of("object",
145                 Map.of("abc",
146                     Map.of("ghi", "object.jkl")));
147         // @formatter:on
148         assertEquals(map, result);
149     }
150
151     @Test
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");
155
156         Map<String, Object> result = ControlLoopUtils.toObject(props, "");
157         // @formatter:off
158         Map<String,Object> map =
159             Map.of("array",
160                 Map.of("abc",
161                     List.of(
162                         Map.of("ghi", "array.jkl"))));
163         // @formatter:on
164         assertEquals(map, result);
165     }
166
167     @Test
168     @SuppressWarnings("unchecked")
169     public void testCompressLists() throws IOException, CoderException {
170         assertEquals("plain-string", ControlLoopUtils.compressLists("plain-string").toString());
171
172         // @formatter:off
173         Map<String, Object> map =
174             Map.of(
175                 "cmp.abc", "cmp.def",
176                 "cmp.ghi",
177                     Arrays.asList(null, "cmp.list1", null, "cmp.list2",
178                         Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2", null))));
179         // @formatter:on
180
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);
184
185         ControlLoopUtils.compressLists(map);
186
187         // @formatter:off
188         Map<String, Object> expected =
189             Map.of(
190                 "cmp.abc", "cmp.def",
191                 "cmp.ghi",
192                     Arrays.asList("cmp.list1", "cmp.list2",
193                         Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2"))));
194         // @formatter:on
195         assertEquals(expected, map);
196     }
197
198     /**
199      * Makes properties containing the specified key/value pairs. The property set returns
200      * names in the order listed.
201      *
202      * @return a new properties containing the specified key/value pairs
203      */
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);
207
208         Set<String> keySet = new AbstractSet<>() {
209             @Override
210             public Iterator<String> iterator() {
211                 return keyList.iterator();
212             }
213
214             @Override
215             public int size() {
216                 return 2;
217             }
218         };
219
220         Properties props = new Properties() {
221             private static final long serialVersionUID = 1L;
222
223             @Override
224             public Set<String> stringPropertyNames() {
225                 return keySet;
226             }
227         };
228
229         props.putAll(Map.of(key1, value1, key2, value2));
230
231         return props;
232     }
233 }