f5f5ad0024c12a471b9b7fff1644893de2f3e42c
[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-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
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.assertNotEquals;
28 import static org.junit.Assert.assertNull;
29
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.TreeMap;
34 import lombok.Getter;
35 import lombok.ToString;
36 import org.junit.Test;
37
38 /**
39  * Test the PfUtils class.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class PfUtilsTest {
44
45     private static final String HELLO = "hello";
46
47     @Test
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));
54     }
55
56     @Test
57     public void testMapList() {
58         List<Object> resultList = PfUtils.mapList(null, item -> {
59             throw new RuntimeException("should not be invoked");
60         });
61         assertNull(resultList);
62
63         List<String> origList = Arrays.asList("abc", "def");
64         List<String> newList = PfUtils.mapList(origList, text -> text + "X");
65
66         assertEquals(Arrays.asList("abcX", "defX"), newList);
67
68         // verify that we can modify the list without throwing an exception
69         newList.remove("abcX");
70         newList.add("something else");
71     }
72
73     @Test
74     public void testMapMap() {
75         Map<String, String> resultMap = PfUtils.mapMap(null, item -> {
76             throw new RuntimeException("should not be invoked");
77         });
78         assertNull(resultMap);
79
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");
84
85         assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString());
86
87         // verify that we can modify the map without throwing an exception
88         newMap.remove("abcX");
89         newMap.put("something", "else");
90     }
91
92     @Test
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());
99
100         assertThatThrownBy(() -> PfUtils.makeCopy(noCopyConstructor)).isInstanceOf(PfModelRuntimeException.class);
101     }
102
103     @Getter
104     @ToString
105     private static class MyObject {
106         private String name;
107
108         public MyObject() {
109             // do nothing
110         }
111
112         @SuppressWarnings("unused")
113         public MyObject(MyObject source) {
114             this.name = source.name;
115         }
116     }
117
118     @Getter
119     private static class NoCopyConstructor {
120         private String name;
121     }
122 }