2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.comm;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyLong;
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;
34 import java.util.Arrays;
35 import java.util.LinkedList;
36 import java.util.Queue;
37 import java.util.concurrent.ScheduledExecutorService;
38 import java.util.concurrent.ScheduledFuture;
39 import java.util.concurrent.TimeUnit;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.pdpx.main.XacmlState;
49 @RunWith(MockitoJUnitRunner.class)
50 public class XacmlPdpHearbeatPublisherTest {
52 private static final long INTERVAL1 = 1000L;
53 private static final long INTERVAL2 = 2000L;
54 private static final long INTERVAL_INVALID = 0;
57 private TopicSinkClient client;
60 private XacmlState state;
63 private ScheduledExecutorService executor;
66 private ScheduledFuture<?> timer1;
69 private ScheduledFuture<?> timer2;
72 private PdpStatus status;
74 private Queue<ScheduledFuture<?>> timers;
76 private XacmlPdpHearbeatPublisher publisher;
80 * Initializes objects, including the publisher.
84 when(state.genHeartbeat()).thenReturn(status);
86 timers = new LinkedList<>(Arrays.asList(timer1, timer2));
88 when(executor.scheduleWithFixedDelay(any(), anyLong(), anyLong(), any())).thenAnswer(args -> timers.remove());
90 publisher = new MyPublisher(client, state);
94 public void testRun() {
97 verify(state).genHeartbeat();
98 verify(client).send(status);
102 public void testTerminate() {
104 publisher.terminate();
107 // now start it and then try again
109 publisher.terminate();
111 // timer2 should still be in the queue
112 assertSame(timer2, timers.peek());
115 // repeat - nothing more should happen
116 publisher.terminate();
118 // timer2 should still be in the queue
119 assertSame(timer2, timers.peek());
123 public void testRestart() {
124 // not started yet - should only update the interval
125 publisher.restart(INTERVAL1);
127 assertEquals(INTERVAL1, publisher.getIntervalMs());
128 assertSame(timer1, timers.peek());
132 verify(executor).scheduleWithFixedDelay(publisher, 0, INTERVAL1, TimeUnit.MILLISECONDS);
134 // null interval - no changes
135 publisher.restart(null);
136 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
137 assertSame(timer2, timers.peek());
139 // same interval - no changes
140 publisher.restart(INTERVAL1);
141 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
142 assertSame(timer2, timers.peek());
144 // invalid interval - no changes
145 publisher.restart(INTERVAL_INVALID);
146 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
147 assertSame(timer2, timers.peek());
149 // new interval - old timer should be cancelled and new started
150 publisher.restart(INTERVAL2);
151 verify(timer1).cancel(anyBoolean());
152 verify(executor).scheduleWithFixedDelay(publisher, 0, INTERVAL2, TimeUnit.MILLISECONDS);
156 public void testStart() {
159 verify(executor).scheduleWithFixedDelay(publisher, 0, XacmlPdpHearbeatPublisher.DEFAULT_INTERVAL_MS,
160 TimeUnit.MILLISECONDS);
162 // repeat - nothing more should happen
164 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
165 verify(timer1, never()).cancel(anyBoolean());
169 public void testMakeTimerThread() {
170 // create a plain listener to test the "real" makeTimer() method
171 publisher = new XacmlPdpHearbeatPublisher(client, state);
173 assertThatCode(() -> {
175 publisher.restart(100L);
176 publisher.terminate();
177 }).doesNotThrowAnyException();
180 private class MyPublisher extends XacmlPdpHearbeatPublisher {
182 public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
183 super(topicSinkClient, state);
187 protected ScheduledExecutorService makeTimerThread() {