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.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;
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;
40 class InactiveStateTest extends SupportBasicStateTester {
42 private InactiveState state;
50 public void setUp() throws Exception {
53 state = new InactiveState(mgr);
57 void testProcessLeader() {
58 State next = mock(State.class);
59 when(mgr.goActive()).thenReturn(next);
61 String[] arr = {PREV_HOST, MY_HOST, HOST1};
62 BucketAssignments asgn = new BucketAssignments(arr);
63 Leader msg = new Leader(PREV_HOST, asgn);
65 assertEquals(next, state.process(msg));
66 verify(mgr).startDistributing(asgn);
70 void testProcessLeader_Invalid() {
71 Leader msg = new Leader(PREV_HOST, null);
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();
81 void testProcessQuery() {
82 State next = mock(State.class);
83 when(mgr.goQuery()).thenReturn(next);
85 assertEquals(next, state.process(new Query()));
87 Identification ident = captureAdminMessage(Identification.class);
88 assertEquals(MY_HOST, ident.getSource());
89 assertEquals(ASGN3, ident.getAssignments());
93 void testGoInatcive() {
94 assertNull(state.goInactive());
101 Pair<Long, StateTimerTask> timer = onceTasks.remove();
103 assertEquals(STD_REACTIVATE_WAIT_MS, timer.getLeft().longValue());
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);
109 assertEquals(next, timer.getRight().fire());
113 void testInactiveState() {
115 * Prove the state is attached to the manager by invoking getHost(), which
116 * delegates to the manager.
118 assertEquals(MY_HOST, state.getHost());