1f1e9de39f6e884ce9069fb0fe4eda7be4bbd5b0
[policy/drools-applications.git] /
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     /**
50      * Setup method.
51      */
52     @BeforeClass
53     public static void setUp() {
54         SystemPersistence.manager.setConfigurationDir("src/test/resources");
55         testController = PolicyEngine.manager.createPolicyController("metrics",
56                 SystemPersistence.manager.getControllerProperties("metrics"));
57     }
58
59     @AfterClass
60     public static void tearDown() {
61         SystemPersistence.manager.setConfigurationDir(configPath.toString());
62     }
63
64     @Test
65     public void cacheDefaults() {
66         assertTrue(ControlLoopMetrics.manager.getCacheSize() == 3);
67         assertTrue(ControlLoopMetrics.manager.getTransactionTimeout() == 10);
68         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 0);
69     }
70
71     @Test
72     public void invalidNotifications() {
73         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
74         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
75         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
76         this.cacheDefaults();
77
78         UUID requestId = UUID.randomUUID();
79         notification.setRequestId(requestId);
80
81         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
82         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
83         this.cacheDefaults();
84     }
85
86     @Test
87     public void validActiveNotification() {
88         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
89         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
90         UUID requestId = UUID.randomUUID();
91         notification.setRequestId(requestId);
92         notification.setNotification(ControlLoopNotificationType.ACTIVE);
93
94         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
95         assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
96         assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
97         assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
98         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);
99
100         /* let the entries expire */
101         try {
102             Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 5) * 1000L);
103         } catch (InterruptedException e) {
104             /* nothing to do */
105         }
106
107         assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
108         this.cacheDefaults();
109     }
110
111     @Test
112     public void reset() {
113         VirtualControlLoopNotification notification = this.generateNotification();
114         new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT",
115                 notification);
116
117         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
118
119         ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
120                 ControlLoopMetrics.manager.getTransactionTimeout());
121         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
122         this.cacheDefaults();
123     }
124
125     @Test
126     public void removeTransaction() {
127         VirtualControlLoopNotification notification = this.generateNotification();
128         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
129         ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
130
131         ControlLoopMetrics.manager.transactionEvent(testController, notification);
132         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
133         ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
134         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
135     }
136
137     @Test
138     public void eviction() {
139         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
140         for (int i = 0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
141             VirtualControlLoopNotification notification = generateNotification();
142             feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
143             assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
144         }
145
146         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheOccupancy());
147
148         VirtualControlLoopNotification overflowNotification = generateNotification();
149         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", overflowNotification);
150         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheOccupancy());
151         assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestId()));
152         assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
153         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
154         assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
155         assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());
156
157         /* let the entries expire */
158         try {
159             Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout() + 5) * 1000L);
160         } catch (InterruptedException e) {
161             /* nothing to do */
162         }
163
164         ControlLoopMetrics.manager.refresh();
165         assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
166                 .getCacheOccupancy());
167         assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
168         assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
169         assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
170
171         this.cacheDefaults();
172     }
173
174     private VirtualControlLoopNotification generateNotification() {
175         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
176         UUID requestId = UUID.randomUUID();
177         notification.setRequestId(requestId);
178         notification.setNotification(ControlLoopNotificationType.ACTIVE);
179         return notification;
180     }
181
182     @Test
183     public void getSequenceNumber() {
184         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
185         assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
186     }
187 }