abc5db8e6458f882ddfe1077c0cf842959b2a2b9
[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.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;
29
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;
42
43 /**
44  * ControlLoopMetrics Tests.
45  */
46 public class ControlLoopMetricsFeatureTest {
47
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;
51
52     /**
53      * Setup method.
54      */
55     @BeforeClass
56     public static void setUp() {
57         SystemPersistence.manager.setConfigurationDir("src/test/resources");
58         testController = PolicyEngine.manager.createPolicyController("metrics",
59                 SystemPersistence.manager.getControllerProperties("metrics"));
60     }
61
62     @AfterClass
63     public static void tearDown() {
64         SystemPersistence.manager.setConfigurationDir(configPath.toString());
65     }
66
67     @Test
68     public void cacheDefaults() {
69         assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
70         assertEquals(2, ControlLoopMetrics.manager.getTransactionTimeout());
71         assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
72     }
73
74     @Test
75     public void invalidNotifications() {
76         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
77         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
78         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
79         this.cacheDefaults();
80
81         UUID requestId = UUID.randomUUID();
82         notification.setRequestId(requestId);
83
84         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
85         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
86         this.cacheDefaults();
87     }
88
89     @Test
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);
96
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);
102
103         /* wait for the entries to expire */
104         await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
105                         .until(() -> ControlLoopMetrics.manager.getTransaction(requestId) == null);
106
107         this.cacheDefaults();
108     }
109
110     @Test
111     public void reset() {
112         VirtualControlLoopNotification notification = this.generateNotification();
113         new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
114                 notification);
115
116         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
117
118         ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
119                 ControlLoopMetrics.manager.getTransactionTimeout());
120         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
121         this.cacheDefaults();
122     }
123
124     @Test
125     public void removeTransaction() {
126         VirtualControlLoopNotification notification = this.generateNotification();
127         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
128         ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
129
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()));
134     }
135
136     @Test
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()));
143         }
144
145         assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
146
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());
155
156         /* wait for the entries to expire */
157         await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
158                         .until(() -> ControlLoopMetrics.manager.getTransactions().isEmpty());
159
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());
166
167         this.cacheDefaults();
168     }
169
170     private VirtualControlLoopNotification generateNotification() {
171         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
172         UUID requestId = UUID.randomUUID();
173         notification.setRequestId(requestId);
174         notification.setNotification(ControlLoopNotificationType.ACTIVE);
175         return notification;
176     }
177
178     @Test
179     public void getSequenceNumber() {
180         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
181         assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
182     }
183 }