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 ch.qos.logback.classic.Logger;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.TreeMap;
35 import java.util.concurrent.atomic.AtomicInteger;
36 import lombok.Builder;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
43 import org.slf4j.LoggerFactory;
45 public class UtilTest {
46 protected static final String EXPECTED_EXCEPTION = "expected exception";
49 * Used to attach an appender to the class' logger.
51 private static final Logger logger = (Logger) LoggerFactory.getLogger(Util.class);
52 private static final ExtractAppender appender = new ExtractAppender();
55 * Initializes statics.
58 public static void setUpBeforeClass() {
59 appender.setContext(logger.getLoggerContext());
62 logger.addAppender(appender);
66 public static void tearDownAfterClass() {
72 appender.clearExtractions();
76 public void testIdent() {
77 Object object = new Object();
78 String result = Util.ident(object).toString();
80 assertNotEquals(object.toString(), result);
81 assertThat(result).startsWith("@");
82 assertTrue(result.length() > 1);
86 public void testRunFunction() {
87 // no exception, no log
88 AtomicInteger count = new AtomicInteger();
89 Util.runFunction(() -> count.incrementAndGet(), "no error");
90 assertEquals(1, count.get());
91 assertEquals(0, appender.getExtracted().size());
94 Runnable runnable = () -> {
95 count.incrementAndGet();
96 throw new IllegalStateException("expected exception");
99 appender.clearExtractions();
100 Util.runFunction(runnable, "error with no args");
101 List<String> output = appender.getExtracted();
102 assertEquals(1, output.size());
103 assertThat(output.get(0)).contains("error with no args");
105 appender.clearExtractions();
106 Util.runFunction(runnable, "error {} {} arg(s)", "with", 2);
107 output = appender.getExtracted();
108 assertEquals(1, output.size());
109 assertThat(output.get(0)).contains("error with 2 arg(s)");
113 public void testTranslate() {
115 final Abc abc = Abc.builder().intValue(1).strValue("hello").anotherString("another").build();
116 Abc abc2 = Util.translate("abc to abc", abc, Abc.class);
117 assertEquals(abc, abc2);
120 Similar sim = Util.translate("abc to similar", abc, Similar.class);
121 assertEquals(abc.getIntValue(), sim.getIntValue());
122 assertEquals(abc.getStrValue(), sim.getStrValue());
125 @SuppressWarnings("unchecked")
126 Map<String, Object> map = Util.translate("abc to map", abc, TreeMap.class);
127 assertEquals("{anotherString=another, intValue=1, strValue=hello}", map.toString());
130 @SuppressWarnings("unchecked")
131 Map<String, Object> map2 = Util.translate("map to map", map, LinkedHashMap.class);
132 assertEquals(map.toString(), map2.toString());
135 abc2 = Util.translate("map to abc", map, Abc.class);
136 assertEquals(abc, abc2);
140 public void testTranslateToMap() {
141 assertNull(Util.translateToMap("map: null", null));
144 final Abc abc = Abc.builder().intValue(2).strValue("world").anotherString("some").build();
145 Map<String, Object> map = new TreeMap<>(Util.translateToMap("map: abc to map", abc));
146 assertEquals("{anotherString=some, intValue=2, strValue=world}", map.toString());
149 Map<String, Object> map2 = Util.translateToMap("map: map to map", map);
150 assertEquals(map.toString(), map2.toString());
152 assertThatIllegalArgumentException().isThrownBy(() -> Util.translateToMap("map: string", "some string"))
153 .withMessageContaining("map: string");
158 public static class Abc {
159 private int intValue;
160 private String strValue;
161 private String anotherString;
164 // this shares some fields with Abc so the data should transfer
167 public static class Similar {
168 private int intValue;
169 private String strValue;
172 // throws an exception when getXxx() is used
173 public static class DataWithException {
174 @SuppressWarnings("unused")
175 private int intValue;
177 public int getIntValue() {
178 throw new IllegalStateException();