Remove deprecated DMAAP dependency
[policy/drools-applications.git] / controlloop / common / feature-controlloop-trans / src / test / java / org / onap / policy / drools / apps / controlloop / feature / trans / ControlLoopMetricsFeatureTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023-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.drools.apps.controlloop.feature.trans;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import java.nio.file.Path;
33 import java.util.UUID;
34 import java.util.concurrent.TimeUnit;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.VirtualControlLoopNotification;
43 import org.onap.policy.controlloop.util.Serialization;
44 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
45 import org.onap.policy.drools.system.PolicyController;
46 import org.onap.policy.drools.system.PolicyEngineConstants;
47
48 /**
49  * ControlLoopMetrics Tests.
50  */
51 class ControlLoopMetricsFeatureTest {
52
53     private static final String POLICY_CL_MGT = "POLICY-CL-MGT";
54     private static final Path configPath = SystemPersistenceConstants.getManager().getConfigurationPath();
55     private static PolicyController testController;
56
57     private static void resetStats() {
58         PolicyEngineConstants.getManager().getStats().getGroupStat().setAverageExecutionTime(0d);
59         PolicyEngineConstants.getManager().getStats().getGroupStat().setLastExecutionTime(0L);
60         PolicyEngineConstants.getManager().getStats().getGroupStat().setLastStart(0L);
61         PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedCount(0L);
62         PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedFailCount(0L);
63         PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedSuccessCount(0L);
64         PolicyEngineConstants.getManager().getStats().getGroupStat().setTotalElapsedTime(0d);
65     }
66
67     /**
68      * Setup method.
69      */
70     @BeforeAll
71     public static void setUp() {
72         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
73         testController = PolicyEngineConstants.getManager().createPolicyController("metrics",
74                 SystemPersistenceConstants.getManager().getControllerProperties("metrics"));
75     }
76
77     @AfterAll
78     public static void tearDown() {
79         SystemPersistenceConstants.getManager().setConfigurationDir(configPath.toString());
80         resetStats();
81     }
82
83     @BeforeEach
84     public void beforeTest() {
85         resetStats();
86     }
87
88     @Test
89     void testCacheDefaults() {
90         assertEquals(3, ControlLoopMetricsManager.getManager().getCacheSize());
91         assertEquals(2, ControlLoopMetricsManager.getManager().getTransactionTimeout());
92         assertEquals(0, ControlLoopMetricsManager.getManager().getCacheOccupancy());
93     }
94
95     @Test
96     void testInvalidNotifications() {
97         var feature = new ControlLoopMetricsFeature();
98         var notification = new VirtualControlLoopNotification();
99         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
100         this.testCacheDefaults();
101
102         var requestId = UUID.randomUUID();
103         notification.setRequestId(requestId);
104
105         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
106         assertNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
107         this.testCacheDefaults();
108     }
109
110     @Test
111     void testValidActiveNotification() {
112         var feature = new ControlLoopMetricsFeature();
113         var notification = new VirtualControlLoopNotification();
114         var requestId = UUID.randomUUID();
115         notification.setRequestId(requestId);
116         notification.setNotification(ControlLoopNotificationType.ACTIVE);
117
118         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
119         assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
120         assertTrue(ControlLoopMetricsManager.getManager().getTransaction(requestId).getFrom()
121                         .contains(testController.getName()));
122         assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId).getNotificationTime());
123         assertEquals(1, ControlLoopMetricsManager.getManager().getCacheOccupancy());
124
125         /* wait for the entries to expire */
126         await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
127                         .until(() -> ControlLoopMetricsManager.getManager().getTransaction(requestId) == null);
128
129         this.testCacheDefaults();
130     }
131
132     @Test
133     void testReset() {
134         var notification = this.generateNotification();
135         new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT,
136                 notification);
137
138         assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
139
140         ControlLoopMetricsManager.getManager().resetCache(ControlLoopMetricsManager.getManager().getCacheSize(),
141                 ControlLoopMetricsManager.getManager().getTransactionTimeout());
142         assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
143         this.testCacheDefaults();
144     }
145
146     @Test
147     void testRemoveTransaction() {
148         var notification = this.generateNotification();
149         assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
150         ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
151
152         ControlLoopMetricsManager.getManager().transactionEvent(testController, notification);
153         assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
154         ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
155         assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
156     }
157
158     @Test
159     void testEviction() {
160         var feature = new ControlLoopMetricsFeature();
161         for (int i = 0; i < ControlLoopMetricsManager.getManager().getCacheSize(); i++) {
162             var notification = generateNotification();
163             feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
164             assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
165         }
166
167         assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
168                         ControlLoopMetricsManager.getManager().getCacheOccupancy());
169
170         var overflowNotification = generateNotification();
171         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, overflowNotification);
172         assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
173                         ControlLoopMetricsManager.getManager().getCacheOccupancy());
174         assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(overflowNotification.getRequestId()));
175         assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(),
176                 ControlLoopMetricsManager.getManager().getCacheSize());
177         assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
178                 ControlLoopMetricsManager.getManager().getCacheSize());
179         assertFalse(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
180         assertFalse(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
181
182         /* wait for the entries to expire */
183         await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
184                         .until(() -> ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
185
186         ControlLoopMetricsManager.getManager().refresh();
187         assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(),
188                 ControlLoopMetricsManager.getManager().getCacheOccupancy());
189         assertNotEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
190                 ControlLoopMetricsManager.getManager().getCacheSize());
191         assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
192         assertTrue(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
193
194         this.testCacheDefaults();
195     }
196
197     private VirtualControlLoopNotification generateNotification() {
198         var notification = new VirtualControlLoopNotification();
199         var requestId = UUID.randomUUID();
200         notification.setRequestId(requestId);
201         notification.setNotification(ControlLoopNotificationType.ACTIVE);
202         return notification;
203     }
204
205     @Test
206     void getSequenceNumber() {
207         var feature = new ControlLoopMetricsFeature();
208         assertEquals(ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY, feature.getSequenceNumber());
209     }
210
211     @Test
212     void testSuccessControlLoop() {
213         var feature = new ControlLoopMetricsFeature();
214
215         var activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json");
216         var active =
217                 Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class);
218         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, active);
219         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
220
221         var opStartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json");
222         var opStart =
223                 Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class);
224         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, opStart);
225         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
226
227         var permitNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-permit.json");
228         var permit =
229                 Serialization.gsonPretty.fromJson(permitNotification, VirtualControlLoopNotification.class);
230         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, permit);
231         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
232
233         var restartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-restart.json");
234         var restart =
235                 Serialization.gsonPretty.fromJson(restartNotification, VirtualControlLoopNotification.class);
236         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, restart);
237         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
238
239         var restartSuccessNotification =
240                 ResourceUtils.getResourceAsString("policy-cl-mgt-restart-success.json");
241         var restartSuccess =
242                 Serialization.gsonPretty.fromJson(restartSuccessNotification, VirtualControlLoopNotification.class);
243         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, restartSuccess);
244         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
245
246         var finalSuccessNotification =
247                 ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json");
248         var finalSuccess =
249                 Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class);
250         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, finalSuccess);
251         assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());
252         assertEquals(1,
253                 PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedSuccessCount());
254         assertEquals(0,
255                 PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedFailCount());
256         assertEquals(1, PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedCount());
257         assertEquals(1587409937684L,
258                 PolicyEngineConstants.getManager().getStats().getGroupStat().getLastExecutionTime());
259         assertEquals(461d,
260                 PolicyEngineConstants.getManager().getStats().getGroupStat().getAverageExecutionTime(), 0.0d);
261         assertEquals(1587409937223L,
262                 PolicyEngineConstants.getManager().getStats().getGroupStat().getLastStart());
263         assertEquals(461d,
264                 PolicyEngineConstants.getManager().getStats().getGroupStat().getTotalElapsedTime(), 0.0d);
265     }
266
267     @Test
268     void testUntrackedNotifications() throws InterruptedException {
269         var feature = new ControlLoopMetricsFeature();
270
271         var finalSuccessNotification =
272                 ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json");
273         var finalSuccess =
274                 Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class);
275         finalSuccess.setRequestId(UUID.randomUUID());
276         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, finalSuccess);
277         assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());
278
279         var opStartNotification =
280                 ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json");
281         var opStart =
282                 Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class);
283         feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, opStart);
284         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
285
286         Thread.sleep((ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1) * 1000L);  // NOSONAR
287         assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());
288     }
289 }