2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 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.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.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;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.drools.pooling.message.Forward;
39 import org.onap.policy.drools.pooling.message.Heartbeat;
40 import org.onap.policy.drools.pooling.message.Identification;
41 import org.onap.policy.drools.pooling.message.Leader;
42 import org.onap.policy.drools.pooling.message.Message;
43 import org.onap.policy.drools.pooling.message.Offline;
44 import org.onap.policy.drools.pooling.message.Query;
45 import org.onap.policy.drools.utils.Triple;
47 public class StartStateTest extends SupportBasicStateTester {
49 private StartState state;
56 public void setUp() throws Exception {
59 state = new StartState(mgr);
63 public void testGetFilter() {
64 Map<String, Object> filter = state.getFilter();
66 FilterUtilsTest utils = new FilterUtilsTest();
68 utils.checkArray(FilterUtils.CLASS_OR, 2, filter);
69 utils.checkEquals(FilterUtils.MSG_CHANNEL, Message.ADMIN, utils.getItem(filter, 0));
72 filter = utils.getItem(filter, 1);
74 utils.checkArray(FilterUtils.CLASS_AND, 2, filter);
75 utils.checkEquals(FilterUtils.MSG_CHANNEL, MY_HOST, utils.getItem(filter, 0));
76 utils.checkEquals(FilterUtils.MSG_TIMESTAMP, String.valueOf(state.getHbTimestampMs()),
77 utils.getItem(filter, 1));
81 public void testStart() {
84 Pair<String, Heartbeat> msg = capturePublishedMessage(Heartbeat.class);
86 assertEquals(MY_HOST, msg.getLeft());
87 assertEquals(state.getHbTimestampMs(), msg.getRight().getTimestampMs());
91 * Verify heartbeat generator
93 Triple<Long, Long, StateTimerTask> generator = repeatedTasks.removeFirst();
95 assertEquals(STD_INTER_HEARTBEAT_MS, generator.first().longValue());
96 assertEquals(STD_INTER_HEARTBEAT_MS, generator.second().longValue());
98 // invoke the task - it should generate another heartbeat
99 assertEquals(null, generator.third().fire());
100 verify(mgr, times(2)).publish(MY_HOST, msg.getRight());
103 assertEquals(null, generator.third().fire());
104 verify(mgr, times(3)).publish(MY_HOST, msg.getRight());
108 * Verify heartbeat checker
110 Pair<Long, StateTimerTask> checker = onceTasks.removeFirst();
112 assertEquals(STD_HEARTBEAT_WAIT_MS, checker.getLeft().longValue());
114 // invoke the task - it should go to the state returned by the mgr
115 State next = mock(State.class);
116 when(mgr.goInactive()).thenReturn(next);
118 assertEquals(next, checker.getRight().fire());
120 verify(mgr).startDistributing(null);
124 public void testStartStatePoolingManager() {
126 * Prove the state is attached to the manager by invoking getHost(), which
127 * delegates to the manager.
129 assertEquals(MY_HOST, state.getHost());
133 public void testStartStateState() {
134 // create a new state from the current state
135 state = new StartState(mgr);
138 * Prove the state is attached to the manager by invoking getHost(), which
139 * delegates to the manager.
141 assertEquals(MY_HOST, state.getHost());
145 public void testProcessForward() {
146 Forward msg = new Forward();
147 msg.setChannel(MY_HOST);
148 assertNull(state.process(msg));
150 verify(mgr).handle(msg);
154 public void testProcessHeartbeat() {
155 Heartbeat msg = new Heartbeat();
157 // no matching data in heart beat
158 assertNull(state.process(msg));
159 verify(mgr, never()).publishAdmin(any());
161 // same source, different time stamp
162 msg.setSource(MY_HOST);
163 msg.setTimestampMs(state.getHbTimestampMs() - 1);
164 assertNull(state.process(msg));
165 verify(mgr, never()).publishAdmin(any());
167 // same time stamp, different source
168 msg.setSource("unknown");
169 msg.setTimestampMs(state.getHbTimestampMs());
170 assertNull(state.process(msg));
171 verify(mgr, never()).publishAdmin(any());
173 // matching heart beat
174 msg.setSource(MY_HOST);
175 msg.setTimestampMs(state.getHbTimestampMs());
177 State next = mock(State.class);
178 when(mgr.goQuery()).thenReturn(next);
180 assertEquals(next, state.process(msg));
182 verify(mgr).publishAdmin(any(Query.class));
186 public void testProcessIdentification() {
187 assertNull(state.process(new Identification(MY_HOST, null)));
191 public void testProcessLeader() {
192 assertNull(state.process(new Leader(MY_HOST, null)));
196 public void testProcessOffline() {
197 assertNull(state.process(new Offline(HOST1)));
201 public void testProcessQuery() {
202 assertNull(state.process(new Query()));
206 public void testGetHbTimestampMs() {
207 long tcurrent = System.currentTimeMillis();
208 assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
210 tcurrent = System.currentTimeMillis();
211 assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);