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