Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-messages / src / test / java / org / onap / policy / drools / pooling / state / StartStateTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018, 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020, 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.pooling.state;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.apache.commons.lang3.tuple.Triple;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.drools.pooling.message.Heartbeat;
39 import org.onap.policy.drools.pooling.message.Identification;
40 import org.onap.policy.drools.pooling.message.Leader;
41 import org.onap.policy.drools.pooling.message.Offline;
42 import org.onap.policy.drools.pooling.message.Query;
43
44 class StartStateTest extends SupportBasicStateTester {
45
46     private StartState state;
47
48     /**
49      * Setup.
50      */
51     @Override
52     @BeforeEach
53     public void setUp() throws Exception {
54         super.setUp();
55
56         state = new StartState(mgr);
57     }
58
59     @Test
60     void testStart() {
61         state.start();
62
63         Pair<String, Heartbeat> msg = capturePublishedMessage(Heartbeat.class);
64
65         assertEquals(MY_HOST, msg.getLeft());
66         assertEquals(state.getHbTimestampMs(), msg.getRight().getTimestampMs());
67
68
69         /*
70          * Verify heartbeat generator
71          */
72         Triple<Long, Long, StateTimerTask> generator = repeatedTasks.removeFirst();
73
74         assertEquals(STD_INTER_HEARTBEAT_MS, generator.getLeft().longValue());
75         assertEquals(STD_INTER_HEARTBEAT_MS, generator.getMiddle().longValue());
76
77         // invoke the task - it should generate another heartbeat
78         assertNull(generator.getRight().fire());
79         verify(mgr, times(2)).publish(MY_HOST, msg.getRight());
80
81         // and again
82         assertNull(generator.getRight().fire());
83         verify(mgr, times(3)).publish(MY_HOST, msg.getRight());
84
85
86         /*
87          * Verify heartbeat checker
88          */
89         Pair<Long, StateTimerTask> checker = onceTasks.removeFirst();
90
91         assertEquals(STD_HEARTBEAT_WAIT_MS, checker.getLeft().longValue());
92
93         // invoke the task - it should go to the state returned by the mgr
94         State next = mock(State.class);
95         when(mgr.goInactive()).thenReturn(next);
96
97         assertEquals(next, checker.getRight().fire());
98
99         verify(mgr).startDistributing(null);
100     }
101
102     @Test
103     void testStartStatePoolingManager() {
104         /*
105          * Prove the state is attached to the manager by invoking getHost(), which
106          * delegates to the manager.
107          */
108         assertEquals(MY_HOST, state.getHost());
109     }
110
111     @Test
112     void testStartStateState() {
113         // create a new state from the current state
114         state = new StartState(mgr);
115
116         /*
117          * Prove the state is attached to the manager by invoking getHost(), which
118          * delegates to the manager.
119          */
120         assertEquals(MY_HOST, state.getHost());
121     }
122
123     @Test
124     void testProcessHeartbeat() {
125         Heartbeat msg = new Heartbeat();
126
127         // no matching data in heart beat
128         assertNull(state.process(msg));
129         verify(mgr, never()).publishAdmin(any());
130
131         // same source, different time stamp
132         msg.setSource(MY_HOST);
133         msg.setTimestampMs(state.getHbTimestampMs() - 1);
134         assertNull(state.process(msg));
135         verify(mgr, never()).publishAdmin(any());
136
137         // same time stamp, different source
138         msg.setSource("unknown");
139         msg.setTimestampMs(state.getHbTimestampMs());
140         assertNull(state.process(msg));
141         verify(mgr, never()).publishAdmin(any());
142
143         // matching heart beat
144         msg.setSource(MY_HOST);
145         msg.setTimestampMs(state.getHbTimestampMs());
146
147         State next = mock(State.class);
148         when(mgr.goQuery()).thenReturn(next);
149
150         assertEquals(next, state.process(msg));
151
152         verify(mgr).publishAdmin(any(Query.class));
153     }
154
155     @Test
156     void testProcessIdentification() {
157         assertNull(state.process(new Identification(MY_HOST, null)));
158     }
159
160     @Test
161     void testProcessLeader() {
162         assertNull(state.process(new Leader(MY_HOST, null)));
163     }
164
165     @Test
166     void testProcessOffline() {
167         assertNull(state.process(new Offline(HOST1)));
168     }
169
170     @Test
171     void testProcessQuery() {
172         assertNull(state.process(new Query()));
173     }
174
175     @Test
176     void testGetHbTimestampMs() {
177         long tcurrent = System.currentTimeMillis();
178         assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
179
180         tcurrent = System.currentTimeMillis();
181         assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
182     }
183
184 }