aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyObjectUtilsTest.java
blob: 93b30643aa3c534a5ddf355b1fe2dc99f8d6deeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.utils.properties;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;

public class PropertyObjectUtilsTest {

    @Test
    public void testToObject() {
        Map<String, String> map = Map.of("abc", "def", "ghi", "jkl");
        Properties props = new Properties();
        props.putAll(map);

        // with empty prefix
        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        assertEquals(map, result);

        // with dotted prefix - other items skipped
        map = Map.of("pfx.abc", "def", "ghi", "jkl", "pfx.mno", "pqr", "differentpfx.stu", "vwx");
        props.clear();
        props.putAll(Map.of("pfx.abc", "def", "ghi", "jkl", "pfx.mno", "pqr", "differentpfx.stu", "vwx"));
        result = PropertyObjectUtils.toObject(props, "pfx.");
        map = Map.of("abc", "def", "mno", "pqr");
        assertEquals(map, result);

        // undotted prefix - still skips other items
        result = PropertyObjectUtils.toObject(props, "pfx");
        assertEquals(map, result);
    }

    @Test
    public void testSetProperty() {
        // one, two, and three components in the name, the last two with subscripts
        Map<String, Object> map = Map.of("one", "one.abc", "two.def", "two.ghi", "three.jkl.mno[0]", "three.pqr",
                        "three.jkl.mno[1]", "three.stu");
        Properties props = new Properties();
        props.putAll(map);

        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        // @formatter:off
        map = Map.of(
                "one", "one.abc",
                "two", Map.of("def", "two.ghi"),
                "three", Map.of("jkl",
                            Map.of("mno",
                                List.of("three.pqr", "three.stu"))));
        // @formatter:on
        assertEquals(map, result);
    }

    @Test
    public void testGetNode() {
        Map<String, Object> map = Map.of("abc[0].def", "node.ghi", "abc[0].jkl", "node.mno", "abc[1].def", "node.pqr");
        Properties props = new Properties();
        props.putAll(map);

        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        // @formatter:off
        map = Map.of(
                "abc",
                    List.of(
                        Map.of("def", "node.ghi", "jkl", "node.mno"),
                        Map.of("def", "node.pqr")
                    ));
        // @formatter:on
        assertEquals(map, result);

    }

    @Test
    public void testExpand() {
        // add subscripts out of order
        Properties props = makeProperties("abc[2]", "expand.def", "abc[1]", "expand.ghi");

        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        // @formatter:off
        Map<String, Object> map =
            Map.of("abc",
                Arrays.asList(null, "expand.ghi", "expand.def"));
        // @formatter:on
        assertEquals(map, result);

    }

    @Test
    public void testGetObject() {
        // first value is primitive, while second is a map
        Properties props = makeProperties("object.abc", "object.def", "object.abc.ghi", "object.jkl");

        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        // @formatter:off
        Map<String, Object> map =
            Map.of("object",
                Map.of("abc",
                    Map.of("ghi", "object.jkl")));
        // @formatter:on
        assertEquals(map, result);
    }

    @Test
    public void testGetArray() {
        // first value is primitive, while second is an array
        Properties props = makeProperties("array.abc", "array.def", "array.abc[0].ghi", "array.jkl");

        Map<String, Object> result = PropertyObjectUtils.toObject(props, "");
        // @formatter:off
        Map<String, Object> map =
            Map.of("array",
                Map.of("abc",
                    List.of(
                        Map.of("ghi", "array.jkl"))));
        // @formatter:on
        assertEquals(map, result);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testCompressLists() throws IOException, CoderException {
        assertEquals("plain-string", PropertyObjectUtils.compressLists("plain-string").toString());

        // @formatter:off
        Map<String, Object> map =
            Map.of(
                "cmp.abc", "cmp.def",
                "cmp.ghi",
                    Arrays.asList(null, "cmp.list1", null, "cmp.list2",
                        Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2", null))));
        // @formatter:on

        // the data structure needs to be modifiable, so we'll encode/decode it
        StandardCoder coder = new StandardCoder();
        map = coder.decode(coder.encode(map), LinkedHashMap.class);

        PropertyObjectUtils.compressLists(map);

        // @formatter:off
        Map<String, Object> expected =
            Map.of(
                "cmp.abc", "cmp.def",
                "cmp.ghi",
                    Arrays.asList("cmp.list1", "cmp.list2",
                        Map.of("cmp.map", Arrays.asList("cmp.map.list1", "cmp.map1.list2"))));
        // @formatter:on
        assertEquals(expected, map);
    }

    /**
     * Makes properties containing the specified key/value pairs. The property set returns
     * names in the order listed.
     *
     * @return a new properties containing the specified key/value pairs
     */
    private Properties makeProperties(String key1, String value1, String key2, String value2) {
        // control the order in which the names are returned
        List<String> keyList = List.of(key1, key2);

        Set<String> keySet = new AbstractSet<>() {
            @Override
            public Iterator<String> iterator() {
                return keyList.iterator();
            }

            @Override
            public int size() {
                return 2;
            }
        };

        Properties props = new Properties() {
            private static final long serialVersionUID = 1L;

            @Override
            public Set<String> stringPropertyNames() {
                return keySet;
            }
        };

        props.putAll(Map.of(key1, value1, key2, value2));

        return props;
    }
}