661803315e57bef7f3b5f483df3329dc861032d4
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.drools.apps.controlloop.feature.trans;
22
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;
28
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;
40
41 /**
42  * ControlLoopMetrics Tests.
43  */
44 public class ControlLoopMetricsFeatureTest {
45
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;
49
50     /**
51      * Setup method.
52      */
53     @BeforeClass
54     public static void setUp() {
55         SystemPersistence.manager.setConfigurationDir("src/test/resources");
56         testController = PolicyEngine.manager.createPolicyController("metrics",
57                 SystemPersistence.manager.getControllerProperties("metrics"));
58     }
59
60     @AfterClass
61     public static void tearDown() {
62         SystemPersistence.manager.setConfigurationDir(configPath.toString());
63     }
64
65     @Test
66     public void cacheDefaults() {
67         assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
68         assertEquals(10, ControlLoopMetrics.manager.getTransactionTimeout());
69         assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
70     }
71
72     @Test
73     public void invalidNotifications() {
74         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
75         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
76         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
77         this.cacheDefaults();
78
79         UUID requestId = UUID.randomUUID();
80         notification.setRequestId(requestId);
81
82         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
83         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
84         this.cacheDefaults();
85     }
86
87     @Test
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);
94
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);
100
101         /* let the entries expire */
102         Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 1) * 1000L);
103
104         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
105         this.cacheDefaults();
106     }
107
108     @Test
109     public void reset() {
110         VirtualControlLoopNotification notification = this.generateNotification();
111         new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
112                 notification);
113
114         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
115
116         ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
117                 ControlLoopMetrics.manager.getTransactionTimeout());
118         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
119         this.cacheDefaults();
120     }
121
122     @Test
123     public void removeTransaction() {
124         VirtualControlLoopNotification notification = this.generateNotification();
125         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
126         ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
127
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()));
132     }
133
134     @Test
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()));
141         }
142
143         assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
144
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());
153
154         /* let the entries expire */
155         Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 1) * 1000L);
156
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());
163
164         this.cacheDefaults();
165     }
166
167     private VirtualControlLoopNotification generateNotification() {
168         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
169         UUID requestId = UUID.randomUUID();
170         notification.setRequestId(requestId);
171         notification.setNotification(ControlLoopNotificationType.ACTIVE);
172         return notification;
173     }
174
175     @Test
176     public void getSequenceNumber() {
177         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
178         assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
179     }
180 }