a66aba2c643c4b7116b7cce9d1f14f95c11fd2c3
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfUtilsTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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;
28
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import lombok.Getter;
34 import lombok.ToString;
35 import org.junit.Test;
36
37 /**
38  * Test the PfUtils class.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class PfUtilsTest {
43
44     private static final String HELLO = "hello";
45
46     @Test
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));
53     }
54
55     @Test
56     public void testMapList() {
57         List<Object> resultList = PfUtils.mapList(null, item -> {
58             throw new RuntimeException("should not be invoked");
59         });
60         assertNull(resultList);
61
62         List<String> origList = Arrays.asList("abc", "def");
63         List<String> newList = PfUtils.mapList(origList, text -> text + "X");
64
65         assertEquals(Arrays.asList("abcX", "defX"), newList);
66
67         // verify that we can modify the list without throwing an exception
68         newList.remove("abcX");
69         newList.add("something else");
70     }
71
72     @Test
73     public void testMapMap() {
74         Map<String,String> resultMap = PfUtils.mapMap(null, item -> {
75             throw new RuntimeException("should not be invoked");
76         });
77         assertNull(resultMap);
78
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");
83
84         assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString());
85
86         // verify that we can modify the map without throwing an exception
87         newMap.remove("abcX");
88         newMap.put("something", "else");
89     }
90
91     @Test
92     public void testMakeCopy() {
93         assertNull(PfUtils.makeCopy((MyObject) null));
94
95         MyObject origObject = new MyObject();
96         origObject.name = HELLO;
97         assertEquals(origObject.toString(), PfUtils.makeCopy(origObject).toString());
98
99         assertThatThrownBy(() -> PfUtils.makeCopy(new NoCopyConstructor())).isInstanceOf(PfModelRuntimeException.class);
100     }
101
102     @Getter
103     @ToString
104     private static class MyObject {
105         private String name;
106
107         public MyObject() {
108             // do nothing
109         }
110
111         @SuppressWarnings("unused")
112         public MyObject(MyObject source) {
113             this.name = source.name;
114         }
115     }
116
117     @Getter
118     private static class NoCopyConstructor {
119         private String name;
120     }
121 }