068a72365ba9b3366b4e4173d7082d626bcd4096
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actorserviceprovider;
23
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;
30
31 import ch.qos.logback.classic.Logger;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.TreeMap;
36 import java.util.concurrent.atomic.AtomicInteger;
37 import lombok.Builder;
38 import lombok.Data;
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;
45
46 class UtilTest {
47     protected static final String EXPECTED_EXCEPTION = "expected exception";
48
49     /**
50      * Used to attach an appender to the class' logger.
51      */
52     private static final Logger logger = (Logger) LoggerFactory.getLogger(Util.class);
53     private static final ExtractAppender appender = new ExtractAppender();
54
55     /**
56      * Initializes statics.
57      */
58     @BeforeAll
59     static void setUpBeforeClass() {
60         appender.setContext(logger.getLoggerContext());
61         appender.start();
62
63         logger.addAppender(appender);
64     }
65
66     @AfterAll
67     static void tearDownAfterClass() {
68         appender.stop();
69     }
70
71     @BeforeEach
72    void setUp() {
73         appender.clearExtractions();
74     }
75
76     @Test
77    void testIdent() {
78         Object object = new Object();
79         String result = Util.ident(object).toString();
80
81         assertNotEquals(object.toString(), result);
82         assertThat(result).startsWith("@");
83         assertTrue(result.length() > 1);
84     }
85
86     @Test
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());
93
94         // with an exception
95         Runnable runnable = () -> {
96             count.incrementAndGet();
97             throw new IllegalStateException("expected exception");
98         };
99
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");
105
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)");
111     }
112
113     @Test
114    void testTranslate() {
115         // Abc => Abc
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);
119
120         // Abc => Similar
121         Similar sim = Util.translate("abc to similar", abc, Similar.class);
122         assertEquals(abc.getIntValue(), sim.getIntValue());
123         assertEquals(abc.getStrValue(), sim.getStrValue());
124
125         // Abc => Map
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());
129
130         // Map => Map
131         @SuppressWarnings("unchecked")
132         Map<String, Object> map2 = Util.translate("map to map", map, LinkedHashMap.class);
133         assertEquals(map.toString(), map2.toString());
134
135         // Map => Abc
136         abc2 = Util.translate("map to abc", map, Abc.class);
137         assertEquals(abc, abc2);
138     }
139
140     @Test
141     void testTranslateToMap() {
142         assertNull(Util.translateToMap("map: null", null));
143
144         // Abc => Map
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());
148
149         // Map => Map
150         Map<String, Object> map2 = Util.translateToMap("map: map to map", map);
151         assertEquals(map.toString(), map2.toString());
152
153         assertThatIllegalArgumentException().isThrownBy(() -> Util.translateToMap("map: string", "some string"))
154                         .withMessageContaining("map: string");
155     }
156
157     @Data
158     @Builder
159     static class Abc {
160         private int intValue;
161         private String strValue;
162         private String anotherString;
163     }
164
165     // this shares some fields with Abc so the data should transfer
166     @Data
167     @Builder
168     static class Similar {
169         private int intValue;
170         private String strValue;
171     }
172
173     // throws an exception when getXxx() is used
174     static class DataWithException {
175         @SuppressWarnings("unused")
176         private int intValue;
177
178         int getIntValue() {
179             throw new IllegalStateException();
180         }
181     }
182 }