2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import ch.qos.logback.classic.Logger;
32 import java.util.LinkedHashMap;
33 import java.util.List;
35 import java.util.TreeMap;
36 import java.util.concurrent.atomic.AtomicInteger;
37 import lombok.Builder;
39 import org.junit.jupiter.api.AfterAll;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
44 import org.slf4j.LoggerFactory;
47 protected static final String EXPECTED_EXCEPTION = "expected exception";
50 * Used to attach an appender to the class' logger.
52 private static final Logger logger = (Logger) LoggerFactory.getLogger(Util.class);
53 private static final ExtractAppender appender = new ExtractAppender();
56 * Initializes statics.
59 static void setUpBeforeClass() {
60 appender.setContext(logger.getLoggerContext());
63 logger.addAppender(appender);
67 static void tearDownAfterClass() {
73 appender.clearExtractions();
78 Object object = new Object();
79 String result = Util.ident(object).toString();
81 assertNotEquals(object.toString(), result);
82 assertThat(result).startsWith("@");
83 assertTrue(result.length() > 1);
87 void testRunFunction() {
88 // no exception, no log
89 AtomicInteger count = new AtomicInteger();
90 Util.runFunction(() -> count.incrementAndGet(), "no error");
91 assertEquals(1, count.get());
92 assertEquals(0, appender.getExtracted().size());
95 Runnable runnable = () -> {
96 count.incrementAndGet();
97 throw new IllegalStateException("expected exception");
100 appender.clearExtractions();
101 Util.runFunction(runnable, "error with no args");
102 List<String> output = appender.getExtracted();
103 assertEquals(1, output.size());
104 assertThat(output.get(0)).contains("error with no args");
106 appender.clearExtractions();
107 Util.runFunction(runnable, "error {} {} arg(s)", "with", 2);
108 output = appender.getExtracted();
109 assertEquals(1, output.size());
110 assertThat(output.get(0)).contains("error with 2 arg(s)");
114 void testTranslate() {
116 final Abc abc = Abc.builder().intValue(1).strValue("hello").anotherString("another").build();
117 Abc abc2 = Util.translate("abc to abc", abc, Abc.class);
118 assertEquals(abc, abc2);
121 Similar sim = Util.translate("abc to similar", abc, Similar.class);
122 assertEquals(abc.getIntValue(), sim.getIntValue());
123 assertEquals(abc.getStrValue(), sim.getStrValue());
126 @SuppressWarnings("unchecked")
127 Map<String, Object> map = Util.translate("abc to map", abc, TreeMap.class);
128 assertEquals("{anotherString=another, intValue=1, strValue=hello}", map.toString());
131 @SuppressWarnings("unchecked")
132 Map<String, Object> map2 = Util.translate("map to map", map, LinkedHashMap.class);
133 assertEquals(map.toString(), map2.toString());
136 abc2 = Util.translate("map to abc", map, Abc.class);
137 assertEquals(abc, abc2);
141 void testTranslateToMap() {
142 assertNull(Util.translateToMap("map: null", null));
145 final Abc abc = Abc.builder().intValue(2).strValue("world").anotherString("some").build();
146 Map<String, Object> map = new TreeMap<>(Util.translateToMap("map: abc to map", abc));
147 assertEquals("{anotherString=some, intValue=2, strValue=world}", map.toString());
150 Map<String, Object> map2 = Util.translateToMap("map: map to map", map);
151 assertEquals(map.toString(), map2.toString());
153 assertThatIllegalArgumentException().isThrownBy(() -> Util.translateToMap("map: string", "some string"))
154 .withMessageContaining("map: string");
160 private int intValue;
161 private String strValue;
162 private String anotherString;
165 // this shares some fields with Abc so the data should transfer
168 static class Similar {
169 private int intValue;
170 private String strValue;
173 // throws an exception when getXxx() is used
174 static class DataWithException {
175 @SuppressWarnings("unused")
176 private int intValue;
179 throw new IllegalStateException();