2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNull;
29 import java.util.Arrays;
30 import java.util.List;
32 import java.util.TreeMap;
34 import lombok.ToString;
35 import org.junit.Test;
38 * Test the PfUtils class.
40 * @author Liam Fallon (liam.fallon@est.tech)
42 public class PfUtilsTest {
44 private static final String HELLO = "hello";
47 public void testCompareObjects() {
48 assertEquals(0, PfUtils.compareObjects(null, null));
49 assertEquals(-1, PfUtils.compareObjects(HELLO, null));
50 assertEquals(1, PfUtils.compareObjects(null, HELLO));
51 assertFalse(PfUtils.compareObjects(HELLO, "goodbye") == 0);
52 assertEquals(0, PfUtils.compareObjects(HELLO, HELLO));
56 public void testMapList() {
57 List<Object> resultList = PfUtils.mapList(null, item -> {
58 throw new RuntimeException("should not be invoked");
60 assertNull(resultList);
62 List<String> origList = Arrays.asList("abc", "def");
63 List<String> newList = PfUtils.mapList(origList, text -> text + "X");
65 assertEquals(Arrays.asList("abcX", "defX"), newList);
67 // verify that we can modify the list without throwing an exception
68 newList.remove("abcX");
69 newList.add("something else");
73 public void testMapMap() {
74 Map<String, String> resultMap = PfUtils.mapMap(null, item -> {
75 throw new RuntimeException("should not be invoked");
77 assertNull(resultMap);
79 Map<String, String> origMap = new TreeMap<>();
80 origMap.put("key2A", "xyz2");
81 origMap.put("key2B", "pdq2");
82 Map<String, String> newMap = PfUtils.mapMap(origMap, text -> text + "X");
84 assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString());
86 // verify that we can modify the map without throwing an exception
87 newMap.remove("abcX");
88 newMap.put("something", "else");
92 public void testMakeCopy() {
93 assertNull(PfUtils.makeCopy((MyObject) null));
95 MyObject origObject = new MyObject();
96 origObject.name = HELLO;
97 assertEquals(origObject.toString(), PfUtils.makeCopy(origObject).toString());
99 assertThatThrownBy(() -> PfUtils.makeCopy(new NoCopyConstructor())).isInstanceOf(PfModelRuntimeException.class);
104 private static class MyObject {
111 @SuppressWarnings("unused")
112 public MyObject(MyObject source) {
113 this.name = source.name;
118 private static class NoCopyConstructor {