2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.pooling.state;
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;
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;
44 class StartStateTest extends SupportBasicStateTester {
46 private StartState state;
53 public void setUp() throws Exception {
56 state = new StartState(mgr);
63 Pair<String, Heartbeat> msg = capturePublishedMessage(Heartbeat.class);
65 assertEquals(MY_HOST, msg.getLeft());
66 assertEquals(state.getHbTimestampMs(), msg.getRight().getTimestampMs());
70 * Verify heartbeat generator
72 Triple<Long, Long, StateTimerTask> generator = repeatedTasks.removeFirst();
74 assertEquals(STD_INTER_HEARTBEAT_MS, generator.getLeft().longValue());
75 assertEquals(STD_INTER_HEARTBEAT_MS, generator.getMiddle().longValue());
77 // invoke the task - it should generate another heartbeat
78 assertNull(generator.getRight().fire());
79 verify(mgr, times(2)).publish(MY_HOST, msg.getRight());
82 assertNull(generator.getRight().fire());
83 verify(mgr, times(3)).publish(MY_HOST, msg.getRight());
87 * Verify heartbeat checker
89 Pair<Long, StateTimerTask> checker = onceTasks.removeFirst();
91 assertEquals(STD_HEARTBEAT_WAIT_MS, checker.getLeft().longValue());
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);
97 assertEquals(next, checker.getRight().fire());
99 verify(mgr).startDistributing(null);
103 void testStartStatePoolingManager() {
105 * Prove the state is attached to the manager by invoking getHost(), which
106 * delegates to the manager.
108 assertEquals(MY_HOST, state.getHost());
112 void testStartStateState() {
113 // create a new state from the current state
114 state = new StartState(mgr);
117 * Prove the state is attached to the manager by invoking getHost(), which
118 * delegates to the manager.
120 assertEquals(MY_HOST, state.getHost());
124 void testProcessHeartbeat() {
125 Heartbeat msg = new Heartbeat();
127 // no matching data in heart beat
128 assertNull(state.process(msg));
129 verify(mgr, never()).publishAdmin(any());
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());
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());
143 // matching heart beat
144 msg.setSource(MY_HOST);
145 msg.setTimestampMs(state.getHbTimestampMs());
147 State next = mock(State.class);
148 when(mgr.goQuery()).thenReturn(next);
150 assertEquals(next, state.process(msg));
152 verify(mgr).publishAdmin(any(Query.class));
156 void testProcessIdentification() {
157 assertNull(state.process(new Identification(MY_HOST, null)));
161 void testProcessLeader() {
162 assertNull(state.process(new Leader(MY_HOST, null)));
166 void testProcessOffline() {
167 assertNull(state.process(new Offline(HOST1)));
171 void testProcessQuery() {
172 assertNull(state.process(new Query()));
176 void testGetHbTimestampMs() {
177 long tcurrent = System.currentTimeMillis();
178 assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
180 tcurrent = System.currentTimeMillis();
181 assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);