Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-pooling-messages / src / test / java / org / onap / policy / drools / pooling / state / InactiveStateTest.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.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.drools.pooling.message.BucketAssignments;
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.Query;
39
40 class InactiveStateTest extends SupportBasicStateTester {
41
42     private InactiveState state;
43
44     /**
45      * Setup.
46      *
47      */
48     @Override
49     @BeforeEach
50     public void setUp() throws Exception {
51         super.setUp();
52
53         state = new InactiveState(mgr);
54     }
55
56     @Test
57     void testProcessLeader() {
58         State next = mock(State.class);
59         when(mgr.goActive()).thenReturn(next);
60
61         String[] arr = {PREV_HOST, MY_HOST, HOST1};
62         BucketAssignments asgn = new BucketAssignments(arr);
63         Leader msg = new Leader(PREV_HOST, asgn);
64
65         assertEquals(next, state.process(msg));
66         verify(mgr).startDistributing(asgn);
67     }
68
69     @Test
70     void testProcessLeader_Invalid() {
71         Leader msg = new Leader(PREV_HOST, null);
72
73         // should stay in the same state, and not start distributing
74         assertNull(state.process(msg));
75         verify(mgr, never()).startDistributing(any());
76         verify(mgr, never()).goActive();
77         verify(mgr, never()).goInactive();
78     }
79
80     @Test
81     void testProcessQuery() {
82         State next = mock(State.class);
83         when(mgr.goQuery()).thenReturn(next);
84
85         assertEquals(next, state.process(new Query()));
86
87         Identification ident = captureAdminMessage(Identification.class);
88         assertEquals(MY_HOST, ident.getSource());
89         assertEquals(ASGN3, ident.getAssignments());
90     }
91
92     @Test
93     void testGoInatcive() {
94         assertNull(state.goInactive());
95     }
96
97     @Test
98     void testStart() {
99         state.start();
100
101         Pair<Long, StateTimerTask> timer = onceTasks.remove();
102
103         assertEquals(STD_REACTIVATE_WAIT_MS, timer.getLeft().longValue());
104
105         // invoke the task - it should go to the state returned by the mgr
106         State next = mock(State.class);
107         when(mgr.goStart()).thenReturn(next);
108
109         assertEquals(next, timer.getRight().fire());
110     }
111
112     @Test
113     void testInactiveState() {
114         /*
115          * Prove the state is attached to the manager by invoking getHost(), which
116          * delegates to the manager.
117          */
118         assertEquals(MY_HOST, state.getHost());
119     }
120
121 }