2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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.assertNotEquals;
28 import static org.junit.Assert.assertNull;
30 import java.util.Arrays;
31 import java.util.List;
33 import java.util.TreeMap;
35 import lombok.ToString;
36 import org.junit.Test;
39 * Test the PfUtils class.
41 * @author Liam Fallon (liam.fallon@est.tech)
43 public class PfUtilsTest {
45 private static final String HELLO = "hello";
48 public void testCompareObjects() {
49 assertEquals(0, PfUtils.compareObjects(null, null));
50 assertEquals(-1, PfUtils.compareObjects(HELLO, null));
51 assertEquals(1, PfUtils.compareObjects(null, HELLO));
52 assertNotEquals(PfUtils.compareObjects(HELLO, "goodbye"), 0);
53 assertEquals(0, PfUtils.compareObjects(HELLO, HELLO));
57 public void testMapList() {
58 List<Object> resultList = PfUtils.mapList(null, item -> {
59 throw new RuntimeException("should not be invoked");
61 assertNull(resultList);
63 List<String> origList = Arrays.asList("abc", "def");
64 List<String> newList = PfUtils.mapList(origList, text -> text + "X");
66 assertEquals(Arrays.asList("abcX", "defX"), newList);
68 // verify that we can modify the list without throwing an exception
69 newList.remove("abcX");
70 newList.add("something else");
74 public void testMapMap() {
75 Map<String, String> resultMap = PfUtils.mapMap(null, item -> {
76 throw new RuntimeException("should not be invoked");
78 assertNull(resultMap);
80 Map<String, String> origMap = new TreeMap<>();
81 origMap.put("key2A", "xyz2");
82 origMap.put("key2B", "pdq2");
83 Map<String, String> newMap = PfUtils.mapMap(origMap, text -> text + "X");
85 assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString());
87 // verify that we can modify the map without throwing an exception
88 newMap.remove("abcX");
89 newMap.put("something", "else");
93 public void testMakeCopy() {
94 assertNull(PfUtils.makeCopy((MyObject) null));
95 NoCopyConstructor noCopyConstructor = new NoCopyConstructor();
96 MyObject origObject = new MyObject();
97 origObject.name = HELLO;
98 assertEquals(origObject.toString(), PfUtils.makeCopy(origObject).toString());
100 assertThatThrownBy(() -> PfUtils.makeCopy(noCopyConstructor)).isInstanceOf(PfModelRuntimeException.class);
105 private static class MyObject {
112 @SuppressWarnings("unused")
113 public MyObject(MyObject source) {
114 this.name = source.name;
119 private static class NoCopyConstructor {