2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider;
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;
30 import java.util.LinkedHashMap;
32 import java.util.TreeMap;
33 import java.util.concurrent.atomic.AtomicInteger;
34 import lombok.Builder;
36 import org.junit.Test;
38 public class UtilTest {
41 public void testIdent() {
42 Object object = new Object();
43 String result = Util.ident(object).toString();
45 assertNotEquals(object.toString(), result);
46 assertThat(result).startsWith("@");
47 assertTrue(result.length() > 1);
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());
58 Runnable runnable = () -> {
59 count.incrementAndGet();
60 throw new IllegalStateException("expected exception");
63 Util.logException(runnable, "error with no args");
64 Util.logException(runnable, "error {} {} arg(s)", "with", 1);
68 public void testTranslate() {
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);
75 Similar sim = Util.translate("abc to similar", abc, Similar.class);
76 assertEquals(abc.getIntValue(), sim.getIntValue());
77 assertEquals(abc.getStrValue(), sim.getStrValue());
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());
85 @SuppressWarnings("unchecked")
86 Map<String, Object> map2 = Util.translate("map to map", map, LinkedHashMap.class);
87 assertEquals(map.toString(), map2.toString());
90 abc2 = Util.translate("map to abc", map, Abc.class);
91 assertEquals(abc, abc2);
95 public void testTranslateToMap() {
96 assertNull(Util.translateToMap("map: null", null));
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());
104 Map<String, Object> map2 = Util.translateToMap("map: map to map", map);
105 assertEquals(map.toString(), map2.toString());
107 assertThatIllegalArgumentException().isThrownBy(() -> Util.translateToMap("map: string", "some string"))
108 .withMessageContaining("map: string");
113 public static class Abc {
114 private int intValue;
115 private String strValue;
116 private String anotherString;
119 // this shares some fields with Abc so the data should transfer
122 public static class Similar {
123 private int intValue;
124 private String strValue;