c652e83747c4148228a4156d502e50709f948a85
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import java.util.concurrent.atomic.AtomicInteger;
34 import lombok.Builder;
35 import lombok.Data;
36 import org.junit.Test;
37
38 public class UtilTest {
39
40     @Test
41     public void testIdent() {
42         Object object = new Object();
43         String result = Util.ident(object).toString();
44
45         assertNotEquals(object.toString(), result);
46         assertThat(result).startsWith("@");
47         assertTrue(result.length() > 1);
48     }
49
50     @Test
51     public void testLogException() {
52         // no exception, no log
53         AtomicInteger count = new AtomicInteger();
54         Util.logException(() -> count.incrementAndGet(), "no error");
55         assertEquals(1, count.get());
56
57         // with an exception
58         Runnable runnable = () -> {
59             count.incrementAndGet();
60             throw new IllegalStateException("expected exception");
61         };
62
63         Util.logException(runnable, "error with no args");
64         Util.logException(runnable, "error {} {} arg(s)", "with", 1);
65     }
66
67     @Test
68     public void testTranslate() {
69         // Abc => Abc
70         final Abc abc = Abc.builder().intValue(1).strValue("hello").anotherString("another").build();
71         Abc abc2 = Util.translate("abc to abc", abc, Abc.class);
72         assertEquals(abc, abc2);
73
74         // Abc => Similar
75         Similar sim = Util.translate("abc to similar", abc, Similar.class);
76         assertEquals(abc.getIntValue(), sim.getIntValue());
77         assertEquals(abc.getStrValue(), sim.getStrValue());
78
79         // Abc => Map
80         @SuppressWarnings("unchecked")
81         Map<String, Object> map = Util.translate("abc to map", abc, TreeMap.class);
82         assertEquals("{anotherString=another, intValue=1, strValue=hello}", map.toString());
83
84         // Map => Map
85         @SuppressWarnings("unchecked")
86         Map<String, Object> map2 = Util.translate("map to map", map, LinkedHashMap.class);
87         assertEquals(map.toString(), map2.toString());
88
89         // Map => Abc
90         abc2 = Util.translate("map to abc", map, Abc.class);
91         assertEquals(abc, abc2);
92     }
93
94     @Test
95     public void testTranslateToMap() {
96         assertNull(Util.translateToMap("map: null", null));
97
98         // Abc => Map
99         final Abc abc = Abc.builder().intValue(2).strValue("world").anotherString("some").build();
100         Map<String, Object> map = new TreeMap<>(Util.translateToMap("map: abc to map", abc));
101         assertEquals("{anotherString=some, intValue=2, strValue=world}", map.toString());
102
103         // Map => Map
104         Map<String, Object> map2 = Util.translateToMap("map: map to map", map);
105         assertEquals(map.toString(), map2.toString());
106
107         assertThatIllegalArgumentException().isThrownBy(() -> Util.translateToMap("map: string", "some string"))
108                         .withMessageContaining("map: string");
109     }
110
111     @Data
112     @Builder
113     public static class Abc {
114         private int intValue;
115         private String strValue;
116         private String anotherString;
117     }
118
119     // this shares some fields with Abc so the data should transfer
120     @Data
121     @Builder
122     public static class Similar {
123         private int intValue;
124         private String strValue;
125     }
126 }