2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.drools.apps.controlloop.feature.trans;
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.nio.file.Path;
31 import java.util.UUID;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.controlloop.ControlLoopNotificationType;
38 import org.onap.policy.controlloop.VirtualControlLoopNotification;
39 import org.onap.policy.drools.persistence.SystemPersistence;
40 import org.onap.policy.drools.system.PolicyController;
41 import org.onap.policy.drools.system.PolicyEngine;
44 * ControlLoopMetrics Tests.
46 public class ControlLoopMetricsFeatureTest {
48 private static final String POLICY_CL_MGT = "POLICY-CL-MGT";
49 private static final Path configPath = SystemPersistence.manager.getConfigurationPath();
50 private static PolicyController testController;
56 public static void setUp() {
57 SystemPersistence.manager.setConfigurationDir("src/test/resources");
58 testController = PolicyEngine.manager.createPolicyController("metrics",
59 SystemPersistence.manager.getControllerProperties("metrics"));
63 public static void tearDown() {
64 SystemPersistence.manager.setConfigurationDir(configPath.toString());
68 public void cacheDefaults() {
69 assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
70 assertEquals(2, ControlLoopMetrics.manager.getTransactionTimeout());
71 assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
75 public void invalidNotifications() {
76 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
77 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
78 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
81 UUID requestId = UUID.randomUUID();
82 notification.setRequestId(requestId);
84 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
85 assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
90 public void validActiveNotification() throws InterruptedException {
91 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
92 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
93 UUID requestId = UUID.randomUUID();
94 notification.setRequestId(requestId);
95 notification.setNotification(ControlLoopNotificationType.ACTIVE);
97 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
98 assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
99 assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
100 assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
101 assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);
103 /* wait for the entries to expire */
104 await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
105 .until(() -> ControlLoopMetrics.manager.getTransaction(requestId) == null);
107 this.cacheDefaults();
111 public void reset() {
112 VirtualControlLoopNotification notification = this.generateNotification();
113 new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
116 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
118 ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
119 ControlLoopMetrics.manager.getTransactionTimeout());
120 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
121 this.cacheDefaults();
125 public void removeTransaction() {
126 VirtualControlLoopNotification notification = this.generateNotification();
127 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
128 ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
130 ControlLoopMetrics.manager.transactionEvent(testController, notification);
131 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
132 ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
133 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
137 public void eviction() throws InterruptedException {
138 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
139 for (int i = 0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
140 VirtualControlLoopNotification notification = generateNotification();
141 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
142 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
145 assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
147 VirtualControlLoopNotification overflowNotification = generateNotification();
148 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, overflowNotification);
149 assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
150 assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestId()));
151 assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
152 assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
153 assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
154 assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());
156 /* wait for the entries to expire */
157 await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
158 .until(() -> ControlLoopMetrics.manager.getTransactions().isEmpty());
160 ControlLoopMetrics.manager.refresh();
161 assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
162 .getCacheOccupancy());
163 assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
164 assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
165 assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
167 this.cacheDefaults();
170 private VirtualControlLoopNotification generateNotification() {
171 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
172 UUID requestId = UUID.randomUUID();
173 notification.setRequestId(requestId);
174 notification.setNotification(ControlLoopNotificationType.ACTIVE);
179 public void getSequenceNumber() {
180 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
181 assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);