Copy policy-endpoints from drools-pdp to common
[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 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.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.nio.file.Path;
29 import java.util.UUID;
30
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 Path configPath = SystemPersistence.manager.getConfigurationPath();
47     private static PolicyController testController;
48
49     @BeforeClass
50     public static void setUp() {
51         SystemPersistence.manager.setConfigurationDir("src/test/resources");
52         testController = PolicyEngine.manager.createPolicyController("metrics",
53                 SystemPersistence.manager.getControllerProperties("metrics"));
54     }
55
56     @AfterClass
57     public static void tearDown() {
58         SystemPersistence.manager.setConfigurationDir(configPath.toString());
59     }
60
61     @Test
62     public void cacheDefaults() {
63         assertTrue(ControlLoopMetrics.manager.getCacheSize() == 3);
64         assertTrue(ControlLoopMetrics.manager.getTransactionTimeout() == 10);
65         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 0);
66     }
67
68     @Test
69     public void invalidNotifications() {
70         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
71         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
72         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
73         this.cacheDefaults();
74
75         UUID requestId = UUID.randomUUID();
76         notification.setRequestId(requestId);
77
78         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
79         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
80         this.cacheDefaults();
81     }
82
83     @Test
84     public void validActiveNotification() {
85         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
86         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
87         UUID requestId = UUID.randomUUID();
88         notification.setRequestId(requestId);
89         notification.setNotification(ControlLoopNotificationType.ACTIVE);
90
91         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
92         assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
93         assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
94         assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
95         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);
96
97         /* let the entries expire */
98         try {
99             Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 5) * 1000L);
100         } catch (InterruptedException e) {
101             /* nothing to do */
102         }
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() {
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         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheOccupancy());
144
145         VirtualControlLoopNotification overflowNotification = generateNotification();
146         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", overflowNotification);
147         assertTrue(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         try {
156             Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 5) * 1000L);
157         } catch (InterruptedException e) {
158             /* nothing to do */
159         }
160
161         ControlLoopMetrics.manager.refresh();
162         assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
163                 .getCacheOccupancy());
164         assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
165         assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
166         assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
167
168         this.cacheDefaults();
169     }
170
171     private VirtualControlLoopNotification generateNotification() {
172         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
173         UUID requestId = UUID.randomUUID();
174         notification.setRequestId(requestId);
175         notification.setNotification(ControlLoopNotificationType.ACTIVE);
176         return notification;
177     }
178
179     @Test
180     public void getSequenceNumber() {
181         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
182         assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
183     }
184 }