6e3db3f973dc28eb661c27accf8fcd1c14838817
[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 java.nio.file.Path;
24 import java.util.UUID;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.onap.policy.controlloop.ControlLoopNotificationType;
29 import org.onap.policy.controlloop.VirtualControlLoopNotification;
30 import org.onap.policy.drools.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.drools.persistence.SystemPersistence;
32 import org.onap.policy.drools.system.PolicyController;
33 import org.onap.policy.drools.system.PolicyEngine;
34
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertNull;
37 import static org.junit.Assert.assertNotNull;
38 import static org.junit.Assert.assertTrue;
39
40 /**
41  * ControlLoopMetrics Tests
42  */
43 public class ControlLoopMetricsFeatureTest {
44
45     private static final Path configPath = SystemPersistence.manager.getConfigurationPath();
46     private static PolicyController testController;
47
48     @BeforeClass
49     public static void setUp() {
50         SystemPersistence.manager.setConfigurationDir("src/test/resources");
51         testController =
52             PolicyEngine.manager.createPolicyController
53                 ("metrics", 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", notification);
112
113         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
114
115         ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(), ControlLoopMetrics.manager.getTransactionTimeout());
116         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
117         this.cacheDefaults();
118     }
119
120     @Test
121     public void removeTransaction() {
122         VirtualControlLoopNotification notification = this.generateNotification();
123         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
124         ControlLoopMetrics.manager.removeTransaction(notification.getRequestID());
125
126         ControlLoopMetrics.manager.transactionEvent(testController, notification);
127         assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
128         ControlLoopMetrics.manager.removeTransaction(notification.getRequestID());
129         assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
130     }
131
132     @Test
133     public void eviction() {
134         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
135         for (int i=0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
136             VirtualControlLoopNotification notification = generateNotification();
137             feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", notification);
138             assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestID()));
139         }
140
141         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheOccupancy());
142
143         VirtualControlLoopNotification overflowNotification = generateNotification();
144         feature.beforeDeliver(testController, CommInfrastructure.DMAAP, "POLICY-CL-MGT", overflowNotification);
145         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheOccupancy());
146         assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestID()));
147         assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
148         assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
149         assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
150         assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());
151
152         /* let the entries expire */
153         try {
154             Thread.sleep((ControlLoopMetrics.manager.getTransactionTimeout()+5)*1000L);
155         } catch (InterruptedException e) {
156             /* nothing to do */
157         }
158
159         ControlLoopMetrics.manager.refresh();
160         assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheOccupancy());
161         assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
162         assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
163         assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
164
165         this.cacheDefaults();
166     }
167
168     private VirtualControlLoopNotification generateNotification() {
169         VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
170         UUID requestId = UUID.randomUUID();
171         notification.setRequestID(requestId);
172         notification.setNotification(ControlLoopNotificationType.ACTIVE);
173         return notification;
174     }
175
176     @Test
177     public void getSequenceNumber() {
178         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
179         assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
180     }
181 }