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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
29 import java.nio.file.Path;
30 import java.util.UUID;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.controlloop.ControlLoopNotificationType;
36 import org.onap.policy.controlloop.VirtualControlLoopNotification;
37 import org.onap.policy.drools.persistence.SystemPersistence;
38 import org.onap.policy.drools.system.PolicyController;
39 import org.onap.policy.drools.system.PolicyEngine;
42 * ControlLoopMetrics Tests.
44 public class ControlLoopMetricsFeatureTest {
46 private static final String POLICY_CL_MGT = "POLICY-CL-MGT";
47 private static final Path configPath = SystemPersistence.manager.getConfigurationPath();
48 private static PolicyController testController;
54 public static void setUp() {
55 SystemPersistence.manager.setConfigurationDir("src/test/resources");
56 testController = PolicyEngine.manager.createPolicyController("metrics",
57 SystemPersistence.manager.getControllerProperties("metrics"));
61 public static void tearDown() {
62 SystemPersistence.manager.setConfigurationDir(configPath.toString());
66 public void cacheDefaults() {
67 assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
68 assertEquals(10, ControlLoopMetrics.manager.getTransactionTimeout());
69 assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
73 public void invalidNotifications() {
74 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
75 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
76 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
79 UUID requestId = UUID.randomUUID();
80 notification.setRequestId(requestId);
82 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
83 assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
88 public void validActiveNotification() throws InterruptedException {
89 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
90 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
91 UUID requestId = UUID.randomUUID();
92 notification.setRequestId(requestId);
93 notification.setNotification(ControlLoopNotificationType.ACTIVE);
95 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
96 assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
97 assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
98 assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
99 assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);
101 /* let the entries expire */
102 Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 1) * 1000L);
104 assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
105 this.cacheDefaults();
109 public void reset() {
110 VirtualControlLoopNotification notification = this.generateNotification();
111 new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
114 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
116 ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
117 ControlLoopMetrics.manager.getTransactionTimeout());
118 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
119 this.cacheDefaults();
123 public void removeTransaction() {
124 VirtualControlLoopNotification notification = this.generateNotification();
125 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
126 ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
128 ControlLoopMetrics.manager.transactionEvent(testController, notification);
129 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
130 ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
131 assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
135 public void eviction() throws InterruptedException {
136 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
137 for (int i = 0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
138 VirtualControlLoopNotification notification = generateNotification();
139 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
140 assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
143 assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
145 VirtualControlLoopNotification overflowNotification = generateNotification();
146 feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, overflowNotification);
147 assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
148 assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestId()));
149 assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
150 assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
151 assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
152 assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());
154 /* let the entries expire */
155 Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 1) * 1000L);
157 ControlLoopMetrics.manager.refresh();
158 assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
159 .getCacheOccupancy());
160 assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
161 assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
162 assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
164 this.cacheDefaults();
167 private VirtualControlLoopNotification generateNotification() {
168 VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
169 UUID requestId = UUID.randomUUID();
170 notification.setRequestId(requestId);
171 notification.setNotification(ControlLoopNotificationType.ACTIVE);
176 public void getSequenceNumber() {
177 ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
178 assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);