Fix checkstyle for features submodules.
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / state / IdleStateTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.pooling.state;
22
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Map;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.drools.pooling.message.BucketAssignments;
34 import org.onap.policy.drools.pooling.message.Forward;
35 import org.onap.policy.drools.pooling.message.Heartbeat;
36 import org.onap.policy.drools.pooling.message.Identification;
37 import org.onap.policy.drools.pooling.message.Leader;
38 import org.onap.policy.drools.pooling.message.Message;
39 import org.onap.policy.drools.pooling.message.Offline;
40 import org.onap.policy.drools.pooling.message.Query;
41
42 public class IdleStateTest extends BasicStateTester {
43
44     private IdleState state;
45
46     /**
47      * Setup.
48      */
49     @Before
50     public void setUp() throws Exception {
51         super.setUp();
52
53         state = new IdleState(mgr);
54     }
55
56     @Test
57     public void testGetFilter() {
58         Map<String, Object> filter = state.getFilter();
59
60         FilterUtilsTest utils = new FilterUtilsTest();
61
62         utils.checkArray(FilterUtils.CLASS_OR, 2, filter);
63         utils.checkEquals(FilterUtils.MSG_CHANNEL, Message.ADMIN, utils.getItem(filter, 0));
64         utils.checkEquals(FilterUtils.MSG_CHANNEL, MY_HOST, utils.getItem(filter, 1));
65     }
66
67     @Test
68     public void testProcessForward() {
69         Forward msg = new Forward();
70         msg.setChannel(MY_HOST);
71         assertNull(state.process(msg));
72
73         verify(mgr).handle(msg);
74     }
75
76     @Test
77     public void testProcessHeartbeat() {
78         assertNull(state.process(new Heartbeat(PREV_HOST, 0L)));
79         verifyNothingPublished();
80     }
81
82     @Test
83     public void testProcessIdentification() {
84         assertNull(state.process(new Identification(PREV_HOST, null)));
85         verifyNothingPublished();
86     }
87
88     @Test
89     public void testProcessLeader() {
90         BucketAssignments asgn = new BucketAssignments(new String[] {HOST2, PREV_HOST, MY_HOST});
91         Leader msg = new Leader(PREV_HOST, asgn);
92
93         State next = mock(State.class);
94         when(mgr.goActive()).thenReturn(next);
95
96         // should stay in current state, but start distributing
97         assertNull(state.process(msg));
98         verify(mgr).startDistributing(asgn);
99     }
100
101     @Test
102     public void testProcessOffline() {
103         assertNull(state.process(new Offline(PREV_HOST)));
104         verifyNothingPublished();
105     }
106
107     @Test
108     public void testProcessQuery() {
109         assertNull(state.process(new Query()));
110         verifyNothingPublished();
111     }
112
113     /**
114      * Verifies that nothing was published on either channel.
115      */
116     private void verifyNothingPublished() {
117         verify(mgr, never()).publish(any(), any());
118         verify(mgr, never()).publishAdmin(any());
119     }
120 }