1fd49c5057131cef878709ea9f5ffd0360689be3
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / state / StartStateTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
33
34 import java.util.Map;
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;
46
47 public class StartStateTest extends SupportBasicStateTester {
48
49     private StartState state;
50
51     /**
52      * Setup.
53      */
54     @Override
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58
59         state = new StartState(mgr);
60     }
61
62     @Test
63     public void testGetFilter() {
64         Map<String, Object> filter = state.getFilter();
65
66         FilterUtilsTest utils = new FilterUtilsTest();
67
68         utils.checkArray(FilterUtils.CLASS_OR, 2, filter);
69         utils.checkEquals(FilterUtils.MSG_CHANNEL, Message.ADMIN, utils.getItem(filter, 0));
70
71         // get the sub-filter
72         filter = utils.getItem(filter, 1);
73
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));
78     }
79
80     @Test
81     public void testStart() {
82         state.start();
83
84         Pair<String, Heartbeat> msg = capturePublishedMessage(Heartbeat.class);
85
86         assertEquals(MY_HOST, msg.getLeft());
87         assertEquals(state.getHbTimestampMs(), msg.getRight().getTimestampMs());
88
89
90         /*
91          * Verify heartbeat generator
92          */
93         Triple<Long, Long, StateTimerTask> generator = repeatedTasks.removeFirst();
94
95         assertEquals(STD_INTER_HEARTBEAT_MS, generator.first().longValue());
96         assertEquals(STD_INTER_HEARTBEAT_MS, generator.second().longValue());
97
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());
101
102         // and again
103         assertEquals(null, generator.third().fire());
104         verify(mgr, times(3)).publish(MY_HOST, msg.getRight());
105
106
107         /*
108          * Verify heartbeat checker
109          */
110         Pair<Long, StateTimerTask> checker = onceTasks.removeFirst();
111
112         assertEquals(STD_HEARTBEAT_WAIT_MS, checker.getLeft().longValue());
113
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);
117
118         assertEquals(next, checker.getRight().fire());
119
120         verify(mgr).startDistributing(null);
121     }
122
123     @Test
124     public void testStartStatePoolingManager() {
125         /*
126          * Prove the state is attached to the manager by invoking getHost(), which
127          * delegates to the manager.
128          */
129         assertEquals(MY_HOST, state.getHost());
130     }
131
132     @Test
133     public void testStartStateState() {
134         // create a new state from the current state
135         state = new StartState(mgr);
136
137         /*
138          * Prove the state is attached to the manager by invoking getHost(), which
139          * delegates to the manager.
140          */
141         assertEquals(MY_HOST, state.getHost());
142     }
143
144     @Test
145     public void testProcessForward() {
146         Forward msg = new Forward();
147         msg.setChannel(MY_HOST);
148         assertNull(state.process(msg));
149
150         verify(mgr).handle(msg);
151     }
152
153     @Test
154     public void testProcessHeartbeat() {
155         Heartbeat msg = new Heartbeat();
156
157         // no matching data in heart beat
158         assertNull(state.process(msg));
159         verify(mgr, never()).publishAdmin(any());
160
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());
166
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());
172
173         // matching heart beat
174         msg.setSource(MY_HOST);
175         msg.setTimestampMs(state.getHbTimestampMs());
176
177         State next = mock(State.class);
178         when(mgr.goQuery()).thenReturn(next);
179
180         assertEquals(next, state.process(msg));
181
182         verify(mgr).publishAdmin(any(Query.class));
183     }
184
185     @Test
186     public void testProcessIdentification() {
187         assertNull(state.process(new Identification(MY_HOST, null)));
188     }
189
190     @Test
191     public void testProcessLeader() {
192         assertNull(state.process(new Leader(MY_HOST, null)));
193     }
194
195     @Test
196     public void testProcessOffline() {
197         assertNull(state.process(new Offline(HOST1)));
198     }
199
200     @Test
201     public void testProcessQuery() {
202         assertNull(state.process(new Query()));
203     }
204
205     @Test
206     public void testGetHbTimestampMs() {
207         long tcurrent = System.currentTimeMillis();
208         assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
209
210         tcurrent = System.currentTimeMillis();
211         assertTrue(new StartState(mgr).getHbTimestampMs() >= tcurrent);
212     }
213
214 }