aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java
blob: abc5db8e6458f882ddfe1077c0cf842959b2a2b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.apps.controlloop.feature.trans;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.nio.file.Path;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.drools.persistence.SystemPersistence;
import org.onap.policy.drools.system.PolicyController;
import org.onap.policy.drools.system.PolicyEngine;

/**
 * ControlLoopMetrics Tests.
 */
public class ControlLoopMetricsFeatureTest {

    private static final String POLICY_CL_MGT = "POLICY-CL-MGT";
    private static final Path configPath = SystemPersistence.manager.getConfigurationPath();
    private static PolicyController testController;

    /**
     * Setup method.
     */
    @BeforeClass
    public static void setUp() {
        SystemPersistence.manager.setConfigurationDir("src/test/resources");
        testController = PolicyEngine.manager.createPolicyController("metrics",
                SystemPersistence.manager.getControllerProperties("metrics"));
    }

    @AfterClass
    public static void tearDown() {
        SystemPersistence.manager.setConfigurationDir(configPath.toString());
    }

    @Test
    public void cacheDefaults() {
        assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
        assertEquals(2, ControlLoopMetrics.manager.getTransactionTimeout());
        assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
    }

    @Test
    public void invalidNotifications() {
        ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
        VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
        feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
        this.cacheDefaults();

        UUID requestId = UUID.randomUUID();
        notification.setRequestId(requestId);

        feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
        assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
        this.cacheDefaults();
    }

    @Test
    public void validActiveNotification() throws InterruptedException {
        ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
        VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
        UUID requestId = UUID.randomUUID();
        notification.setRequestId(requestId);
        notification.setNotification(ControlLoopNotificationType.ACTIVE);

        feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
        assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
        assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
        assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
        assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);

        /* wait for the entries to expire */
        await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
                        .until(() -> ControlLoopMetrics.manager.getTransaction(requestId) == null);

        this.cacheDefaults();
    }

    @Test
    public void reset() {
        VirtualControlLoopNotification notification = this.generateNotification();
        new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
                notification);

        assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));

        ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
                ControlLoopMetrics.manager.getTransactionTimeout());
        assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
        this.cacheDefaults();
    }

    @Test
    public void removeTransaction() {
        VirtualControlLoopNotification notification = this.generateNotification();
        assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
        ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());

        ControlLoopMetrics.manager.transactionEvent(testController, notification);
        assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
        ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
        assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
    }

    @Test
    public void eviction() throws InterruptedException {
        ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
        for (int i = 0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
            VirtualControlLoopNotification notification = generateNotification();
            feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
            assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
        }

        assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());

        VirtualControlLoopNotification overflowNotification = generateNotification();
        feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, overflowNotification);
        assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
        assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestId()));
        assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
        assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
        assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
        assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());

        /* wait for the entries to expire */
        await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
                        .until(() -> ControlLoopMetrics.manager.getTransactions().isEmpty());

        ControlLoopMetrics.manager.refresh();
        assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
                .getCacheOccupancy());
        assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
        assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
        assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());

        this.cacheDefaults();
    }

    private VirtualControlLoopNotification generateNotification() {
        VirtualControlLoopNotification notification = new VirtualControlLoopNotification();
        UUID requestId = UUID.randomUUID();
        notification.setRequestId(requestId);
        notification.setNotification(ControlLoopNotificationType.ACTIVE);
        return notification;
    }

    @Test
    public void getSequenceNumber() {
        ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
        assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY);
    }
}